When adding a new label batch the MAX batch_id is retrieved from creator_batches and incremented, however, the updated value is not saved until new items are added to the batch. This means that if one library opens a new batch but does not add items before another library opens the batch they will both be assigned the same batch number To recreate: 1 -In one browser window, go to tools->label creator and click the new batch button 2 - Before adding items, open a new browser, and go tools->label creator and click the new batch button 3 - Note that both batches have the same number listed 4 - Add an item to the first batch - you should now see one item in the batch 5 - Add an item to the second batch, you should see two items in the batch I can also verify this happens pretty often in our shared system. C4:Creators 44 sub new { 45 my ($invocant) = shift; 46 my $type = ref($invocant) || $invocant; 47 my $self = { 48 batch_id => 0, 49 items => [], 50 branch_code => 'NB', 51 batch_stat => 0, # False if any data has changed and the db has not been updated 52 @_, 53 }; 54 my $sth = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM creator_batches;"); 55 $sth->execute(); 56 my $batch_id = $sth->fetchrow_array; 57 $self->{'batch_id'} = ++$batch_id unless $self->{'batch_id'} != 0; # this allows batch_id to be passed in for individual label printing 58 bless ($self, $type); 59 return $self; 60 } 61 62 sub add_item { 63 my $self = shift; 64 my $number = shift; 65 ref($self) =~ m/C4::(.+)::.+$/; 66 my $number_type = ($1 eq 'Patroncards' ? 'borrower_number' : 'item_number'); 67 my $query = "INSERT INTO creator_batches (batch_id, $number_type, branch_code, creator) VALUES (?,?,?,?);"; 68 my $sth = C4::Context->dbh->prepare($query); 69 # $sth->{'TraceLevel'} = 3; 70 $sth->execute($self->{'batch_id'}, $number, $self->{'branch_code'}, $1); 71 if ($sth->err) { 72 warn sprintf('Database returned the following error on attempted INSERT: %s', $sth->errstr); 73 return -1; 74 } 75 $query = "SELECT max(label_id) FROM creator_batches WHERE batch_id=? AND $number_type=? AND branch_code=?;"; 76 my $sth1 = C4::Context->dbh->prepare($query); 77 $sth1->execute($self->{'batch_id'}, $number, $self->{'branch_code'}); 78 my $label_id = $sth1->fetchrow_array; 79 push (@{$self->{'items'}}, {$number_type => $number, label_id => $label_id}); 80 $self->{'batch_stat'} = 1; 81 return 0; 82 }
*** Bug 8042 has been marked as a duplicate of this bug. ***
Created attachment 35600 [details] [review] Bug 12911 - batch_id for new label batches can be assigned to several batches -Added code to make a placeholder and thus reserve the batch number. -Added code to delete old placeholders everytime a new batch is made. -Modified code in SELECT query to disc Testing Plan -Open two koha staff windows in different browsers/inprivate. -On both navigate Home>Tools>Label Creator -Make a new batch in the first window -Make a new batch in the second window *The new batches created should have different batch numbers -Add items to one of the batches *Only the items added should show in the summary below. No placeholders should be shown -Go back to manage batches page on either window *The batch created which did not have any items added to it should not be displayed in this window
I tried testing and got odd results with one number displaying as batch id, but the results saving into the DB with a higher batch id, I suspect there is some double increment due to the placeholder records. I am wondering if instead of adding a placeholder at object creation the batch id could be assigned after the first items are added? Maybe assign batch ID -1, don't display it in the add screen, and then in the add items function check for batch id -1 and create the new items then?
Created attachment 35719 [details] [review] Bug 12911 - batch_id for new labels batches can be asssigned to several batches Currently batch_id is assigned upon creation of a new batch object. This patch leaves batch_id as 0 at creation and adds a check when adding items. If batch is new then batch_id is created then Test plan: 1 -In one browser window, go to tools->label creator and click the new batch button 2 - Before adding items, open a new browser, and go tools->label creator and click the new batch button 3 - Note that both batches have the same number listed 4 - Add an item to the first batch - you should now see one item in the batch 5 - Add an item to the second batch, you should see two items in the batch 6 - Apply patch and repeat steps 1&2 7 - Note that neither batch lists a batch number 8 - Add an item to the first batch, you should see one item and a batch number 9 - Add an item to the second batch, you should see one item and a new batch number
Created attachment 35807 [details] [review] [Signed-off] Bug 12911 - batch_id for new labels batches can be asssigned to several batches Currently batch_id is assigned upon creation of a new batch object. This patch leaves batch_id as 0 at creation and adds a check when adding items. If batch is new then batch_id is created then Test plan: 1 -In one browser window, go to tools->label creator and click the new batch button 2 - Before adding items, open a new browser, and go tools->label creator and click the new batch button 3 - Note that both batches have the same number listed 4 - Add an item to the first batch - you should now see one item in the batch 5 - Add an item to the second batch, you should see two items in the batch 6 - Apply patch and repeat steps 1&2 7 - Note that neither batch lists a batch number 8 - Add an item to the first batch, you should see one item and a batch number 9 - Add an item to the second batch, you should see one item and a new batch number Patch behaves as expected. Signed-off-by: Marc Véron <veron@veron.ch>
Created attachment 35898 [details] [review] [PASSED QA] Bug 12911 - batch_id for new labels batches can be asssigned to several batches Currently batch_id is assigned upon creation of a new batch object. This patch leaves batch_id as 0 at creation and adds a check when adding items. If batch is new then batch_id is created then Test plan: 1 -In one browser window, go to tools->label creator and click the new batch button 2 - Before adding items, open a new browser, and go tools->label creator and click the new batch button 3 - Note that both batches have the same number listed 4 - Add an item to the first batch - you should now see one item in the batch 5 - Add an item to the second batch, you should see two items in the batch 6 - Apply patch and repeat steps 1&2 7 - Note that neither batch lists a batch number 8 - Add an item to the first batch, you should see one item and a batch number 9 - Add an item to the second batch, you should see one item and a new batch number Patch behaves as expected. Signed-off-by: Marc Veron <veron@veron.ch> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Patch pushed to master. Thanks Nick!
Pushed to 3.18.x will be in 3.18.7
> Test plan: > 1 -In one browser window, go to tools->label creator and click the new batch > button > 2 - Before adding items, open a new browser, and go tools->label creator and > click the new batch button > 3 - Note that both batches have the same number listed > 4 - Add an item to the first batch - you should now see one item in the batch > 5 - Add an item to the second batch, you should see two items in the batch > 6 - Apply patch and repeat steps 1&2 > 7 - Note that neither batch lists a batch number > 8 - Add an item to the first batch, you should see one item and a batch > number > 9 - Add an item to the second batch, you should see one item and a new batch > number But does not work if you add a second item! See bug 14739 and please provide a quick fix. This bug is much more annoying than the one fixed by this patch.