From 3d61550e22c36a02e88bfa1fbd3152b8bfc69d11 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 27 Aug 2014 12:36:04 +0200 Subject: [PATCH] Bug 12830: Move the order-related code into Koha::Acquisition::Order The C4::Acquisition module should be exploded in order to add readability and maintainability to this part of the code. This patch is a POC, it introduces a new Koha::Acquisition::Order module and put in it the code from NewOrder and NewOrderItem. Test plan: 1/ Create an order, modify it, receive it, cancel the receipt. 2/ Launch the prove command on all unit tests modified by this patch and verify that all pass. Signed-off-by: Paola Rossi Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- C4/Acquisition.pm | 100 ++---------------- Koha/Acquisition/Order.pm | 92 ++++++++++++++++ acqui/addorder.pl | 8 +- acqui/addorderiso2709.pl | 5 +- acqui/finishreceive.pl | 3 +- t/db_dependent/Acquisition.t | 7 +- t/db_dependent/Acquisition/CancelReceipt.t | 19 ++-- .../Acquisition/GetBasketsInfosByBookseller.t | 16 +-- .../Acquisition/GetOrdersByBiblionumber.t | 19 ++-- t/db_dependent/Acquisition/Invoices.t | 19 ++-- t/db_dependent/Acquisition/NewOrder.t | 28 ++--- .../Acquisition/OrderFromSubscription.t | 11 +- t/db_dependent/Acquisition/TransferOrder.t | 9 +- .../Acquisition/close_reopen_basket.t | 13 ++- t/db_dependent/Bookseller.t | 26 +++-- t/db_dependent/Budgets.t | 12 ++- t/db_dependent/Letters.t | 7 +- 17 files changed, 219 insertions(+), 175 deletions(-) create mode 100644 Koha/Acquisition/Order.pm diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index e8c779006f..53e1fab332 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -31,6 +31,7 @@ use C4::Debug; use C4::Bookseller qw(GetBookSellerFromId); use C4::Templates qw(gettemplate); use Koha::DateUtils qw( dt_from_string output_pref ); +use Koha::Acquisition::Order; use Time::localtime; use HTML::Entities; @@ -56,13 +57,13 @@ BEGIN { &ModBasketgroup &NewBasketgroup &DelBasketgroup &GetBasketgroup &CloseBasketgroup &GetBasketgroups &ReOpenBasketgroup - &NewOrder &DelOrder &ModOrder &GetOrder &GetOrders &GetOrdersByBiblionumber + &DelOrder &ModOrder &GetOrder &GetOrders &GetOrdersByBiblionumber &GetLateOrders &GetOrderFromItemnumber &SearchOrders &GetHistory &GetRecentAcqui &ModReceiveOrder &CancelReceipt &GetCancelledOrders &TransferOrder &GetLastOrderNotReceivedFromSubscriptionid &GetLastOrderReceivedFromSubscriptionid - &NewOrderItem &ModItemOrder + &ModItemOrder &GetParcels &GetParcel @@ -1228,95 +1229,6 @@ sub GetLastOrderReceivedFromSubscriptionid { } - -#------------------------------------------------------------# - -=head3 NewOrder - - &NewOrder(\%hashref); - -Adds a new order to the database. Any argument that isn't described -below is the new value of the field with the same name in the aqorders -table of the Koha database. - -=over - -=item $hashref->{'basketno'} is the basketno foreign key in aqorders, it is mandatory - -=item $hashref->{'ordernumber'} is a "minimum order number." - -=item $hashref->{'budgetdate'} is effectively ignored. -If it's undef (anything false) or the string 'now', the current day is used. -Else, the upcoming July 1st is used. - -=item $hashref->{'subscription'} may be either "yes", or anything else for "no". - -=item $hashref->{'uncertainprice'} may be 0 for "the price is known" or 1 for "the price is uncertain" - -=item defaults entrydate to Now - -The following keys are used: "biblionumber", "title", "basketno", "quantity", "order_vendornote", "order_internalnote", "rrp", "ecost", "gstrate", "unitprice", "subscription", "sort1", "sort2", "booksellerinvoicenumber", "listprice", "budgetdate", "purchaseordernumber", "branchcode", "booksellerinvoicenumber", "budget_id". - -=back - -=cut - -sub NewOrder { - my $orderinfo = shift; - - my $dbh = C4::Context->dbh; - my @params; - - croak "The ordernumber parameter should not be provided on calling NewOrder" - if $orderinfo->{ordernumber}; - - # if these parameters are missing, we can't continue - for my $key ( qw( basketno quantity biblionumber budget_id ) ) { - croak "Cannot insert order: Mandatory parameter $key is missing" unless $orderinfo->{$key}; - } - - $orderinfo->{quantityreceived} ||= 0; - $orderinfo->{entrydate} ||= output_pref({ dt => dt_from_string, dateformat => 'iso'}); - - # get only the columns of Aqorder - my $schema = Koha::Database->new()->schema; - my $columns = ' '.join(' ', $schema->source('Aqorder')->columns).' '; - my $new_order = { map { $columns =~ / $_ / ? ($_ => $orderinfo->{$_}) : () } keys(%$orderinfo) }; - $new_order->{ordernumber} ||= undef; - - my $order = $schema->resultset('Aqorder')->create($new_order); - my $ordernumber = $order->id; - - unless ( $new_order->{parent_ordernumber} ) { - $order->update({ parent_ordernumber => $ordernumber }); - } - - return $ordernumber; -} - - - - -#------------------------------------------------------------# - -=head3 NewOrderItem - - &NewOrderItem(); - -=cut - -sub NewOrderItem { - my ($itemnumber, $ordernumber) = @_; - my $dbh = C4::Context->dbh; - my $query = qq| - INSERT INTO aqorders_items - (itemnumber, ordernumber) - VALUES (?,?) |; - - my $sth = $dbh->prepare($query); - $sth->execute( $itemnumber, $ordernumber); -} - #------------------------------------------------------------# =head3 ModOrder @@ -1535,7 +1447,7 @@ q{SELECT * FROM aqorders WHERE biblionumber=? AND aqorders.ordernumber=?}, $order->{'rrp'} = $rrp; $order->{ecost} = $ecost; $order->{'orderstatus'} = 'complete'; - $new_ordernumber = NewOrder($order); + $new_ordernumber = Koha::Acquisition::Order->new($order)->insert->{ordernumber}; if ($received_items) { foreach my $itemnumber (@$received_items) { @@ -1932,8 +1844,8 @@ sub TransferOrder { delete $order->{'ordernumber'}; delete $order->{parent_ordernumber}; $order->{'basketno'} = $basketno; - my $newordernumber; - $newordernumber = NewOrder($order); + + my $newordernumber = Koha::Acquisition::Order->new($order)->insert->{ordernumber}; $query = q{ UPDATE aqorders_items diff --git a/Koha/Acquisition/Order.pm b/Koha/Acquisition/Order.pm new file mode 100644 index 0000000000..d6cfa8e7ad --- /dev/null +++ b/Koha/Acquisition/Order.pm @@ -0,0 +1,92 @@ +package Koha::Acquisition::Order; + +use Modern::Perl; + +use Koha::Database; +use Koha::DateUtils qw( dt_from_string output_pref ); + +use Carp qw( croak ); + +use base qw( Class::Accessor ); + +# TODO fetch order from itemnumber (GetOrderFromItemnnumber) +# TODO Move code from GetOrder +sub fetch { + my ( $class, $params ) = @_; + my $ordernumber = $params->{ordernumber}; + return unless $ordernumber; + my $schema = Koha::Database->new->schema; + + my $rs = + $schema->resultset('Aqorder')->search( { ordernumber => $ordernumber }, + { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } ); + + return $class->new( $rs->first ); +} + +sub insert { + my ($self) = @_; + + # if these parameters are missing, we can't continue + for my $key (qw( basketno quantity biblionumber budget_id )) { + croak "Cannot insert order: Mandatory parameter $key is missing" + unless $self->{$key}; + } + + $self->{quantityreceived} ||= 0; + $self->{entrydate} ||= + output_pref( { dt => dt_from_string, dateformat => 'iso' } ); + + my $schema = Koha::Database->new->schema; + my @columns = $schema->source('Aqorder')->columns; + + $self->{ordernumber} ||= undef; + + my $rs = $schema->resultset('Aqorder')->create( + { + map { + exists $self->{$_} ? ( $_ => $self->{$_} ) : () + } @columns + } + ); + $self->{ordernumber} = $rs->id; + + unless ( $self->{parent_ordernumber} ) { + $rs->update( { parent_ordernumber => $self->{ordernumber} } ); + } + + return $self; +} + +sub add_item { + my ( $self, $itemnumber ) = @_; + my $schema = Koha::Database->new->schema; + my $rs = $schema->resultset('AqordersItem'); + $rs->create({ ordernumber => $self->{ordernumber}, itemnumber => $itemnumber }); +} + +# TODO Move code from ModItemOrder +sub update_item { + die "not implemented yet"; +} + +sub del_item { + die "not implemented yet"; +} + +# TODO Move code from ModOrder +sub update { + die "not implemented yet"; +} + +# TODO Move code from DelOrder +sub delete { + die "not implemented yet"; +} + +# TODO Move code from TransferOrder +sub transfer { + die "not implemented yet"; +} + +1; diff --git a/acqui/addorder.pl b/acqui/addorder.pl index da1beb0149..26bd6ae0cf 100755 --- a/acqui/addorder.pl +++ b/acqui/addorder.pl @@ -123,7 +123,7 @@ use strict; use warnings; use CGI; use C4::Auth; # get_template_and_user -use C4::Acquisition; # NewOrder DelOrder ModOrder +use C4::Acquisition; # DelOrder ModOrder use C4::Suggestions; # ModStatus use C4::Biblio; # AddBiblio TransformKohaToMarc use C4::Budgets; @@ -271,11 +271,12 @@ if ( $orderinfo->{quantity} ne '0' ) { $orderinfo->{unitprice} = $orderinfo->{ecost} if not defined $orderinfo->{unitprice} or $orderinfo->{unitprice} eq ''; # if we already have $ordernumber, then it's an ordermodif + my $order = Koha::Acquisition::Order->new($orderinfo); if ($$orderinfo{ordernumber}) { ModOrder( $orderinfo); } else { # else, it's a new line - $orderinfo->{ordernumber} = NewOrder($orderinfo); + $order->insert; } # now, add items if applicable @@ -314,8 +315,7 @@ if ( $orderinfo->{quantity} ne '0' ) { 'ITEM'); my $record=MARC::Record::new_from_xml($xml, 'UTF-8'); my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$$orderinfo{biblionumber}); - NewOrderItem($itemnumber, $$orderinfo{ordernumber}); - + $order->add_item($itemnumber); } } diff --git a/acqui/addorderiso2709.pl b/acqui/addorderiso2709.pl index 2d38c82600..15c83eaec4 100755 --- a/acqui/addorderiso2709.pl +++ b/acqui/addorderiso2709.pl @@ -46,6 +46,7 @@ use C4::Branch; # GetBranches use C4::Members; use Koha::Number::Price; +use Koha::Acquisition::Order; my $input = new CGI; my ($template, $loggedinuser, $cookie, $userflags) = get_template_and_user({ @@ -257,7 +258,7 @@ if ($op eq ""){ # remove uncertainprice flag if we have found a price in the MARC record $orderinfo{uncertainprice} = 0 if $orderinfo{listprice}; - my $ordernumber = NewOrder( \%orderinfo ); + my $order = Koha::Acquisition::Order->new( \%orderinfo )->insert; # 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 @@ -279,7 +280,7 @@ if ($op eq ""){ my $record = MARC::Record::new_from_xml( $xml, 'UTF-8' ); for (my $qtyloop=1;$qtyloop <= $c_quantity;$qtyloop++) { my ( $biblionumber, $bibitemnum, $itemnumber ) = AddItemFromMarc( $record, $biblionumber ); - NewOrderItem( $itemnumber, $ordernumber ); + $order->add_item( $itemnumber ); } } else { SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' ); diff --git a/acqui/finishreceive.pl b/acqui/finishreceive.pl index 1723520995..7f6ba88afd 100755 --- a/acqui/finishreceive.pl +++ b/acqui/finishreceive.pl @@ -141,6 +141,7 @@ if ($quantityrec > $origquantityrec ) { push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i]; push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i]; } + my $order = Koha::Acquisition::Order->fetch({ ordernumber => $new_ordernumber }); foreach my $item (keys %itemhash){ my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'}, $itemhash{$item}->{'subfields'}, @@ -149,7 +150,7 @@ if ($quantityrec > $origquantityrec ) { $itemhash{$item}->{'indicator'},'ITEM'); my $record=MARC::Record::new_from_xml($xml, 'UTF-8'); my (undef,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber); - NewOrderItem($itemnumber, $new_ordernumber); + $order->add_item( $itemnumber ); } } } diff --git a/t/db_dependent/Acquisition.t b/t/db_dependent/Acquisition.t index 1a559b8305..96a0b5ed57 100755 --- a/t/db_dependent/Acquisition.t +++ b/t/db_dependent/Acquisition.t @@ -8,7 +8,7 @@ use POSIX qw(strftime); use C4::Bookseller qw( GetBookSellerFromId ); -use Test::More tests => 74; +use Test::More tests => 75; BEGIN { use_ok('C4::Acquisition'); @@ -16,6 +16,7 @@ BEGIN { use_ok('C4::Biblio'); use_ok('C4::Budgets'); use_ok('C4::Bookseller'); + use_ok('Koha::Acquisition::Order'); } # Sub used for testing C4::Acquisition subs returning order(s): @@ -25,7 +26,7 @@ BEGIN { # params : # $exp_fields : arrayref whose elements are the keys we expect to find # $original_order_content : hashref whose 2 keys str and num contains hashrefs -# containing content fields of the order created with NewOrder +# containing content fields of the order created with Koha::Acquisition::Order # $order_to_check : hashref whose keys/values are the content of an order # returned by the C4::Acquisition sub we are testing # returns : @@ -244,7 +245,7 @@ for ( 0 .. 4 ) { values %{ $order_content[$_]->{num} }; @ocontent{ keys %{ $order_content[$_]->{str} } } = values %{ $order_content[$_]->{str} }; - $ordernumbers[$_] = C4::Acquisition::NewOrder( \%ocontent ); + $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->insert->{ordernumber}; $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_]; } diff --git a/t/db_dependent/Acquisition/CancelReceipt.t b/t/db_dependent/Acquisition/CancelReceipt.t index 0eea86d0b7..3ef83b383d 100644 --- a/t/db_dependent/Acquisition/CancelReceipt.t +++ b/t/db_dependent/Acquisition/CancelReceipt.t @@ -12,6 +12,7 @@ use C4::Budgets; use t::lib::Mocks; use Koha::DateUtils; +use Koha::Acquisition::Order; use MARC::Record; my $dbh = C4::Context->dbh; @@ -44,14 +45,15 @@ my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, ''); my $itemnumber = AddItem({}, $biblionumber); t::lib::Mocks::mock_preference('AcqCreateItem', 'receiving'); -my $ordernumber = C4::Acquisition::NewOrder( +my $order = Koha::Acquisition::Order->new( { basketno => $basketno1, quantity => 2, biblionumber => $biblionumber, budget_id => $budget->{budget_id}, } -); +)->insert; +my $ordernumber = $order->{ordernumber}; ModReceiveOrder( { @@ -62,28 +64,29 @@ ModReceiveOrder( } ); -NewOrderItem($itemnumber, $ordernumber); +$order->add_item( $itemnumber ); CancelReceipt($ordernumber); -my $order = GetOrder( $ordernumber ); +$order = GetOrder( $ordernumber ); is(scalar GetItemnumbersFromOrder($order->{ordernumber}), 0, "Create items on receiving: 0 item exist after cancelling a receipt"); my $itemnumber1 = AddItem({}, $biblionumber); my $itemnumber2 = AddItem({}, $biblionumber); t::lib::Mocks::mock_preference('AcqCreateItem', 'ordering'); t::lib::Mocks::mock_preference('AcqItemSetSubfieldsWhenReceiptIsCancelled', '7=9'); # notforloan is mapped with 952$7 -$ordernumber = C4::Acquisition::NewOrder( +$order = Koha::Acquisition::Order->new( { basketno => $basketno1, quantity => 2, biblionumber => $biblionumber, budget_id => $budget->{budget_id}, } -); +)->insert; +$ordernumber = $order->{ordernumber}; -NewOrderItem($itemnumber1, $ordernumber); -NewOrderItem($itemnumber2, $ordernumber); +$order->add_item( $itemnumber1 ); +$order->add_item( $itemnumber2 ); my ( undef, $new_ordernumber ) = ModReceiveOrder( { diff --git a/t/db_dependent/Acquisition/GetBasketsInfosByBookseller.t b/t/db_dependent/Acquisition/GetBasketsInfosByBookseller.t index 1c82ceddc3..342d431e20 100644 --- a/t/db_dependent/Acquisition/GetBasketsInfosByBookseller.t +++ b/t/db_dependent/Acquisition/GetBasketsInfosByBookseller.t @@ -4,11 +4,14 @@ use Modern::Perl; use Test::More tests => 18; use Data::Dumper; -use C4::Acquisition qw( NewOrder NewBasket GetBasketsInfosByBookseller ); +use C4::Acquisition qw( NewBasket GetBasketsInfosByBookseller ); use C4::Biblio qw( AddBiblio ); use C4::Bookseller qw( AddBookseller ); use C4::Budgets qw( AddBudget ); use C4::Context; + +use Koha::Acquisition::Order; + my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; @@ -34,28 +37,29 @@ my $budgetid = C4::Budgets::AddBudget( ); my $budget = C4::Budgets::GetBudget( $budgetid ); -my ($ordernumber1, $ordernumber2, $ordernumber3); my ($biblionumber1, $biblioitemnumber1) = AddBiblio(MARC::Record->new, ''); my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, ''); my ($biblionumber3, $biblioitemnumber3) = AddBiblio(MARC::Record->new, ''); -$ordernumber1 = C4::Acquisition::NewOrder( +my $order1 = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 2, biblionumber => $biblionumber1, budget_id => $budget->{budget_id}, } -); +)->insert; +my $ordernumber1 = $order1->{ordernumber}; -$ordernumber2 = C4::Acquisition::NewOrder( +my $order2 = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 4, biblionumber => $biblionumber2, budget_id => $budget->{budget_id}, } -); +)->insert; +my $ordernumber2 = $order2->{ordernumber}; my $baskets = C4::Acquisition::GetBasketsInfosByBookseller( $supplierid ); is( scalar(@$baskets), 1, 'Start: 1 basket' ); diff --git a/t/db_dependent/Acquisition/GetOrdersByBiblionumber.t b/t/db_dependent/Acquisition/GetOrdersByBiblionumber.t index be6146d49e..adc11b63fa 100644 --- a/t/db_dependent/Acquisition/GetOrdersByBiblionumber.t +++ b/t/db_dependent/Acquisition/GetOrdersByBiblionumber.t @@ -7,6 +7,9 @@ use C4::Acquisition; use C4::Biblio; use C4::Bookseller; use C4::Budgets; + +use Koha::Acquisition::Order; + use MARC::Record; #Start transaction @@ -36,35 +39,37 @@ my $budgetid = C4::Budgets::AddBudget( my $budget = C4::Budgets::GetBudget( $budgetid ); -my ($ordernumber1, $ordernumber2, $ordernumber3); my ($biblionumber1, $biblioitemnumber1) = AddBiblio(MARC::Record->new, ''); my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, ''); -$ordernumber1 = C4::Acquisition::NewOrder( +my $order1 = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 24, biblionumber => $biblionumber1, budget_id => $budget->{budget_id}, } -); +)->insert; +my $ordernumber1 = $order1->{ordernumber}; -$ordernumber2 = C4::Acquisition::NewOrder( +my $order2 = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 42, biblionumber => $biblionumber2, budget_id => $budget->{budget_id}, } -); +)->insert; +my $ordernumber2 = $order1->{ordernumber}; -$ordernumber3 = C4::Acquisition::NewOrder( +my $order3 = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 4, biblionumber => $biblionumber2, budget_id => $budget->{budget_id}, } -); +)->insert; +my $ordernumber3 = $order1->{ordernumber}; my @orders = GetOrdersByBiblionumber(); is(scalar(@orders), 0, 'GetOrdersByBiblionumber : no argument, return undef'); diff --git a/t/db_dependent/Acquisition/Invoices.t b/t/db_dependent/Acquisition/Invoices.t index 9c6dcbc691..eac6a237e4 100644 --- a/t/db_dependent/Acquisition/Invoices.t +++ b/t/db_dependent/Acquisition/Invoices.t @@ -9,6 +9,8 @@ use warnings; use C4::Bookseller qw( GetBookSellerFromId ); use C4::Biblio qw( AddBiblio ); +use Koha::Acquisition::Order; + use Test::More tests => 22; BEGIN { @@ -42,7 +44,6 @@ my $budgetid = C4::Budgets::AddBudget( ); my $budget = C4::Budgets::GetBudget( $budgetid ); -my ($ordernumber1, $ordernumber2, $ordernumber3); my $bibrec1 = MARC::Record->new(); $bibrec1->append_fields( MARC::Field->new('020', '', '', 'a' => '1234567890'), @@ -53,25 +54,28 @@ $bibrec1->append_fields( my ($biblionumber1, $biblioitemnumber1) = AddBiblio($bibrec1, ''); my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, ''); my ($biblionumber3, $biblioitemnumber3) = AddBiblio(MARC::Record->new, ''); -$ordernumber1 = C4::Acquisition::NewOrder( + +my $order1 = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 2, biblionumber => $biblionumber1, budget_id => $budget->{budget_id}, } -); +)->insert; +my $ordernumber1 = $order1->{ordernumber}; -$ordernumber2 = C4::Acquisition::NewOrder( +my $order2 = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 1, biblionumber => $biblionumber2, budget_id => $budget->{budget_id}, } -); +)->insert; +my $ordernumber2 = $order2->{ordernumber}; -$ordernumber3 = C4::Acquisition::NewOrder( +my $order3 = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 1, @@ -80,7 +84,8 @@ $ordernumber3 = C4::Acquisition::NewOrder( ecost => 42, rrp => 42, } -); +)->insert; +my $ordernumber3 = $order3->{ordernumber}; my $invoiceid1 = AddInvoice(invoicenumber => 'invoice1', booksellerid => $booksellerid, unknown => "unknown"); my $invoiceid2 = AddInvoice(invoicenumber => 'invoice2', booksellerid => $booksellerid, unknown => "unknown", diff --git a/t/db_dependent/Acquisition/NewOrder.t b/t/db_dependent/Acquisition/NewOrder.t index 7c7cd815a1..aa9bfeda15 100644 --- a/t/db_dependent/Acquisition/NewOrder.t +++ b/t/db_dependent/Acquisition/NewOrder.t @@ -9,6 +9,7 @@ use C4::Bookseller; use C4::Budgets; use MARC::Record; use Koha::DateUtils qw( dt_from_string output_pref ); +use Koha::Acquisition::Order; my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; @@ -41,12 +42,12 @@ my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, ''); # returns undef and croaks if basketno, quantity, biblionumber or budget_id is missing -my $ordernumber = eval { C4::Acquisition::NewOrder() }; +my $order = eval { Koha::Acquisition::Order->new->insert }; my $return_error = $@; ok( - ( ! defined $ordernumber ) + ( ! defined $order ) && ( defined $return_error ), - "NewOrder with no params returns undef and croaks" + "Inserting an order with no params returns undef and croaks" ); my $mandatoryparams = { @@ -59,28 +60,27 @@ my @mandatoryparams_keys = keys %$mandatoryparams; foreach my $mandatoryparams_key (@mandatoryparams_keys) { my %test_missing_mandatoryparams = %$mandatoryparams; delete $test_missing_mandatoryparams{$mandatoryparams_key}; - eval { - $ordernumber = - C4::Acquisition::NewOrder( \%test_missing_mandatoryparams ); + $order = eval { + Koha::Acquisition::Order->new( \%test_missing_mandatoryparams )->insert; }; $return_error = $@; my $expected_error = "Cannot insert order: Mandatory parameter $mandatoryparams_key is missing"; ok( - ( !( defined $ordernumber ) ) + ( !( defined $order ) ) && ( index( $return_error, $expected_error ) >= 0 ), -"NewOrder with no $mandatoryparams_key returns undef and croaks with expected error message" +"Inserting an order with no $mandatoryparams_key returns undef and croaks with expected error message" ); } -$ordernumber = C4::Acquisition::NewOrder( +$order = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 24, biblionumber => $biblionumber1, budget_id => $budget->{budget_id}, } -); - -my $order = C4::Acquisition::GetOrder( $ordernumber ); -is( $order->{quantityreceived}, 0, 'NewOrder set quantityreceivedto 0 if undef is given' ); -is( $order->{entrydate}, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), 'NewOrder set entrydate to today' ); +)->insert; +my $ordernumber = $order->{ordernumber}; +$order = Koha::Acquisition::Order->fetch({ ordernumber => $ordernumber }); +is( $order->{quantityreceived}, 0, 'Koha::Acquisition::Order->insert set quantityreceivedto 0 if undef is given' ); +is( $order->{entrydate}, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), 'Koha::Acquisition::Order->insert set entrydate to today' ); diff --git a/t/db_dependent/Acquisition/OrderFromSubscription.t b/t/db_dependent/Acquisition/OrderFromSubscription.t index e03c023780..438c3fd96a 100644 --- a/t/db_dependent/Acquisition/OrderFromSubscription.t +++ b/t/db_dependent/Acquisition/OrderFromSubscription.t @@ -8,6 +8,8 @@ use_ok('C4::Biblio'); use_ok('C4::Budgets'); use_ok('C4::Serials'); +use Koha::Acquisition::Order; + # Start transaction my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; @@ -53,8 +55,8 @@ ok($basketno = NewBasket($booksellerid, 1), "NewBasket( $booksellerid , 1 ) re my $cost = 42.00; my $subscription = GetSubscription( $subscriptionid ); -my $ordernumber; -$ordernumber = NewOrder({ + +my $order = Koha::Acquisition::Order->new({ biblionumber => $subscription->{biblionumber}, entrydate => '01-01-2013', quantity => 1, @@ -68,12 +70,13 @@ $ordernumber = NewOrder({ orderstatus => 'new', subscriptionid => $subscription->{subscriptionid}, budget_id => $budget_id, -}); +})->insert; +my $ordernumber = $order->{ordernumber}; my $is_currently_on_order = subscriptionCurrentlyOnOrder( $subscription->{subscriptionid} ); is ( $is_currently_on_order, 1, "The subscription is currently on order"); -my $order = GetLastOrderNotReceivedFromSubscriptionid( $subscription->{subscriptionid} ); +$order = GetLastOrderNotReceivedFromSubscriptionid( $subscription->{subscriptionid} ); is ( $order->{subscriptionid}, $subscription->{subscriptionid}, "test subscriptionid for the last order not received"); ok( $order->{ecost} == $cost, "test cost for the last order not received"); diff --git a/t/db_dependent/Acquisition/TransferOrder.t b/t/db_dependent/Acquisition/TransferOrder.t index dfe57fff96..d86824021c 100644 --- a/t/db_dependent/Acquisition/TransferOrder.t +++ b/t/db_dependent/Acquisition/TransferOrder.t @@ -10,6 +10,7 @@ use C4::Items; use C4::Bookseller; use C4::Budgets; use Koha::DateUtils; +use Koha::Acquisition::Order; use MARC::Record; my $dbh = C4::Context->dbh; @@ -54,18 +55,18 @@ my $budget = C4::Budgets::GetBudget( $budgetid ); my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, ''); my $itemnumber = AddItem({}, $biblionumber); -my $ordernumber = C4::Acquisition::NewOrder( +my $order = Koha::Acquisition::Order->new( { basketno => $basketno1, quantity => 2, biblionumber => $biblionumber, budget_id => $budget->{budget_id}, } -); -NewOrderItem($itemnumber, $ordernumber); +)->insert; +my $ordernumber = $order->{ordernumber}; +$order->add_item( $itemnumber ); # Begin tests -my $order; is(scalar GetOrders($basketno1), 1, "1 order in basket1"); ($order) = GetOrders($basketno1); is(scalar GetItemnumbersFromOrder($order->{ordernumber}), 1, "1 item in basket1's order"); diff --git a/t/db_dependent/Acquisition/close_reopen_basket.t b/t/db_dependent/Acquisition/close_reopen_basket.t index d5a47892ae..a5ae64d508 100644 --- a/t/db_dependent/Acquisition/close_reopen_basket.t +++ b/t/db_dependent/Acquisition/close_reopen_basket.t @@ -9,6 +9,8 @@ use C4::Bookseller; use C4::Budgets; use C4::Context; +use Koha::Acquisition::Order; + # Start transaction my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; @@ -43,24 +45,25 @@ my $budget = C4::Budgets::GetBudget( $budgetid ); my ($biblionumber1, $biblioitemnumber1) = AddBiblio(MARC::Record->new, ''); my ($biblionumber2, $biblioitemnumber2) = AddBiblio(MARC::Record->new, ''); -my ($ordernumber1, $ordernumber2); -$ordernumber1 = C4::Acquisition::NewOrder( +my $order1 = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 24, biblionumber => $biblionumber1, budget_id => $budget->{budget_id}, } -); +)->insert; +my $ordernumber1 = $order1->{ordernumber}; -$ordernumber2 = C4::Acquisition::NewOrder( +my $order2 = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 42, biblionumber => $biblionumber2, budget_id => $budget->{budget_id}, } -); +)->insert; +my $ordernumber2 = $order2->{ordernumber}; my $nb_biblio = C4::Acquisition::GetBiblioCountByBasketno( $basketno ); is ( $nb_biblio, 2, "There are 2 biblio for this basket" ); diff --git a/t/db_dependent/Bookseller.t b/t/db_dependent/Bookseller.t index 3c713b3e30..023ad456e9 100644 --- a/t/db_dependent/Bookseller.t +++ b/t/db_dependent/Bookseller.t @@ -14,6 +14,8 @@ use C4::Serials; use C4::Budgets; use C4::Biblio; +use Koha::Acquisition::Order; + BEGIN { use_ok('C4::Bookseller'); } @@ -350,8 +352,7 @@ is(scalar(@subscriptions), 1, 'search for subscriptions by call number'); is(scalar(@subscriptions), 1, 'search for subscriptions by location'); #Add 4 orders -my ( $ordernumber1, $ordernumber2, $ordernumber3, $ordernumber4 ); -$ordernumber1 = C4::Acquisition::NewOrder( +my $order1 = Koha::Acquisition::Order->new( { basketno => $basketno1, quantity => 24, @@ -368,8 +369,10 @@ $ordernumber1 = C4::Acquisition::NewOrder( ecost => 10, datereceived => '01-06-2013' } -); -$ordernumber2 = C4::Acquisition::NewOrder( +)->insert; +my $ordernumber1 = $order1->{ordernumber}; + +my $order2 = Koha::Acquisition::Order->new( { basketno => $basketno2, quantity => 20, @@ -384,8 +387,10 @@ $ordernumber2 = C4::Acquisition::NewOrder( rrp => 10, ecost => 10, } -); -$ordernumber3 = C4::Acquisition::NewOrder( +)->insert; +my $ordernumber2 = $order2->{ordernumber}; + +my $order3 = Koha::Acquisition::Order->new( { basketno => $basketno3, quantity => 20, @@ -400,8 +405,10 @@ $ordernumber3 = C4::Acquisition::NewOrder( rrp => 11, ecost => 11, } -); -$ordernumber4 = C4::Acquisition::NewOrder( +)->insert; +my $ordernumber3 = $order3->{ordernumber}; + +my $order4 = Koha::Acquisition::Order->new( { basketno => $basketno4, quantity => 20, @@ -417,7 +424,8 @@ $ordernumber4 = C4::Acquisition::NewOrder( ecost => 11, quantityreceived => 20 } -); +)->insert; +my $ordernumber4 = $order4->{ordernumber}; #Test cases: # Sample datas : diff --git a/t/db_dependent/Budgets.t b/t/db_dependent/Budgets.t index c3e2ef4f8e..3ac733cc1c 100755 --- a/t/db_dependent/Budgets.t +++ b/t/db_dependent/Budgets.t @@ -10,6 +10,8 @@ use C4::Bookseller; use C4::Acquisition; use C4::Dates; +use Koha::Acquisition::Order; + use YAML; my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; @@ -304,7 +306,7 @@ my $item_quantity = 2; my $number_of_orders_to_move = 0; for my $infos (@order_infos) { for ( 1 .. $infos->{pending_quantity} ) { - my $ordernumber = C4::Acquisition::NewOrder( + my $order = Koha::Acquisition::Order->new( { basketno => $basketno, biblionumber => $biblionumber, @@ -321,12 +323,13 @@ for my $infos (@order_infos) { uncertainprice => 0, gstrate => 0, } - ); + )->insert; + my $ordernumber = $order->{ordernumber}; push @{ $budgets{$infos->{budget_id}} }, $ordernumber; $number_of_orders_to_move++; } for ( 1 .. $infos->{spent_quantity} ) { - my $ordernumber = C4::Acquisition::NewOrder( + my $order = Koha::Acquisition::Order->new( { basketno => $basketno, biblionumber => $biblionumber, @@ -343,7 +346,8 @@ for my $infos (@order_infos) { uncertainprice => 0, gstrate => 0, } - ); + )->insert; + my $ordernumber = $order->{ordernumber}; ModReceiveOrder({ biblionumber => $biblionumber, ordernumber => $ordernumber, diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index 32b760ec28..8d7991e0e0 100644 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -43,6 +43,7 @@ use_ok('C4::Bookseller'); use_ok('C4::Letters'); use t::lib::Mocks; use Koha::DateUtils qw( dt_from_string output_pref ); +use Koha::Acquisition::Order; my $dbh = C4::Context->dbh; @@ -295,7 +296,6 @@ my $budgetid = C4::Budgets::AddBudget({ budget_name => "budget_name_test_letters", }); -my $ordernumber; my $bib = MARC::Record->new(); if (C4::Context->preference('marcflavour') eq 'UNIMARC') { $bib->append_fields( @@ -308,14 +308,15 @@ if (C4::Context->preference('marcflavour') eq 'UNIMARC') { } ($biblionumber, $biblioitemnumber) = AddBiblio($bib, ''); -$ordernumber = C4::Acquisition::NewOrder( +my $order = Koha::Acquisition::Order->new( { basketno => $basketno, quantity => 1, biblionumber => $biblionumber, budget_id => $budgetid, } -); +)->insert; +my $ordernumber = $order->{ordernumber}; C4::Acquisition::CloseBasket( $basketno ); my $err; -- 2.39.5