Bug 32823: Fix cataloguing/value_builder/unimarc_field_100_authorities.pl
[srvgit] / cataloguing / merge.pl
index a882daa..7fe2188 100755 (executable)
 use Modern::Perl;
 use CGI qw ( -utf8 );
 
-use C4::Output;
-use C4::Auth;
-use C4::Items;
-use C4::Biblio;
-use C4::Serials;
-use C4::Koha;
-use C4::Reserves qw/MergeHolds/;
-use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/;
+use C4::Output qw( output_html_with_http_headers );
+use C4::Auth qw( get_template_and_user );
+use C4::Biblio qw(
+    DelBiblio
+    GetBiblioData
+    GetFrameworkCode
+    GetMarcFromKohaField
+    GetMarcStructure
+    ModBiblio
+    TransformHtmlToMarc
+);
+use C4::Serials qw( CountSubscriptionFromBiblionumber );
+use C4::Reserves qw( MergeHolds );
+use C4::Acquisition qw( ModOrder GetOrdersByBiblionumber );
+
+use Koha::BiblioFrameworks;
+use Koha::Biblios;
+use Koha::Items;
 use Koha::MetadataRecord;
 
-my $input = new CGI;
-my @biblionumbers = $input->param('biblionumber');
+my $input = CGI->new;
+my @biblionumbers = $input->multi_param('biblionumber');
 my $merge = $input->param('merge');
 
 my @errors;
@@ -42,7 +52,6 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         template_name   => "cataloguing/merge.tt",
         query           => $input,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { editcatalogue => 'edit_catalogue' },
     }
 );
@@ -75,28 +84,20 @@ if ($merge) {
     }
 
     # Rewriting the leader
-    $record->leader(GetMarcBiblio($ref_biblionumber)->leader());
+    my $biblio = Koha::Biblios->find($ref_biblionumber);
+    $record->leader($biblio->metadata->record->leader());
 
     my $frameworkcode = $input->param('frameworkcode');
-    my @notmoveditems;
 
     # Modifying the reference record
     ModBiblio($record, $ref_biblionumber, $frameworkcode);
 
-    # Moving items from the other record to the reference record
+    # Moving items and article requests from the other record to the reference record
+    $biblio = $biblio->get_from_storage;
     foreach my $biblionumber (@biblionumbers) {
-        my $itemnumbers = get_itemnumbers_of($biblionumber);
-        foreach my $itemnumber (@{ $itemnumbers->{$biblionumber} }) {
-        my $res = MoveItemFromBiblio($itemnumber, $biblionumber, $ref_biblionumber);
-        if (not defined $res) {
-            push @notmoveditems, $itemnumber;
-        }
-    }
-    }
-    # If some items could not be moved :
-    if (scalar(@notmoveditems) > 0) {
-        my $itemlist = join(' ',@notmoveditems);
-        push @errors, { code => "CANNOT_MOVE", value => $itemlist };
+        my $from_biblio = Koha::Biblios->find($biblionumber);
+        $from_biblio->items->move_to_biblio($biblio);
+        $from_biblio->article_requests->update({ biblionumber => $ref_biblionumber }, { no_triggers => 1 });
     }
 
     my $sth_subscription = $dbh->prepare("
@@ -108,11 +109,15 @@ if ($merge) {
     my $sth_serial = $dbh->prepare("
         UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
     ");
+    my $sth_suggestions = $dbh->prepare("
+        UPDATE suggestions SET biblionumber = ? WHERE biblionumber = ?
+    ");
 
     my $report_header = {};
     foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
         # build report
-        my $marcrecord = GetMarcBiblio($biblionumber);
+        my $biblio = Koha::Biblios->find($biblionumber);
+        my $marcrecord = $biblio->metadata->record;
         my %report_record = (
             biblionumber => $biblionumber,
             fields => {},
@@ -152,12 +157,13 @@ if ($merge) {
         }
 
     # Moving serials
-        $sth_serial->execute($ref_biblionumber, $biblionumber);
+    $sth_serial->execute($ref_biblionumber, $biblionumber);
 
-    # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
+    # Moving suggestions
+    $sth_suggestions->execute($ref_biblionumber, $biblionumber);
+
+    # Moving orders (orders linked to items of frombiblio have already been moved by move_to_biblio)
     my @allorders = GetOrdersByBiblionumber($biblionumber);
-    my @tobiblioitem = GetBiblioItemByBiblioNumber ($ref_biblionumber);
-    my $tobiblioitem_biblioitemnumber = $tobiblioitem [0]-> {biblioitemnumber };
     foreach my $myorder (@allorders) {
         $myorder->{'biblionumber'} = $ref_biblionumber;
         ModOrder ($myorder);
@@ -178,7 +184,7 @@ if ($merge) {
         result => 1,
         report_records => \@report_records,
         report_header => $report_header,
-        ref_biblionumber => $input->param('ref_biblionumber')
+        ref_biblionumber => scalar $input->param('ref_biblionumber')
     );
 
 #-------------------------
@@ -199,11 +205,12 @@ if ($merge) {
         # Creating a loop for display
         my @records;
         foreach my $biblionumber (@biblionumbers) {
-            my $marcrecord = GetMarcBiblio($biblionumber);
+            my $biblio = Koha::Biblios->find($biblionumber);
+            my $marcrecord = $biblio->metadata->record;
             my $frameworkcode = GetFrameworkCode($biblionumber);
-            my $recordObj = new Koha::MetadataRecord({'record' => $marcrecord, schema => $marcflavour});
+            my $recordObj = Koha::MetadataRecord->new({'record' => $marcrecord, schema => $marcflavour});
             my $record = {
-                biblionumber => $biblionumber,
+                recordid => $biblionumber,
                 record => $marcrecord,
                 frameworkcode => $frameworkcode,
                 display => $recordObj->createMergeHash($tagslib),
@@ -245,18 +252,8 @@ if ($merge) {
             records => \@records,
         );
 
-        my $frameworks = getframeworks;
-        my @frameworkselect;
-        foreach my $thisframeworkcode ( keys %$frameworks ) {
-            my %row = (
-                value         => $thisframeworkcode,
-                frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
-            );
-            push @frameworkselect, \%row;
-        }
-        $template->param(
-            frameworkselect => \@frameworkselect,
-        );
+        my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
+        $template->param( frameworks => $frameworks );
     }
 }