From b760eb47256c3a4284df2d01bd042c164d7e94b0 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 26 Jan 2022 15:58:26 +0100 Subject: [PATCH] Bug 29955: Move populate_order_with_prices to Koha namespace MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit and split the subroutine into 2 smaller subroutines (one for ordering, the other for receiving) Test plan: 1. Create a vendor and an acquisition basket 2. In this basket, create new orders using all the different methods (from an existing record, from a suggestion, from a new record, ...) then close the basket and receive these orders. Make sure it works the same with and without the patch 3. Run tests in t/Prices.t, t/db_dependent/Acquisition/populate_order_with_prices.t, and t/db_dependent/Budgets.t Signed-off-by: Joonas Kylmälä Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi --- C4/Acquisition.pm | 134 ----------- Koha/Acquisition/Order.pm | 129 ++++++++++ acqui/addorder.pl | 121 +++++----- acqui/addorderiso2709.pl | 39 +-- acqui/finishreceive.pl | 22 +- t/Prices.t | 224 +++++++----------- .../Acquisition/populate_order_with_prices.t | 190 +++++++-------- t/db_dependent/Budgets.t | 24 +- 8 files changed, 392 insertions(+), 491 deletions(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index 51ece49e66..899dcc076a 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -67,7 +67,6 @@ BEGIN { GetOrderFromItemnumber SearchOrders GetHistory GetRecentAcqui ModReceiveOrder CancelReceipt - populate_order_with_prices TransferOrder ModItemOrder @@ -2825,139 +2824,6 @@ sub GetBiblioCountByBasketno { return $sth->fetchrow; } -=head3 populate_order_with_prices - -$order = populate_order_with_prices({ - order => $order #a hashref with the order values - booksellerid => $booksellerid #FIXME - should obtain from order basket - receiving => 1 # boolean representing order stage, should pass only this or ordering - ordering => 1 # boolean representing order stage -}); - - -Sets calculated values for an order - all values are stored with full precision -regardless of rounding preference except for tax value which is calculated -on rounded values if requested - -For ordering the values set are: - rrp_tax_included - rrp_tax_excluded - ecost_tax_included - ecost_tax_excluded - tax_value_on_ordering -For receiving the value set are: - unitprice_tax_included - unitprice_tax_excluded - tax_value_on_receiving - -Note: When receiving, if the rounded value of the unitprice matches the rounded -value of the ecost then then ecost (full precision) is used. - -Returns a hashref of the order - -FIXME: Move this to Koha::Acquisition::Order.pm - -=cut - -sub populate_order_with_prices { - my ($params) = @_; - - my $order = $params->{order}; - my $booksellerid = $params->{booksellerid}; - return unless $booksellerid; - - my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid ); - - my $receiving = $params->{receiving}; - my $ordering = $params->{ordering}; - my $discount = $order->{discount}; - $discount /= 100 if $discount > 1; - - if ($ordering) { - $order->{tax_rate_on_ordering} //= $order->{tax_rate}; - if ( $bookseller->listincgst ) { - - # The user entered the prices tax included - $order->{unitprice} += 0; - $order->{unitprice_tax_included} = $order->{unitprice}; - $order->{rrp_tax_included} = $order->{rrp}; - - # price tax excluded = price tax included / ( 1 + tax rate ) - $order->{unitprice_tax_excluded} = $order->{unitprice_tax_included} / ( 1 + $order->{tax_rate_on_ordering} ); - $order->{rrp_tax_excluded} = $order->{rrp_tax_included} / ( 1 + $order->{tax_rate_on_ordering} ); - - # ecost tax included = rrp tax included ( 1 - discount ) - $order->{ecost_tax_included} = $order->{rrp_tax_included} * ( 1 - $discount ); - - # ecost tax excluded = rrp tax excluded * ( 1 - discount ) - $order->{ecost_tax_excluded} = $order->{rrp_tax_excluded} * ( 1 - $discount ); - - # tax value = quantity * ecost tax excluded * tax rate - # we should use the unitprice if included - my $cost_tax_included = $order->{unitprice_tax_included} == 0 ? $order->{ecost_tax_included} : $order->{unitprice_tax_included}; - my $cost_tax_excluded = $order->{unitprice_tax_excluded} == 0 ? $order->{ecost_tax_excluded} : $order->{unitprice_tax_excluded}; - $order->{tax_value_on_ordering} = ( get_rounded_price($cost_tax_included) - get_rounded_price($cost_tax_excluded) ) * $order->{quantity}; - - } - else { - # The user entered the prices tax excluded - $order->{unitprice_tax_excluded} = $order->{unitprice}; - $order->{rrp_tax_excluded} = $order->{rrp}; - - # price tax included = price tax excluded * ( 1 - tax rate ) - $order->{unitprice_tax_included} = $order->{unitprice_tax_excluded} * ( 1 + $order->{tax_rate_on_ordering} ); - $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 ); - - # ecost tax included = rrp tax excluded * ( 1 + tax rate ) * ( 1 - discount ) = ecost tax excluded * ( 1 + tax rate ) - $order->{ecost_tax_included} = $order->{ecost_tax_excluded} * ( 1 + $order->{tax_rate_on_ordering} ); - - # tax value = quantity * ecost tax included * tax rate - # we should use the unitprice if included - my $cost_tax_excluded = $order->{unitprice_tax_excluded} == 0 ? $order->{ecost_tax_excluded} : $order->{unitprice_tax_excluded}; - $order->{tax_value_on_ordering} = $order->{quantity} * get_rounded_price($cost_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 - if ( Koha::Number::Price->new( $order->{unitprice} )->round == Koha::Number::Price->new( $order->{ecost_tax_included} )->round ) { - $order->{unitprice} = $order->{ecost_tax_included}; - } - - # The user entered the unit price tax included - $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_on_receiving} ); - } - else { - # 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 - if ( Koha::Number::Price->new( $order->{unitprice} )->round == Koha::Number::Price->new( $order->{ecost_tax_excluded} )->round ) { - $order->{unitprice} = $order->{ecost_tax_excluded}; - } - - # 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_on_receiving} ); - } - - # tax value = quantity * unit price tax excluded * tax rate - $order->{tax_value_on_receiving} = $order->{quantity} * get_rounded_price($order->{unitprice_tax_excluded}) * $order->{tax_rate_on_receiving}; - } - - return $order; -} - =head3 GetOrderUsers $order_users_ids = &GetOrderUsers($ordernumber); diff --git a/Koha/Acquisition/Order.pm b/Koha/Acquisition/Order.pm index 214d7fe69c..d9161a3e9b 100644 --- a/Koha/Acquisition/Order.pm +++ b/Koha/Acquisition/Order.pm @@ -20,6 +20,7 @@ use Modern::Perl; use Carp qw( croak ); use C4::Biblio qw( DelBiblio ); +use C4::Acquisition; use Koha::Acquisition::Baskets; use Koha::Acquisition::Funds; @@ -31,6 +32,7 @@ use Koha::Exceptions::Object; use Koha::Biblios; use Koha::Holds; use Koha::Items; +use Koha::Number::Price; use Koha::Subscriptions; use base qw(Koha::Object); @@ -457,6 +459,133 @@ sub duplicate_to { return $new_order; } +=head3 populate_with_prices_for_ordering + +Sets calculated values for an order - all values are stored with full precision +regardless of rounding preference except for tax value which is calculated on +rounded values if requested + + $order->populate_with_prices_for_ordering() + +The values set are: + rrp_tax_included + rrp_tax_excluded + ecost_tax_included + ecost_tax_excluded + tax_value_on_ordering + +=cut + +sub populate_with_prices_for_ordering { + my ($self) = @_; + + my $bookseller = $self->basket->bookseller; + return unless $bookseller; + + my $discount = $self->discount || 0; + $discount /= 100 if $discount > 1; + + if ( $bookseller->listincgst ) { + # The user entered the prices tax included + $self->unitprice($self->unitprice + 0); + $self->unitprice_tax_included($self->unitprice); + $self->rrp_tax_included($self->rrp); + + # price tax excluded = price tax included / ( 1 + tax rate ) + $self->unitprice_tax_excluded( $self->unitprice_tax_included / ( 1 + $self->tax_rate_on_ordering ) ); + $self->rrp_tax_excluded( $self->rrp_tax_included / ( 1 + $self->tax_rate_on_ordering ) ); + + # ecost tax included = rrp tax included ( 1 - discount ) + $self->ecost_tax_included($self->rrp_tax_included * ( 1 - $discount )); + + # ecost tax excluded = rrp tax excluded * ( 1 - discount ) + $self->ecost_tax_excluded($self->rrp_tax_excluded * ( 1 - $discount )); + + # tax value = quantity * ecost tax excluded * tax rate + # we should use the unitprice if included + my $cost_tax_included = $self->unitprice_tax_included == 0 ? $self->ecost_tax_included : $self->unitprice_tax_included; + my $cost_tax_excluded = $self->unitprice_tax_excluded == 0 ? $self->ecost_tax_excluded : $self->unitprice_tax_excluded; + $self->tax_value_on_ordering( ( C4::Acquisition::get_rounded_price($cost_tax_included) - C4::Acquisition::get_rounded_price($cost_tax_excluded) ) * $self->quantity ); + } else { + # The user entered the prices tax excluded + $self->unitprice_tax_excluded($self->unitprice); + $self->rrp_tax_excluded($self->rrp); + + # price tax included = price tax excluded * ( 1 - tax rate ) + $self->unitprice_tax_included($self->unitprice_tax_excluded * ( 1 + $self->tax_rate_on_ordering )); + $self->rrp_tax_included($self->rrp_tax_excluded * ( 1 + $self->tax_rate_on_ordering )); + + # ecost tax excluded = rrp tax excluded * ( 1 - discount ) + $self->ecost_tax_excluded($self->rrp_tax_excluded * ( 1 - $discount )); + + # ecost tax included = rrp tax excluded * ( 1 + tax rate ) * ( 1 - discount ) = ecost tax excluded * ( 1 + tax rate ) + $self->ecost_tax_included($self->ecost_tax_excluded * ( 1 + $self->tax_rate_on_ordering )); + + # tax value = quantity * ecost tax included * tax rate + # we should use the unitprice if included + my $cost_tax_excluded = $self->unitprice_tax_excluded == 0 ? $self->ecost_tax_excluded : $self->unitprice_tax_excluded; + $self->tax_value_on_ordering($self->quantity * C4::Acquisition::get_rounded_price($cost_tax_excluded) * $self->tax_rate_on_ordering); + } +} + +=head3 populate_with_prices_for_receiving + +Sets calculated values for an order - all values are stored with full precision +regardless of rounding preference except for tax value which is calculated on +rounded values if requested + + $order->populate_with_prices_for_receiving() + +The values set are: + unitprice_tax_included + unitprice_tax_excluded + tax_value_on_receiving + +Note: When receiving, if the rounded value of the unitprice matches the rounded +value of the ecost then then ecost (full precision) is used. + +=cut + +sub populate_with_prices_for_receiving { + my ($self) = @_; + + my $bookseller = $self->basket->bookseller; + return unless $bookseller; + + my $discount = $self->discount || 0; + $discount /= 100 if $discount > 1; + + 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 + if ( Koha::Number::Price->new( $self->unitprice )->round == Koha::Number::Price->new( $self->ecost_tax_included )->round ) { + $self->unitprice($self->ecost_tax_included); + } + + # The user entered the unit price tax included + $self->unitprice_tax_included($self->unitprice); + + # unit price tax excluded = unit price tax included / ( 1 + tax rate ) + $self->unitprice_tax_excluded($self->unitprice_tax_included / ( 1 + $self->tax_rate_on_receiving )); + } else { + # 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 + if ( Koha::Number::Price->new($self->unitprice)->round == Koha::Number::Price->new($self->ecost_tax_excluded)->round ) { + $self->unitprice($self->ecost_tax_excluded); + } + + # The user entered the unit price tax excluded + $self->unitprice_tax_excluded($self->unitprice); + + + # unit price tax included = unit price tax included * ( 1 + tax rate ) + $self->unitprice_tax_included($self->unitprice_tax_excluded * ( 1 + $self->tax_rate_on_receiving )); + } + + # tax value = quantity * unit price tax excluded * tax rate + $self->tax_value_on_receiving($self->quantity * C4::Acquisition::get_rounded_price($self->unitprice_tax_excluded) * $self->tax_rate_on_receiving); +} + =head3 to_api_mapping This method returns the mapping for representing a Koha::Acquisition::Order object diff --git a/acqui/addorder.pl b/acqui/addorder.pl index 318b148c8a..abd576256e 100755 --- a/acqui/addorder.pl +++ b/acqui/addorder.pl @@ -72,7 +72,7 @@ the ISBN of the record ordered. =item C the quantity to order. -=item C +=item C the price of this order. =item C @@ -121,7 +121,7 @@ use Modern::Perl; use CGI qw ( -utf8 ); use JSON qw ( to_json encode_json ); use C4::Auth qw( get_template_and_user ); -use C4::Acquisition qw( FillWithDefaultValues populate_order_with_prices ModOrder ModOrderUsers ); +use C4::Acquisition qw( FillWithDefaultValues ModOrderUsers ); use C4::Suggestions qw( ModSuggestion ); use C4::Biblio qw( AddBiblio @@ -228,9 +228,30 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( ); # get CGI parameters -my $orderinfo = $input->Vars; -$orderinfo->{'list_price'} ||= 0; -$orderinfo->{'uncertainprice'} ||= 0; +my $orderinfo = { + ordernumber => scalar $input->param('ordernumber'), + basketno => scalar $input->param('basketno'), + biblionumber => scalar $input->param('biblionumber'), + invoiceid => scalar $input->param('invoiceid'), + quantity => scalar $input->param('quantity'), + budget_id => scalar $input->param('budget_id'), + currency => scalar $input->param('currency'), + listprice => scalar $input->param('listprice'), + uncertainprice => scalar $input->param('uncertainprice'), + tax_rate_on_ordering => scalar $input->param('tax_rate'), + discount => scalar $input->param('discount'), + rrp => scalar $input->param('rrp'), + replacementprice => scalar $input->param('replacementprice'), + ecost => scalar $input->param('ecost'), + unitprice => scalar $input->param('unitprice'), + order_internalnote => scalar $input->param('order_internalnote'), + order_vendornote => scalar $input->param('order_vendornote'), + sort1 => scalar $input->param('sort1'), + sort2 => scalar $input->param('sort2'), + subscriptionid => scalar $input->param('subscriptionid'), +}; + +$orderinfo->{uncertainprice} ||= 0; $orderinfo->{subscriptionid} ||= undef; my $user = $input->remote_user; @@ -272,18 +293,18 @@ if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) { #if it doesn't create it $record = TransformKohaToMarc( { - "biblio.title" => "$$orderinfo{title}", - "biblio.author" => $$orderinfo{author} ? $$orderinfo{author} : "", - "biblio.seriestitle" => $$orderinfo{series} ? $$orderinfo{series} : "", - "biblioitems.isbn" => $$orderinfo{isbn} ? $$orderinfo{isbn} : "", - "biblioitems.ean" => $$orderinfo{ean} ? $$orderinfo{ean} : "", - "biblioitems.publishercode" => $$orderinfo{publishercode} ? $$orderinfo{publishercode} : "", - "biblioitems.publicationyear" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "", - "biblio.copyrightdate" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "", - "biblioitems.itemtype" => $$orderinfo{itemtype} ? $$orderinfo{itemtype} : "", - "biblioitems.editionstatement"=> $$orderinfo{editionstatement} ? $$orderinfo{editionstatement} : "", - }); - + "biblio.title" => $input->param('title') || '', + "biblio.author" => $input->param('author') || '', + "biblio.seriestitle" => $input->param('series') || '', + "biblioitems.isbn" => $input->param('isbn') || '', + "biblioitems.ean" => $input->param('ean') || '', + "biblioitems.publishercode" => $input->param('publishercode') || '', + "biblioitems.publicationyear" => $input->param('publicationyear') || '', + "biblio.copyrightdate" => $input->param('publicationyear') || '', + "biblioitems.itemtype" => $input->param('itemtype') || '', + "biblioitems.editionstatement"=> $input->param('editionstatement') || '', + } + ); } C4::Acquisition::FillWithDefaultValues( $record ); @@ -294,10 +315,10 @@ if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) { } # change suggestion status if applicable - if ( $orderinfo->{suggestionid} ) { + if ( my $suggestionid = $input->param('suggestionid') ) { ModSuggestion( { - suggestionid => $orderinfo->{suggestionid}, + suggestionid => $suggestionid, biblionumber => $orderinfo->{biblionumber}, STATUS => 'ORDERED', } @@ -306,47 +327,31 @@ if ( $basket->{is_standing} || $orderinfo->{quantity} ne '0' ) { $orderinfo->{unitprice} = $orderinfo->{ecost} if not defined $orderinfo->{unitprice} or $orderinfo->{unitprice} eq ''; - $orderinfo = C4::Acquisition::populate_order_with_prices( - { - order => $orderinfo, - booksellerid => $orderinfo->{booksellerid}, - ordering => 1, - } - ); - - # if we already have $ordernumber, then it's an ordermodif - my $order = Koha::Acquisition::Order->new($orderinfo); + my $order; + my $log_action_name; if ( $orderinfo->{ordernumber} ) { - ModOrder($orderinfo); - # Log the order modification - if (C4::Context->preference("AcquisitionLog")) { - my $infos = {}; - foreach my $field(@log_order_fields) { - $infos->{$field} = $orderinfo->{$field}; - } - logaction( - 'ACQUISITIONS', - 'MODIFY_ORDER', - $orderinfo->{ordernumber}, - encode_json($infos) - ); - } + $order = Koha::Acquisition::Orders->find($orderinfo->{ordernumber}); + $order->set($orderinfo); + $log_action_name = 'MODIFY_ORDER'; + } else { + $order = Koha::Acquisition::Order->new($orderinfo); + $log_action_name = 'CREATE_ORDER'; } - else { # else, it's a new line - $order->store; - # Log the order creation - if (C4::Context->preference("AcquisitionLog")) { - my $infos = {}; - foreach my $field(@log_order_fields) { - $infos->{$field} = $orderinfo->{$field}; - } - logaction( - 'ACQUISITIONS', - 'CREATE_ORDER', - $order->ordernumber, - encode_json($infos) - ); + $order->populate_with_prices_for_ordering(); + $order->store; + + # Log the order creation + if (C4::Context->preference("AcquisitionLog")) { + my $infos = {}; + foreach my $field(@log_order_fields) { + $infos->{$field} = $order->$field; } + logaction( + 'ACQUISITIONS', + $log_action_name, + $order->ordernumber, + encode_json($infos) + ); } my $order_users_ids = $input->param('users_ids'); my @order_users = split( /:/, $order_users_ids ); @@ -417,7 +422,7 @@ if (C4::Context->preference("AcquisitionLog") && $basketno) { } my $booksellerid=$$orderinfo{booksellerid}; -if (my $import_batch_id=$$orderinfo{import_batch_id}) { +if (my $import_batch_id = $input->param('import_batch_id')) { print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=$basketno&booksellerid=$booksellerid"); } elsif ( defined $orderinfo->{invoiceid} ) { print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=" . $orderinfo->{invoiceid}); diff --git a/acqui/addorderiso2709.pl b/acqui/addorderiso2709.pl index 9d5911564c..aecb0ef8eb 100755 --- a/acqui/addorderiso2709.pl +++ b/acqui/addorderiso2709.pl @@ -33,7 +33,6 @@ use C4::Output qw( output_html_with_http_headers ); use C4::ImportBatch qw( GetImportRecordsRange GetImportRecordMarc GetImportRecordMatches SetImportRecordStatus SetMatchedBiblionumber SetImportBatchStatus GetImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction ); use C4::Matcher; use C4::Search qw( FindDuplicate ); -use C4::Acquisition qw( populate_order_with_prices ); use C4::Biblio qw( AddBiblio GetMarcFromKohaField @@ -43,7 +42,6 @@ use C4::Biblio qw( ); use C4::Items qw( PrepareItemrecordDisplay AddItemFromMarc ); use C4::Budgets qw( GetBudget GetBudgets GetBudgetHierarchy CanUserUseBudget GetBudgetByCode ); -use C4::Acquisition qw( populate_order_with_prices ); use C4::Suggestions; # GetSuggestion use C4::Members; @@ -273,7 +271,8 @@ if ($op eq ""){ # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation $price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR"; $price = Koha::Number::Price->new($price)->unformat; - $orderinfo{tax_rate} = $bookseller->tax_rate; + $orderinfo{tax_rate_on_ordering} = $bookseller->tax_rate; + $orderinfo{tax_rate_on_receiving} = $bookseller->tax_rate; my $c = $c_discount ? $c_discount : $bookseller->discount; $orderinfo{discount} = $c; if ( $c ) { @@ -282,7 +281,6 @@ if ($op eq ""){ } $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate; $orderinfo{unitprice} = $orderinfo{ecost}; - $orderinfo{total} = $orderinfo{ecost} * $infos->{quantity}; } else { $orderinfo{listprice} = 0; } @@ -291,18 +289,9 @@ if ($op eq ""){ # remove uncertainprice flag if we have found a price in the MARC record $orderinfo{uncertainprice} = 0 if $orderinfo{listprice}; - %orderinfo = %{ - C4::Acquisition::populate_order_with_prices( - { - order => \%orderinfo, - booksellerid => $booksellerid, - ordering => 1, - receiving => 1, - } - ) - }; - my $order = Koha::Acquisition::Order->new( \%orderinfo )->store; + $order->populate_with_prices_for_ordering(); + $order->populate_with_prices_for_receiving(); $order->add_item( $_ ) for @{ $budget_hash->{$budget_id}->{itemnumbers} }; } } @@ -331,7 +320,8 @@ if ($op eq ""){ # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation $c_price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR"; $c_price = Koha::Number::Price->new($c_price)->unformat; - $orderinfo{tax_rate} = $bookseller->tax_rate; + $orderinfo{tax_rate_on_ordering} = $bookseller->tax_rate; + $orderinfo{tax_rate_on_receiving} = $bookseller->tax_rate; my $c = $c_discount ? $c_discount : $bookseller->discount; $orderinfo{discount} = $c; if ( $c ) { @@ -340,7 +330,6 @@ if ($op eq ""){ } $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate; $orderinfo{unitprice} = $orderinfo{ecost}; - $orderinfo{total} = $orderinfo{ecost} * $c_quantity; } else { $orderinfo{listprice} = 0; } @@ -348,18 +337,10 @@ if ($op eq ""){ # remove uncertainprice flag if we have found a price in the MARC record $orderinfo{uncertainprice} = 0 if $orderinfo{listprice}; - %orderinfo = %{ - C4::Acquisition::populate_order_with_prices( - { - order => \%orderinfo, - booksellerid => $booksellerid, - ordering => 1, - receiving => 1, - } - ) - }; - - my $order = Koha::Acquisition::Order->new( \%orderinfo )->store; + my $order = Koha::Acquisition::Order->new( \%orderinfo ); + $order->populate_with_prices_for_ordering(); + $order->populate_with_prices_for_receiving(); + $order->store; # 4th, add items if applicable # parse the item sent by the form, and create an item just for the import_record_id we are dealing with diff --git a/acqui/finishreceive.pl b/acqui/finishreceive.pl index c2d51686eb..b0dd1b7fae 100755 --- a/acqui/finishreceive.pl +++ b/acqui/finishreceive.pl @@ -26,7 +26,7 @@ use C4::Auth qw( checkauth ); use JSON qw( encode_json ); use C4::Output; use C4::Context; -use C4::Acquisition qw( GetInvoice GetOrder populate_order_with_prices ModReceiveOrder ); +use C4::Acquisition qw( GetInvoice GetOrder ModReceiveOrder ); use C4::Biblio qw( GetFrameworkCode GetMarcFromKohaField TransformHtmlToXml ); use C4::Items qw( GetMarcItem ModItemFromMarc AddItemFromMarc ); use C4::Log qw(logaction); @@ -98,29 +98,23 @@ if ($quantityrec > $origquantityrec ) { } } - $order->{order_internalnote} = $input->param("order_internalnote"); - $order->{tax_rate_on_receiving} = $input->param("tax_rate"); - $order->{replacementprice} = $replacementprice; - $order->{unitprice} = $unitprice; + $order_obj->order_internalnote(scalar $input->param("order_internalnote")); + $order_obj->tax_rate_on_receiving(scalar $input->param("tax_rate")); + $order_obj->replacementprice($replacementprice); + $order_obj->unitprice($unitprice); - $order = C4::Acquisition::populate_order_with_prices( - { - order => $order, - booksellerid => $booksellerid, - receiving => 1 - } - ); + $order_obj->populate_with_prices_for_receiving(); # save the quantity received. if ( $quantityrec > 0 ) { if ( $order_obj->subscriptionid ) { # Quantity can only be modified if linked to a subscription - $order->{quantity} = $quantity; # quantityrec will be deduced from this value in ModReceiveOrder + $order_obj->quantity($quantity); # quantityrec will be deduced from this value in ModReceiveOrder } ( $datereceived, $new_ordernumber ) = ModReceiveOrder( { biblionumber => $biblionumber, - order => $order, + order => $order_obj->unblessed, quantityreceived => $quantityrec, user => $user, invoice => $invoice, diff --git a/t/Prices.t b/t/Prices.t index b0832c5977..2217c27e64 100755 --- a/t/Prices.t +++ b/t/Prices.t @@ -8,13 +8,12 @@ use Module::Load::Conditional qw/check_install/; BEGIN { if ( check_install( module => 'Test::DBIx::Class' ) ) { - plan tests => 16; + plan tests => 15; } else { plan skip_all => "Need Test::DBIx::Class" } } -use_ok('C4::Acquisition', qw( populate_order_with_prices )); use_ok('C4::Context'); use_ok('Koha::Number::Price'); @@ -38,11 +37,18 @@ fixtures_ok [ [ 3, '1 0', 1, 0 ], [ 4, '1 1', 1, 1 ], ], + Aqbasket => [ + [ qw/ basketno basketname booksellerid / ], + [ 1, '0 0', 1 ], + [ 2, '0 1', 2 ], + [ 3, '1 0', 3 ], + [ 4, '1 1', 4 ], + ], ], 'add currency fixtures'; my $bookseller_module = Test::MockModule->new('Koha::Acquisition::Bookseller'); -my ( $basketno_0_0, $basketno_1_1 ); +my ( $basketno_0_0, $basketno_0_1, $basketno_1_0, $basketno_1_1 ) = (1, 2, 3, 4); my ( $invoiceid_0_0, $invoiceid_1_1 ); my $today; @@ -53,7 +59,7 @@ for my $currency_format ( qw( US FR ) ) { my $biblionumber_0_0 = 42; - my $order_0_0 = { + my $order_0_0 = Koha::Acquisition::Order->new({ biblionumber => $biblionumber_0_0, quantity => 2, listprice => 82, @@ -63,21 +69,16 @@ for my $currency_format ( qw( US FR ) ) { invoiceid => $invoiceid_0_0, rrp => 82.00, ecost => 73.80, - tax_rate => 0.0500, + tax_rate_on_ordering => 0.0500, + tax_rate_on_receiving => 0.0500, discount => 10, datereceived => $today - }; - $order_0_0 = C4::Acquisition::populate_order_with_prices( - { - order => $order_0_0, - booksellerid => 1, - ordering => 1, - } - ); + }); + $order_0_0->populate_with_prices_for_ordering(); compare( { - got => $order_0_0->{rrp_tax_included}, + got => $order_0_0->rrp_tax_included, expected => 86.10, conf => '0 0', field => 'rrp_tax_included' @@ -85,7 +86,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_0_0->{rrp_tax_excluded}, + got => $order_0_0->rrp_tax_excluded, expected => 82.00, conf => '0 0', field => 'rrp_tax_excluded' @@ -93,7 +94,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_0_0->{ecost_tax_included}, + got => $order_0_0->ecost_tax_included, expected => 77.49, conf => '0 0', field => 'ecost_tax_included' @@ -101,7 +102,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_0_0->{ecost_tax_excluded}, + got => $order_0_0->ecost_tax_excluded, expected => 73.80, conf => '0 0', field => 'ecost_tax_excluded' @@ -109,24 +110,18 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_0_0->{tax_value_on_ordering}, + 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, - booksellerid => 1, - receiving => 1, - } - ); + $order_0_0->populate_with_prices_for_receiving(); compare( { - got => $order_0_0->{unitprice_tax_included}, + got => $order_0_0->unitprice_tax_included, expected => 77.49, conf => '0 0', field => 'unitprice_tax_included' @@ -134,7 +129,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_0_0->{unitprice_tax_excluded}, + got => $order_0_0->unitprice_tax_excluded, expected => 73.80, conf => '0 0', field => 'unitprice_tax_excluded' @@ -142,7 +137,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_0_0->{tax_value_on_receiving}, + got => $order_0_0->tax_value_on_receiving, expected => 7.38, conf => '0 0', field => 'tax_value' @@ -154,7 +149,7 @@ for my $currency_format ( qw( US FR ) ) { plan tests => 11; my $biblionumber_1_1 = 43; - my $order_1_1 = { + my $order_1_1 = Koha::Acquisition::Order->new({ biblionumber => $biblionumber_1_1, quantity => 2, listprice => 82, @@ -164,22 +159,17 @@ for my $currency_format ( qw( US FR ) ) { invoiceid => $invoiceid_1_1, rrp => 82.00, ecost => 73.80, - tax_rate => 0.0500, + tax_rate_on_ordering => 0.0500, + tax_rate_on_receiving => 0.0500, discount => 10, datereceived => $today - }; + }); - $order_1_1 = C4::Acquisition::populate_order_with_prices( - { - order => $order_1_1, - booksellerid => 4, - ordering => 1, - } - ); + $order_1_1->populate_with_prices_for_ordering(); compare( { - got => $order_1_1->{rrp_tax_included}, + got => $order_1_1->rrp_tax_included, expected => 82.00, conf => '1 1', field => 'rrp_tax_included' @@ -187,7 +177,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_1->{rrp_tax_excluded}, + got => $order_1_1->rrp_tax_excluded, expected => 78.10, conf => '1 1', field => 'rrp_tax_excluded' @@ -195,7 +185,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_1->{ecost_tax_included}, + got => $order_1_1->ecost_tax_included, expected => 73.80, conf => '1 1', field => 'ecost_tax_included' @@ -203,7 +193,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_1->{ecost_tax_excluded}, + got => $order_1_1->ecost_tax_excluded, expected => 70.29, conf => '1 1', field => 'ecost_tax_excluded' @@ -211,24 +201,18 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_1->{tax_value_on_ordering}, + got => $order_1_1->tax_value_on_ordering, expected => 7.03, conf => '1 1', field => 'tax_value' } ); - $order_1_1 = C4::Acquisition::populate_order_with_prices( - { - order => $order_1_1, - booksellerid => 4, - receiving => 1, - } - ); + $order_1_1->populate_with_prices_for_receiving(); compare( { - got => $order_1_1->{unitprice_tax_included}, + got => $order_1_1->unitprice_tax_included, expected => 73.80, conf => '1 1', field => 'unitprice_tax_included' @@ -236,7 +220,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_1->{unitprice_tax_excluded}, + got => $order_1_1->unitprice_tax_excluded, expected => 70.29, conf => '1 1', field => 'unitprice_tax_excluded' @@ -244,15 +228,17 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_1->{tax_value_on_receiving}, + got => $order_1_1->tax_value_on_receiving, expected => 7.03, conf => '1 1', field => 'tax_value' } ); - # When unitprice is 0.00 C4::Acquisition->populate_order_with_prices() falls back to using ecost_tax_included and ecost_tax_excluded - $order_1_1 = { + # When unitprice is 0.00 + # Koha::Acquisition::Order::populate_with_prices_for_ordering() falls + # back to using ecost_tax_included and ecost_tax_excluded + $order_1_1 = Koha::Acquisition::Order->new({ biblionumber => $biblionumber_1_1, quantity => 1, listprice => 10, @@ -262,22 +248,17 @@ for my $currency_format ( qw( US FR ) ) { invoiceid => $invoiceid_1_1, rrp => 10.00, ecost => 10.00, - tax_rate => 0.1500, + tax_rate_on_ordering => 0.1500, + tax_rate_on_receiving => 0.1500, discount => 0, datereceived => $today - }; + }); - $order_1_1 = C4::Acquisition::populate_order_with_prices( - { - order => $order_1_1, - booksellerid => 4, - ordering => 1, - } - ); + $order_1_1->populate_with_prices_for_ordering(); compare( { - got => $order_1_1->{ecost_tax_included}, + got => $order_1_1->ecost_tax_included, expected => 10.00, conf => '1 1', field => 'ecost_tax_included' @@ -285,7 +266,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_1->{ecost_tax_excluded}, + got => $order_1_1->ecost_tax_excluded, expected => 8.70, conf => '1 1', field => 'ecost_tax_excluded' @@ -293,7 +274,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_1->{tax_value_on_ordering}, + got => $order_1_1->tax_value_on_ordering, expected => 1.30, conf => '1 1', field => 'tax_value' @@ -305,32 +286,27 @@ for my $currency_format ( qw( US FR ) ) { plan tests => 9; my $biblionumber_1_0 = 44; - my $order_1_0 = { + my $order_1_0 = Koha::Acquisition::Order->new({ biblionumber => $biblionumber_1_0, quantity => 2, listprice => 82, unitprice => 0, quantityreceived => 2, - basketno => $basketno_1_1, + basketno => $basketno_1_0, invoiceid => $invoiceid_1_1, rrp => 82.00, ecost => 73.80, - tax_rate => 0.0500, + tax_rate_on_ordering => 0.0500, + tax_rate_on_receiving => 0.0500, discount => 10, datereceived => $today - }; + }); - $order_1_0 = C4::Acquisition::populate_order_with_prices( - { - order => $order_1_0, - booksellerid => 3, - ordering => 1, - } - ); + $order_1_0->populate_with_prices_for_ordering(); compare( { - got => $order_1_0->{rrp_tax_included}, + got => $order_1_0->rrp_tax_included, expected => 82, conf => '1 0', field => 'rrp_tax_included' @@ -338,7 +314,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_0->{rrp_tax_excluded}, + got => $order_1_0->rrp_tax_excluded, expected => 78.10, conf => '1 0', field => 'rrp_tax_excluded' @@ -346,7 +322,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_0->{ecost_tax_included}, + got => $order_1_0->ecost_tax_included, expected => 73.80, conf => '1 0', field => 'ecost_tax_included' @@ -354,7 +330,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_0->{ecost_tax_excluded}, + got => $order_1_0->ecost_tax_excluded, expected => 70.29, conf => '1 0', field => 'ecost_tax_excluded' @@ -364,41 +340,30 @@ for my $currency_format ( qw( US FR ) ) { # (note that in addorder.pl and addorderiso2709 the unitprice may/will be set to the ecost compare( { - got => $order_1_0->{tax_value_on_ordering}, + got => $order_1_0->tax_value_on_ordering, expected => 7.03, conf => '1 0', field => 'tax_value' } ); - $order_1_0->{unitprice} = 70.29; - $order_1_0 = C4::Acquisition::populate_order_with_prices( - { - order => $order_1_0, - booksellerid => 3, - ordering => 1, - } - ); + $order_1_0->unitprice(70.29); + $order_1_0->populate_with_prices_for_ordering(); + # If a unitprice is provided at ordering, we calculate the tax from that compare( { - got => $order_1_0->{tax_value_on_ordering}, + got => $order_1_0->tax_value_on_ordering, expected => 6.69, conf => '1 0', field => 'tax_value' } ); - $order_1_0 = C4::Acquisition::populate_order_with_prices( - { - order => $order_1_0, - booksellerid => 3, - receiving => 1, - } - ); + $order_1_0->populate_with_prices_for_receiving(); compare( { - got => $order_1_0->{unitprice_tax_included}, + got => $order_1_0->unitprice_tax_included, expected => 73.80, conf => '1 0', field => 'unitprice_tax_included' @@ -406,7 +371,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_0->{unitprice_tax_excluded}, + got => $order_1_0->unitprice_tax_excluded, expected => 70.29, conf => '1 0', field => 'unitprice_tax_excluded' @@ -414,7 +379,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_1_0->{tax_value_on_receiving}, + got => $order_1_0->tax_value_on_receiving, expected => 7.03, conf => '1 0', field => 'tax_value' @@ -426,32 +391,27 @@ for my $currency_format ( qw( US FR ) ) { plan tests => 9; my $biblionumber_0_1 = 45; - my $order_0_1 = { + my $order_0_1 = Koha::Acquisition::Order->new({ biblionumber => $biblionumber_0_1, quantity => 2, listprice => 82, unitprice => 0, quantityreceived => 2, - basketno => $basketno_1_1, + basketno => $basketno_0_1, invoiceid => $invoiceid_1_1, rrp => 82.00, ecost => 73.80, - tax_rate => 0.0500, + tax_rate_on_ordering => 0.0500, + tax_rate_on_receiving => 0.0500, discount => 10, datereceived => $today - }; + }); - $order_0_1 = C4::Acquisition::populate_order_with_prices( - { - order => $order_0_1, - booksellerid => 2, - ordering => 1, - } - ); + $order_0_1->populate_with_prices_for_ordering(); compare( { - got => $order_0_1->{rrp_tax_included}, + got => $order_0_1->rrp_tax_included, expected => 86.10, conf => '0 1', field => 'rrp_tax_included' @@ -459,7 +419,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_0_1->{rrp_tax_excluded}, + got => $order_0_1->rrp_tax_excluded, expected => 82.00, conf => '0 1', field => 'rrp_tax_excluded' @@ -467,7 +427,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_0_1->{ecost_tax_included}, + got => $order_0_1->ecost_tax_included, expected => 77.49, conf => '0 1', field => 'ecost_tax_included' @@ -475,7 +435,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_0_1->{ecost_tax_excluded}, + got => $order_0_1->ecost_tax_excluded, expected => 73.80, conf => '0 1', field => 'ecost_tax_excluded' @@ -485,40 +445,29 @@ for my $currency_format ( qw( US FR ) ) { # (note that in addorder.pl and addorderiso2709 the unitprice may/will be set to the ecost compare( { - got => $order_0_1->{tax_value_on_ordering}, + got => $order_0_1->tax_value_on_ordering, expected => 7.38, conf => '0 1', field => 'tax_value' } ); - $order_0_1->{unitprice} = 77.490000; - $order_0_1 = C4::Acquisition::populate_order_with_prices( - { - order => $order_0_1, - booksellerid => 2, - ordering => 1, - } - ); + $order_0_1->unitprice(77.490000); + $order_0_1->populate_with_prices_for_ordering(); + # If a unitprice is provided at ordering, we calculate the tax from that compare( { - got => $order_0_1->{tax_value_on_ordering}, + got => $order_0_1->tax_value_on_ordering, expected => 7.75, conf => '0 1', field => 'tax_value' } ); - $order_0_1 = C4::Acquisition::populate_order_with_prices( - { - order => $order_0_1, - booksellerid => 2, - receiving => 1, - } - ); + $order_0_1->populate_with_prices_for_receiving(); compare( { - got => $order_0_1->{unitprice_tax_included}, + got => $order_0_1->unitprice_tax_included, expected => 77.49, conf => '0 1', field => 'unitprice_tax_included' @@ -526,7 +475,7 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_0_1->{unitprice_tax_excluded}, + got => $order_0_1->unitprice_tax_excluded, expected => 73.80, conf => '0 1', field => 'unitprice_tax_excluded' @@ -534,14 +483,13 @@ for my $currency_format ( qw( US FR ) ) { ); compare( { - got => $order_0_1->{tax_value_on_receiving}, + got => $order_0_1->tax_value_on_receiving, expected => 7.38, conf => '0 1', field => 'tax_value' } ); }; - } sub compare { diff --git a/t/db_dependent/Acquisition/populate_order_with_prices.t b/t/db_dependent/Acquisition/populate_order_with_prices.t index 0aa3a1af91..3bf9fa32f4 100755 --- a/t/db_dependent/Acquisition/populate_order_with_prices.t +++ b/t/db_dependent/Acquisition/populate_order_with_prices.t @@ -3,9 +3,10 @@ use Modern::Perl; use Test::More tests => 44; -use C4::Acquisition qw( populate_order_with_prices ); use C4::Context; use Koha::Database; +use Koha::Acquisition::Bookseller; +use Koha::Acquisition::Order; use t::lib::TestBuilder; use t::lib::Mocks; @@ -37,158 +38,135 @@ my $bookseller_exc_tax = Koha::Acquisition::Bookseller->new( } )->store; -my $order_exc_tax = { - tax_rate => .1965, +my $basket_exc_tax = Koha::Acquisition::Basket->new( + { + basketname => 'Basket tax excluded', + booksellerid => $bookseller_exc_tax->id, + } +)->store; + +my $order_exc_tax = Koha::Acquisition::Order->new({ + tax_rate_on_ordering => .1965, + tax_rate_on_receiving => .1965, discount => .42, rrp => 16.99, unitprice => "0.00", quantity => 8, -}; + basketno => $basket_exc_tax->basketno, +}); #Vendor prices exclude tax, no rounding, ordering t::lib::Mocks::mock_preference('OrderPriceRounding', ''); -my $order_with_prices = C4::Acquisition::populate_order_with_prices({ - ordering => 1, - booksellerid => $bookseller_exc_tax->id, - order => $order_exc_tax, -}); +$order_exc_tax->populate_with_prices_for_ordering(); -is( $order_with_prices->{rrp_tax_excluded}+0 ,16.99 ,"Ordering tax excluded, no round: rrp tax excluded is rrp"); -is( $order_with_prices->{rrp_tax_included}+0 ,20.328535 ,"Ordering tax excluded, no round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)"); -is( $order_with_prices->{ecost_tax_excluded}+0 ,9.8542 ,"Ordering tax excluded, no round: ecost tax excluded is rrp * ( 1 - discount )"); -is( $order_with_prices->{ecost_tax_included}+0 ,11.7905503 ,"Ordering tax excluded, no round: ecost tax included is ecost tax excluded * (1 + tax rate on ordering)"); -is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4908024 ,"Ordering tax excluded, no round: tax value on ordering is quantity * ecost_tax_excluded * tax rate on ordering if no unitprice"); +is( $order_exc_tax->rrp_tax_excluded+0 ,16.99 ,"Ordering tax excluded, no round: rrp tax excluded is rrp"); +is( $order_exc_tax->rrp_tax_included+0 ,20.328535 ,"Ordering tax excluded, no round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)"); +is( $order_exc_tax->ecost_tax_excluded+0 ,9.8542 ,"Ordering tax excluded, no round: ecost tax excluded is rrp * ( 1 - discount )"); +is( $order_exc_tax->ecost_tax_included+0 ,11.7905503 ,"Ordering tax excluded, no round: ecost tax included is ecost tax excluded * (1 + tax rate on ordering)"); +is( $order_exc_tax->tax_value_on_ordering+0 ,15.4908024 ,"Ordering tax excluded, no round: tax value on ordering is quantity * ecost_tax_excluded * tax rate on ordering if no unitprice"); -$order_exc_tax->{unitprice} = 9.85; +$order_exc_tax->unitprice(9.85); -$order_with_prices = C4::Acquisition::populate_order_with_prices({ - ordering => 1, - booksellerid => $bookseller_exc_tax->id, - order => $order_exc_tax, -}); +$order_exc_tax->populate_with_prices_for_ordering(); -is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85 ,"Ordering tax excluded, no round: rrp tax excluded is rrp"); -is( $order_with_prices->{unitprice_tax_included}+0 ,11.785525 ,"Ordering tax excluded, no round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)"); -is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4842 ,"Ordering tax excluded, no round: tax value on ordering is quantity * unitprice_tax_excluded * tax rate on ordering if unitprice"); +is( $order_exc_tax->unitprice_tax_excluded+0 ,9.85 ,"Ordering tax excluded, no round: rrp tax excluded is rrp"); +is( $order_exc_tax->unitprice_tax_included+0 ,11.785525 ,"Ordering tax excluded, no round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)"); +is( $order_exc_tax->tax_value_on_ordering+0 ,15.4842 ,"Ordering tax excluded, no round: tax value on ordering is quantity * unitprice_tax_excluded * tax rate on ordering if unitprice"); #Vendor prices exclude tax, no rounding, receiving -$order_with_prices = C4::Acquisition::populate_order_with_prices({ - receiving => 1, - booksellerid => $bookseller_exc_tax->id, - order => $order_exc_tax, -}); +$order_exc_tax->populate_with_prices_for_receiving(); -is( $order_with_prices->{unitprice}+0 ,9.8542 ,"Receiving tax excluded, no round, rounded ecost tax excluded = rounded unitprice : unitprice is ecost tax excluded"); -is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.8542 ,"Receiving tax excluded, no round, rounded ecost tax excluded = rounded unitprice : unitprice tax excluded is ecost tax excluded"); -is( $order_with_prices->{unitprice_tax_included}+0 ,11.7905503 ,"Receiving tax excluded, no round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)"); -is( $order_with_prices->{tax_value_on_receiving}+0 ,15.4908024 ,"Receiving tax excluded, no round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving"); +is( $order_exc_tax->unitprice+0 ,9.8542 ,"Receiving tax excluded, no round, rounded ecost tax excluded = rounded unitprice : unitprice is ecost tax excluded"); +is( $order_exc_tax->unitprice_tax_excluded+0 ,9.8542 ,"Receiving tax excluded, no round, rounded ecost tax excluded = rounded unitprice : unitprice tax excluded is ecost tax excluded"); +is( $order_exc_tax->unitprice_tax_included+0 ,11.7905503 ,"Receiving tax excluded, no round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)"); +is( $order_exc_tax->tax_value_on_receiving+0 ,15.4908024 ,"Receiving tax excluded, no round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving"); -$order_exc_tax->{unitprice} = 9.85; +$order_exc_tax->unitprice(9.85); #populate order with prices updates the passed in order hashref #we need to reset after additional tests and changes #Vendor prices exclude tax, rounding to nearest cent, ordering t::lib::Mocks::mock_preference('OrderPriceRounding', 'nearest_cent'); -$order_with_prices = C4::Acquisition::populate_order_with_prices({ - ordering => 1, - booksellerid => $bookseller_exc_tax->id, - order => $order_exc_tax, -}); +$order_exc_tax->populate_with_prices_for_ordering(); -is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85 ,"Ordering tax excluded, round: unitprice tax excluded is unitprice"); -is( $order_with_prices->{unitprice_tax_included}+0 ,11.785525 ,"Ordering tax excluded, round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)"); -is( $order_with_prices->{rrp_tax_excluded}+0 ,16.99 ,"Ordering tax excluded, round: rrp tax excluded is rrp"); -is( $order_with_prices->{rrp_tax_included}+0 ,20.328535 ,"Ordering tax excluded, round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)"); -is( $order_with_prices->{ecost_tax_excluded}+0 ,9.8542 ,"Ordering tax excluded, round: ecost tax excluded is rrp * ( 1 - discount )"); -is( $order_with_prices->{ecost_tax_included}+0 ,11.7905503 ,"Ordering tax excluded, round: ecost tax included is ecost tax excluded * (1 + tax rate on ordering)"); -is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4842 ,"Ordering tax excluded, round: tax value on ordering is quantity * ecost_tax_excluded * tax rate on ordering"); +is( $order_exc_tax->unitprice_tax_excluded+0 ,9.85 ,"Ordering tax excluded, round: unitprice tax excluded is unitprice"); +is( $order_exc_tax->unitprice_tax_included+0 ,11.785525 ,"Ordering tax excluded, round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)"); +is( $order_exc_tax->rrp_tax_excluded+0 ,16.99 ,"Ordering tax excluded, round: rrp tax excluded is rrp"); +is( $order_exc_tax->rrp_tax_included+0 ,20.328535 ,"Ordering tax excluded, round: rrp tax included is rr tax excluded * (1 + tax rate on ordering)"); +is( $order_exc_tax->ecost_tax_excluded+0 ,9.8542 ,"Ordering tax excluded, round: ecost tax excluded is rrp * ( 1 - discount )"); +is( $order_exc_tax->ecost_tax_included+0 ,11.7905503 ,"Ordering tax excluded, round: ecost tax included is ecost tax excluded * (1 + tax rate on ordering)"); +is( $order_exc_tax->tax_value_on_ordering+0 ,15.4842 ,"Ordering tax excluded, round: tax value on ordering is quantity * ecost_tax_excluded * tax rate on ordering"); #Vendor prices exclude tax, no rounding, receiving -$order_with_prices = C4::Acquisition::populate_order_with_prices({ - receiving => 1, - booksellerid => $bookseller_exc_tax->id, - order => $order_exc_tax, -}); +$order_exc_tax->populate_with_prices_for_receiving(); -is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.8542 ,"Receiving tax excluded, round, rounded ecost tax excluded = rounded unitprice : unitprice tax excluded is ecost tax excluded"); -is( $order_with_prices->{unitprice_tax_included}+0 ,11.7905503 ,"Receiving tax excluded, round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)"); -is( $order_with_prices->{tax_value_on_receiving}+0 ,15.4842 ,"Receiving tax excluded, round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving"); +is( $order_exc_tax->unitprice_tax_excluded+0 ,9.8542 ,"Receiving tax excluded, round, rounded ecost tax excluded = rounded unitprice : unitprice tax excluded is ecost tax excluded"); +is( $order_exc_tax->unitprice_tax_included+0 ,11.7905503 ,"Receiving tax excluded, round: unitprice tax included is unitprice tax excluded * (1 + tax rate on ordering)"); +is( $order_exc_tax->tax_value_on_receiving+0 ,15.4842 ,"Receiving tax excluded, round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving"); +my $basket_inc_tax = Koha::Acquisition::Basket->new( + { + basketname => 'Basket tax included', + booksellerid => $bookseller_inc_tax->id, + } +)->store; -my $order_inc_tax = { - tax_rate => .1965, +my $order_inc_tax = Koha::Acquisition::Order->new({ + tax_rate_on_ordering => .1965, + tax_rate_on_receiving => .1965, discount => .42, rrp => 20.33, unitprice => 0.00, quantity => 8, -}; + basketno => $basket_inc_tax->basketno, +}); #Vendor prices include tax, no rounding, ordering t::lib::Mocks::mock_preference('OrderPriceRounding', ''); -$order_with_prices = C4::Acquisition::populate_order_with_prices({ - ordering => 1, - booksellerid => $bookseller_inc_tax->id, - order => $order_inc_tax, -}); +$order_inc_tax->populate_with_prices_for_ordering(); -is( $order_with_prices->{rrp_tax_included}+0 ,20.33 ,"Ordering tax included, no round: rrp tax included is rrp"); -is( $order_with_prices->{rrp_tax_excluded}+0 ,16.9912244045132 ,"Ordering tax included, no round: rrp tax excluded is rrp tax included / (1 + tax rate on ordering)"); -is( $order_with_prices->{ecost_tax_included}+0 ,11.7914 ,"Ordering tax included, no round: ecost tax included is rrp tax included * (1 - discount)"); -is( $order_with_prices->{ecost_tax_excluded}+0 ,9.85491015461764 ,"Ordering tax included, no round: ecost tax excluded is rrp tax excluded * ( 1 - discount )"); -is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4919187630589 ,"Ordering tax included, no round: tax value on ordering is ( ecost tax included - ecost tax excluded ) * quantity if no unitprice"); - -$order_inc_tax->{unitprice} = 11.79; -$order_with_prices = C4::Acquisition::populate_order_with_prices({ - ordering => 1, - booksellerid => $bookseller_inc_tax->id, - order => $order_inc_tax, -}); +is( $order_inc_tax->rrp_tax_included+0 ,20.33 ,"Ordering tax included, no round: rrp tax included is rrp"); +is( $order_inc_tax->rrp_tax_excluded+0 ,16.9912244045132 ,"Ordering tax included, no round: rrp tax excluded is rrp tax included / (1 + tax rate on ordering)"); +is( $order_inc_tax->ecost_tax_included+0 ,11.7914 ,"Ordering tax included, no round: ecost tax included is rrp tax included * (1 - discount)"); +is( $order_inc_tax->ecost_tax_excluded+0 ,9.85491015461764 ,"Ordering tax included, no round: ecost tax excluded is rrp tax excluded * ( 1 - discount )"); +is( $order_inc_tax->tax_value_on_ordering+0 ,15.4919187630589 ,"Ordering tax included, no round: tax value on ordering is ( ecost tax included - ecost tax excluded ) * quantity if no unitprice"); + +$order_inc_tax->unitprice(11.79); +$order_inc_tax->populate_with_prices_for_ordering(); -is( $order_with_prices->{unitprice_tax_included}+0 ,11.79 ,"Ordering tax included, no round: unitprice tax included is unitprice"); -is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85374007521939 ,"Ordering tax included, no round: unitprice tax excluded is unitprice tax included / (1 + tax_rate_on_ordering "); -is( $order_with_prices->{tax_value_on_ordering}+0 ,15.4900793982449 ,"Ordering tax included, no round: tax value on ordering is ( unitprice tax included - unitprice tax excluded ) * quantity if unitprice"); +is( $order_inc_tax->unitprice_tax_included+0 ,11.79 ,"Ordering tax included, no round: unitprice tax included is unitprice"); +is( $order_inc_tax->unitprice_tax_excluded+0 ,9.85374007521939 ,"Ordering tax included, no round: unitprice tax excluded is unitprice tax included / (1 + tax_rate_on_ordering "); +is( $order_inc_tax->tax_value_on_ordering+0 ,15.4900793982449 ,"Ordering tax included, no round: tax value on ordering is ( unitprice tax included - unitprice tax excluded ) * quantity if unitprice"); #Vendor prices include tax, no rounding, receiving -$order_with_prices = C4::Acquisition::populate_order_with_prices({ - receiving => 1, - booksellerid => $bookseller_inc_tax->id, - order => $order_inc_tax, -}); +$order_inc_tax->populate_with_prices_for_receiving(); -is( $order_with_prices->{unitprice}+0 ,11.7914 ,"Receiving tax included, no round, rounded ecost tax excluded = rounded unitprice : unitprice is ecost tax excluded"); -is( $order_with_prices->{unitprice_tax_included}+0 ,11.7914 ,"Receiving tax included, no round: unitprice tax included is unitprice"); -is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85491015461764 ,"Receiving tax included, no round: unitprice tax excluded is unitprice tax included / (1 + tax rate on receiving)"); -is( $order_with_prices->{tax_value_on_receiving}+0 ,15.4919187630589 ,"Receiving tax included, no round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving"); +is( $order_inc_tax->unitprice+0 ,11.7914 ,"Receiving tax included, no round, rounded ecost tax excluded = rounded unitprice : unitprice is ecost tax excluded"); +is( $order_inc_tax->unitprice_tax_included+0 ,11.7914 ,"Receiving tax included, no round: unitprice tax included is unitprice"); +is( $order_inc_tax->unitprice_tax_excluded+0 ,9.85491015461764 ,"Receiving tax included, no round: unitprice tax excluded is unitprice tax included / (1 + tax rate on receiving)"); +is( $order_inc_tax->tax_value_on_receiving+0 ,15.4919187630589 ,"Receiving tax included, no round: tax value on receiving is quantity * unitprice_tax_excluded * tax rate on receiving"); #Vendor prices include tax, rounding to nearest cent, ordering t::lib::Mocks::mock_preference('OrderPriceRounding', 'nearest_cent'); -$order_inc_tax->{unitprice} = 11.79; -$order_with_prices = C4::Acquisition::populate_order_with_prices({ - ordering => 1, - booksellerid => $bookseller_inc_tax->id, - order => $order_inc_tax, -}); +$order_inc_tax->unitprice(11.79); +$order_inc_tax->populate_with_prices_for_ordering(); -is( $order_with_prices->{unitprice_tax_included}+0 ,11.79 ,"Ordering tax included, round: unitprice tax included is unitprice"); -is( $order_with_prices->{unitprice_tax_excluded}+0,9.85374007521939 ,"Ordering tax included, round: unitprice tax excluded is unitprice tax included / (1 + tax_rate_on_ordering "); -is( $order_with_prices->{rrp_tax_included}+0 ,20.33 ,"Ordering tax included, round: rrp tax included is rrp"); -is( $order_with_prices->{rrp_tax_excluded}+0 ,16.9912244045132 ,"Ordering tax included, round: rrp tax excluded is rounded rrp tax included * (1 + tax rate on ordering)"); -is( $order_with_prices->{ecost_tax_included}+0 ,11.7914 ,"Ordering tax included, round: ecost tax included is rounded rrp * ( 1 - discount )"); -is( $order_with_prices->{ecost_tax_excluded}+0 ,9.85491015461764 ,"Ordering tax included, round: ecost tax excluded is rounded ecost tax excluded * (1 - discount)"); -is( $order_with_prices->{tax_value_on_ordering}+0 ,15.52 ,"Ordering tax included, round: tax value on ordering is (ecost_tax_included - ecost_tax_excluded) * quantity"); +is( $order_inc_tax->unitprice_tax_included+0 ,11.79 ,"Ordering tax included, round: unitprice tax included is unitprice"); +is( $order_inc_tax->unitprice_tax_excluded+0,9.85374007521939 ,"Ordering tax included, round: unitprice tax excluded is unitprice tax included / (1 + tax_rate_on_ordering "); +is( $order_inc_tax->rrp_tax_included+0 ,20.33 ,"Ordering tax included, round: rrp tax included is rrp"); +is( $order_inc_tax->rrp_tax_excluded+0 ,16.9912244045132 ,"Ordering tax included, round: rrp tax excluded is rounded rrp tax included * (1 + tax rate on ordering)"); +is( $order_inc_tax->ecost_tax_included+0 ,11.7914 ,"Ordering tax included, round: ecost tax included is rounded rrp * ( 1 - discount )"); +is( $order_inc_tax->ecost_tax_excluded+0 ,9.85491015461764 ,"Ordering tax included, round: ecost tax excluded is rounded ecost tax excluded * (1 - discount)"); +is( $order_inc_tax->tax_value_on_ordering+0 ,15.52 ,"Ordering tax included, round: tax value on ordering is (ecost_tax_included - ecost_tax_excluded) * quantity"); #Vendor prices include tax, no rounding, receiving -$order_with_prices = C4::Acquisition::populate_order_with_prices({ - receiving => 1, - booksellerid => $bookseller_inc_tax->id, - order => $order_inc_tax, -}); +$order_inc_tax->populate_with_prices_for_receiving(); -is( $order_with_prices->{unitprice_tax_included}+0 ,11.7914 ,"Receiving tax included, round: rounded ecost tax included = rounded unitprice : unitprice tax excluded is ecost tax included"); -is( $order_with_prices->{unitprice_tax_excluded}+0 ,9.85491015461764 ,"Receiving tax included, round: unitprice tax excluded is unitprice tax included / (1 + tax rate on ordering)"); -is( $order_with_prices->{tax_value_on_receiving}+0 ,15.4842 ,"Receiving tax included, round: tax value on receiving is quantity * (rounded unitprice_tax_excluded) * tax rate on receiving"); +is( $order_inc_tax->unitprice_tax_included+0 ,11.7914 ,"Receiving tax included, round: rounded ecost tax included = rounded unitprice : unitprice tax excluded is ecost tax included"); +is( $order_inc_tax->unitprice_tax_excluded+0 ,9.85491015461764 ,"Receiving tax included, round: unitprice tax excluded is unitprice tax included / (1 + tax rate on ordering)"); +is( $order_inc_tax->tax_value_on_receiving+0 ,15.4842 ,"Receiving tax included, round: tax value on receiving is quantity * (rounded unitprice_tax_excluded) * tax rate on receiving"); $schema->storage->txn_rollback(); diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t index 0db09284a3..2b7716cb80 100755 --- a/t/db_dependent/Budgets.t +++ b/t/db_dependent/Budgets.t @@ -8,7 +8,7 @@ BEGIN { } use C4::Context; use C4::Biblio qw( AddBiblio ); -use C4::Acquisition qw( NewBasket AddInvoice GetInvoice ModReceiveOrder populate_order_with_prices ); +use C4::Acquisition qw( NewBasket AddInvoice GetInvoice ModReceiveOrder ); use Koha::ActionLogs; use Koha::Acquisition::Booksellers; @@ -37,10 +37,14 @@ my $library = $builder->build({ source => 'Branch', }); +my $patron = $builder->build_object({ class => 'Koha::Patrons' }); +$patron->store; + t::lib::Mocks::mock_userenv( { flags => 1, - userid => 'my_userid', + userid => $patron->userid, + borrowernumber => $patron->borrowernumber, branch => $library->{branchcode}, } ); @@ -1073,11 +1077,9 @@ subtest 'GetBudgetSpent GetBudgetOrdered GetBudgetsPlanCell tests' => sub { #Okay we have basically what the user would enter, now we do some maths - $spent_orderinfo = C4::Acquisition::populate_order_with_prices({ - order => $spent_orderinfo, - booksellerid => $spent_orderinfo->{booksellerid}, - ordering => 1, - }); + my $spent_order_obj = Koha::Acquisition::Order->new($spent_orderinfo); + $spent_order_obj->populate_with_prices_for_ordering(); + $spent_orderinfo = $spent_order_obj->unblessed(); #And let's place the order @@ -1160,11 +1162,9 @@ subtest 'GetBudgetSpent GetBudgetOrdered GetBudgetsPlanCell tests' => sub { #Do our maths - $spent_orderinfo = C4::Acquisition::populate_order_with_prices({ - order => $spent_orderinfo, - booksellerid => $spent_orderinfo->{booksellerid}, - receiving => 1, - }); + $spent_order_obj = Koha::Acquisition::Order->new($spent_orderinfo); + $spent_order_obj->populate_with_prices_for_receiving(); + $spent_orderinfo = $spent_order_obj->unblessed(); my $received_order = $builder->build({ source => 'Aqorder', value => $spent_orderinfo }); #And receive a copy of the order so we have both spent and ordered values -- 2.39.5