Bug 24083: (follow-up) Respond to QA feedback
[koha.git] / t / db_dependent / Circulation / issue.t
blob6c8c9ba30b4fd15d46f0a1d875258de42776757c
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 46;
21 use DateTime::Duration;
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
26 use C4::Biblio;
27 use C4::Circulation;
28 use C4::Context;
29 use C4::Items;
30 use C4::Reserves;
31 use Koha::Checkouts;
32 use Koha::Database;
33 use Koha::DateUtils;
34 use Koha::Holds;
35 use Koha::Items;
36 use Koha::Library;
37 use Koha::Patrons;
38 use Koha::CirculationRules;
39 use Koha::Statistics;
41 BEGIN {
42 require_ok('C4::Circulation');
45 can_ok(
46 'C4::Circulation',
47 qw(AddIssue
48 AddIssuingCharge
49 AddRenewal
50 AddReturn
51 GetBiblioIssues
52 GetIssuingCharges
53 GetOpenIssue
54 GetRenewCount
55 GetUpcomingDueIssues
59 #Start transaction
60 my $schema = Koha::Database->schema;
61 $schema->storage->txn_begin;
62 my $dbh = C4::Context->dbh;
64 my $builder = t::lib::TestBuilder->new();
66 $dbh->do(q|DELETE FROM issues|);
67 $dbh->do(q|DELETE FROM items|);
68 $dbh->do(q|DELETE FROM borrowers|);
69 $dbh->do(q|DELETE FROM categories|);
70 $dbh->do(q|DELETE FROM accountlines|);
71 $dbh->do(q|DELETE FROM circulation_rules|);
72 $dbh->do(q|DELETE FROM reserves|);
73 $dbh->do(q|DELETE FROM old_reserves|);
74 $dbh->do(q|DELETE FROM statistics|);
76 # Generate sample datas
77 my $itemtype = $builder->build(
78 { source => 'Itemtype',
79 value => { notforloan => undef, rentalcharge => 0 }
81 )->{itemtype};
82 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
83 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
84 my $branchcode_3 = $builder->build({ source => 'Branch' })->{branchcode};
85 my $categorycode = $builder->build({
86 source => 'Category',
87 value => { enrolmentfee => undef }
88 })->{categorycode};
90 # A default issuingrule should always be present
91 Koha::CirculationRules->set_rules(
93 itemtype => '*',
94 categorycode => '*',
95 branchcode => '*',
96 rules => {
97 lengthunit => 'days',
98 issuelength => 0,
99 renewalperiod => 0,
100 renewalsallowed => 0
105 # Add Dates
106 my $dt_today = dt_from_string;
107 my $today = output_pref(
108 { dt => $dt_today,
109 dateformat => 'iso',
110 timeformat => '24hr',
111 dateonly => 1
115 my $dt_today2 = dt_from_string;
116 my $dur10 = DateTime::Duration->new( days => -10 );
117 $dt_today2->add_duration($dur10);
118 my $daysago10 = output_pref(
119 { dt => $dt_today2,
120 dateformat => 'iso',
121 timeformat => '24hr',
122 dateonly => 1
126 # Add biblio and item
127 my $record = MARC::Record->new();
128 $record->append_fields(
129 MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
131 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
133 my $barcode_1 = 'barcode_1';
134 my $barcode_2 = 'barcode_2';
135 my $item_id1 = Koha::Item->new(
137 biblionumber => $biblionumber,
138 barcode => $barcode_1,
139 itemcallnumber => 'callnumber1',
140 homebranch => $branchcode_1,
141 holdingbranch => $branchcode_1,
142 itype => $itemtype
144 )->store->itemnumber;
145 my $item_id2 = Koha::Item->new(
147 biblionumber => $biblionumber,
148 barcode => $barcode_2,
149 itemcallnumber => 'callnumber2',
150 homebranch => $branchcode_2,
151 holdingbranch => $branchcode_2,
152 notforloan => 1,
153 itype => $itemtype
155 )->store->itemnumber;
157 #Add borrower
158 my $borrower_id1 = Koha::Patron->new({
159 firstname => 'firstname1',
160 surname => 'surname1 ',
161 categorycode => $categorycode,
162 branchcode => $branchcode_1
163 })->store->borrowernumber;
164 my $patron_1 = Koha::Patrons->find( $borrower_id1 );
165 my $borrower_1 = $patron_1->unblessed;
166 my $borrower_id2 = Koha::Patron->new({
167 firstname => 'firstname2',
168 surname => 'surname2 ',
169 categorycode => $categorycode,
170 branchcode => $branchcode_2,
171 })->store->borrowernumber;
172 my $patron_2 = Koha::Patrons->find( $borrower_id2 );
173 my $borrower_2 = $patron_2->unblessed;
175 t::lib::Mocks::mock_userenv({ patron => $patron_1 });
177 #Begin Tests
179 #Test AddIssue
180 my $query = " SELECT count(*) FROM issues";
181 my $sth = $dbh->prepare($query);
182 $sth->execute;
183 my $countissue = $sth -> fetchrow_array;
184 is ($countissue ,0, "there is no issue");
185 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
186 is( ref $issue1, 'Koha::Checkout',
187 'AddIssue returns a Koha::Checkout object' );
188 my $datedue1 = dt_from_string( $issue1->date_due() );
189 like(
190 $datedue1,
191 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
192 "Koha::Checkout->date_due() returns a date"
194 my $issue_id1 = $issue1->issue_id;
196 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
197 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
199 $sth->execute;
200 $countissue = $sth -> fetchrow_array;
201 is ($countissue,1,"1 issues have been added");
203 #Test AddIssuingCharge
204 $query = " SELECT count(*) FROM accountlines";
205 $sth = $dbh->prepare($query);
206 $sth->execute;
207 my $countaccount = $sth->fetchrow_array;
208 is ($countaccount,0,"0 accountline exists");
209 my $checkout = Koha::Checkouts->find( $issue_id1 );
210 my $charge = C4::Circulation::AddIssuingCharge( $checkout, 10, 'RENT' );
211 is( ref( $charge ), 'Koha::Account::Line', "An issuing charge has been added" );
212 is( $charge->issue_id, $issue_id1, 'Issue id is set correctly for issuing charge' );
213 my $offset = Koha::Account::Offsets->find( { debit_id => $charge->id } );
214 is( $offset->credit_id, undef, 'Offset was created');
215 $sth->execute;
216 $countaccount = $sth->fetchrow_array;
217 is ($countaccount,1,"1 accountline has been added");
219 # Test AddRenewal
221 my $se = Test::MockModule->new( 'C4::Context' );
222 $se->mock( 'interface', sub {return 'intranet'});
224 # Let's renew this one at a different library for statistical purposes to test Bug 17781
225 # Mocking userenv with a different branchcode
226 t::lib::Mocks::mock_userenv({ patron => $patron_2, branchcode => $branchcode_3 });
228 my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 );
230 # Restoring the userenv with the original branchcode
231 t::lib::Mocks::mock_userenv({ patron => $patron_1});
233 like(
234 $datedue3,
235 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
236 "AddRenewal returns a date"
239 my $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $borrower_id1, $item_id1, $branchcode_3 );
240 ok( $stat, "Bug 17781 - 'Improper branchcode set during renewal' still fixed" );
242 subtest 'Show that AddRenewal respects OpacRenewalBranch and interface' => sub {
243 plan tests => 10;
245 my $item_library = $builder->build_object( { class => 'Koha::Libraries' } );
246 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
247 my $logged_in_user = $builder->build_object( { class => 'Koha::Patrons' } );
248 t::lib::Mocks::mock_userenv( { patron => $logged_in_user } );
250 my $OpacRenewalBranch = {
251 opacrenew => "OPACRenew",
252 checkoutbranch => $logged_in_user->branchcode,
253 patronhomebranch => $patron->branchcode,
254 itemhomebranch => $item_library->branchcode,
255 none => "",
258 while ( my ( $syspref, $expected_branchcode ) = each %$OpacRenewalBranch ) {
260 t::lib::Mocks::mock_preference( 'OpacRenewalBranch', $syspref );
263 $se->mock( 'interface', sub { return 'opac' } );
265 my $item = $builder->build_sample_item(
266 { library => $item_library->branchcode, itype => $itemtype } );
267 my $opac_renew_issue =
268 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
270 AddRenewal( $patron->borrowernumber, $item->itemnumber,
271 "Stavromula", $datedue1, $daysago10 );
273 my $stat = Koha::Statistics->search(
274 { itemnumber => $item->itemnumber, type => 'renew' } )->next;
275 is( $stat->branch, $expected_branchcode,
276 "->renewal_branchcode is respected for OpacRenewalBranch = $syspref"
281 $se->mock( 'interface', sub { return 'intranet' } );
283 my $item = $builder->build_sample_item(
284 { library => $item_library->branchcode, itype => $itemtype } );
285 my $opac_renew_issue =
286 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
288 AddRenewal( $patron->borrowernumber, $item->itemnumber,
289 "Stavromula", $datedue1, $daysago10 );
291 my $stat = Koha::Statistics->search(
292 { itemnumber => $item->itemnumber, type => 'renew' } )->next;
293 is( $stat->branch, $logged_in_user->branchcode,
294 "->renewal_branchcode is always logged in branch for intranet"
300 # Unseen rewnewals
301 t::lib::Mocks::mock_preference('UnseenRenewals', 1);
303 # Does an unseen renewal increment the issue's count
304 my ( $unseen_before ) = ( C4::Circulation::GetRenewCount( $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber} ) )[3];
305 AddRenewal( $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber}, $branchcode_1, undef, undef, 0 );
306 my ( $unseen_after ) = ( C4::Circulation::GetRenewCount( $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber} ) )[3];
307 is( $unseen_after, $unseen_before + 1, 'unseen_renewals increments' );
309 # Does a seen renewal reset the unseen count
310 AddRenewal( $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber}, $branchcode_1, undef, undef, 1 );
311 my ( $unseen_reset ) = ( C4::Circulation::GetRenewCount( $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber} ) )[3];
312 is( $unseen_reset, 0, 'seen renewal resets the unseen count' );
314 #Test GetBiblioIssues
315 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
317 #Test GetOpenIssue
318 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
319 is( GetOpenIssue(-1), undef,
320 "With wrong parameter GetOpenIssue returns undef" );
321 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
323 my @renewcount;
324 #Test GetRenewCount
325 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
326 #Without anything in DB
327 @renewcount = C4::Circulation::GetRenewCount();
328 is_deeply(
329 \@renewcount,
330 [ 0, 0, 0 ], # FIXME Need to be fixed, see FIXME in GetRenewCount
331 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
333 @renewcount = C4::Circulation::GetRenewCount(-1);
334 is_deeply(
335 \@renewcount,
336 [ 0, 0, 0 ], # FIXME Need to be fixed
337 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
339 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
340 is_deeply(
341 \@renewcount,
342 [ 2, 0, 0 ],
343 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
346 #With something in DB
347 @renewcount = C4::Circulation::GetRenewCount();
348 is_deeply(
349 \@renewcount,
350 [ 0, 0, 0 ],
351 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
353 @renewcount = C4::Circulation::GetRenewCount(-1);
354 is_deeply(
355 \@renewcount,
356 [ 0, 0, 0 ],
357 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
359 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
360 is_deeply(
361 \@renewcount,
362 [ 2, 0, 0 ],
363 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
366 # Add a default rule: renewal is allowed
367 Koha::CirculationRules->set_rules(
369 categorycode => undef,
370 itemtype => undef,
371 branchcode => undef,
372 rules => {
373 renewalsallowed => 3,
377 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
378 is_deeply(
379 \@renewcount,
380 [ 2, 3, 1 ],
381 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
384 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
385 $datedue3, $daysago10 );
386 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
387 is_deeply(
388 \@renewcount,
389 [ 3, 3, 0 ],
390 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
393 $dbh->do("DELETE FROM old_issues");
394 AddReturn($barcode_1);
395 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
396 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
398 $dbh->do("DELETE FROM old_issues");
399 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
400 AddReturn($barcode_1, undef, undef, dt_from_string('2014-04-01 23:42'));
401 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
402 ok( $return->{returndate} eq '2014-04-01 23:42:00', "Item returned with a return date of '2014-04-01 23:42' has that return date" );
404 my $itemnumber = Koha::Item->new(
406 biblionumber => $biblionumber,
407 barcode => 'barcode_3',
408 itemcallnumber => 'callnumber3',
409 homebranch => $branchcode_1,
410 holdingbranch => $branchcode_1,
411 notforloan => 1,
412 itype => $itemtype
414 )->store->itemnumber;
416 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
417 AddReturn( 'barcode_3', $branchcode_1 );
418 my $item = Koha::Items->find( $itemnumber );
419 ok( $item->notforloan eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
421 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
422 AddReturn( 'barcode_3', $branchcode_1 );
423 $item = Koha::Items->find( $itemnumber );
424 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
426 AddReturn( 'barcode_3', $branchcode_1 );
427 $item = Koha::Items->find( $itemnumber );
428 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
430 my $itemnumber2 = Koha::Item->new(
432 biblionumber => $biblionumber,
433 barcode => 'barcode_4',
434 itemcallnumber => 'callnumber4',
435 homebranch => $branchcode_1,
436 holdingbranch => $branchcode_1,
437 location => 'FIC',
438 itype => $itemtype
440 )->store->itemnumber;
442 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', q{} );
443 AddReturn( 'barcode_4', $branchcode_1 );
444 my $item2 = Koha::Items->find( $itemnumber2 );
445 ok( $item2->location eq 'FIC', 'UpdateItemLocationOnCheckin does not modify value when not enabled' );
447 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', 'FIC: GEN' );
448 AddReturn( 'barcode_4', $branchcode_1 );
449 $item2 = Koha::Items->find( $itemnumber2 );
450 is( $item2->location, 'GEN', q{UpdateItemLocationOnCheckin updates location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
451 is( $item2->permanent_location, 'GEN', q{UpdateItemLocationOnCheckin updates permanent_location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
452 AddReturn( 'barcode_4', $branchcode_1 );
453 $item2 = Koha::Items->find( $itemnumber2 );
454 ok( $item2->location eq 'GEN', q{UpdateItemLocationOnCheckin does not update location value from 'GEN' with setting "FIC: GEN"} );
456 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', '_ALL_: CART' );
457 AddReturn( 'barcode_4', $branchcode_1 );
458 $item2 = Koha::Items->find( $itemnumber2 );
459 ok( $item2->location eq 'CART', q{UpdateItemLocationOnCheckin updates location value from 'GEN' with setting "_ALL_: CART"} );
460 Koha::Item::Transfer->new({
461 itemnumber => $itemnumber2,
462 frombranch => $branchcode_2,
463 tobranch => $branchcode_1,
464 datesent => '2020-01-01'
465 })->store;
466 AddReturn( 'barcode_4', $branchcode_1 );
467 $item2 = Koha::Items->find( $itemnumber2 );
468 ok( $item2->location eq 'CART', q{UpdateItemLocationOnCheckin updates location value from 'GEN' with setting "_ALL_: CART" when transfer filled} );
470 ok( $item2->permanent_location eq 'GEN', q{UpdateItemLocationOnCheckin does not update permanent_location value from 'GEN' with setting "_ALL_: CART"} );
471 AddIssue( $borrower_1, 'barcode_4', $daysago10,0, $today, '' );
472 $item2 = Koha::Items->find( $itemnumber2 );
473 ok( $item2->location eq 'GEN', q{Location updates from 'CART' to permanent location on issue} );
475 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', "GEN: _BLANK_\n_BLANK_: PROC\nPROC: _PERM_" );
476 AddReturn( 'barcode_4', $branchcode_1 );
477 $item2 = Koha::Items->find( $itemnumber2 );
478 ok( $item2->location eq '', q{UpdateItemLocationOnCheckin updates location value from 'GEN' to '' with setting "GEN: _BLANK_"} );
479 AddReturn( 'barcode_4', $branchcode_1 );
480 $item2 = Koha::Items->find( $itemnumber2 );
481 ok( $item2->location eq 'PROC' , q{UpdateItemLocationOnCheckin updates location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
482 ok( $item2->permanent_location eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
483 AddReturn( 'barcode_4', $branchcode_1 );
484 $item2 = Koha::Items->find( $itemnumber2 );
485 ok( $item2->location eq '' , q{UpdateItemLocationOnCheckin updates location value from 'PROC' to '' with setting "PROC: _PERM_" } );
486 ok( $item2->permanent_location eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location from '' with setting "PROC: _PERM_" } );
491 # Bug 14640 - Cancel the hold on checking out if asked
492 my $reserve_id = AddReserve(
494 branchcode => $branchcode_1,
495 borrowernumber => $borrower_id1,
496 biblionumber => $biblionumber,
497 priority => 1,
498 notes => "a note",
499 title => "a title",
502 ok( $reserve_id, 'The reserve should have been inserted' );
503 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
504 my $hold = Koha::Holds->find( $reserve_id );
505 is( $hold, undef, 'The reserve should have been correctly cancelled' );
507 #End transaction
508 $schema->storage->txn_rollback;