From dafdb9b607dc57c165f5a34cc8083913926351b4 Mon Sep 17 00:00:00 2001
From: Olli-Antti Kivilahti <olli-antti.kivilahti@jns.fi>
Date: Fri, 7 Nov 2014 14:31:02 +0200
Subject: [PATCH] Bug 13182 - Overlay an existing Biblio with the acquired
 import_record

If we know we already have a matching biblio in Koha, we can optionally overlay the existing Biblio with the
new one. (We don't really need it but looks like Bg_12074 needs it).

TEST PLAN

1. Add Items to an order from a staged file.
2. Go and edit the newly added Biblio. Set the field 003 to "TEST ME GENTLY".
3. Add more Items for this same biblio from the same staged_file.
4. While selecting what to add, check the "Overlay?"-checkbox to replace the existing Bibliographic record
   with the import_record you are ordering.
5. Save the orderline selection.
6. Observe that the existing modified biblio has changed to it's latest revision.
---
 acqui/addorderiso2709.pl                                         | 9 ++++++---
 koha-tmpl/intranet-tmpl/prog/en/modules/acqui/addorderiso2709.tt | 1 +
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/acqui/addorderiso2709.pl b/acqui/addorderiso2709.pl
index 35884c6..efe30e8 100755
--- a/acqui/addorderiso2709.pl
+++ b/acqui/addorderiso2709.pl
@@ -158,6 +158,7 @@ if ($op eq ""){
     my $import_batch_id = $cgiparams->{'import_batch_id'};
     my $biblios = GetImportRecordsRange($import_batch_id);
     my @import_record_id_selected = $input->param("import_record_id");
+    my @overlay = $input->param("overlay");
     my @quantities = $input->param('quantity');
     my @prices = $input->param('price');
     my @budgets_id = $input->param('budget_id');
@@ -172,6 +173,7 @@ if ($op eq ""){
         my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
         my $match = GetImportRecordMatches( $biblio->{'import_record_id'}, 1 );
         my $biblionumber=$#$match > -1?$match->[0]->{'biblionumber'}:0;
+        my $c_overlay = shift @overlay;
         my $c_quantity = shift( @quantities ) || GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour') ) || 1;
         my $c_budget_id = shift( @budgets_id ) || $input->param('all_budget_id') || $budget_id;
         my $c_discount = shift ( @discount);
@@ -179,8 +181,8 @@ if ($op eq ""){
         my $c_sort1 = shift( @sort1 ) || $input->param('all_sort1') || '';
         my $c_sort2 = shift( @sort2 ) || $input->param('all_sort2') || '';
 
-        # 1st insert the biblio, or find it through matcher
-        unless ( $biblionumber ) {
+        # INSERT the biblio if no match found, or if we want to overlay the existing match
+        if ( not($biblionumber) || $c_overlay ) {
             # add the biblio
             my $bibitemnum;
 
@@ -195,7 +197,8 @@ if ($op eq ""){
                     }
                 }
             }
-            ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
+            ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' ) unless $c_overlay;
+            ModBiblio( $marcrecord, $biblionumber, $cgiparams->{'frameworkcode'} || '' ) if $c_overlay;
             SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
 
             #We need to provide information to koha.import_record_matches to tell Koha that this import_record matches the
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/addorderiso2709.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/addorderiso2709.tt
index cf03b7c..e0a4b42 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/addorderiso2709.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/addorderiso2709.tt
@@ -210,6 +210,7 @@
                                 [% END %]
                                 [% IF ( biblio.match_biblionumber ) %]
                                   Matches biblio [% biblio.match_biblionumber %] (score = [% biblio.match_score %]): <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% biblio.match_biblionumber %]">[% biblio.match_citation %]</a>
+                                  Overlay? <input type="checkbox" name="overlay" id="overlay_record_[% biblio.import_record_id %]" value="[% biblio.import_record_id %]" />
                                 [% END %]
                               </span>
                             </li>
-- 
1.9.1