From 5a3433356014bc5667a44368c8f93352491d992b Mon Sep 17 00:00:00 2001 From: Baptiste Wojtkowski Date: Wed, 31 Jul 2024 11:32:50 +0200 Subject: [PATCH] Bug 8425: Increment the barcode on each call of set_barcode() Duplicate barcodes are generated when placing an Order in Acquisitions when AcqCreateItem = "placing an order" and Autobarcode is turned on. The problem is that you can accidentally attach 3 items to an order, but only 1 will be saved to the database. When you go to receive your order, you can only receive 1 item as the other two were never made, since the barcode wasn't unique. In 3.8.0, a software error comes up which prevent any item creation, I believe, but master (3.9.x) doesn't throw any warnings or errors. Yes.. This tries and solve a bug from 3.8.0 /o/ Test plan: 1 - set syspref "autoBarcode" to generated in the form yymm001 2 - set the barcode field to "barcode.pl" in marc structure 3 - create a new basket 4 - add one item to this basket 5 - click on the barcode field -> it should have a barcode 6 - click on add item and click on the new barcode fiels, it should have the same value APPLY PATCH: 7 - click on the barcode field -> it should have a barcode 8 - click on add item and click on the new barcode fields, it should have an higher barcode. Note : The barcode is incremented each time the barcode with an empty value is clicked on. Therefore, if the librarian removes the value from barcode and click again, they could get another barcode. I do not know if it is an issue. Note: This patch is not splitted in two parts anymore Signed-off-by: Sam Sowanick --- C4/Barcodes/ValueBuilder.pm | 5 ++- cataloguing/value_builder/barcode.pl | 63 ++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/C4/Barcodes/ValueBuilder.pm b/C4/Barcodes/ValueBuilder.pm index 76464ccd4b5..8e861193ee9 100644 --- a/C4/Barcodes/ValueBuilder.pm +++ b/C4/Barcodes/ValueBuilder.pm @@ -63,8 +63,11 @@ sub get_barcode { .siblings("select") .val(); + if(typeof offset == 'undefined'){ + var offset = 0; + } if ( \$(elt).val() == '' ) { - \$(elt).val(homebranch + '$nextnum'); + \$(elt).val(homebranch + ($nextnum + offset)); } ~; return $nextnum, $scr; diff --git a/cataloguing/value_builder/barcode.pl b/cataloguing/value_builder/barcode.pl index 5ec7dbece4b..2b408badce3 100755 --- a/cataloguing/value_builder/barcode.pl +++ b/cataloguing/value_builder/barcode.pl @@ -90,23 +90,78 @@ my $builder = sub { # default js body (if not filled by hbyymmincr) $scr or $scr = < -function set_barcode(id, force) { +if(typeof autobarcodetype == 'undefined') { + var autobarcodetype = "$autoBarcodeType"; + var attempt = -1 + var incrementalBarcodeTypes = ["hbyymmincr", "incremental", "annual", "EAN13"]; + var incremental_barcode = incrementalBarcodeTypes.includes(autobarcodetype); +} + +function set_barcode(id, force, offset=0) { $scr } +function calculateChecksum(ean12) { + let sum = 0; + for (let i = 0; i < ean12.length; i++) { + const digit = parseInt(ean12[i], 10); + sum += (i % 2 === 0) ? digit : digit * 3; + } + const checksum = (10 - (sum % 10)) % 10; + return checksum; +} + +function incrementEAN13(ean13, offset) { + // Increment the first 12 digits and recompute the checksum + let ean12 = String(ean13).slice(0, 12); + let incrementedNumber = (parseInt(ean12, 10) + offset).toString().padStart(12, '0'); + const newChecksum = calculateChecksum(incrementedNumber); + return incrementedNumber + newChecksum; +} + function Focus$function_name(event) { - set_barcode(event.data.id, false); + if (incremental_barcode){ + if (document.getElementById(event.data.id).value == ''){ + attempt += 1 + } + set_barcode(event.data.id, false, attempt); + } + else{ + set_barcode(event.data.id, false); + } return false; } function Click$function_name(event) { - set_barcode(event.data.id, true); + if (incremental_barcode){ + if (document.getElementById(event.data.id).value == ''){ + attempt += 1 + } + set_barcode(event.data.id, false, attempt); + } + else{ + set_barcode(event.data.id, false); + } return false; } -- 2.30.2