Bug 13323: Tax rate can change on receiving
authorJonathan Druart <jonathan.druart@biblibre.com>
Wed, 19 Nov 2014 16:01:17 +0000 (17:01 +0100)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 28 Oct 2016 13:46:41 +0000 (13:46 +0000)
This commit permits to update the tax rate on receiving.

Signed-off-by: Laurence Rault <laurence.rault@biblibre.com>
Signed-off-by: Francois Charbonnier <francois.charbonnier@inlibro.com>
Signed-off-by: Sonia Bouis <sonia.bouis@univ-lyon3.fr>
Signed-off-by: Sonia Bouis <koha@univ-lyon3.fr>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
C4/Acquisition.pm
acqui/basket.pl
acqui/basketgroup.pl
acqui/finishreceive.pl
acqui/invoice.pl
acqui/neworderempty.pl
acqui/orderreceive.pl
acqui/parcel.pl
koha-tmpl/intranet-tmpl/prog/en/modules/acqui/orderreceive.tt
t/Prices.t

index 3ccdca1..87e9189 100644 (file)
@@ -1443,12 +1443,12 @@ sub ModReceiveOrder {
         | if defined $order->{unitprice};
 
         $query .= q|
-            , rrp = ?, rrp_tax_included = ?, rrp_tax_excluded = ?
-        | if defined $order->{rrp};
+            ,tax_value_on_receiving = ?
+        | if defined $order->{tax_value_on_receiving};
 
         $query .= q|
-            , ecost = ?, ecost_tax_included = ?, ecost_tax_excluded = ?
-        | if defined $order->{ecost};
+            ,tax_rate_on_receiving = ?
+        | if defined $order->{tax_rate_on_receiving};
 
         $query .= q|
             , order_internalnote = ?
@@ -1462,12 +1462,15 @@ sub ModReceiveOrder {
         if ( defined $order->{unitprice} ) {
             push @params, $order->{unitprice}, $order->{unitprice_tax_included}, $order->{unitprice_tax_excluded};
         }
-        if ( defined $order->{rrp} ) {
-            push @params, $order->{rrp}, $order->{rrp_tax_included}, $order->{rrp_tax_excluded};
+
+        if ( defined $order->{tax_value_on_receiving} ) {
+            push @params, $order->{tax_value_on_receiving};
         }
-        if ( defined $order->{ecost} ) {
-            push @params, $order->{ecost}, $order->{ecost_tax_included}, $order->{ecost_tax_excluded};
+
+        if ( defined $order->{tax_rate_on_receiving} ) {
+            push @params, $order->{tax_rate_on_receiving};
         }
+
         if ( defined $order->{order_internalnote} ) {
             push @params, $order->{order_internalnote};
         }
@@ -2864,12 +2867,13 @@ sub populate_order_with_prices {
     $discount /= 100 if $discount > 1;
 
     if ($ordering) {
+        $order->{tax_rate_on_ordering} //= $order->{tax_rate};
         if ( $bookseller->{listincgst} ) {
             # The user entered the rrp tax included
             $order->{rrp_tax_included} = $order->{rrp};
 
             # rrp tax excluded = rrp tax included / ( 1 + tax rate )
-            $order->{rrp_tax_excluded} = $order->{rrp_tax_included} / ( 1 + $order->{tax_rate} );
+            $order->{rrp_tax_excluded} = $order->{rrp_tax_included} / ( 1 + $order->{tax_rate_on_ordering} );
 
             # ecost tax excluded = rrp tax excluded * ( 1 - discount )
             $order->{ecost_tax_excluded} = $order->{rrp_tax_excluded} * ( 1 - $discount );
@@ -2882,7 +2886,7 @@ sub populate_order_with_prices {
             $order->{rrp_tax_excluded} = $order->{rrp};
 
             # rrp tax included = rrp tax excluded * ( 1 - tax rate )
-            $order->{rrp_tax_included} = $order->{rrp_tax_excluded} * ( 1 + $order->{tax_rate} );
+            $order->{rrp_tax_included} = $order->{rrp_tax_excluded} * ( 1 + $order->{tax_rate_on_ordering} );
 
             # ecost tax excluded = rrp tax excluded * ( 1 - discount )
             $order->{ecost_tax_excluded} = $order->{rrp_tax_excluded} * ( 1 - $discount );
@@ -2890,15 +2894,17 @@ sub populate_order_with_prices {
             # ecost tax included = rrp tax excluded * ( 1 - tax rate ) * ( 1 - discount )
             $order->{ecost_tax_included} =
                 $order->{rrp_tax_excluded} *
-                ( 1 + $order->{tax_rate} ) *
+                ( 1 + $order->{tax_rate_on_ordering} ) *
                 ( 1 - $discount );
         }
 
         # tax value = quantity * ecost tax excluded * tax rate
-        $order->{tax_value} = $order->{quantity} * $order->{ecost_tax_excluded} * $order->{tax_rate};
+        $order->{tax_value_on_ordering} =
+            $order->{quantity} * $order->{ecost_tax_excluded} * $order->{tax_rate_on_ordering};
     }
 
     if ($receiving) {
+        $order->{tax_rate_on_receiving} //= $order->{tax_rate};
         if ( $bookseller->{invoiceincgst} ) {
             # Trick for unitprice. If the unit price rounded value is the same as the ecost rounded value
             # we need to keep the exact ecost value
@@ -2910,7 +2916,7 @@ sub populate_order_with_prices {
             $order->{unitprice_tax_included} = $order->{unitprice};
 
             # unit price tax excluded = unit price tax included / ( 1 + tax rate )
-            $order->{unitprice_tax_excluded} = $order->{unitprice_tax_included} / ( 1 + $order->{tax_rate} );
+            $order->{unitprice_tax_excluded} = $order->{unitprice_tax_included} / ( 1 + $order->{tax_rate_on_receiving} );
         }
         else {
             # Trick for unitprice. If the unit price rounded value is the same as the ecost rounded value
@@ -2922,12 +2928,13 @@ sub populate_order_with_prices {
             # The user entered the unit price tax excluded
             $order->{unitprice_tax_excluded} = $order->{unitprice};
 
+
             # unit price tax included = unit price tax included * ( 1 + tax rate )
-            $order->{unitprice_tax_included} = $order->{unitprice_tax_excluded} * ( 1 + $order->{tax_rate} );
+            $order->{unitprice_tax_included} = $order->{unitprice_tax_excluded} * ( 1 + $order->{tax_rate_on_receiving} );
         }
 
         # tax value = quantity * unit price tax excluded * tax rate
-        $order->{tax_value} = $order->{quantity} * $order->{unitprice_tax_excluded} * $order->{tax_rate};
+        $order->{tax_value_on_receiving} = $order->{quantity} * $order->{unitprice_tax_excluded} * $order->{tax_rate_on_receiving};
     }
 
     return $order;
index a7a01fd..ca2d2d2 100755 (executable)
@@ -329,6 +329,9 @@ if ( $op eq 'list' ) {
             $template->param( uncertainprices => 1 );
         }
 
+        $line->{tax_rate} = $line->{tax_rate_on_ordering};
+        $line->{tax_value} = $line->{tax_value_on_ordering};
+
         push @books_loop, $line;
 
         $foot{$$line{tax_rate}}{tax_rate} = $$line{tax_rate};
@@ -439,6 +442,8 @@ sub get_order_infos {
 
     $line{total_tax_included} = $line{ecost_tax_included} * $line{quantity};
     $line{total_tax_excluded} = $line{ecost_tax_excluded} * $line{quantity};
+    $line{tax_value} = $line{tax_value_on_ordering};
+    $line{tax_rate} = $line{tax_rate_on_ordering};
 
     if ( $line{uncertainprice} ) {
         $line{rrp_tax_excluded} .= ' (Uncertain)';
index a4994d3..9ad5482 100755 (executable)
@@ -168,6 +168,8 @@ sub printbasketgrouppdf{
                 croak $@;
             }
 
+            $ord->{tax_value} = $ord->{tax_value_on_ordering};
+            $ord->{tax_rate} = $ord->{tax_rate_on_ordering};
             $ord->{total_tax_included} = $ord->{ecost_tax_included} * $ord->{quantity};
             $ord->{total_tax_excluded} = $ord->{ecost_tax_excluded} * $ord->{quantity};
 
index 35fa750..10a0e0c 100755 (executable)
@@ -47,7 +47,7 @@ my $ordernumber      = $input->param('ordernumber');
 my $origquantityrec  = $input->param('origquantityrec');
 my $quantityrec      = $input->param('quantityrec');
 my $quantity         = $input->param('quantity');
-my $unitprice        = $input->param('cost');
+my $unitprice        = $input->param('unitprice');
 my $datereceived     = $input->param('datereceived'),
 my $invoiceid        = $input->param('invoiceid');
 my $invoice          = GetInvoice($invoiceid);
@@ -81,6 +81,7 @@ if ($quantityrec > $origquantityrec ) {
     }
 
     $order->{order_internalnote} = $input->param("order_internalnote");
+    $order->{tax_rate_on_receiving} = $input->param("tax_rate");
     $order->{unitprice} = $unitprice;
 
     my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
index 438407c..66f3c58 100755 (executable)
@@ -127,6 +127,9 @@ foreach my $order (@$orders) {
     $line->{total_tax_excluded} = Koha::Number::Price->new( $line->{unitprice_tax_excluded} * $line->{quantity} )->format;
     $line->{total_tax_included} = Koha::Number::Price->new( $line->{unitprice_tax_included} * $line->{quantity} )->format;
 
+    $line->{tax_value} = $line->{tax_value_on_receiving};
+    $line->{tax_rate} = $line->{tax_rate_on_receiving};
+
     $foot{$$line{tax_rate}}{tax_rate} = $$line{tax_rate};
     $foot{$$line{tax_rate}}{tax_value} += $$line{tax_value};
     $total_tax_value += $$line{tax_value};
index abbec2d..99314ae 100755 (executable)
@@ -265,7 +265,7 @@ if ( defined $subscriptionid ) {
         $budget_id              = $lastOrderReceived->{budgetid};
         $data->{listprice}      = $lastOrderReceived->{listprice};
         $data->{uncertainprice} = $lastOrderReceived->{uncertainprice};
-        $data->{tax_rate}        = $lastOrderReceived->{tax_rate};
+        $data->{tax_rate}       = $lastOrderReceived->{tax_rate_on_ordering};
         $data->{discount}       = $lastOrderReceived->{discount};
         $data->{rrp}            = $lastOrderReceived->{rrp};
         $data->{ecost}          = $lastOrderReceived->{ecost};
@@ -350,7 +350,7 @@ $template->param(
     quantityrec      => $quantity,
     rrp              => $data->{'rrp'},
     gst_values       => \@gst_values,
-    tax_rate          => $data->{tax_rate} ? $data->{tax_rate}+0.0 : $bookseller->{tax_rate} ? $bookseller->{tax_rate}+0.0 : 0,
+    tax_rate         => $data->{tax_rate_on_ordering} ? $data->{tax_rate_on_ordering}+0.0 : $bookseller->{tax_rate} ? $bookseller->{tax_rate}+0.0 : 0,
     listprice        => sprintf( "%.2f", $data->{listprice} || $data->{price} || $listprice),
     total            => sprintf( "%.2f", ($data->{ecost} || 0) * ($data->{'quantity'} || 0) ),
     ecost            => sprintf( "%.2f", $data->{ecost} || 0),
index 7bec9de..51017a4 100755 (executable)
@@ -152,32 +152,29 @@ if ($AcqCreateItem eq 'receiving') {
 }
 
 $order->{quantityreceived} = '' if $order->{quantityreceived} == 0;
-$order->{unitprice} = '' if $order->{unitprice} == 0;
-
-my $rrp;
-my $ecost;
-my $unitprice;
-if ( $bookseller->{listincgst} ) {
-    if ( $bookseller->{invoiceincgst} ) {
-        $rrp = $order->{rrp};
-        $ecost = $order->{ecost};
-        $unitprice = $order->{unitprice};
-    } else {
-        $rrp = $order->{rrp} / ( 1 + $order->{tax_rate} );
-        $ecost = $order->{ecost} / ( 1 + $order->{tax_rate} );
-        $unitprice = $order->{unitprice} / ( 1 + $order->{tax_rate} );
+
+my $unitprice = $order->{unitprice};
+my ( $rrp, $ecost );
+if ( $bookseller->{invoiceincgst} ) {
+    $rrp = $order->{rrp_tax_included};
+    $ecost = $order->{ecost_tax_included};
+    unless ( $unitprice != 0 and defined $unitprice) {
+        $unitprice = $order->{ecost_tax_included};
     }
 } else {
-    if ( $bookseller->{invoiceincgst} ) {
-        $rrp = $order->{rrp} * ( 1 + $order->{tax_rate} );
-        $ecost = $order->{ecost} * ( 1 + $order->{tax_rate} );
-        $unitprice = $order->{unitprice} * ( 1 + $order->{tax_rate} );
-    } else {
-        $rrp = $order->{rrp};
-        $ecost = $order->{ecost};
-        $unitprice = $order->{unitprice};
+    $rrp = $order->{rrp_tax_excluded};
+    $ecost = $order->{ecost_tax_excluded};
+    unless ( $unitprice != 0 and defined $unitprice) {
+        $unitprice = $order->{ecost_tax_excluded};
     }
- }
+}
+
+my $tax_rate;
+if( defined $order->{tax_rate_on_receiving} ) {
+    $tax_rate = $order->{tax_rate_on_receiving} + 0.0;
+} else {
+    $tax_rate = $order->{tax_rate_on_ordering} + 0.0;
+}
 
 my $suggestion = GetSuggestionInfoFromBiblionumber($order->{biblionumber});
 
@@ -188,6 +185,11 @@ my $budget = GetBudget( $order->{budget_id} );
 
 my $datereceived = $order->{datereceived} ? dt_from_string( $order->{datereceived} ) : dt_from_string;
 
+# get option values for gist syspref
+my @gst_values = map {
+    option => $_ + 0.0
+}, split( '\|', C4::Context->preference("gist") );
+
 $template->param(
     AcqCreateItem         => $AcqCreateItem,
     count                 => 1,
@@ -206,8 +208,10 @@ $template->param(
     quantity              => $order->{'quantity'},
     quantityreceivedplus1 => $order->{'quantityreceived'} + 1,
     quantityreceived      => $order->{'quantityreceived'},
-    rrp                   => sprintf( "%.2f", $rrp ),
-    ecost                 => sprintf( "%.2f", $ecost ),
+    rrp                   => $rrp,
+    ecost                 => $ecost,
+    unitprice             => $unitprice,
+    tax_rate              => $tax_rate,
     memberfirstname       => $member->{firstname} || "",
     membersurname         => $member->{surname} || "",
     invoiceid             => $invoice->{invoiceid},
@@ -218,6 +222,7 @@ $template->param(
     suggestionid          => $suggestion->{suggestionid},
     surnamesuggestedby    => $suggestion->{surnamesuggestedby},
     firstnamesuggestedby  => $suggestion->{firstnamesuggestedby},
+    gst_values            => \@gst_values,
 );
 
 my $borrower = GetMember( 'borrowernumber' => $loggedinuser );
@@ -255,15 +260,6 @@ foreach my $period (@$periods) {
 
 $template->{'VARS'}->{'budget_loop'} = \@budget_loop;
 
-# regardless of the content of $unitprice e.g 0 or '' or any string will return in these cases 0.00
-# and the 'IF' in the .tt will show 0.00 and not 'ecost' (see BZ 7129)
-# So if $unitprice == 0 we don't create unitprice
-if ( $unitprice != 0) {
-    $template->param(
-        unitprice             => sprintf( "%.2f", $unitprice),
-    );
-}
-
 my $op = $input->param('op');
 if ($op and $op eq 'edit'){
     $template->param(edit   =>   1);
index 79c5604..8f2d46f 100755 (executable)
@@ -111,7 +111,6 @@ unless( $invoiceid and $invoice->{invoiceid} ) {
 
 my $booksellerid = $invoice->{booksellerid};
 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
-my $gst = $bookseller->{tax_rate} // C4::Context->preference("gist") // 0;
 
 my @orders        = @{ $invoice->{orders} };
 my $countlines    = scalar @orders;
@@ -147,6 +146,9 @@ for my $order ( @orders ) {
         $line{holds} += scalar( @$holds );
     }
     $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
+
+    $line{tax_value} = $line{tax_value_on_receiving};
+    $line{tax_rate} = $line{tax_rate_on_receiving};
     $foot{$line{tax_rate}}{tax_rate} = $line{tax_rate};
     $foot{$line{tax_rate}}{tax_value} += $line{tax_value};
     $total_tax_excluded += $line{unitprice_tax_excluded} * $line{quantity};
index 0210a3c..f36ae98 100644 (file)
     <input type="hidden" name="invoiceid" value="[% invoiceid %]" />
     <input type="hidden" name="ordernumber" value="[% ordernumber %]" />
     <input type="hidden" name="booksellerid" value="[% booksellerid %]" />
-    <input type="hidden" name="tax_rate" value="[% tax_rate %]" />
        </div>
        <div class="yui-u">
     <fieldset class="rows">
           [% END %][%# IF (AcqCreateItemReceiving) %]
                </li>
 
+        [% IF ( gst_values ) %]
+            <li>
+                <label for="tax_rate">Tax rate: </label>
+                <select name="tax_rate" id="tax_rate">
+                [% FOREACH gst IN gst_values %]
+                    [% IF gst.option == tax_rate %]
+                        <option value="[% gst.option %]" selected="selected">[% gst.option * 100 | format("%.1f") %]%</option>
+                    [% ELSE %]
+                        <option value="[% gst.option %]">[% gst.option * 100 | format("%.1f") %]%</option>
+                    [% END %]
+                [% END %]
+                </select>
+            </li>
+        [% ELSE %]
+            <input type="hidden" name="tax_rate" value="0" />
+        [% END %]
+
         <li><label for="rrp">Replacement cost: </label>[% rrp | $Price %]</li>
         <li><label for="ecost">Budgeted cost: </label>[% ecost | $Price %]</li>
-        <li><label for="cost">Actual cost:</label>
-        [% IF ( unitprice ) %]
-             <input type="text" size="20" name="cost" id="cost" value="[% unitprice | $Price %]" />
-        [% ELSE %]
-             <input type="text" size="20" name="cost" id="cost" value="[% ecost | $Price %]" />
-        [% END %]</li>
+        <li>
+            <label for="unitprice">Actual cost:</label>
+            <input type="text" size="20" name="unitprice" id="unitprice" value="[% unitprice | $Price %]" />
+        </li>
         <li><label for="order_internalnote">Internal note: </label><textarea name="order_internalnote" width="40" rows="8" >[% order_internalnote %]</textarea></li>
         [% IF order_vendornote %]
             <li><label for="order_vendornote">Vendor note: </label><span>[% order_vendornote %]</span></li>
index c5c744f..7168dd8 100644 (file)
@@ -113,13 +113,14 @@ for my $currency_format ( qw( US FR ) ) {
         );
         compare(
             {
-                got      => $order_0_0->{tax_value},
+                got      => $order_0_0->{tax_value_on_ordering},
                 expected => 7.38,
                 conf     => '0 0',
                 field    => 'tax_value'
             }
         );
 
+        #
         $order_0_0 = C4::Acquisition::populate_order_with_prices(
             {
                 order        => $order_0_0,
@@ -146,7 +147,7 @@ for my $currency_format ( qw( US FR ) ) {
         );
         compare(
             {
-                got      => $order_0_0->{tax_value},
+                got      => $order_0_0->{tax_value_on_receiving},
                 expected => 7.38,
                 conf     => '0 0',
                 field    => 'tax_value'
@@ -221,7 +222,7 @@ for my $currency_format ( qw( US FR ) ) {
         );
         compare(
             {
-                got      => $order_1_1->{tax_value},
+                got      => $order_1_1->{tax_value_on_ordering},
                 expected => 7.03,
                 conf     => '1 1',
                 field    => 'tax_value'
@@ -254,7 +255,7 @@ for my $currency_format ( qw( US FR ) ) {
         );
         compare(
             {
-                got      => $order_1_1->{tax_value},
+                got      => $order_1_1->{tax_value_on_receiving},
                 expected => 7.03,
                 conf     => '1 1',
                 field    => 'tax_value'
@@ -329,7 +330,7 @@ for my $currency_format ( qw( US FR ) ) {
         );
         compare(
             {
-                got      => $order_1_0->{tax_value},
+                got      => $order_1_0->{tax_value_on_ordering},
                 expected => 7.03,
                 conf     => '1 0',
                 field    => 'tax_value'
@@ -362,7 +363,7 @@ for my $currency_format ( qw( US FR ) ) {
         );
         compare(
             {
-                got      => $order_1_0->{tax_value},
+                got      => $order_1_0->{tax_value_on_receiving},
                 expected => 7.03,
                 conf     => '1 0',
                 field    => 'tax_value'
@@ -437,7 +438,7 @@ for my $currency_format ( qw( US FR ) ) {
         );
         compare(
             {
-                got      => $order_0_1->{tax_value},
+                got      => $order_0_1->{tax_value_on_ordering},
                 expected => 7.38,
                 conf     => '0 1',
                 field    => 'tax_value'
@@ -470,7 +471,7 @@ for my $currency_format ( qw( US FR ) ) {
         );
         compare(
             {
-                got      => $order_0_1->{tax_value},
+                got      => $order_0_1->{tax_value_on_receiving},
                 expected => 7.38,
                 conf     => '0 1',
                 field    => 'tax_value'