3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 1;
25 use t::lib::TestBuilder;
27 use C4::Biblio qw( AddBiblio );
28 use C4::Circulation qw( AddOfflineOperation ProcessOfflineOperation GetOfflineOperation );
30 use C4::Reserves qw( AddReserve );
31 use Koha::DateUtils qw( dt_from_string );
35 # Mock userenv, used by AddIssue
38 my $context = Test::MockModule->new('C4::Context');
44 number => $manager_id,
51 my $schema = Koha::Database->schema;
52 $schema->storage->txn_begin;
54 my $dbh = C4::Context->dbh;
56 my $builder = t::lib::TestBuilder->new();
58 subtest "Bug 30114 - Koha offline circulation will always cancel the next hold when issuing item to a patron" => sub {
62 $dbh->do("DELETE FROM pending_offline_operations");
64 # Set item-level item types
65 t::lib::Mocks::mock_preference( "item-level_itypes", 1 );
68 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
70 my $borrower1 = $builder->build({
72 value => { branchcode => $branch }
75 my $borrower2 = $builder->build({
77 value => { branchcode => $branch }
80 my $borrower3 = $builder->build({
82 value => { branchcode => $branch }
85 # Look for the defined MARC field for biblio-level itemtype
86 my $rs = $schema->resultset('MarcSubfieldStructure')->search({
88 kohafield => 'biblioitems.itemtype'
90 my $tagfield = $rs->first->tagfield;
91 my $tagsubfield = $rs->first->tagsubfield;
93 # Create a biblio record with biblio-level itemtype
94 my $record = MARC::Record->new();
95 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
96 my $itype = $builder->build({ source => 'Itemtype' });
97 my $item = $builder->build_sample_item(
99 biblionumber => $biblionumber,
101 itype => $itype->{itemtype},
107 branchcode => $branch,
108 borrowernumber => $borrower1->{borrowernumber},
109 biblionumber => $biblionumber,
111 itemnumber => $item->id,
117 branchcode => $branch,
118 borrowernumber => $borrower2->{borrowernumber},
119 biblionumber => $biblionumber,
121 itemnumber => $item->id,
125 my $now = dt_from_string->truncate( to => 'minute' );
126 AddOfflineOperation( $borrower3->{borrowernumber}, $borrower3->{branchcode}, $now, 'issue', $item->barcode, $borrower3->{cardnumber} );
128 my $offline_rs = Koha::Database->new()->schema()->resultset('PendingOfflineOperation')->search();
129 is( $offline_rs->count, 1, "Found one pending offline operation" );
131 is( Koha::Holds->search({ biblionumber => $biblionumber })->count, 2, "Found two holds for the record" );
133 my $op = GetOfflineOperation( $offline_rs->next->id );
135 my $ret = ProcessOfflineOperation( $op );
137 is( Koha::Holds->search({ biblionumber => $biblionumber })->count, 2, "Still found two holds for the record" );
140 $schema->storage->txn_rollback;