From: Chris Nighswonger Date: Fri, 10 Sep 2010 19:20:51 +0000 (-0400) Subject: Bug 5215 - Barcode field not cleared when using "Add and Duplicate" option on additem.pl X-Git-Url: http://git.rot13.org/?a=commitdiff_plain;h=01247f2fc05eb90949057869522ffcc141940571;p=koha.git Bug 5215 - Barcode field not cleared when using "Add and Duplicate" option on additem.pl The barcode field is not cleared/incremented properly when using the "Add and Duplicate" option. Rather it is pre-populated with the previously used barcode which causes additem.pl to return a duplicate barcode error if the cataloger does not catch the mistake. The problem is caused by the fact that the current item record is simply duplicated and re-loaded into the form. The solution is to add code which either pre-populates with the next barcode number if the incremental pattern is set or clears the p subfield if one of the various patterns using the javascript plugin is selected. In reality C4::Barcodes should be implimented here along with all necessary changes elsewhere. --- diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl index ea4daf93e1..6934807daf 100755 --- a/cataloguing/additem.pl +++ b/cataloguing/additem.pl @@ -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