From acbf8bca1564ce4376d5585e69678d9336236eef Mon Sep 17 00:00:00 2001
From: Robin Sheat <robin@catalyst.net.nz>
Date: Tue, 31 Aug 2010 14:57:22 +1200
Subject: [PATCH] Bug 5186 - allow tax rates to be set to zero

This allows the tax rate for a vendor to be set to zero. Previously, a
zero meant that the system default was used. Now, zero means no tax, and
to have it be the default the field should simply be left empty.
Additionally:
* this will now show on the vendor display if the tax value is from the
  system default
* this includes a database update that changes all the existing 0.00 tax
  settings to be NULL, which preserves existing behaviour.
* this now saves the tax value supplied for new vendors
---
 C4/Bookseller.pm                                   |    5 ++-
 acqui/addorderiso2709.pl                           |    5 ++-
 acqui/basket.pl                                    |    2 +-
 acqui/basketgroup.pl                               |    8 +++---
 acqui/neworderempty.pl                             |    2 +-
 acqui/orderreceive.pl                              |    3 +-
 acqui/parcel.pl                                    |    3 +-
 acqui/supplier.pl                                  |   11 +++------
 acqui/updatesupplier.pl                            |    7 +++++-
 installer/data/mysql/updatedatabase.pl             |    7 ++++++
 .../prog/en/modules/acqui/supplier.tmpl            |   21 +++++++------------
 11 files changed, 41 insertions(+), 33 deletions(-)

diff --git a/C4/Bookseller.pm b/C4/Bookseller.pm
index 8d60938..21ad348 100644
--- a/C4/Bookseller.pm
+++ b/C4/Bookseller.pm
@@ -173,10 +173,10 @@ sub AddBookseller {
                 postal,    phone,         fax,        url,           contact,
                 contpos,   contphone,     contfax,    contaltphone,  contemail,
                 contnotes, active,        listprice,  invoiceprice,  gstreg,
-                listincgst,invoiceincgst,   discount,
+                listincgst,invoiceincgst, gstrate,    discount,
                 notes
             )
-        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
     ";
     my $sth = $dbh->prepare($query);
     $sth->execute(
@@ -191,6 +191,7 @@ sub AddBookseller {
         $data->{'active'},       $data->{'listprice'},
         $data->{'invoiceprice'}, $data->{'gstreg'},
         $data->{'listincgst'},   $data->{'invoiceincgst'},
+        $data->{'gstrate'},
         $data->{'discount'},     $data->{'notes'}
     );
 
diff --git a/acqui/addorderiso2709.pl b/acqui/addorderiso2709.pl
index 903d2db..781df61 100755
--- a/acqui/addorderiso2709.pl
+++ b/acqui/addorderiso2709.pl
@@ -149,7 +149,8 @@ if ($op eq ""){
                   eval "use C4::Bookseller qw/GetBookSellerFromId/;";
                   my $basket = GetBasket($orderinfo{basketno});
                   my $bookseller = GetBookSellerFromId($basket->{booksellerid});
-                  my $gst = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
+                  # '//' is like '||' but tests for defined, rather than true
+                  my $gst = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
                   $orderinfo{'unitprice'} = $orderinfo{listprice} - ($orderinfo{listprice} * ($bookseller->{discount} / 100));
                   $orderinfo{'ecost'} = $orderinfo{unitprice};
               } else {
@@ -166,7 +167,7 @@ if ($op eq ""){
                   eval "use C4::Bookseller qw/GetBookSellerFromId/;";
                   my $basket = GetBasket($orderinfo{basketno});
                   my $bookseller = GetBookSellerFromId($basket->{booksellerid});
-                  my $gst = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
+                  my $gst = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
                   $orderinfo{'unitprice'} = $orderinfo{listprice} - ($orderinfo{listprice} * ($bookseller->{discount} / 100));
                   $orderinfo{'ecost'} = $orderinfo{unitprice};
               } else {
diff --git a/acqui/basket.pl b/acqui/basket.pl
index 005fcb9..221a26b 100755
--- a/acqui/basket.pl
+++ b/acqui/basket.pl
@@ -219,7 +219,7 @@ if ( $op eq 'delete_confirm' ) {
 
     my @results = GetOrders( $basketno );
     
-	my $gist = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
+	my $gist = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
 	my $discount = $bookseller->{'discount'} / 100;
     my $total_rrp;      # RRP Total, its value will be assigned to $total_rrp_gsti or $total_rrp_gste depending of $bookseller->{'listincgst'}
 	my $total_rrp_gsti; # RRP Total, GST included
diff --git a/acqui/basketgroup.pl b/acqui/basketgroup.pl
index 69de099..aba32d4 100755
--- a/acqui/basketgroup.pl
+++ b/acqui/basketgroup.pl
@@ -137,8 +137,8 @@ sub BasketTotal {
     my @orders = GetOrders($basketno);
     for my $order (@orders){
         $total = $total + ( $order->{ecost} * $order->{quantity} );
-        if ($bookseller->{invoiceincgst} && ! $bookseller->{listincgst} && ( $bookseller->{gstrate} || C4::Context->preference("gist") )) {
-            my $gst = $bookseller->{gstrate} || C4::Context->preference("gist");
+        if ($bookseller->{invoiceincgst} && ! $bookseller->{listincgst} && ( $bookseller->{gstrate} // C4::Context->preference("gist") )) {
+            my $gst = $bookseller->{gstrate} // C4::Context->preference("gist");
             $total = $total * ( $gst / 100 +1);
         }
     }
@@ -220,7 +220,7 @@ sub printbasketgrouppdf{
                     push(@ba_order, $ord->{$key});                                                  #Order lines
                 }
                 push(@ba_order, $bookseller->{discount});
-                push(@ba_order, $bookseller->{gstrate}*100 || C4::Context->preference("gist") || 0);
+                push(@ba_order, $bookseller->{gstrate}*100 // C4::Context->preference("gist") // 0);
                 push(@ba_orders, \@ba_order);
                 # Editor Number
                 my $en;
@@ -242,7 +242,7 @@ sub printbasketgrouppdf{
         -type       => 'application/pdf',
         -attachment => ( $basketgroup->{name} || $basketgroupid ) . '.pdf'
     );
-    my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{gstrate} || C4::Context->preference("gist")) || die "pdf generation failed";
+    my $pdf = printpdf($basketgroup, $bookseller, $baskets, \%orders, $bookseller->{gstrate} // C4::Context->preference("gist")) || die "pdf generation failed";
     print $pdf;
     exit;
 }
diff --git a/acqui/neworderempty.pl b/acqui/neworderempty.pl
index a580ae7..dac38d7 100755
--- a/acqui/neworderempty.pl
+++ b/acqui/neworderempty.pl
@@ -356,7 +356,7 @@ $template->param(
     import_batch_id  => $import_batch_id,
 
 # CHECKME: gst-stuff needs verifing, mason.
-    gstrate          => $bookseller->{'gstrate'} || C4::Context->preference("gist"),
+    gstrate          => $bookseller->{'gstrate'} // C4::Context->preference("gist") // 0
     gstreg           => $bookseller->{'gstreg'},
 );
 
diff --git a/acqui/orderreceive.pl b/acqui/orderreceive.pl
index a1e6fce..4927c7f 100755
--- a/acqui/orderreceive.pl
+++ b/acqui/orderreceive.pl
@@ -86,7 +86,8 @@ my $datereceived = $input->param('datereceived');
 $datereceived = $datereceived ? C4::Dates->new($datereceived, 'iso') : C4::Dates->new();
 
 my $bookseller = GetBookSellerFromId($supplierid);
-my $gst= $input->param('gst') || $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
+my $input_gst = ($input->param('gst') eq '' ? undef : $input->param('gst'));
+my $gst= $input_gst // $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
 my $results = SearchOrder($ordernumber,$search);
 
 
diff --git a/acqui/parcel.pl b/acqui/parcel.pl
index 675b07d..890515d 100755
--- a/acqui/parcel.pl
+++ b/acqui/parcel.pl
@@ -71,7 +71,8 @@ my $bookseller=GetBookSellerFromId($supplierid);
 
 my $invoice=$input->param('invoice') || '';
 my $freight=$input->param('freight');
-my $gst= $input->param('gst') || $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
+my $input_gst = ($input->param('gst') eq '' ? undef : $input->param('gst'));
+my $gst= $input_gst // $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
 my $datereceived =  ($input->param('op') eq 'new') ? C4::Dates->new($input->param('datereceived')) 
 					:  C4::Dates->new($input->param('datereceived'), 'iso')   ;
 $datereceived = C4::Dates->new() unless $datereceived;
diff --git a/acqui/supplier.pl b/acqui/supplier.pl
index 6c49a90..a614392 100755
--- a/acqui/supplier.pl
+++ b/acqui/supplier.pl
@@ -69,10 +69,9 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 my $seller_gstrate = $supplier->{'gstrate'};
 
 # ensure the scalar isn't flagged as a string
-$seller_gstrate = ( defined $seller_gstrate ) ? $seller_gstrate + 0 : 0;
-my $tax_rate = $seller_gstrate || C4::Context->preference('gist');
+$seller_gstrate = ( defined $seller_gstrate ) ? $seller_gstrate + 0 : undef;
+my $tax_rate = $seller_gstrate // C4::Context->preference('gist') // 0;
 $tax_rate *= 100;
-
 #build array for currencies
 if ( $op eq 'display' ) {
 
@@ -83,8 +82,6 @@ if ( $op eq 'display' ) {
         $_->{contractenddate}   = format_date( $_->{contractenddate} );
     }
 
-    my $gstrate = defined $supplier->{gstrate} ? $supplier->{gstrate} * 100 : 0;
-
     $template->param(
         id            => $id,
         name          => $supplier->{'name'},
@@ -108,11 +105,11 @@ if ( $op eq 'display' ) {
         gstreg        => $supplier->{'gstreg'},
         listincgst    => $supplier->{'listincgst'},
         invoiceincgst => $supplier->{'invoiceincgst'},
-        gstrate       => $gstrate,
         discount      => $supplier->{'discount'},
         invoiceprice  => $supplier->{'invoiceprice'},
         listprice     => $supplier->{'listprice'},
         GST           => $tax_rate,
+        default_tax   => $seller_gstrate,
         basketcount   => $supplier->{'basketcount'},
         contracts     => $contracts
     );
@@ -131,7 +128,7 @@ if ( $op eq 'display' ) {
           };
     }
 
-    my $gstrate = defined $supplier->{gstrate} ? $supplier->{gstrate} * 100 : 0;
+    my $gstrate = defined $supplier->{gstrate} ? $supplier->{gstrate} * 100 : '';
     $template->param(
         id           => $id,
         name         => $supplier->{'name'},
diff --git a/acqui/updatesupplier.pl b/acqui/updatesupplier.pl
index e324a29..b5957d7 100755
--- a/acqui/updatesupplier.pl
+++ b/acqui/updatesupplier.pl
@@ -98,7 +98,12 @@ $data{'gstreg'}=$input->param('gst');
 $data{'listincgst'}=$input->param('list_gst');
 $data{'invoiceincgst'}=$input->param('invoice_gst');
 #have to transform this into fraction so it's easier to use
-$data{'gstrate'}=$input->param('gstrate')/100;
+my $gstrate = $input->param('gstrate');
+if ($gstrate eq '') {
+    $data{'gstrate'} = undef;
+} else {
+    $data{'gstrate'} = $input->param('gstrate')/100;
+}
 $data{'discount'}=$input->param('discount');
 $data{'active'}=$input->param('status');
 if($data{'name'}) {
diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl
index a0b5c4a..0cc7b7a 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -3701,6 +3701,13 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
     SetVersion ($DBversion);
 }
 
+$DBversion = "3.01.00.146";
+if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
+    $dbh->do("UPDATE aqbooksellers SET gstrate=NULL WHERE gstrate=0.0");
+    print "Upgrade to $DBversion done (Add index on guarantorid)\n";
+    SetVersion ($DBversion);
+}
+
 =item DropAllForeignKeys($table)
 
   Drop all foreign keys of the table $table
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/supplier.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/supplier.tmpl
index 83b8631..e7d2313 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/supplier.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/supplier.tmpl
@@ -106,44 +106,39 @@ if (f.company.value == "") {
             </li>
             </ol>
             <ol class="radio">
-            <!-- TMPL_IF NAME="GST" --><li><label for="gstyes" class="radio">Tax Number Registered:</label>
+            <li><label for="gstyes" class="radio">Tax Number Registered:</label>
                 <!-- TMPL_IF name="gstreg" -->
                     <label for="gstyes">Yes</label> <input type="radio" name="gst" id="gstyes" value="1" checked="checked" />
                     <label for="gstno">No</label> <input type="radio" name="gst" id="gstno" value="0" />
                 <!-- TMPL_ELSE -->
                     <label for="gstyes">Yes</label> <input type="radio" name="gst" id="gstyes" value="1" />
                     <label for="gstno">No</label> <input type="radio" name="gst" id="gstno" value="0" checked="checked" />
-                <!-- /TMPL_IF --></li><!-- /TMPL_IF -->
+                <!-- /TMPL_IF --></li>
             
-            <!-- TMPL_IF NAME="GST" --><li><label for="list_gstyes" class="radio">Ordering prices:</label>
+            <li><label for="list_gstyes" class="radio">Ordering prices:</label>
                 <!-- TMPL_IF name="listincgst" -->
                     <label for="list_gstyes">Include tax</label> <input type="radio" id="list_gstyes" name="list_gst" value="1" checked="checked" />
                     <label for="list_gstno">Don't include tax</label> <input type="radio" id="list_gstno" name="list_gst" value="0" />
                 <!-- TMPL_ELSE -->
                     <label for="list_gstyes">Include tax</label> <input type="radio" id="list_gstyes" name="list_gst" value="1" />
                     <label for="list_gstno">Don't include tax</label> <input type="radio" id="list_gstno" name="list_gst" value="0" checked="checked" />
-                <!-- /TMPL_IF --></li><!-- /TMPL_IF -->
+                <!-- /TMPL_IF --></li>
             
-            <!-- TMPL_IF NAME="GST" --><li><label for="invoice_gstyes" class="radio">Invoice prices:</label>
+            <li><label for="invoice_gstyes" class="radio">Invoice prices:</label>
                 <!-- TMPL_IF name= "invoiceincgst" -->
                     <label for="invoice_gstyes">Include tax</label> <input type="radio" id="invoice_gstyes" name="invoice_gst" value="1" checked="checked" />
                     <label for="invoice_gstno">Don't include tax</label> <input type="radio" id="invoice_gstno" name="invoice_gst" value="0" />
                     <!-- TMPL_ELSE -->
                     <label for="invoice_gstyes">Include tax</label> <input type="radio" id="invoice_gstyes" name="invoice_gst" value="1" />
                     <label for="invoice_gstno">Don't include tax</label> <input type="radio" id="invoice_gstno" name="invoice_gst" value="0" checked="checked" />
-                <!-- /TMPL_IF --></li><!-- /TMPL_IF -->
+                <!-- /TMPL_IF --></li>
             </ol>
             <ol>
             <li><label for="discount">Discount</label>
                 <input type="text" size="6" id="discount" name="discount" value="<!-- TMPL_VAR NAME="discount" -->" /> %</li>
-            <!-- TMPL_IF NAME="GST" --><li><label for="gstrate">Tax rate</label><input type="text" name="gstrate" id="gstrate" size="5" value="<!-- TMPL_VAR name="gstrate" -->"/>%</li><!-- /TMPL_IF -->
+            <li><label for="gstrate">Tax rate</label><input type="text" name="gstrate" id="gstrate" size="5" value="<!-- TMPL_VAR name="gstrate" -->"/>%</li>
             <li><label for="notes">Notes</label>
                 <textarea cols="40" rows="4" id="notes" name="notes" ><!-- TMPL_VAR NAME="notes" --></textarea></li></ol>			
-            <!-- TMPL_UNLESS NAME="GST" -->
-                <input type="hidden" name="gst" value="0" />
-                <input type="hidden" name="list_gst" value="0" />
-                <input type="hidden"  name="invoice_gst" value="0" />
-            <!-- /TMPL_UNLESS -->
         </fieldset>
         <fieldset class="action"><input type="button" value="Save" onclick="check(this.form);" /> <!--TMPL_IF Name="id"-->
         <a class="cancel" href="/cgi-bin/koha/acqui/supplier.pl?supplierid=<!--TMPL_VAR Name="id"-->"><!--TMPL_ELSE--><a class="cancel" href="/cgi-bin/koha/acqui/acqui-home.pl">
@@ -198,7 +193,7 @@ if (f.company.value == "") {
             <p><strong>Discount: </strong>
                 <!-- TMPL_VAR NAME="discount" --> %</p>
             <!-- TMPL_IF NAME="GST" --><p><strong>Tax rate: </strong>
-                <!-- TMPL_VAR name="GST" -->%</p><!-- /TMPL_IF -->
+                <!-- TMPL_VAR name="GST" -->%<!-- TMPL_UNLESS name="default_tax" --> (default)<!-- /TMPL_UNLESS --></p><!-- /TMPL_IF -->
             <!-- TMPL_IF NAME="notes" --><p><strong>Notes: </strong>
                 <!-- TMPL_VAR NAME="notes" --></p><!-- /TMPL_IF -->
         </div>
-- 
1.7.0.4