Bug 30328 - Add ability to generate barcode with library specific prefix
Summary: Add ability to generate barcode with library specific prefix
Status: ASSIGNED
Alias: None
Product: Koha
Classification: Unclassified
Component: Cataloging (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement
Assignee: Emmi Takkinen
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-03-22 09:34 UTC by Emmi Takkinen
Modified: 2023-10-19 07:30 UTC (History)
2 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:


Attachments
Bug 30328: Add ability to generate barcode with branch specific prefix (15.39 KB, patch)
2022-03-25 11:14 UTC, Emmi Takkinen
Details | Diff | Splinter Review
Bug 30328: Add ability to generate barcode with branch specific prefix (15.39 KB, patch)
2022-04-05 10:32 UTC, Emmi Takkinen
Details | Diff | Splinter Review
Bug 30328: Add ability to generate barcode with branch specific prefix (17.68 KB, patch)
2022-04-27 06:09 UTC, Emmi Takkinen
Details | Diff | Splinter Review
Bug 30328: Add ability to generate barcode with branch specific prefix (18.11 KB, patch)
2022-04-28 06:25 UTC, Emmi Takkinen
Details | Diff | Splinter Review
Bug 30328: Add ability to generate barcode with branch specific prefix (18.19 KB, patch)
2022-04-28 09:08 UTC, Emmi Takkinen
Details | Diff | Splinter Review
Bug 30328: Add ability to generate barcode with branch specific prefix (19.05 KB, patch)
2022-08-18 09:47 UTC, Emmi Takkinen
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Emmi Takkinen 2022-03-22 09:34:55 UTC
We should add more flexibility to generating barcodes by letting libraries define branch specific prefix for their barcodes. For this we need to add new barcode option to syspref "autobarcode" and new systempreference where prefixes are set.
Comment 1 Emmi Takkinen 2022-03-25 11:14:34 UTC Comment hidden (obsolete)
Comment 2 Emmi Takkinen 2022-04-05 10:32:23 UTC Comment hidden (obsolete)
Comment 3 Emmi Takkinen 2022-04-27 06:09:39 UTC
Created attachment 134009 [details] [review]
Bug 30328: Add ability to generate barcode with branch specific prefix

We should add more flexibility to generating barcodes by letting libraries
define branch specific prefix for their barcodes. This patch adds new option
"preyyyymmincr" in systempreference "autoBarcode" and new systempreference
"BarcodePrefix" to define branch specific prefix

Test plan for adding item:

1. Apply patch and update database
2. Set syspref "autoBarcode" as
3. Add some values to syspref "Barcodeprefix" e.g.:
	Default: DEF
	CPL: CPL
	FFL: FFL
4. Now change your library to CPL (if needed) and add an item
5. Click barcode input field
   => barcode should be CPL<current_year><current_month>00001
6. Add another item and click barcode field
   => barcode should be CPL<current_year><current_month>00002
7. Change your library to FFL and add an item
   => barcode should now be FFL<current_year><current_month>00001
8. Change your library to e.g. FPL (or another as long as it doesn't have prefix value
in "Barcodeprefix"), add an item and click barcode field
   => barcode should now be DEF<current_year><current_month>00001
9. Try to add multiple items
   => barcodes incremental value should increase in order (no skipping values, no errors)

Test plan for adding multiple aqcuisitions:

1. Set syspref "AcqCreateItem" as "placing order".
2. Create an order and add multiple items to it.
3. Save the order.
4. Navigate to that biblios detail page to which you added items.
   => ordered items barcodes incremental value should increase in order (no skipping
      values, no errors)

Also prove t/db_dependent/Barcodes_ValueBuilder.t and t/Barcodes_preyyyymmincr.t

Sponsored-by: Koha-Suomi Oy
Comment 4 Emmi Takkinen 2022-04-27 06:11:23 UTC
Previous patch didn't have custom methods in C4/Barcodes/preyyyymmincr.pm so adding an order with multiple items produced error 500. Added methods and test plan for this scenario.
Comment 5 Owen Leonard 2022-04-27 10:43:51 UTC
I think the description of the BarcodePrefix preference should say that it is used in connection with the autoBarcode preference, and include a link to it (and vice versa). For example:

"Define library specific barcode prefixes in YAML format. This defines the behavior of the <code><prefix>yyyymm0001</code> option of <a>autoBarcode</a>."

And added to the autoBarcode description, "Define the behavior of the <code><prefix>yyyymm0001</code> option using <a>BarcodePrefix</a>."

Should the BarcodePrefix preference provide an example for the syntax which is expected? Or perhaps a link to the wiki like OpacHiddenItems has?

Otherwise everything was working well when tested in cataloging.
Comment 6 Owen Leonard 2022-04-27 10:49:24 UTC
Also: The BarcodePrefix textarea should be a CodeMirror editor using "text/x-yaml" syntax. See OpacHiddenItems in opac.pref for example.
Comment 7 Emmi Takkinen 2022-04-28 06:25:34 UTC
Created attachment 134202 [details] [review]
Bug 30328: Add ability to generate barcode with branch specific prefix

We should add more flexibility to generating barcodes by letting libraries
define branch specific prefix for their barcodes. This patch adds new option
"preyyyymmincr" in systempreference "autoBarcode" and new systempreference
"BarcodePrefix" to define branch specific prefix

Test plan for adding item:

1. Apply patch and update database
2. Set syspref "autoBarcode" as
3. Add some values to syspref "Barcodeprefix" e.g.:
	Default: DEF
	CPL: CPL
	FFL: FFL
4. Now change your library to CPL (if needed) and add an item
5. Click barcode input field
   => barcode should be CPL<current_year><current_month>00001
6. Add another item and click barcode field
   => barcode should be CPL<current_year><current_month>00002
7. Change your library to FFL and add an item
   => barcode should now be FFL<current_year><current_month>00001
8. Change your library to e.g. FPL (or another as long as it doesn't have prefix value
in "Barcodeprefix"), add an item and click barcode field
   => barcode should now be DEF<current_year><current_month>00001
9. Try to add multiple items
   => barcodes incremental value should increase in order (no skipping values, no errors)

Test plan for adding multiple aqcuisitions:

1. Set syspref "AcqCreateItem" as "placing order".
2. Create an order and add multiple items to it.
3. Save the order.
4. Navigate to that biblios detail page to which you added items.
   => ordered items barcodes incremental value should increase in order (no skipping
      values, no errors)

Also prove t/db_dependent/Barcodes_ValueBuilder.t and t/Barcodes_preyyyymmincr.t

Sponsored-by: Koha-Suomi Oy
Comment 8 Emmi Takkinen 2022-04-28 06:49:33 UTC
(In reply to Owen Leonard from comment #5)
> I think the description of the BarcodePrefix preference should say that it
> is used in connection with the autoBarcode preference, and include a link to
> it (and vice versa). For example:
> 
> "Define library specific barcode prefixes in YAML format. This defines the
> behavior of the <code><prefix>yyyymm0001</code> option of
> <a>autoBarcode</a>."
> 
> And added to the autoBarcode description, "Define the behavior of the
> <code><prefix>yyyymm0001</code> option using <a>BarcodePrefix</a>."
> 
Added links to descriptions and also changed BarcodePrefix as CodeMirror editor.

> Should the BarcodePrefix preference provide an example for the syntax which
> is expected? Or perhaps a link to the wiki like OpacHiddenItems has?
> 

Oh, I didn't notice this one. But I guess it would be more convenient to provide example on the sysprefs description rather than external wiki page. Something like 
"Syntax for barcodes is e.g. <code>DEF2022040001</code>".
Comment 9 Emmi Takkinen 2022-04-28 09:08:08 UTC
Created attachment 134206 [details] [review]
Bug 30328: Add ability to generate barcode with branch specific prefix

We should add more flexibility to generating barcodes by letting libraries
define branch specific prefix for their barcodes. This patch adds new option
"preyyyymmincr" in systempreference "autoBarcode" and new systempreference
"BarcodePrefix" to define branch specific prefix

Test plan for adding item:

1. Apply patch and update database
2. Set syspref "autoBarcode" as
3. Add some values to syspref "Barcodeprefix" e.g.:
	Default: DEF
	CPL: CPL
	FFL: FFL
4. Now change your library to CPL (if needed) and add an item
5. Click barcode input field
   => barcode should be CPL<current_year><current_month>00001
6. Add another item and click barcode field
   => barcode should be CPL<current_year><current_month>00002
7. Change your library to FFL and add an item
   => barcode should now be FFL<current_year><current_month>00001
8. Change your library to e.g. FPL (or another as long as it doesn't have prefix value
in "Barcodeprefix"), add an item and click barcode field
   => barcode should now be DEF<current_year><current_month>00001
9. Try to add multiple items
   => barcodes incremental value should increase in order (no skipping values, no errors)

Test plan for adding multiple aqcuisitions:

1. Set syspref "AcqCreateItem" as "placing order".
2. Create an order and add multiple items to it.
3. Save the order.
4. Navigate to that biblios detail page to which you added items.
   => ordered items barcodes incremental value should increase in order (no skipping
      values, no errors)

Also prove t/db_dependent/Barcodes_ValueBuilder.t and t/Barcodes_preyyyymmincr.t
Comment 10 Owen Leonard 2022-05-05 09:04:34 UTC
I'm getting an error when I try to use the "Add multiple copies" option:

DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::mysql::st execute failed: Data too long for column 'barcode' at row 1 at /kohadevbox/koha/Koha/Object.pm line 170
 at /usr/share/perl5/DBIx/Class/Exception.pm line 77

I'm not sure why this happened--it wasn't because I'd added a prefix which would cause the barcode to be too long. However, it does highlight that it is a possibility: If someone uses a prefix 10 characters long the barcode will always exceed the size of items.barcode.

It also occurs to me... Shouldn't the prefix be determined by the item's home library, not by the user's logged-in library? In our system, for instance, cataloging for all the branches is done at one location.
Comment 11 Emmi Takkinen 2022-05-05 09:21:24 UTC
(In reply to Owen Leonard from comment #10)
> I'm getting an error when I try to use the "Add multiple copies" option:
> 
> DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::mysql::st
> execute failed: Data too long for column 'barcode' at row 1 at
> /kohadevbox/koha/Koha/Object.pm line 170
>  at /usr/share/perl5/DBIx/Class/Exception.pm line 77
> 
> I'm not sure why this happened--it wasn't because I'd added a prefix which
> would cause the barcode to be too long. However, it does highlight that it
> is a possibility: If someone uses a prefix 10 characters long the barcode
> will always exceed the size of items.barcode.
> 
Last changes which fixed ability to add multiple aqcuisitions items, broke the ability to add multiple items. Will fix it asap.

> It also occurs to me... Shouldn't the prefix be determined by the item's
> home library, not by the user's logged-in library? In our system, for
> instance, cataloging for all the branches is done at one location.

This sure doesn't take that possibility into account. I'll take a look at this also.
Comment 12 Emmi Takkinen 2022-08-18 09:47:16 UTC
Created attachment 139350 [details] [review]
Bug 30328: Add ability to generate barcode with branch specific prefix

We should add more flexibility to generating barcodes by letting libraries
define branch specific prefix for their barcodes. This patch adds new option
"preyyyymmincr" in systempreference "autoBarcode" and new systempreference
"BarcodePrefix" to define branch specific prefix

Test plan for adding item:

1. Apply patch and update database
2. Set syspref "autoBarcode" as
3. Add some values to syspref "Barcodeprefix" e.g.:
	Default: DEF
	CPL: CPL
	FFL: FFL
4. Now change your library to CPL (if needed) and add an item
5. Click barcode input field
   => barcode should be CPL<current_year><current_month>00001
6. Add another item and click barcode field
   => barcode should be CPL<current_year><current_month>00002
7. Change your library to FFL and add an item
   => barcode should now be FFL<current_year><current_month>00001
8. Change your library to e.g. FPL (or another as long as it doesn't have prefix value
in "Barcodeprefix"), add an item and click barcode field
   => barcode should now be DEF<current_year><current_month>00001
9. Try to add multiple items
   => barcodes incremental value should increase in order (no skipping values, no errors)

Test plan for adding multiple aqcuisitions:

1. Set syspref "AcqCreateItem" as "placing order".
2. Create an order and add multiple items to it.
3. Save the order.
4. Navigate to that biblios detail page to which you added items.
   => ordered items barcodes incremental value should increase in order (no skipping
      values, no errors)

Also prove t/db_dependent/Barcodes_ValueBuilder.t and t/Barcodes_preyyyymmincr.t

Sponsored-by: Koha-Suomi Oy
Comment 13 Emmi Takkinen 2022-08-18 09:48:35 UTC
Fixed adding multiple items. Still thinking if we really can use items home library to determine barcode prefix.
Comment 14 Nick Clemens (kidclamp) 2022-08-26 17:08:22 UTC
(In reply to Emmi Takkinen from comment #13)
> Fixed adding multiple items. Still thinking if we really can use items home
> library to determine barcode prefix.

There are plugin hooks for barcodes now:
https://github.com/bywatersolutions/dev-koha-plugin-kitchen-sink/commit/0ee1ea69bf01b47e63c1406e4f2a0831ebec906b

And we have a plugin for prefixing barcodes with library prefixes:
https://github.com/bywatersolutions/koha-plugin-barcode-prefixer

Just mentioning as that may work for this case
Comment 15 Katrin Fischer 2023-06-17 18:59:17 UTC
What is the status of this? Emmi, did you decide to use the plugin?
Comment 16 Emmi Takkinen 2023-06-19 07:01:43 UTC
(In reply to Katrin Fischer from comment #15)
> What is the status of this? Emmi, did you decide to use the plugin?

We tested it and unfortunately it doesn't fit our workflow :( 

I haven't been able to work with this for a while but I'm determined to finally catch up with all my assigned bugs which have been left hanging :D
Comment 17 Katrin Fischer 2023-06-22 19:39:50 UTC
(In reply to Emmi Takkinen from comment #16)
> (In reply to Katrin Fischer from comment #15)
> > What is the status of this? Emmi, did you decide to use the plugin?
> 
> We tested it and unfortunately it doesn't fit our workflow :( 
> 
> I haven't been able to work with this for a while but I'm determined to
> finally catch up with all my assigned bugs which have been left hanging :D

Happy to hear that :) Going into summer in this part of the world we are always lacking hands (with the people attached to them).
Comment 18 Emmi Takkinen 2023-10-19 07:30:24 UTC
There are couple problems here. First, if incremental part of barcode is generated by fetching max value from last added item, there is a change that a duplicate barcode is generated. Using timestamp (yyyymmddHHMMSS) would probably be an usable solution here, but we haven't tested this yet. 

Second is getting items home library. This requires creating an AJAX call, which isn't problem itself. Problem is that Kohas way of generating barcodes is... interesting. And complex. I tested this a little while ago and using AJAX call advanced incremental part of barcode by 2 or even more. Which can cause issues in large libraries when limit of monthly increment parts is filled before end of month. But again, using timestamp could be useful here.