From 4517d2b5fbb1b145cbc7633d79395be19e27910d Mon Sep 17 00:00:00 2001
From: Amit Gupta <amit.gupta@informaticsglobal.com>
Date: Sun, 12 Jul 2015 16:31:14 +0530
Subject: [PATCH] Bug 14520 - Prevent to override the existing currency code.

On creating a currency, if it already exists, it will replace the
existing one.
This patch prevent that and display a message to the user interface.

Test case
1. Create a new currency 'INR' with a symbol 'INR' and rate '1'.
2. Click on Edit and update the symbol with 'RS'.
3. Create a currency 'INR' with a symbol 'RUP' and rate '1'
you should get a warning message "This currency already exists".
---
 admin/currency.pl                                  |   14 +++++++++++---
 .../prog/en/modules/admin/currency.tt              |   15 +++++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/admin/currency.pl b/admin/currency.pl
index f62f761..783a855 100755
--- a/admin/currency.pl
+++ b/admin/currency.pl
@@ -51,6 +51,7 @@ our $offset      = $input->param('offset') || 0;
 my $op          = $input->param('op')     || q{};
 my $script_name = '/cgi-bin/koha/admin/currency.pl';
 our $pagesize = 20;
+my @messages;
 
 our ($template, $loggedinuser, $cookie) = get_template_and_user({
     template_name => 'admin/currency.tt',
@@ -72,7 +73,7 @@ if ( $op eq 'add_form' ) {
     add_form($searchfield);
 } elsif ( $op eq 'save' ) {
     add_validate();
-    print $input->redirect('/cgi-bin/koha/admin/currency.pl');
+    default_path();
 } elsif ( $op eq 'delete_confirm' ) {
     delete_confirm($searchfield);
 } elsif ( $op eq 'delete_confirmed' ) {
@@ -107,6 +108,7 @@ sub default_path {
     $template->param(
         loop           => \@display_curr,
         activecurrency => defined $activecurrency,
+        messages       => \@messages,
     );
 
     if ( $offset > 0 ) {
@@ -174,6 +176,7 @@ sub add_form {
 
 sub add_validate {
     $template->param( add_validate => 1 );
+    my $is_a_modif = $input->param('is_a_modif');
 
     my $rec = {
         rate     => $input->param('rate'),
@@ -190,7 +193,7 @@ sub add_validate {
     my ($row_count) = $dbh->selectrow_array(
         'select count(*) as count from currency where currency = ?',
         {}, $input->param('currency') );
-    if ($row_count) {
+    if ($row_count and $is_a_modif) {
         $dbh->do(
 q|UPDATE currency SET rate = ?, symbol = ?, isocode = ?, active = ? WHERE currency = ? |,
             {},
@@ -200,7 +203,7 @@ q|UPDATE currency SET rate = ?, symbol = ?, isocode = ?, active = ? WHERE curren
             $rec->{active},
             $rec->{currency}
         );
-    } else {
+    } elsif (not $row_count and not $is_a_modif) {
         $dbh->do(
 q|INSERT INTO currency (currency, rate, symbol, isocode, active) VALUES (?,?,?,?,?) |,
             {},
@@ -211,6 +214,11 @@ q|INSERT INTO currency (currency, rate, symbol, isocode, active) VALUES (?,?,?,?
             $rec->{active}
         );
 
+    } else {
+            push @messages, {
+                type => 'error',
+                code => 'already_exists'
+            };
     }
     return;
 }
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/currency.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/currency.tt
index 57c38cf..52fb71d 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/currency.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/currency.tt
@@ -68,6 +68,20 @@
 </div>
 [% END %]
 
+[% FOREACH message IN messages %]
+    [% IF message.type == 'success' %]
+        <div class="dialog message">
+    [% ELSIF message.type == 'warning' %]
+        <div class="dialog alert">
+    [% ELSIF message.type == 'error' %]
+        <div class="dialog error" style="margin:auto;">
+    [% END %]
+    [% IF message.code == 'already_exists' %]
+        This currency already exists.
+    [% END %]
+ </div>
+[% END %]
+
 [% IF ( add_form ) %]
 
 <form action="[% script_name %]" name="Aform" method="post" class="validated">
@@ -81,6 +95,7 @@
     <ol>
         <li>
     [% IF ( searchfield ) %]
+            <input type="hidden" name="is_a_modif" value="1" />
             <span class="label">Currency: </span>
             <input type="hidden" name="currency" id="currency" value="[% searchfield %]" />[% searchfield %]
     [% ELSE %]
-- 
1.7.9.5