+
+ # 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
+ if (!$exist_itemnumber) {
+ my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
+ set_item_default_location($oldbibitemnum);
+
+ # 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++;
+ }
+
+ # Preparing the next iteration
+ $oldbarcode = $barcodevalue;
+ }
+ undef($itemrecord);
+ }
+ }
+
+