Bug 29234: Further clean Z3950 Tests
[srvgit] / acqui / addorderiso2709.pl
index 93d4e22..fb31a8b 100755 (executable)
@@ -30,7 +30,7 @@ use Encode;
 use C4::Context;
 use C4::Auth qw( get_template_and_user );
 use C4::Output qw( output_html_with_http_headers );
-use C4::ImportBatch qw( GetImportRecordMatches SetImportBatchStatus GetImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction );
+use C4::ImportBatch qw( SetImportBatchStatus GetImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction );
 use C4::Matcher;
 use C4::Search qw( FindDuplicate );
 use C4::Biblio qw(
@@ -51,6 +51,7 @@ use Koha::Acquisition::Baskets;
 use Koha::Acquisition::Currencies;
 use Koha::Acquisition::Orders;
 use Koha::Acquisition::Booksellers;
+use Koha::ImportBatches;
 use Koha::Import::Records;
 use Koha::Patrons;
 
@@ -135,6 +136,8 @@ if ($op eq ""){
 
     # retrieve the file you want to import
     my $import_batch_id = $cgiparams->{'import_batch_id'};
+    my $import_batch = Koha::ImportBatches->find( $import_batch_id );
+    my $overlay_action = $import_batch->overlay_action;
     my $import_records = Koha::Import::Records->search({
         import_batch_id => $import_batch_id,
     });
@@ -157,8 +160,9 @@ if ($op eq ""){
         # Check if this import_record_id was selected
         next if not grep { $_ eq $import_record->import_record_id } @import_record_id_selected;
         my $marcrecord = $import_record->get_marc_record || die "couldn't translate marc information";
-        my $match = GetImportRecordMatches( $import_record->import_record_id, 1 );
-        my $biblionumber=$#$match > -1?$match->[0]->{'biblionumber'}:0;
+        my $matches = $import_record->get_import_record_matches({ chosen => 1 });
+        my $match = $matches->count ? $matches->next : undef;
+        my $biblionumber = $match ? $match->candidate_match_id : 0;
         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);
@@ -168,7 +172,13 @@ if ($op eq ""){
         my $c_price = shift( @prices ) || GetMarcPrice($marcrecord, C4::Context->preference('marcflavour'));
 
         # Insert the biblio, or find it through matcher
-        unless ( $biblionumber ) {
+        if ( $biblionumber ) { # If matched during staging we can continue
+            $import_record->status('imported')->store;
+            if( $overlay_action eq 'replace' ){
+                my $biblio = Koha::Biblios->find( $biblionumber );
+                $import_record->replace({ biblio => $biblio });
+            }
+        } else { # Otherwise we check for duplicates, and skip if they exist
             if ($matcher_id) {
                 if ( $matcher_id eq '_TITLE_AUTHOR_' ) {
                     $duplifound = 1 if FindDuplicate($marcrecord);
@@ -182,10 +192,8 @@ if ($op eq ""){
                 $duplinbatch = $import_batch_id and next if $duplifound;
             }
 
-            # add the biblio
-            my $bibitemnum;
-
-            # remove ISBN -
+            # remove hyphens (-) from ISBN
+            # FIXME: This should probably be optional
             my ( $isbnfield, $isbnsubfield ) = GetMarcFromKohaField( 'biblioitems.isbn' );
             if ( $marcrecord->field($isbnfield) ) {
                 foreach my $field ( $marcrecord->field($isbnfield) ) {
@@ -196,9 +204,9 @@ if ($op eq ""){
                     }
                 }
             }
-            ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
-            $import_record->status('imported')->store;
-        } else {
+
+            # add the biblio
+            ( $biblionumber, undef ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
             $import_record->status('imported')->store;
         }
 
@@ -333,30 +341,28 @@ if ($op eq ""){
                 $orderinfo{listprice} = 0;
             }
 
-        # remove uncertainprice flag if we have found a price in the MARC record
-        $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
-
-        my $order = Koha::Acquisition::Order->new( \%orderinfo );
-        $order->populate_with_prices_for_ordering();
-        $order->populate_with_prices_for_receiving();
-        $order->store;
-
-        # 4th, add items if applicable
-        # parse the item sent by the form, and create an item just for the import_record_id we are dealing with
-        # this is not optimised, but it's working !
-        if ( $basket->effective_create_items eq 'ordering' && !$basket->is_standing ) {
-            my @tags         = $input->multi_param('tag');
-            my @subfields    = $input->multi_param('subfield');
-            my @field_values = $input->multi_param('field_value');
-            my @serials      = $input->multi_param('serial');
-            my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values );
-            my $record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
-            for (my $qtyloop=1;$qtyloop <= $c_quantity;$qtyloop++) {
-                my ( $biblionumber, $bibitemnum, $itemnumber ) = AddItemFromMarc( $record, $biblionumber );
-                $order->add_item( $itemnumber );
+            # remove uncertainprice flag if we have found a price in the MARC record
+            $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
+
+            my $order = Koha::Acquisition::Order->new( \%orderinfo );
+            $order->populate_with_prices_for_ordering();
+            $order->populate_with_prices_for_receiving();
+            $order->store;
+
+            # 4th, add items if applicable
+            # parse the item sent by the form, and create an item just for the import_record_id we are dealing with
+            # this is not optimised, but it's working !
+            if ( $basket->effective_create_items eq 'ordering' && !$basket->is_standing ) {
+                my @tags         = $input->multi_param('tag');
+                my @subfields    = $input->multi_param('subfield');
+                my @field_values = $input->multi_param('field_value');
+                my @serials      = $input->multi_param('serial');
+                my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values );
+                my $record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
+                for (my $qtyloop=1;$qtyloop <= $c_quantity;$qtyloop++) {
+                    my ( $biblionumber, undef, $itemnumber ) = AddItemFromMarc( $record, $biblionumber );
+                    $order->add_item( $itemnumber );
                 }
-            } else {
-                $import_record->status( 'imported' )->store;
             }
         }
         $imported++;
@@ -472,7 +478,9 @@ sub import_biblios_list {
     while ( my $import_record = $import_records->next ) {
         my $item_id = 1;
         $biblio_count++;
-        my $match = GetImportRecordMatches($import_record->import_record_id, 1);
+        my $matches = $import_record->get_import_record_matches({ chosen => 1 });
+        my $match = $matches->count ? $matches->next : undef;
+        my $match_biblio = $match ? Koha::Biblios->find({ biblionumber => $match->candidate_match_id }) : undef;
         my %cellrecord = (
             import_record_id => $import_record->import_record_id,
             import_biblio => $import_record->import_biblio,
@@ -480,9 +488,9 @@ sub import_biblios_list {
             status => $import_record->status,
             record_sequence => $import_record->record_sequence,
             overlay_status => $import_record->overlay_status,
-            match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
-            match_citation     => $#$match > -1 ? $match->[0]->{'title'} || '' . ' ' . $match->[0]->{'author'} || '': '',
-            match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
+            match_biblionumber => $match ? $match->candidate_match_id : 0,
+            match_citation     => $match_biblio ? ($match_biblio->title || '') . ' ' .( $match_biblio->author || ''): '',
+            match_score => $match ? $match->score : 0,
         );
         my $marcrecord = $import_record->get_marc_record || die "couldn't translate marc information";