Bug 5215 - Barcode field not cleared when using "Add and Duplicate" option on additem.pl
[koha.git] / cataloguing / additem.pl
index ea4daf9..6934807 100755 (executable)
@@ -33,6 +33,8 @@ use C4::Dates;
 
 use MARC::File::XML;
 
+my $dbh = C4::Context->dbh;
+
 sub find_value {
     my ($tagfield,$insubfield,$record) = @_;
     my $result;
@@ -69,8 +71,27 @@ sub set_item_default_location {
     }
 }
 
+# NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code
+# NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
+sub _increment_barcode {
+    my ($record, $frameworkcode) = @_;
+    my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
+    unless ($record->field($tagfield)->subfield($tagsubfield)) {
+        my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
+        $sth_barcode->execute;
+        my ($newbarcode) = $sth_barcode->fetchrow;
+        $newbarcode++;
+        # OK, we have the new barcode, now create the entry in MARC record
+        my $fieldItem = $record->field($tagfield);
+        $record->delete_field($fieldItem);
+        $fieldItem->add_subfields($tagsubfield => $newbarcode);
+        $record->insert_fields_ordered($fieldItem);
+    }
+    return $record;
+}
+
+
 my $input = new CGI;
-my $dbh = C4::Context->dbh;
 my $error        = $input->param('error');
 my $biblionumber = $input->param('biblionumber');
 my $itemnumber   = $input->param('itemnumber');
@@ -126,22 +147,8 @@ if ($op eq "additem") {
     my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
     my $number_of_copies           = $input->param('number_of_copies');
 
-    # if autoBarcode is set to 'incremental', calculate barcode...
-       # NOTE: This code is subject to change in 3.2 with the implemenation of ajax based autobarcode code
-       # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
     if (C4::Context->preference('autoBarcode') eq 'incremental') {
-        my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
-        unless ($record->field($tagfield)->subfield($tagsubfield)) {
-            my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
-            $sth_barcode->execute;
-            my ($newbarcode) = $sth_barcode->fetchrow;
-            $newbarcode++;
-            # OK, we have the new barcode, now create the entry in MARC record
-            my $fieldItem = $record->field($tagfield);
-            $record->delete_field($fieldItem);
-            $fieldItem->add_subfields($tagsubfield => $newbarcode);
-            $record->insert_fields_ordered($fieldItem);
-        }
+        $record = _increment_barcode($record, $frameworkcode);
     }
 
     my $addedolditem = TransformMarcToKoha($dbh,$record);
@@ -164,22 +171,18 @@ if ($op eq "additem") {
 
     # If we have to add & duplicate
     if ($add_duplicate_submit) {
-
-        # We try to get the next barcode
-        use C4::Barcodes;
-        my $barcodeobj = C4::Barcodes->new;
-        my $barcodevalue = $barcodeobj->next_value($addedolditem->{'barcode'}) if $barcodeobj;
-        my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
-        if ($record->field($tagfield)->subfield($tagsubfield)) {
-            # If we got the next codebar value, we put it in the record
-            if ($barcodevalue) {
-                $record->field($tagfield)->update($tagsubfield => $barcodevalue);
-            # If not, we delete the recently inserted barcode from the record (so the user can input a barcode himself)
-            } else {
-                $record->field($tagfield)->update($tagsubfield => '');
-            }
-        }
         $itemrecord = $record;
+        if (C4::Context->preference('autoBarcode') eq 'incremental') {
+            $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
+        }
+        else {
+            # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
+            my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
+            my $fieldItem = $itemrecord->field($tagfield);
+            $itemrecord->delete_field($fieldItem);
+            $fieldItem->delete_subfields($tagsubfield);
+            $itemrecord->insert_fields_ordered($fieldItem);
+        }
     }
 
     # If we have to add multiple copies