Merge remote branch 'koha-fbc/k_bug_5215' into master
authorGalen Charlton <gmcharlt@gmail.com>
Wed, 6 Oct 2010 11:12:25 +0000 (07:12 -0400)
committerGalen Charlton <gmcharlt@gmail.com>
Wed, 6 Oct 2010 11:12:30 +0000 (07:12 -0400)
Signed-off-by: Galen Charlton <gmcharlt@gmail.com>
1  2 
cataloguing/additem.pl

diff --combined cataloguing/additem.pl
@@@ -33,6 -33,8 +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 +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 +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);
  
      # 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
@@@ -433,10 -436,7 +436,10 @@@ foreach my $tag (sort keys %{$tagslib}
            foreach my $thisbranch (@$branches) {
                push @authorised_values, $thisbranch->{value};
                $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
 -             # $value = $thisbranch->{value} if $thisbranch->{selected};
 +              # in edit item this is set to the data value otherwise use default
 +              if ($op ne 'edititem' && $thisbranch->{selected} ) {
 +                  $value = $thisbranch->{value};
 +              }
            }
        }
        elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {