From 84a46deddf1eaccc33ae864e4f01015673feb580 Mon Sep 17 00:00:00 2001
From: Hammat Wele <hammat.wele@inlibro.com>
Date: Tue, 11 Jun 2024 14:08:45 +0000
Subject: [PATCH] Bug 37070: Incorrect barcode generation when adding orders to
 basket
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When the autoBarcode preference is set to « generated in the form 1, 2, 3 » and the maximum barcode is length 16 (ex 1000000000000000),
when adding orders to the basket,the numbers generated are in the hexadecimal form.

to reproduce:

1- Set the system preference autoBarcode to « generated in the form 1, 2, 3 »
2- Search for a biblio record
3- In the record details, click on New -> New item
4- fill the Barcode field to a number with length 16 (1000000000000000) and add the item
5- Create a suggestion
    5-1- Go to Acquisitions and click on suggestions
    5-2- Create a suggestion and accept it
6- Add a new order to a basket
    6-1- Go to Acquisitions and find a vendor
    6-2- Create a new Basket
    6-3- on the Basket click on 'Add to basket' and select 'From a suggestion'
    6-4- Add the order created on 5-2- to the basket
    6-5- On the item form click on Add item
    6-6- Select a Fund and save the order
7- In the orders table click on the record and check the item Barcode
    ---> the Barcode is in hexadecimal form
8- Cancel order and delete the catalog record
9- Apply the patch
10- Repeat step 6-4, 6-5, 6-6
11- Check the item Barcode
    ---> the Barcode is not in hexadecimal form
12- run prove t/db_dependent/Barcodes.t
---
 C4/Barcodes.pm            |  2 +-
 t/db_dependent/Barcodes.t | 24 +++++++++++++++++++++++-
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/C4/Barcodes.pm b/C4/Barcodes.pm
index 957d60f283..dfca945a33 100644
--- a/C4/Barcodes.pm
+++ b/C4/Barcodes.pm
@@ -95,7 +95,7 @@ sub max {
 }
 sub db_max {
 	my $self = shift;
-	my $query = "SELECT max(abs(barcode)) FROM items LIMIT 1"; # Possible problem if multiple barcode types populated
+	my $query = "SELECT max(abs(CAST(barcode AS UNSIGNED))) FROM items LIMIT 1"; # Possible problem if multiple barcode types populated
 	my $sth = C4::Context->dbh->prepare($query);
 	$sth->execute();
 	return $sth->fetchrow_array || $self->initial;
diff --git a/t/db_dependent/Barcodes.t b/t/db_dependent/Barcodes.t
index 2846f4fe89..b5a976b841 100755
--- a/t/db_dependent/Barcodes.t
+++ b/t/db_dependent/Barcodes.t
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 75;
+use Test::More tests => 76;
 use Test::Warn;
 use Test::MockModule;
 use t::lib::TestBuilder;
@@ -132,6 +132,28 @@ subtest 'Test generation of hbyymmincr barcodes from DB values' => sub {
     $schema->storage->txn_rollback;
 };
 
+subtest 'Test generation of incremental barcodes from DB values' => sub {
+
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+    $builder->schema->resultset('Issue')->delete_all;
+    $builder->schema->resultset('Item')->delete_all;
+    my $barcodeobj;
+
+    my $item_1 = $builder->build_sample_item(
+        {
+            barcode => '1000000000000000'
+        }
+    );
+
+    $barcodeobj = C4::Barcodes->new('incremental');
+
+    is( $barcodeobj->db_max(), '1000000000000000', "(hbyymmincr) First barcode saved to db is equal to db_max" );
+    is( $barcodeobj->value(),  '1000000000000001', 'incremental barcode' );
+
+    $schema->storage->txn_rollback;
+};
 
 $schema->storage->txn_begin;
 
-- 
2.34.1