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 => 2;
25 use t::lib::TestBuilder;
27 use C4::Biblio qw( AddBiblio );
28 use C4::Circulation qw( AddOfflineOperation ProcessOfflineOperation GetOfflineOperation ProcessOfflineIssue );
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 34529: Offline circulation should be able to accept userid as well as cardnumber" => sub {
62 $dbh->do("DELETE FROM pending_offline_operations");
64 $branch = $builder->build( { source => 'Branch' } )->{branchcode};
66 my $borrower1 = $builder->build(
69 value => { branchcode => $branch }
73 my $biblio = t::lib::TestBuilder->new->build_sample_biblio;
74 my $item1 = $builder->build_sample_item(
76 biblionumber => $biblio->id,
80 my $item2 = $builder->build_sample_item(
82 biblionumber => $biblio->id,
90 cardnumber => $borrower1->{cardnumber},
91 barcode => $item1->barcode
95 "ProcessOfflineIssue succeeds with cardnumber"
98 ProcessOfflineIssue( { cardnumber => $borrower1->{userid}, barcode => $item2->barcode } ),
100 "ProcessOfflineIssue succeeds with user id"
105 subtest "Bug 30114 - Koha offline circulation will always cancel the next hold when issuing item to a patron" => sub {
109 $dbh->do("DELETE FROM pending_offline_operations");
111 # Set item-level item types
112 t::lib::Mocks::mock_preference( "item-level_itypes", 1 );
115 $branch = $builder->build( { source => 'Branch' } )->{branchcode};
118 my $borrower1 = $builder->build(
120 source => 'Borrower',
121 value => { branchcode => $branch }
125 my $borrower2 = $builder->build(
127 source => 'Borrower',
128 value => { branchcode => $branch }
132 my $borrower3 = $builder->build(
134 source => 'Borrower',
135 value => { branchcode => $branch }
139 # Look for the defined MARC field for biblio-level itemtype
140 my $rs = $schema->resultset('MarcSubfieldStructure')->search(
143 kohafield => 'biblioitems.itemtype'
146 my $tagfield = $rs->first->tagfield;
147 my $tagsubfield = $rs->first->tagsubfield;
149 # Create a biblio record with biblio-level itemtype
150 my $record = MARC::Record->new();
151 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
152 my $itype = $builder->build( { source => 'Itemtype' } );
153 my $item = $builder->build_sample_item(
155 biblionumber => $biblionumber,
157 itype => $itype->{itemtype},
163 branchcode => $branch,
164 borrowernumber => $borrower1->{borrowernumber},
165 biblionumber => $biblionumber,
167 itemnumber => $item->id,
173 branchcode => $branch,
174 borrowernumber => $borrower2->{borrowernumber},
175 biblionumber => $biblionumber,
177 itemnumber => $item->id,
181 my $now = dt_from_string->truncate( to => 'minute' );
183 $borrower3->{borrowernumber}, $borrower3->{branchcode}, $now, 'issue', $item->barcode,
184 $borrower3->{cardnumber}
187 my $offline_rs = Koha::Database->new()->schema()->resultset('PendingOfflineOperation')->search();
188 is( $offline_rs->count, 1, "Found one pending offline operation" );
190 is( Koha::Holds->search( { biblionumber => $biblionumber } )->count, 2, "Found two holds for the record" );
192 my $op = GetOfflineOperation( $offline_rs->next->id );
194 my $ret = ProcessOfflineOperation($op);
196 is( Koha::Holds->search( { biblionumber => $biblionumber } )->count, 2, "Still found two holds for the record" );
199 $schema->storage->txn_rollback;