Merge remote branch 'kc/new/biblibre_reports' into kcmaster
[koha.git] / cataloguing / additem.pl
index 67dc812..6dc190a 100755 (executable)
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
-use CGI;
 use strict;
+#use warnings; FIXME - Bug 2505
+use CGI;
 use C4::Auth;
 use C4::Output;
 use C4::Biblio;
@@ -32,6 +33,8 @@ use C4::Dates;
 
 use MARC::File::XML;
 
+my $dbh = C4::Context->dbh;
+
 sub find_value {
     my ($tagfield,$insubfield,$record) = @_;
     my $result;
@@ -68,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');
@@ -80,11 +102,11 @@ my $frameworkcode = &GetFrameworkCode($biblionumber);
 # Defining which userflag is needing according to the framework currently used
 my $userflags;
 if (defined $input->param('frameworkcode')) {
-    $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_catalogue";
+    $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
 }
 
 if (not defined $userflags) {
-    $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_catalogue";
+    $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
 }
 
 my ($template, $loggedinuser, $cookie)
@@ -125,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);
@@ -163,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
@@ -265,7 +269,10 @@ if ($op eq "additem") {
         my $items = &GetItemsByBiblioitemnumber($biblioitem->{biblioitemnumber});
 
         foreach my $item (@$items){
-            &DelItem($dbh,$biblionumber,$item->{itemnumber});
+            # FIXME although it won't delete items that have loans
+            # or waiting holds on them, should explicitly tell operator
+            # about items that are not deleted
+            &DelItemCheck($dbh,$biblionumber,$item->{itemnumber});
         }
        }
 #-------------------------------------------------------------------------------
@@ -421,7 +428,7 @@ foreach my $tag (sort keys %{$tagslib}) {
         }
     }
 
-    my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
+    my $attributes_no_value = qq(id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
     my $attributes          = qq($attributes_no_value value="$value" );
     if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
       my @authorised_values;
@@ -432,7 +439,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" ) {
@@ -503,7 +513,7 @@ foreach my $tag (sort keys %{$tagslib}) {
           -override => 1,
           -size     => 1,
           -multiple => 0,
-          -tabindex => 1,
+         # -tabindex => 1,
           -id       => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
           -class    => "input_marceditor",
       );
@@ -522,8 +532,12 @@ foreach my $tag (sort keys %{$tagslib}) {
         if (do $plugin) {
             my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
             my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
+            my $change = index($javascript, 'function Change') > -1 ?
+                "return Change$function_name($subfield_data{random}, '$subfield_data{id}');" :
+                'return 1;';
             $subfield_data{marc_value} = qq[<input $attributes
                 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
+                onchange=" $change"
                  onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
                 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
                 $javascript];