Bug 37223

Summary: Deleting the last item in a label batch needs to redirect to ?op=new
Product: Koha Reporter: Phil Ringnalda <phil>
Component: Label/patron card printingAssignee: Chris Nighswonger <cnighswonger>
Status: NEW --- QA Contact: Testopia <testopia>
Severity: major    
Priority: P5 - low    
Version: Main   
Hardware: All   
OS: All   
Change sponsored?: --- Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:

Description Phil Ringnalda 2024-06-28 18:27:10 UTC
When you delete the last item in a label batch, it effectively deletes the batch, but leaves you with an add form which still retains the batch_id. That means that if someone else creates a new batch before you add another item to "your" empty list, their batch will take that same batch_id, and when you add your item you will add it to their list.

Steps to reproduce:
1. Cataloging - Label creator - New label batch
2. Add any item to the batch
3. In the list of items in the batch, click delete
4. Different browser, log in as a user at a different branch
5. Cataloging - Label creator - New label batch
6. Add any item to the batch
7. Back to the original browser, add any item to the "empty" batch
8. You now have a batch with two items in it when you only added one
9. Try to solve your confusion by going to Manage - label batches, maybe
   you added it to the wrong batch?

Wups, no, there are no batches to manage, because now that batch you added an item to is owned by a different branch and you can't manage it to remove your accidental add.
Comment 1 Phil Ringnalda 2024-07-02 18:53:50 UTC
The unobvious thing behind "effectively deletes the batch" is that batches only exist by one or more items having a batch_id in the creator_batches table, where the primary key is label_id.

C4::Labels::Batch->new sets batch_id to 0, and it stays that way until add_item is called, when batch_id is set to one more than SELECT MAX(batch_id) FROM creator_batches;. And deleting a batch consists of calling remove_item until they are all gone.