Bug 10572: Add phone to message_transport_types table for new installs
[koha.git] / cataloguing / merge.pl
index 020001d..e24b767 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/perl 
+#!/usr/bin/perl
 
 
 # Copyright 2009 BibLibre
@@ -29,6 +29,8 @@ use C4::Biblio;
 use C4::Serials;
 use C4::Koha;
 use C4::Reserves qw/MergeHolds/;
+use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/;
+use Koha::MetadataRecord;
 
 my $input = new CGI;
 my @biblionumber = $input->param('biblionumber');
@@ -69,29 +71,30 @@ 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) {
-           my $res = MoveItemFromBiblio($itemnumber, $frombiblio, $tobiblio);
-           if (not defined $res) {
-               push @notmoveditems, $itemnumber;
-           }
-       }
+    foreach my $itemnumber (@$itloop) {
+        my $res = MoveItemFromBiblio($itemnumber, $frombiblio, $tobiblio);
+        if (not defined $res) {
+            push @notmoveditems, $itemnumber;
+        }
+    }
     }
     # If some items could not be moved :
     if (scalar(@notmoveditems) > 0) {
-               my $itemlist = join(' ',@notmoveditems);
-               push @errors, "The following items could not be moved from the old record to the new one: $itemlist";
+        my $itemlist = join(' ',@notmoveditems);
+        push @errors, { code => "CANNOT_MOVE", value => $itemlist };
     }
 
     # Moving subscriptions from the other record to the reference record
     my $subcount = CountSubscriptionFromBiblionumber($frombiblio);
     if ($subcount > 0) {
-       $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
-       $sth->execute($tobiblio, $frombiblio);
+    $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
+    $sth->execute($tobiblio, $frombiblio);
 
-       $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
-       $sth->execute($tobiblio, $frombiblio);
+    $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
+    $sth->execute($tobiblio, $frombiblio);
 
     }
 
@@ -101,18 +104,28 @@ 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
-       MergeHolds($dbh,$tobiblio,$frombiblio);
-       my $error = DelBiblio($frombiblio);
-       push @errors, $error if ($error); 
+    # Move holds
+    MergeHolds($dbh,$tobiblio,$frombiblio);
+    my $error = DelBiblio($frombiblio);
+    push @errors, $error if ($error);
     }
 
     # Parameters
     $template->param(
-       result => 1,
-       biblio1 => $input->param('biblio1')
+    result => 1,
+    biblio1 => $input->param('biblio1')
     );
 
 #-------------------------
@@ -123,7 +136,7 @@ if ($merge) {
     my $biblionumber = $input->param('biblionumber');
 
     if (scalar(@biblionumber) != 2) {
-        push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
+        push @errors, { code => "WRONG_COUNT", value => scalar(@biblionumber) };
     }
     else {
         my $data1 = GetBiblioData($biblionumber[0]);
@@ -156,8 +169,12 @@ if ($merge) {
             my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
 
             # Creating a loop for display
-            my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
-            my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
+
+            my $recordObj1 = new Koha::MetadataRecord({ 'record' => GetMarcBiblio($mergereference), 'schema' => lc C4::Context->preference('marcflavour') });
+            my $recordObj2 = new Koha::MetadataRecord({ 'record' => GetMarcBiblio($notreference), 'schema' => lc C4::Context->preference('marcflavour') });
+
+            my @record1 = $recordObj1->createMergeHash($tagslib);
+            my @record2 = $recordObj2->createMergeHash($tagslib);
 
             # Parameters
             $template->param(
@@ -206,8 +223,7 @@ if ($merge) {
 
 if (@errors) {
     # Errors
-    my @errors_loop  = map{{error => $_}}@errors;
-    $template->param( errors  => \@errors_loop );
+    $template->param( errors  => \@errors );
 }
 
 output_html_with_http_headers $input, $cookie, $template->output;
@@ -220,70 +236,3 @@ exit;
 # ------------------------
 # Functions
 # ------------------------
-sub _createMarcHash {
-     my $record = shift;
-    my $tagslib = shift;
-    my @array;
-    my @fields = $record->fields();
-
-
-    foreach my $field (@fields) {
-       my $fieldtag = $field->tag();
-       if ($fieldtag < 10) {
-           if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
-               push @array, { 
-                       field => [ 
-                                   {
-                                       tag => $fieldtag, 
-                                       key => createKey(), 
-                                       value => $field->data(),
-                                   }
-                               ]
-                           };    
-           }
-       } else {
-           my @subfields = $field->subfields();
-           my @subfield_array;
-           foreach my $subfield (@subfields) {
-               if ($tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
-                   push @subfield_array, {  
-                                        subtag => @$subfield[0],
-                                       subkey => createKey(), 
-                                        value => @$subfield[1],
-                                      };
-               }
-
-           }
-
-           if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
-               push @array, {
-                       field => [  
-                                   {
-                                       tag => $fieldtag, 
-                                       key => createKey(), 
-                                       indicator1 => $field->indicator(1), 
-                                       indicator2 => $field->indicator(2), 
-                                       subfield   => [@subfield_array], 
-                                   }
-                               ]
-                           };  
-           }
-
-       }
-    }
-    return [@array];
-
-}
-
-=head2 CreateKey
-
-Create a random value to set it into the input name
-
-=cut
-
-sub createKey {
-    return int(rand(1000000));
-}
-
-
-