[Koha-patches] [PATCH] Bug 2593 : way to duplicate items

Matthias Meusburger matthias.meusburger at biblibre.com
Fri Jul 17 17:23:50 CEST 2009


---
 cataloguing/additem.pl                             |  103 ++++++++++++++++++--
 .../prog/en/modules/cataloguing/additem.tmpl       |   24 +++++-
 2 files changed, 119 insertions(+), 8 deletions(-)

diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl
index f11380a..f98ee55 100755
--- a/cataloguing/additem.pl
+++ b/cataloguing/additem.pl
@@ -97,6 +97,13 @@ if ($op eq "additem") {
     my @indicator = $input->param('indicator');
     my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
     my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
+
+    # type of add
+    my $add_submit                 = $input->param('add_submit');
+    my $add_duplicate_submit       = $input->param('add_duplicate_submit');
+    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
@@ -114,16 +121,98 @@ if ($op eq "additem") {
             $record->insert_fields_ordered($fieldItem);
         }
     }
-# check for item barcode # being unique
+
     my $addedolditem = TransformMarcToKoha($dbh,$record);
-    my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
-    push @errors,"barcode_not_unique" if($exist_itemnumber);
-    # if barcode exists, don't create, but report The problem.
-    my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber) unless ($exist_itemnumber);
-    $nextop = "additem";
-    if ($exist_itemnumber) {
+
+    # If we have to add or add & duplicate, we add the item
+    if ($add_submit || $add_duplicate_submit) {
+	# check for item barcode # being unique
+	my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
+	push @errors,"barcode_not_unique" if($exist_itemnumber);
+	# if barcode exists, don't create, but report The problem.
+	my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber) unless ($exist_itemnumber);
+	$nextop = "additem";
+	if ($exist_itemnumber) {
+	    $itemrecord = $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 we have to add multiple copies
+    if ($add_multiple_copies_submit) {
+
+        use C4::Barcodes;
+        my $barcodeobj = C4::Barcodes->new;
+        my $oldbarcode = $addedolditem->{'barcode'};
+        my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
+
+	# If there is a barcode and we can't find him new values, we can't add multiple copies
+        my $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
+	if ($oldbarcode && !$testbarcode) {
+
+	    push @errors, "no_next_barcode";
+	    $itemrecord = $record;
+
+	} else {
+	# We add each item
+
+	    # For the first iteration
+	    my $barcodevalue = $oldbarcode;
+	    my $exist_itemnumber;
+
+
+	    for (my $i = 0; $i < $number_of_copies;) {
+
+		# If there is a barcode
+		if ($barcodevalue) {
+
+		    # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
+		    $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
+
+		    # Putting it into the record
+		    if ($barcodevalue) {
+			$record->field($tagfield)->update($tagsubfield => $barcodevalue);
+		    }
+
+		    # Checking if the barcode already exists
+		    $exist_itemnumber = get_item_from_barcode($barcodevalue);
+		}
+
+		# Adding the item
+		my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber) unless ($exist_itemnumber);
+
+		# We count the item only if it was really added
+		# That way, all items are added, even if there was some already existing barcodes
+		# FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
+		$i++ unless ($exist_itemnumber);
+
+		# Preparing the next iteration
+		$oldbarcode = $barcodevalue;
+	    }
+	    undef($itemrecord);
+	}
+    }
+
+
 #-------------------------------------------------------------------------------
 } elsif ($op eq "edititem") {
 #-------------------------------------------------------------------------------
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tmpl
index ca745e4..5b43114 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tmpl
@@ -23,6 +23,7 @@ function active(numlayer)
     }
 }
 function Check(f) {
+
     var total_errors=0;
 //	alert(f.field_value.length);
     for (i=0 ; i<f.field_value.length ; i++) {
@@ -45,6 +46,13 @@ function Check(f) {
         return false;
     }
 }
+function CheckMultipleAdd(f) {
+
+    if (!f || isNaN(f) || !parseInt(f) == f || f <= 0) { 
+	alert(_("Please enter a number of items to create.")); 
+	return false;	
+    }
+}
 function Dopop(link,i) {
     defaultvalue=document.forms[0].field_value[i].value;
     newin=window.open(link+"&result="+defaultvalue,"valuebuilder",'width=500,height=400,toolbar=false,scrollbars=yes');
@@ -169,6 +177,7 @@ function set_to_today(id, force) {
 <h1>Items for <!-- TMPL_VAR name="title" escape="html" --> <!-- TMPL_IF NAME="author" --> by <!-- TMPL_VAR name="author" --><!-- /TMPL_IF --> (Record #<!-- TMPL_VAR NAME="biblionumber" -->)</h1>
 
 <!-- TMPL_IF NAME="barcode_not_unique" --><div class="dialog alert"><strong>Error saving item</strong>: Barcode must be unique.</div><!-- /TMPL_IF -->
+<!-- TMPL_IF NAME="no_next_barcode" --><div class="dialog alert"><strong>Error saving items</strong>: Unable to automatically determine values for barcodes. No item has been inserted.</div><!-- /TMPL_IF -->
 <!-- TMPL_IF NAME="book_on_loan" --><div class="dialog alert"><strong>Cannot Delete</strong>: item is checked out.</div><!-- /TMPL_IF -->
 <!-- TMPL_IF NAME="book_reserved" --><div class="dialogalert"><strong>Cannot Delete</strong>: item has a waiting hold.</div><!-- /TMPL_IF -->
 
@@ -230,7 +239,20 @@ function set_to_today(id, force) {
     <input type="hidden" name="itemnumber" value="<!-- TMPL_VAR NAME="itemnumber" -->" />
 
 <fieldset class="action">    <!-- TMPL_IF name="opisadd" -->
-    <input type="submit" value="Add item" onclick="return Check(this.form)" />
+    <input type="submit" name="phony_submit" value="phony_submit" id="phony_submit" style="display:none;" onclick="return false;" />
+    <!-- Note : We use here a false submit button because we have several submit buttons and we don't want the user to believe he validated the adding of multiple copies 
+		when pressing the enter key, while in fact it is the first submit button that is validated, in our case the "add (single) item" button. 
+		It is a bit tricky, but necessary in the sake of UI correctness. 
+    -->
+
+    <input type="submit" name="add_submit" value="Add item" onclick="return Check(this.form)" />
+    <input type="submit" name="add_duplicate_submit" value="Add &amp; Duplicate" onclick="return Check(this.form)" />
+    <input type="submit" name="add_multiple_copies" value="Add Multiple Copies" onclick="javascript:this.nextSibling.style.visibility='visible';document.f.number_of_copies.focus(); return false;" /><span id="add_multiple_copies_span" style="visibility:hidden">
+	<label for="number_of_copies">Number of copies to add : </label>
+	<input type="text" id="number_of_copies" name="number_of_copies" value="" size="2" />
+	<input type="submit" id="add_multiple_copies_submit" name="add_multiple_copies_submit" value="Add" onclick="javascript:return Check(this.form) &amp;&amp; CheckMultipleAdd(this.form.number_of_copies.value);" />
+    </span>
+
     <!-- TMPL_ELSE -->
     <input type="hidden" name="tag" value="<!-- TMPL_VAR NAME="itemtagfield" -->" />
     <input type="hidden" name="subfield" value="<!-- TMPL_VAR NAME="itemtagsubfield" -->" />
-- 
1.6.0.4




More information about the Koha-patches mailing list