Bug 7593: Move orders on the destination record when merging 2 records
authorMathieu Saby <mathieu.saby@univ-rennes2.fr>
Wed, 13 Mar 2013 15:12:03 +0000 (16:12 +0100)
committerJared Camins-Esakov <jcamins@cpbibliography.com>
Sun, 12 May 2013 14:29:57 +0000 (10:29 -0400)
Revised patch according to QA comments. No more dependent from bz 9780.

At present, merging records breaks the link order/record, except
if an item of the deleted record is used in the order.
This is a serious issue for libraries creating items on receipt.
This patch moves existing orders from deleted record to destination record.
It creates a new function Acquisitions::GetOrdersByBiblionumber,
that could be used by other patches later.

To test :
Check the problem :
1. Set syspref AcqCreateItem = Create an item when receiving an order
1. Create a basket with one order
2. Put the record used by this order in a list
3. Put an other record in the list
4. Merge the 2 records, keeping as a reference the record NOT used in the order
5. In the order, you will see for that order "Deleted bibliographic information..."
6. Apply the patch
7. Repeat steps 1-4
8. In the order, you will see the title/author of the kept record.
9. Set syspref AcqCreateItem = Create an item when placing an order
10. Repeat steps 1-4 (an item will be created)
11. In the oreder, you will see the title/author of the kept record
    (it is already the case at present. the patch should not alter this behavior)

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Test plan, test suite and QA script pass.
Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
C4/Acquisition.pm
cataloguing/merge.pl
t/db_dependent/Acquisition.t

index 77010c5..a685d5a 100644 (file)
@@ -53,8 +53,7 @@ BEGIN {
         &ModBasketgroup &NewBasketgroup &DelBasketgroup &GetBasketgroup &CloseBasketgroup
         &GetBasketgroups &ReOpenBasketgroup
 
-        &NewOrder &DelOrder &ModOrder &GetPendingOrders &GetOrder &GetOrders
-        &GetOrderNumber &GetLateOrders &GetOrderFromItemnumber
+        &NewOrder &DelOrder &ModOrder &GetPendingOrders &GetOrder &GetOrders &GetOrdersByBiblionumber
         &SearchOrder &GetHistory &GetRecentAcqui
         &ModReceiveOrder &CancelReceipt &ModOrderBiblioitemNumber
         &GetCancelledOrders
@@ -949,6 +948,41 @@ sub GetOrders {
     return @$results;
 }
 
+#------------------------------------------------------------#
+=head3 GetOrdersByBiblionumber
+
+  @orders = &GetOrdersByBiblionumber($biblionumber);
+
+Looks up the orders with linked to a specific $biblionumber, including
+cancelled orders and received orders.
+
+return :
+C<@orders> is an array of references-to-hash, whose keys are the
+fields from the aqorders, biblio, and biblioitems tables in the Koha database.
+
+=cut
+
+sub GetOrdersByBiblionumber {
+    my $biblionumber = shift;
+    return unless $biblionumber;
+    my $dbh   = C4::Context->dbh;
+    my $query  ="
+        SELECT biblio.*,biblioitems.*,
+                aqorders.*,
+                aqbudgets.*
+        FROM    aqorders
+            LEFT JOIN aqbudgets        ON aqbudgets.budget_id = aqorders.budget_id
+            LEFT JOIN biblio           ON biblio.biblionumber = aqorders.biblionumber
+            LEFT JOIN biblioitems      ON biblioitems.biblionumber =biblio.biblionumber
+        WHERE   aqorders.biblionumber=?
+    ";
+    my $sth = $dbh->prepare($query);
+    $sth->execute($biblionumber);
+    my $results = $sth->fetchall_arrayref({});
+    $sth->finish;
+    return @$results;
+}
+
 #------------------------------------------------------------#
 
 =head3 GetOrderNumber
index 6a46d2f..019b468 100755 (executable)
@@ -29,6 +29,7 @@ use C4::Biblio;
 use C4::Serials;
 use C4::Koha;
 use C4::Reserves qw/MergeHolds/;
+use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/;
 
 my $input = new CGI;
 my @biblionumber = $input->param('biblionumber');
@@ -69,6 +70,7 @@ if ($merge) {
     ModBiblio($record, $tobiblio, $frameworkcode);
 
     # Moving items from the other record to the reference record
+    # Also moving orders from the other record to the reference record, only if the order is linked to an item of the other record
     my $itemnumbers = get_itemnumbers_of($frombiblio);
     foreach my $itloop ($itemnumbers->{$frombiblio}) {
     foreach my $itemnumber (@$itloop) {
@@ -101,6 +103,16 @@ if ($merge) {
 
     # TODO : Moving reserves
 
+    # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
+    my @allorders = GetOrdersByBiblionumber($frombiblio);
+    my @tobiblioitem = GetBiblioItemByBiblioNumber ($tobiblio);
+    my $tobiblioitem_biblioitemnumber = $tobiblioitem [0]-> {biblioitemnumber };
+    foreach my $myorder (@allorders) {
+        $myorder->{'biblionumber'} = $tobiblio;
+        ModOrder ($myorder);
+    # TODO : add error control (in ModOrder?)
+    }
+
     # Deleting the other record
     if (scalar(@errors) == 0) {
     # Move holds
index c015425..db43118 100755 (executable)
@@ -10,7 +10,7 @@ use POSIX qw(strftime);
 
 use C4::Bookseller qw( GetBookSellerFromId );
 
-use Test::More tests => 37;
+use Test::More tests => 42;
 
 BEGIN {
     use_ok('C4::Acquisition');
@@ -32,6 +32,23 @@ my $grouped    = 0;
 my $orders = GetPendingOrders( $supplierid, $grouped );
 isa_ok( $orders, 'ARRAY' );
 
+# testing GetOrdersByBiblionumber
+# result should be undef if no params
+my @ordersbybiblionumber = GetOrdersByBiblionumber();
+is(@ordersbybiblionumber,0,'GetOrdersByBiblionumber : no argument, return undef');
+# TODO : create 2 orders using the same biblionumber, and check the result of GetOrdersByBiblionumber
+# if a record is used in at least one order, result should be an array with at least one element
+SKIP: {
+   skip 'No Orders, cannot test GetOrdersByBiblionumber', 3 unless( scalar @$orders );
+   my $testorder = @$orders[0];
+   my $testbiblio = $testorder->{'biblionumber'};
+   my @listorders = GetOrdersByBiblionumber($testbiblio);
+   isa_ok( ref @listorders, 'ARRAY','GetOrdersByBiblionumber : result is an array' );
+   ok( scalar (@listorders) >0,'GetOrdersByBiblionumber : result contains at least one element' );
+   my @matched_biblios = grep {$_->{biblionumber} == $testbiblio} @listorders;
+   ok ( @matched_biblios == @listorders, "all orders match");
+}
+
 my @lateorders = GetLateOrders(0);
 SKIP: {
    skip 'No Late Orders, cannot test AddClaim', 1 unless @lateorders;