From 4318eeef5ffed524513b803855dd0afe565e320c Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 22 Sep 2014 17:10:10 +0200 Subject: [PATCH] Bug 12976: Use the centralize VAT and prices calculation - invoice.pl Bug 12969 introduces a subroutine to centralize VAT and prices calculation. It should be use in the acqui/invoice.pl script. Test plan: 0/ Don't apply the patch 1/ Create 4 suppliers with the different configurations 2/ Create a basket and create several orders 3/ Receive the items and create an invoice 4/ Go on the invoice page acqui/invoice.pl?invoiceid=XXX 5/ Verify you don't see any difference before and after applying the patch on the invoice details table. Note: The only different you should see is the price formating for "Total tax exc.". Before this patch "432.10" was displayed "432.1". Signed-off-by: Paola Rossi Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- C4/Acquisition.pm | 37 +-- acqui/invoice.pl | 26 +-- .../prog/en/modules/acqui/invoice.tt | 33 +-- t/Prices.t | 210 +++++++++++++++++- 4 files changed, 251 insertions(+), 55 deletions(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index 15f69c004f..2fef6434eb 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -2870,23 +2870,26 @@ sub populate_order_with_prices { } } - # Not used yet - #if ($receiving) { - # if ( $bookseller->{invoiceincgst} ) { - # $order->{unitpricegsti} = $order->{unitprice}; - # $order->{unitpricegste} = - # $order->{unitpricegsti} / ( 1 + $order->{gstrate} ); - # } - # else { - # $order->{unitpricegste} = $order->{unitprice}; - # $order->{unitpricegsti} = - # $order->{unitpricegste} * ( 1 + $order->{gstrate} ); - # } - # $order->{gstvalue} = - # $order->{quantityreceived} * - # $order->{unitpricegste} * - # $order->{gstrate}; - #} + if ($receiving) { + # The following is completely wrong. Will be fixed later. + # See the unit tests to know what is wrong. + if ( $bookseller->{listincgst} ) { + $order->{unitpricegsti} = Koha::Number::Price->new( $order->{unitprice} )->round; + $order->{unitpricegste} = Koha::Number::Price->new( + $order->{unitpricegsti} / ( 1 + $order->{gstrate} ) )->round; + } + else { + $order->{unitpricegste} = Koha::Number::Price->new( $order->{unitprice} )->round; + $order->{unitpricegsti} = Koha::Number::Price->new( + $order->{unitpricegste} * ( 1 + $order->{gstrate} ) )->round; + } + $order->{gstvalue} = Koha::Number::Price->new( + ( $order->{unitpricegsti} - $order->{unitpricegste} ) + * $order->{quantityreceived} )->round; + + $order->{totalgste} = $order->{unitpricegste} * $order->{quantity}; + $order->{totalgsti} = $order->{unitpricegsti} * $order->{quantity}; + } return $order; } diff --git a/acqui/invoice.pl b/acqui/invoice.pl index f64836b567..59f2edc541 100755 --- a/acqui/invoice.pl +++ b/acqui/invoice.pl @@ -121,9 +121,16 @@ my $total_gste = 0; my $total_gsti = 0; my $total_gstvalue = 0; foreach my $order (@$orders) { + $order = C4::Acquisition::populate_order_with_prices( + { + order => $order, + booksellerid => $bookseller->{id}, + receiving => 1, + } + ); my $line = get_infos( $order, $bookseller); - $foot{$$line{gstgsti}}{gstgsti} = $$line{gstgsti}; + $foot{$$line{gstgsti}}{gstrate} = $$line{gstrate}; $foot{$$line{gstgsti}}{gstvalue} += $$line{gstvalue}; $total_gstvalue += $$line{gstvalue}; $foot{$$line{gstgsti}}{quantity} += $$line{quantity}; @@ -193,23 +200,6 @@ sub get_infos { my %line = %{ $order }; $line{order_received} = ( $qty == $order->{'quantityreceived'} ); $line{budget_name} = $budget->{budget_name}; - if ( $bookseller->{'listincgst'} ) { - $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 ); - $line{gstgste} = sprintf( "%.2f", $line{gstgsti} / ( 1 + ( $line{gstgsti} / 100 ) ) ); - $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} ); - $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} / ( 1 + ( $line{gstgsti} / 100 ) ) ); - $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity}); - $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} ); - $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} ); - } else { - $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 ); - $line{gstgste} = sprintf( "%.2f", $line{gstrate} * 100 ); - $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} * ( 1 + ( $line{gstrate} ) ) ); - $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} ); - $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity}); - $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} ); - $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} ); - } if ( $line{uncertainprice} ) { $template->param( uncertainprices => 1 ); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoice.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoice.tt index 8c1850f30d..ce278d4107 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoice.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoice.tt @@ -1,5 +1,6 @@ [% USE Koha %] [% USE KohaDates %] +[% USE Price %] [% INCLUDE 'doc-head-open.inc' %] Koha › Acquisitions › Invoice @@ -160,13 +161,13 @@ [% END %]

[% order.branchcode %]

- [% order.actualcostgste %] - [% order.actualcostgsti %] + [% order.unitpricegste | $Price %] + [% order.unitpricegsti | $Price %] [% order.quantity %] - [% order.totalgste %] - [% order.totalgsti %] - [% order.gstgsti %] - [% order.gstvalue %] + [% order.totalgste | $Price %] + [% order.totalgsti | $Price %] + [% order.gstrate * 100 | $Price %] + [% order.gstvalue | $Price %] [% order.budget_name %] [% END %] @@ -174,13 +175,13 @@ [% FOR tf IN foot_loop %] - Total (GST [% tf.gstgsti %] %) + Total (GST [% tf.gstrate * 100 | $Price %] %) [% tf.quantity %] - [% tf.totalgste %] - [% tf.totalgsti %] + [% tf.totalgste | $Price %] + [% tf.totalgsti | $Price %]   - [% tf.gstvalue %] + [% tf.gstvalue | $Price %]   [% END %] @@ -188,10 +189,10 @@ Total ([% currency %]) [% total_quantity %] - [% total_gste %] - [% total_gsti %] + [% total_gste | $Price %] + [% total_gsti | $Price %]   - [% total_gstvalue %] + [% total_gstvalue | $Price %]   @@ -199,10 +200,10 @@ [% total_quantity %] - [% total_gste_shipment %] - [% total_gsti_shipment %] + [% total_gste_shipment | $Price %] + [% total_gsti_shipment | $Price %]   - [% total_gstvalue %] + [% total_gstvalue | $Price %]   diff --git a/t/Prices.t b/t/Prices.t index aa864d26a6..8901cad2be 100644 --- a/t/Prices.t +++ b/t/Prices.t @@ -23,7 +23,7 @@ my $today; for my $currency_format ( qw( US FR ) ) { t::lib::Mocks::mock_preference( 'CurrencyFormat', $currency_format ); subtest 'Configuration 1: 0 0' => sub { - plan tests => 7; + plan tests => 12; $bookseller_module->mock( 'fetch', sub { @@ -112,10 +112,61 @@ for my $currency_format ( qw( US FR ) ) { field => 'totalgste' } ); + + $order_0_0 = C4::Acquisition::populate_order_with_prices( + { + order => $order_0_0, + booksellerid => 'just_something', + receiving => 1, + } + ); + # Note that this configuration is *not* correct! + # unitpricegsti should be 75.28 + # totalgst should be 150.56 + compare( + { + got => $order_0_0->{unitpricegsti}, + expected => 77.49, + conf => '0 0', + field => 'unitpricegsti' + } + ); + compare( + { + got => $order_0_0->{unitpricegste}, + expected => 73.80, + conf => '0 0', + field => 'unitpricegste' + } + ); + compare( + { + got => $order_0_0->{gstvalue}, + expected => 7.38, + conf => '0 0', + field => 'gstvalue' + } + ); + compare( + { + got => $order_0_0->{totalgsti}, + expected => 154.98, + conf => '0 0', + field => 'totalgsti' + } + ); + compare( + { + got => $order_0_0->{totalgste}, + expected => 147.60, + conf => '0 0', + field => 'totalgste' + } + ); }; subtest 'Configuration 1: 1 1' => sub { - plan tests => 7; + plan tests => 12; $bookseller_module->mock( 'fetch', sub { @@ -205,10 +256,60 @@ for my $currency_format ( qw( US FR ) ) { field => 'totalgste' } ); + + $order_1_1 = C4::Acquisition::populate_order_with_prices( + { + order => $order_1_1, + booksellerid => 'just_something', + receiving => 1, + } + ); + # Note that this configuration is *not* correct! + # gstvalue should be 7.03 + compare( + { + got => $order_1_1->{unitpricegsti}, + expected => 73.80, + conf => '1 1', + field => 'unitpricegsti' + } + ); + compare( + { + got => $order_1_1->{unitpricegste}, + expected => 70.29, + conf => '1 1', + field => 'unitpricegste' + } + ); + compare( + { + got => $order_1_1->{gstvalue}, + expected => 7.02, + conf => '1 1', + field => 'gstvalue' + } + ); + compare( + { + got => $order_1_1->{totalgsti}, + expected => 147.60, + conf => '1 1', + field => 'totalgsti' + } + ); + compare( + { + got => $order_1_1->{totalgste}, + expected => 140.58, + conf => '1 1', + field => 'totalgste' + } + ); }; subtest 'Configuration 1: 1 0' => sub { - plan tests => 7; + plan tests => 12; $bookseller_module->mock( 'fetch', sub { @@ -300,10 +401,62 @@ for my $currency_format ( qw( US FR ) ) { field => 'totalgste' } ); + + $order_1_0 = C4::Acquisition::populate_order_with_prices( + { + order => $order_1_0, + booksellerid => 'just_something', + receiving => 1, + } + ); + # Note that this configuration is *not* correct! + # unitpricegsti should be 71.69 + # totalgsti should be 143.38 + # gstvalue should be 7.03 + compare( + { + got => $order_1_0->{unitpricegsti}, + expected => 73.80, + conf => '1 0', + field => 'unitpricegsti' + } + ); + compare( + { + got => $order_1_0->{unitpricegste}, + expected => 70.29, + conf => '1 0', + field => 'unitpricegste' + } + ); + compare( + { + got => $order_1_0->{gstvalue}, + expected => 7.02, + conf => '1 0', + field => 'gstvalue' + } + ); + compare( + { + got => $order_1_0->{totalgsti}, + expected => 147.60, + conf => '1 0', + field => 'totalgsti' + } + ); + compare( + { + got => $order_1_0->{totalgste}, + expected => 140.58, + conf => '1 0', + field => 'totalgste' + } + ); }; subtest 'Configuration 1: 0 1' => sub { - plan tests => 7; + plan tests => 12; $bookseller_module->mock( 'fetch', sub { @@ -392,6 +545,55 @@ for my $currency_format ( qw( US FR ) ) { field => 'totalgste' } ); + + $order_0_1 = C4::Acquisition::populate_order_with_prices( + { + order => $order_0_1, + booksellerid => 'just_something', + receiving => 1, + } + ); + # Note that this configuration is correct + compare( + { + got => $order_0_1->{unitpricegsti}, + expected => 77.49, + conf => '0 1', + field => 'unitpricegsti' + } + ); + compare( + { + got => $order_0_1->{unitpricegste}, + expected => 73.80, + conf => '0 1', + field => 'unitpricegste' + } + ); + compare( + { + got => $order_0_1->{gstvalue}, + expected => 7.38, + conf => '0 1', + field => 'gstvalue' + } + ); + compare( + { + got => $order_0_1->{totalgsti}, + expected => 154.98, + conf => '0 1', + field => 'totalgsti' + } + ); + compare( + { + got => $order_0_1->{totalgste}, + expected => 147.60, + conf => '0 1', + field => 'totalgste' + } + ); }; } -- 2.39.5