From f8d275bb69ba598a60437e967777ebe54059b2f0 Mon Sep 17 00:00:00 2001 From: Mathieu Saby Date: Wed, 23 Oct 2013 00:38:25 +0200 Subject: [PATCH 2/2] Bug 10869 Followup : change DelOrder and adds UT Content-Type: text/plain; charset="utf-8" Changes made to DelOrder : Returns undef if no param DelOrder does no more need a biblionumber. It can be retreived inside the sub. DelOrder is now cleaning aqorders_items. UT added for DelOrder To test : A. With syspref AcqCreateItem = on placing an order 1. Fill a basket with some orders (some records with already existing items or holds, some without items). New items linked with the order will be created 2. Keep the basket open 3. In the catalog, supress all the items attached to one of the record used in the basket, then suppress the record 4. In the basket, check you can delete the order that was using deleted record (click on link "Delete order" in the right column of the table) 5. After deleting the order, check there is a new line in the "Deleted orders" table 6. Regression test : try to delete other orders, and check the deletion of the order implies the deletion of items created when ordering a/ If no items were attached to the record before you created the basket, you can delete some other orders, AND their record. Try to delete order and record. b/ If some items were attached to the record before you created the basket, you can delete some other orders, BUT NOT their record. Try to delete only order. After you have deleted it, the items created when ordering must have been deleted. 7. Add some other orders to your basket (some records with already existing items or holds, some without items). New items linked with the order will be created 8. In the catalog, supress all the items attached to one of the record used in the basket, then suppress the record 9. Close the basket 10. Go on receipt page 11. Search the orders of your basket 12. In receipt page, check you can delete the order that was using deleted record (click on link "Delete order" in the right column of the table) 13. Regression test : try to delete other orders a/ If no items were attached to the record before you created the basket, you can delete some other orders, AND their record. Try to delete order and record. b/ If some items were attached to the record before you created the basket, you can delete some other orders, BUT NOT their record. Try to delete only order. After you have deleted it, the items created when ordering must have been deleted. B. With syspref AcqCreateItem = on receiving an order 1. Fill a basket with some orders (some records with already existing items or holds, some without items). 2. Keep the basket open 3. In the catalog, supress all the items attached to one of the record used in the basket, then suppress the record 4. In the basket, check you can delete the order that was using deleted record (click on link "Delete order" in the right column of the table) 5. After deleting the order, check there is a new line in the "Deleted orders" table 6. Regression test : try to delete other orders a/ If no items were attached to the record before you created the basket, you can delete some other orders, AND their record. Try to delete order and record. b/ If some items were attached to the record before you created the basket, you can delete some other orders, BUT NOT their record. Try to delete only order. 7. Add some other orders to your basket (some records with already existing items or holds, some without items). 8. In the catalog, supress all the items attached to one of the record used in the basket, then suppress the record 9. Close the basket 10. Go on receipt page 11. Search the orders of your basket 12. In receipt page, check you can delete the order that was using deleted record (click on link "Delete order" in the right column of the table) 13. Regression test : try to delete other orders a/ If no items were attached to the record before you created the basket, you can delete some other orders, AND their record. Try to delete order and record. b/ If some items were attached to the record before you created the basket, you can delete some other orders, BUT NOT their record. Try to delete only order. and to test the UT : prove t/db_dependant/Acquisition.t fix --- C4/Acquisition.pm | 46 +++++++++++++++++++++++------------ acqui/addorder.pl | 2 +- t/db_dependent/Acquisition.t | 55 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 17 deletions(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index 1bcd3cd..b5c1c9a 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -1285,7 +1285,7 @@ sub NewOrder { =head3 NewOrderItem - &NewOrderItem(); + &NewOrderItem ($itemnumber, $ordernumber); =cut @@ -1752,30 +1752,46 @@ sub SearchOrders { =head3 DelOrder - &DelOrder($ordernumber, $biblionumber); + &DelOrder($ordernumber); -Cancel the order with the given order and biblio numbers. It does not -delete any entries in the aqorders table, it merely marks them as -cancelled. +Cancel the order with the given order, by filling the datecancellationprinted and orderstatus fields. +C<$ordernumber > is mandatory +If no ordernumber is passed, the function returns undef and does not try to update database. +All the items linked with that order are deleted. +Other items are not deleted. =cut sub DelOrder { - my ($ordernumber,$biblionumber) = @_; + my ($ordernumber) = @_; + return unless $ordernumber; my $dbh = C4::Context->dbh; - my $query = " + my $query = qq { UPDATE aqorders SET datecancellationprinted=now(), orderstatus='cancelled' - WHERE ordernumber=? - "; + WHERE ordernumber = ? + }; my $sth = $dbh->prepare($query); $sth->execute($ordernumber ); - $sth->finish; - if ($biblionumber) { - my @itemnumbers = GetItemnumbersFromOrder( $ordernumber ); - foreach my $itemnumber (@itemnumbers){ - C4::Items::DelItem( $dbh, $biblionumber, $itemnumber ); - } +# get the biblionumber (we need it to run DelItem + $query = qq{ + SELECT biblionumber + FROM aqorders + WHERE ordernumber = ? + }; + $sth = $dbh->prepare($query); + $sth->execute($ordernumber ); + my $record = $sth->fetchrow_hashref; +# delete items from items and aqorders_items tables + my @itemnumbers = GetItemnumbersFromOrder( $ordernumber ); + foreach my $itemnumber (@itemnumbers) { + $query = qq{ + DELETE FROM aqorders_items + WHERE aqorders_items.itemnumber = ? + }; + $sth = $dbh->prepare($query); + $sth->execute($itemnumber ); + C4::Items::DelItem( $dbh, $record->{biblionumber}, $itemnumber ) if $record->{biblionumber}; } } diff --git a/acqui/addorder.pl b/acqui/addorder.pl index 5a34856..a3aabae 100755 --- a/acqui/addorder.pl +++ b/acqui/addorder.pl @@ -323,7 +323,7 @@ if ( $orderinfo->{quantity} ne '0' ) { else { # qty=0, delete the line my $biblionumber = $input->param('biblionumber'); - DelOrder($$orderinfo{ordernumber}, $biblionumber ); + DelOrder($$orderinfo{ordernumber}); if ($orderinfo->{delbiblio} == 1){ DelBiblio($biblionumber); } diff --git a/t/db_dependent/Acquisition.t b/t/db_dependent/Acquisition.t index 2585802..caf1ebd 100755 --- a/t/db_dependent/Acquisition.t +++ b/t/db_dependent/Acquisition.t @@ -7,8 +7,10 @@ use Modern::Perl; use POSIX qw(strftime); use C4::Bookseller qw( GetBookSellerFromId ); +use C4::Items qw (AddItem GetItem); +use C4::Branch qw (ModBranch); -use Test::More tests => 62; +use Test::More tests => 67; BEGIN { use_ok('C4::Acquisition'); @@ -223,4 +225,55 @@ is($order3->{'quantityreceived'}, 2, 'Order not split up'); is($order3->{'quantity'}, 2, '2 items on order'); is($order3->{'budget_id'}, $budgetid2, 'Budget has changed'); +# +# test DelOrder +# use: +# &DelOrder($ordernumber); + +# create 3 items on the same record and link 2 of them to the order + +# Add a branch +my $b1 = { + add => 1, + branchcode => 'BRA', + branchname => 'BranchA', + branchaddress1 => 'adr1A', + branchaddress2 => 'adr2A', + branchaddress3 => 'adr3A', + branchzip => 'zipA', + branchcity => 'cityA', + branchstate => 'stateA', + branchcountry => 'countryA', + branchphone => 'phoneA', + branchfax => 'faxA', + branchemail => 'emailA', + branchurl => 'urlA', + branchip => 'ipA', + branchprinter => undef, + branchnotes => 'noteA', + opac_info => 'opacA' +}; +ModBranch($b1); + +my ($item_bibnum1, $item_bibitemnum1, $itemnumber1) = AddItem({ homebranch => 'BRA', holdingbranch => 'BRA' } , $biblionumber2); +my ($item_bibnum2, $item_bibitemnum2, $itemnumber2) = AddItem({ homebranch => 'BRA', holdingbranch => 'BRA' } , $biblionumber2); +my ($item_bibnum3, $item_bibitemnum3, $itemnumber3) = AddItem({ homebranch => 'BRA', holdingbranch => 'BRA' } , $biblionumber2); +NewOrderItem ($itemnumber1, $ordernumber3); +NewOrderItem ($itemnumber2, $ordernumber3); + +# If no ordernumber is passed, the function returns undef and does not try to update database +my $return_DelOrder = DelOrder (); +is($return_DelOrder , undef, 'DelOrder returns undef with no param'); +# Cancel the order with the given order, by filling the datecancellationprinted and orderstatus fields. +DelOrder ($ordernumber3); +$order3 = GetOrder( $ordernumber3 ); +ok(($order3->{'orderstatus'} eq 'cancelled')&& (defined $order3->{'datecancellationprinted'}),'DelOrders update orderstatus and fill datecancellationprinted fields'); + +# All the items linked with that order are deleted. +is(scalar GetItemnumbersFromOrder($ordernumber3), 0, 'DelOrders delete items linked with cancelled order in aqorders_items table'); +ok(!defined GetItem($itemnumber1) && !defined GetItem($itemnumber2), "DelOrder deletes items linked with order in items table"); + +# Other items (created manually) are not deleted. +is(GetItem($itemnumber3)->{biblionumber},$biblionumber2, "DelOrder does not delete item created manually"); + $dbh->rollback; -- 1.7.9.5