Bug 19113 - Barcode value builder not working with numeric branchcode
Summary: Barcode value builder not working with numeric branchcode
Status: Pushed to main
Alias: None
Product: Koha
Classification: Unclassified
Component: Cataloging (show other bugs)
Version: Main
Hardware: All All
: P5 - low minor
Assignee: Blou
QA Contact: Marcel de Rooy
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-08-15 15:03 UTC by Blou
Modified: 2025-02-17 14:06 UTC (History)
10 users (show)

See Also:
Change sponsored?: ---
Patch complexity: Trivial patch
Documentation contact:
Documentation submission:
Text to go in the release notes:
This fixes the generation of item barcodes, where the autoBarcode system preference is set to "generated in the form <branchcode>yymm0001" and the library code is either a number or alphanumeric value. This automatically generated barcode format didn't work in this case, and the number generated would not automatically increment.
Version(s) released in:
25.05.00
Circulation function:


Attachments
Bug 19113 - Barcode value builder not working with numeric branchcode (1.82 KB, patch)
2017-08-15 15:11 UTC, Blou
Details | Diff | Splinter Review
Bug 15685: DBRev 17.06.00.014 (2.90 KB, patch)
2017-10-11 22:48 UTC, PTFS Europe Sandboxes
Details | Diff | Splinter Review
Bug 19113: Barcode value builder not working with numeric branchcode (1.82 KB, patch)
2018-05-23 17:52 UTC, Blou
Details | Diff | Splinter Review
Bug 19113: Barcode value builder not working with numeric branchcode (1.88 KB, patch)
2025-02-08 10:01 UTC, David Nind
Details | Diff | Splinter Review
Bug 19113: Barcode value builder not working with numeric branchcode (2.15 KB, patch)
2025-02-13 15:31 UTC, William Lavoie
Details | Diff | Splinter Review
Bug 19113: Barcode value builder not working with numeric branchcode (2.27 KB, patch)
2025-02-13 15:43 UTC, William Lavoie
Details | Diff | Splinter Review
Bug 19113: Barcode value builder not working with numeric branchcode (2.41 KB, patch)
2025-02-17 08:08 UTC, Marcel de Rooy
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Blou 2017-08-15 15:03:14 UTC
The regexp for the value builder 'hbyymmincr' doesn't allow for numerics in the branchcode, so the nextnum found is always 0 and multiple Add always generate the same barcode.
Comment 1 Blou 2017-08-15 15:11:51 UTC
Created attachment 66019 [details] [review]
Bug 19113 - Barcode value builder not working with numeric branchcode

The regexp for the value builder 'hbyymmincr' doesn't allow for numerics in the branchcode, so the nextnum found is always 0 and multiple Add always generate the same barcode.

This simply allow for numerics within the regex.

Test
- It's a bit complicated.  You need
   - all your branchcodes to be numerics (01,02,71...)
   - all barcodes to already be a bunch of numbers
- Modify the syspref 'autobarcode' to hbyymmincr, the home branch + date one.
- Find an item in the pro-search,
- New -> New Item
- Click on the 'p' to have the value builder create the next barcode.
   - Without the patch, the query won't have found anything, so it'll suggest xxxxxx0001
- Click Add.  It reprensent the form to enter another one.
   - Click on the 'p' edit box, it'll offer you again xxxxxx0001
With the patch, it'll offer valid ones everytime.
Comment 2 PTFS Europe Sandboxes 2017-10-11 22:47:50 UTC
Patch tested with a sandbox, by Jon Knight <J.P.Knight@lboro.ac.uk>
Comment 3 PTFS Europe Sandboxes 2017-10-11 22:48:18 UTC
Created attachment 67981 [details] [review]
Bug 15685: DBRev 17.06.00.014

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Jon Knight <J.P.Knight@lboro.ac.uk>
Comment 4 Marcel de Rooy 2017-10-13 09:14:59 UTC
Please correct the attachments here and add proper signoff line
Comment 5 Blou 2018-05-14 19:06:45 UTC
Comment on attachment 67981 [details] [review]
Bug 15685: DBRev 17.06.00.014

patch 15685 for bug 19113.  Sounds wrong.
Comment 6 Blou 2018-05-23 17:52:38 UTC
Created attachment 75509 [details] [review]
Bug 19113: Barcode value builder not working with numeric branchcode

The regexp for the value builder 'hbyymmincr' doesn't allow for numerics in the branchcode, so the nextnum found is always 0 and multiple Add always generate the same barcode.

This simply allow for numerics within the regex.

Test
- It's a bit complicated.  You need
   - all your branchcodes to be numerics (01,02,71...)
   - all barcodes to already be a bunch of numbers
- Modify the syspref 'autobarcode' to hbyymmincr, the home branch + date one.
- Find an item in the pro-search,
- New -> New Item
- Click on the 'p' to have the value builder create the next barcode.
   - Without the patch, the query won't have found anything, so it'll suggest xxxxxx0001
- Click Add.  It reprensent the form to enter another one.
   - Click on the 'p' edit box, it'll offer you again xxxxxx0001
With the patch, it'll offer valid ones everytime.
Comment 7 Blou 2018-05-23 17:57:00 UTC
Still don't understand why the patch had been replaced by Bug 15685.  So setting back to need sign off.  Not signed off yet.
Comment 8 Mark Tompsett 2018-05-23 21:41:04 UTC
(In reply to Blou from comment #6)
> Test
> - It's a bit complicated.  You need
>    - all your branchcodes to be numerics (01,02,71...)

Who sets their branch codes to purely numerical values?!


>    - all barcodes to already be a bunch of numbers
> - Modify the syspref 'autobarcode' to hbyymmincr, the home branch + date one.

Support for the hbyymm barcode methodology has been deprecated, as far as I know. Why even fix it for this bizarre case?
Comment 9 Mark Tompsett 2018-05-23 21:50:22 UTC
Comment on attachment 75509 [details] [review]
Bug 19113: Barcode value builder not working with numeric branchcode

Review of attachment 75509 [details] [review]:
-----------------------------------------------------------------

::: C4/Barcodes/ValueBuilder.pm
@@ +49,4 @@
>      my $month = $args->{mon};
>      my $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
>      my $sth = C4::Context->dbh->prepare($query);
> +    $sth->execute("^[-a-zA-Z0-9]{1,}$year$month");

If this regexp is wrong, there are several other places it is wrong too. See C4/Barcodes/hbyymmincr.pm db_max for example.

See also line 142:
This format is deprecated and SHOULD NOT BE USED.
Comment 10 Katrin Fischer 2018-05-24 05:57:34 UTC
We have branchcodes that are a mix of capitals and numbers, so I don't think it's an odd case.

The problem with deprecating something just in the source code is that people will still use it - how would they know? The code comments have been there forever, maybe we need to fix it or remove it, but don't leave it like that? Least we could do is add a note to the GUI about it being problematic or deprecated.
Comment 11 Blou 2018-07-20 14:58:56 UTC
I'm biased and in full agreement with Katryn here.

The code does exist and is in use.  And there is certainly no good way to deprecate it.  So why not just fix it.  It's a few adjustments in the code, one patch to push, some or many users happy.

There are many valid reasons to refuse an enhancement, and some good reasons to refuse a fix (ugly, could break other things, badly engineered...).  But I don't think deprecation is valid, unless there's a plan behind it.

What is the suggested PRACTICAL alternative for the user at this point?
Comment 12 Mark Tompsett 2018-07-20 15:19:34 UTC
(In reply to M. Tompsett from comment #9)
> If this regexp is wrong, there are several other places it is wrong too. See
> C4/Barcodes/hbyymmincr.pm db_max for example.

You can't just fix this place then. Fix them all. And include a full test plan. I'll look at it if all places are fixed, and I remember to get back to this.
Comment 13 Fridolin Somers 2024-05-22 11:17:55 UTC
Looks like still valid.

Should also impact C4/Barcodes/hbyymmincr.pm
Comment 14 Sukhmandeep 2024-10-15 21:03:04 UTC
I looked through the code and I believe we can delete the second instance. I removed the db_max method from the hbyymmincr.pm file and tested to see if the correct barcode is generated when the patch is applied, and everything seems to be working fine.

I don't think there is any other barcode that follows the hbyymmXXXX format. If someone could also go through the process on their side (remove the db_max method and check if the full test works), it would allow us to delete the method without any issues.

The test plan is the same as before:
Test
- It's a bit complicated.  You need
   - all your branchcodes to be numerics (01,02,71...)
   - all barcodes to already be a bunch of numbers
- Modify the syspref 'autobarcode' to hbyymmincr, the home branch + date one.
- Find an item in the pro-search,
- New -> New Item
- Click on the 'p' to have the value builder create the next barcode.
   - Without the patch, the query won't have found anything, so it'll suggest xxxxxx0001
- Click Add.  It reprensent the form to enter another one.
   - Click on the 'p' edit box, it'll offer you again xxxxxx0001
With the patch, it'll offer valid ones everytime.
Comment 15 David Nind 2024-12-25 21:10:38 UTC
I had a go at testing, but I notice no difference before and after the patch is applied.

I don't really understand what has actually changed here, or what the issue is that this fixes - since I can't replicate the issue.

Testing notes (using KTD)
=========================

Before the patch
----------------

1. The latest/highest value barcode in the sample data is 39999000019193.
2. Set the autoBarcode system preference to "generated in the form <branchcode>yymm0001".
3. Add a new item to a record and click in the "p - Barcode field:":
   ==> Result: the barcode generated is CPL24120001
4. Add another item to the record and click in the "p - Barcode field":
   ==> Result: the barcode generated is CPL24120002

I can't replicate the problem originally reported:

"The regexp for the value builder 'hbyymmincr' doesn't allow for numerics in the branchcode, so the nextnum found is always 0 and multiple Add always generate the same barcode."

The results are what I expected - the barcode in the new pattern increments by 1 for new items, CPL24120001, CPL24120002, CPL24120003, and so on.

If I delete all the barcodes (1. Access the database: koha-mysql kohadev 2. Remove all the barcodes: update items set barcode=NULL;) and add a manual barcode, such as 35, the barcodes still increment as expected from CPL24120001.

If I use the "Add item", "Add and duplicate", and "Add multiple copies of this item", and also add n item to another record, I can't replicate the issue as described.

After the patch
---------------

I reset everything so that the changes made to the data before the patch are gone.

1. The latest/highest value barcode in the sample data is 39999000019193.
2. Set the autoBarcode system preference to "generated in the form <branchcode>yymm0001".
3. Add a new item to a record and click in the "p - Barcode field:":
   ==> Result: the barcode generated is CPL24120001
4. Add another item to the record and click in the "p - Barcode field":
   ==> Result: the barcode generated is CPL24120002

I noticed no change in behavour or results.
Comment 16 David Nind 2024-12-25 21:11:42 UTC
Note: I tested using a MARC21 instance, is this something related to UNIMARC?
Comment 17 William Lavoie 2025-02-06 20:30:34 UTC
Hi David, 

Have you changed the branchcode for a numerical value? I tried on KTD, using the following steps :
1- koha-mysql kohadev
2- update items set barcode=NULL;
3- change one of the barcode to any numerical value 
4- update branches set branchcode = 1 where branchcode="MPL" 

Note that for the last command you might receive a warning. Then, when creating a new item I put Midway for both the Home and Current library (I chose Midway but any library will do, it just has to be the same as the one with the numerical branchcode) and I was able to reproduce the problem.
Comment 18 David Nind 2025-02-08 10:01:03 UTC
Created attachment 177659 [details] [review]
Bug 19113: Barcode value builder not working with numeric branchcode

The regexp for the value builder 'hbyymmincr' doesn't allow for numerics in the branchcode, so the nextnum found is always 0 and multiple Add always generate the same barcode.

This simply allow for numerics within the regex.

Test
- It's a bit complicated.  You need
   - all your branchcodes to be numerics (01,02,71...)
   - all barcodes to already be a bunch of numbers
- Modify the syspref 'autobarcode' to hbyymmincr, the home branch + date one.
- Find an item in the pro-search,
- New -> New Item
- Click on the 'p' to have the value builder create the next barcode.
   - Without the patch, the query won't have found anything, so it'll suggest xxxxxx0001
- Click Add.  It reprensent the form to enter another one.
   - Click on the 'p' edit box, it'll offer you again xxxxxx0001
With the patch, it'll offer valid ones everytime.

Signed-off-by: David Nind <david@davidnind.com>
Comment 19 David Nind 2025-02-08 10:11:48 UTC
(In reply to William Lavoie from comment #17)
> Hi David, 
> 
> Have you changed the branchcode for a numerical value? I tried on KTD, using
> the following steps :
> 1- koha-mysql kohadev
> 2- update items set barcode=NULL;
> 3- change one of the barcode to any numerical value 
> 4- update branches set branchcode = 1 where branchcode="MPL" 
> 
> Note that for the last command you might receive a warning. Then, when
> creating a new item I put Midway for both the Home and Current library (I
> chose Midway but any library will do, it just has to be the same as the one
> with the numerical branchcode) and I was able to reproduce the problem.

I've retested - it was my error! I didn't read the bug properly, and didn't get that this is about library codes that have numeric or alphanumeric characters.

How I tested:
1. Create a new library with a library code that is a number:
   - Administration > Basic parameters > Libraries > + New library
   - Library code: 001
   - Name: Library 001
   - Submit
2. Create a new library with a library code that is alphanumeric:
   - Administration > Basic parameters > Libraries > + New library
   - Library code: LIB1
   - Name: Library 1
   - Submit
3. Set the autoBarcode system preference to "generated in the form <branchcode>yymm0001".
4. Find a record with one item (any record will do):
   - Edit the current item:
     . Change the home library and current library to Library 001 (library code is 001)
     . Empty the "p - Barcode field:"
     . Click in another field
     . Click back in the "p - Barcode field:"
     . Generates barcode 00125020001
     . Save changes
     ==> Result: successfully saves
   - Add another item to the same record:
     . Change the home and current library to Library 001 (library code is 001)
     . Click back in the "p - Barcode field:"
     . Generates barcode 00125020001 (note that it isn't incremented)
     . Add item
     ==> Result: Error saving item: Barcode must be unique.
5. Repeat step 4 for another record with one item, but use Library 1 (library code is LIB1):
   - Edit the current item:
     . Change the home library and current library to Library 001 (library code is 001)
     . Empty the "p - Barcode field:"
     . Click in another field
     . Click back in the "p - Barcode field:"
     . Generates barcode LIB125020001
     . Save changes
     ==> Result: successfully saves
   - Add another item to the same record:
     . Change the home and current library to Library 001 (library code is 001)
     . Click back in the "p - Barcode field:"
     . Generates barcode LIB125020001 (note that it isn't incremented)
     . Add item
     ==> Result: Error saving item: Barcode must be unique.
6. Repeat for an existing library that has a library code with only alpha characters (Centerville Public Library, CPL):
   ==> Result: works as expected
    . First barcode is: CPL25020001
    . Second barcode is: CPL25020002
7. Apply the patches and RESET everything (reset_all)
   . Note: this clears all the changes made, need to set autoBarcode system preference
   . Alternative is just to restart everything (restart_all), and note that the barcodes generated will be the next in the sequence, not necessarily 0001 and 0002
8. Repeat step 4 (Library 001, library code 001):
   ==> Result: Now works as expected
   . First barcode is: 00125020001
   . Second barcode is: 00125020002
9. Repeat step 5 (Library 1, library code LIB1):
   ==> Result: Now works as expected
   . First barcode is: LIB125020001
   . Second barcode is: LIB125020002
10. Repeat step 6 (Centerville Public Library, CPL):
   ==> Result: No change, continues to work as expected
   . First barcode is: CPL25020001
   . Second barcode is: CPL25020002
Comment 20 William Lavoie 2025-02-13 15:31:35 UTC
Created attachment 178026 [details] [review]
Bug 19113: Barcode value builder not working with numeric branchcode

This needed a rebase.

Test
- It's a bit complicated.  You need
   - all your branchcodes to be numerics (01,02,71...)
   - all barcodes to already be a bunch of numbers
- Modify the syspref 'autobarcode' to hbyymmincr, the home branch + date one.
- Find an item in the pro-search,
- New -> New Item
- Click on the 'p' to have the value builder create the next barcode.
   - Without the patch, the query won't have found anything, so it'll suggest xxxxxx0001
- Click Add.  It reprensent the form to enter another one.
   - Click on the 'p' edit box, it'll offer you again xxxxxx0001
With the patch, it'll offer valid ones everytime.
Comment 21 William Lavoie 2025-02-13 15:43:22 UTC
Created attachment 178028 [details] [review]
Bug 19113: Barcode value builder not working with numeric branchcode

The regexp for the value builder 'hbyymmincr' doesn't allow for numerics in the branchcode,
so the nextnum found is always 0 and multiple Add always generate the same barcode.
This simply allow for numerics within the regex.

Test
- It's a bit complicated.  You need
   - all your branchcodes to be numerics (01,02,71...)
   - all barcodes to already be a bunch of numbers
- Modify the syspref 'autobarcode' to hbyymmincr, the home branch + date one.
- Find an item in the pro-search,
- New -> New Item
- Click on the 'p' to have the value builder create the next barcode.
   - Without the patch, the query won't have found anything, so it'll suggest xxxxxx0001
- Click Add.  It reprensent the form to enter another one.
   - Click on the 'p' edit box, it'll offer you again xxxxxx0001
With the patch, it'll offer valid ones everytime.
Comment 22 Marcel de Rooy 2025-02-17 08:08:56 UTC
Created attachment 178132 [details] [review]
Bug 19113: Barcode value builder not working with numeric branchcode

The regexp for the value builder 'hbyymmincr' doesn't allow for numerics in the branchcode,
so the nextnum found is always 0 and multiple Add always generate the same barcode.
This simply allow for numerics within the regex.

Test
- It's a bit complicated.  You need
   - all your branchcodes to be numerics (01,02,71...)
   - all barcodes to already be a bunch of numbers
- Modify the syspref 'autobarcode' to hbyymmincr, the home branch + date one.
- Find an item in the pro-search,
- New -> New Item
- Click on the 'p' to have the value builder create the next barcode.
   - Without the patch, the query won't have found anything, so it'll suggest xxxxxx0001
- Click Add.  It reprensent the form to enter another one.
   - Click on the 'p' edit box, it'll offer you again xxxxxx0001
With the patch, it'll offer valid ones everytime.

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 23 Marcel de Rooy 2025-02-17 08:09:30 UTC
Added a forgotten signoff line for David too.
Comment 24 Katrin Fischer 2025-02-17 14:06:56 UTC
Pushed for 25.05!

Well done everyone, thank you!