From 148bec22f8da690cd43af236a60088d1dab3bb33 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Fri, 27 Mar 2015 13:36:05 +0100
Subject: [PATCH] [PASSED QA] Bug 6520: Display items for staged record

When records are imported into Koha, the items is stored into the
import_items table.
This marcxml in this table is never retrieved to display items.

Test plan:
1/ Import a records with items
2/ Before importing the batch into the catalog, you can see the marc
of the records, in the table below.
3/ Verify that the items is correctly displayed.

QA note: This patch does not provide test for new subroutines but the
module (C4::ImportBatch) is not tested at all and it will be time
consuming to provide them.

Signed-off-by: Nicole Engard <nengard@bywatersolutions.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
---
 C4/ImportBatch.pm     | 38 ++++++++++++++++++++++++++++++++++----
 catalogue/showmarc.pl |  3 +--
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm
index 08304d7..de900eb 100644
--- a/C4/ImportBatch.pm
+++ b/C4/ImportBatch.pm
@@ -169,12 +169,42 @@ sub GetImportRecordMarc {
     my ($import_record_id) = @_;
 
     my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("SELECT marc, encoding FROM import_records WHERE import_record_id = ?");
-    $sth->execute($import_record_id);
-    my ($marc, $encoding) = $sth->fetchrow();
-    $sth->finish();
+    my ( $marc, $encoding ) = $dbh->selectrow_array(q|
+        SELECT marc, encoding
+        FROM import_records
+        WHERE import_record_id = ?
+    |, undef, $import_record_id );
+
     return $marc, $encoding;
+}
+
+sub GetRecordFromImportBiblio {
+    my ( $import_record_id, $embed_items ) = @_;
+
+    my ($marc) = GetImportRecordMarc($import_record_id);
+    my $record = MARC::Record->new_from_usmarc($marc);
 
+    EmbedItemsInImportBiblio( $record, $import_record_id ) if $embed_items;
+
+    return $record;
+}
+
+sub EmbedItemsInImportBiblio {
+    my ( $record, $import_record_id ) = @_;
+    my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber", '');
+    my $dbh = C4::Context->dbh;
+    my $import_items = $dbh->selectall_arrayref(q|
+        SELECT import_items.marcxml
+        FROM import_items
+        WHERE import_record_id = ?
+    |, { Slice => {} }, $import_record_id );
+    my @item_fields;
+    for my $import_item ( @$import_items ) {
+        my $item_marc = MARC::Record::new_from_xml($import_item->{marcxml});
+        push @item_fields, $item_marc->field($itemtag);
+    }
+    $record->append_fields(@item_fields);
+    return $record;
 }
 
 =head2 GetImportRecordMarcXML
diff --git a/catalogue/showmarc.pl b/catalogue/showmarc.pl
index de7fb22..a48a852 100755
--- a/catalogue/showmarc.pl
+++ b/catalogue/showmarc.pl
@@ -43,8 +43,7 @@ my $view= $input->param('viewas')||'';
 
 my $record;
 if ($importid) {
-    my ($marc) = GetImportRecordMarc($importid);
-    $record = MARC::Record->new_from_usmarc($marc);
+    $record = C4::ImportBatch::GetRecordFromImportBiblio( $importid, 'embed_items' );
 }
 else {
     $record =GetMarcBiblio($biblionumber);
-- 
1.9.1