Bug 23177: (QA follow-up) Remove subtest txn_begin and rollback
[koha-equinox.git] / t / db_dependent / Circulation.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19 use utf8;
20
21 use Test::More tests => 130;
22 use Test::MockModule;
23
24 use Data::Dumper;
25 use DateTime;
26 use Time::Fake;
27 use POSIX qw( floor );
28 use t::lib::Mocks;
29 use t::lib::TestBuilder;
30
31 use C4::Accounts;
32 use C4::Calendar;
33 use C4::Circulation;
34 use C4::Biblio;
35 use C4::Items;
36 use C4::Log;
37 use C4::Reserves;
38 use C4::Overdues qw(UpdateFine CalcFine);
39 use Koha::DateUtils;
40 use Koha::Database;
41 use Koha::IssuingRules;
42 use Koha::Items;
43 use Koha::Checkouts;
44 use Koha::Patrons;
45 use Koha::CirculationRules;
46 use Koha::Subscriptions;
47 use Koha::Account::Lines;
48 use Koha::Account::Offsets;
49 use Koha::ActionLogs;
50
51 sub set_userenv {
52     my ( $library ) = @_;
53     t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
54 }
55
56 sub str {
57     my ( $error, $question, $alert ) = @_;
58     my $s;
59     $s  = %$error    ? ' (error: '    . join( ' ', keys %$error    ) . ')' : '';
60     $s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
61     $s .= %$alert    ? ' (alert: '    . join( ' ', keys %$alert    ) . ')' : '';
62     return $s;
63 }
64
65 sub test_debarment_on_checkout {
66     my ($params) = @_;
67     my $item     = $params->{item};
68     my $library  = $params->{library};
69     my $patron   = $params->{patron};
70     my $due_date = $params->{due_date} || dt_from_string;
71     my $return_date = $params->{return_date} || dt_from_string;
72     my $expected_expiration_date = $params->{expiration_date};
73
74     $expected_expiration_date = output_pref(
75         {
76             dt         => $expected_expiration_date,
77             dateformat => 'sql',
78             dateonly   => 1,
79         }
80     );
81     my @caller      = caller;
82     my $line_number = $caller[2];
83     AddIssue( $patron, $item->{barcode}, $due_date );
84
85     my ( undef, $message ) = AddReturn( $item->{barcode}, $library->{branchcode}, undef, $return_date );
86     is( $message->{WasReturned} && exists $message->{Debarred}, 1, 'AddReturn must have debarred the patron' )
87         or diag('AddReturn returned message ' . Dumper $message );
88     my $debarments = Koha::Patron::Debarments::GetDebarments(
89         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
90     is( scalar(@$debarments), 1, 'Test at line ' . $line_number );
91
92     is( $debarments->[0]->{expiration},
93         $expected_expiration_date, 'Test at line ' . $line_number );
94     Koha::Patron::Debarments::DelUniqueDebarment(
95         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
96 };
97
98 my $schema = Koha::Database->schema;
99 $schema->storage->txn_begin;
100 my $builder = t::lib::TestBuilder->new;
101 my $dbh = C4::Context->dbh;
102
103 # Prevent random failures by mocking ->now
104 my $now_value       = DateTime->now();
105 my $mocked_datetime = Test::MockModule->new('DateTime');
106 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
107
108 # Start transaction
109 $dbh->{RaiseError} = 1;
110
111 my $cache = Koha::Caches->get_instance();
112 $dbh->do(q|DELETE FROM special_holidays|);
113 $dbh->do(q|DELETE FROM repeatable_holidays|);
114 $cache->clear_from_cache('single_holidays');
115
116 # Start with a clean slate
117 $dbh->do('DELETE FROM issues');
118 $dbh->do('DELETE FROM borrowers');
119
120 my $library = $builder->build({
121     source => 'Branch',
122 });
123 my $library2 = $builder->build({
124     source => 'Branch',
125 });
126 my $itemtype = $builder->build(
127     {
128         source => 'Itemtype',
129         value  => {
130             notforloan          => undef,
131             rentalcharge        => 0,
132             rentalcharge_daily => 0,
133             defaultreplacecost  => undef,
134             processfee          => undef
135         }
136     }
137 )->{itemtype};
138 my $patron_category = $builder->build(
139     {
140         source => 'Category',
141         value  => {
142             category_type                 => 'P',
143             enrolmentfee                  => 0,
144             BlockExpiredPatronOpacActions => -1, # Pick the pref value
145         }
146     }
147 );
148
149 my $CircControl = C4::Context->preference('CircControl');
150 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
151
152 my $item = {
153     homebranch => $library2->{branchcode},
154     holdingbranch => $library2->{branchcode}
155 };
156
157 my $borrower = {
158     branchcode => $library2->{branchcode}
159 };
160
161 t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
162
163 # No userenv, PickupLibrary
164 t::lib::Mocks::mock_preference('IndependentBranches', '0');
165 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
166 is(
167     C4::Context->preference('CircControl'),
168     'PickupLibrary',
169     'CircControl changed to PickupLibrary'
170 );
171 is(
172     C4::Circulation::_GetCircControlBranch($item, $borrower),
173     $item->{$HomeOrHoldingBranch},
174     '_GetCircControlBranch returned item branch (no userenv defined)'
175 );
176
177 # No userenv, PatronLibrary
178 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
179 is(
180     C4::Context->preference('CircControl'),
181     'PatronLibrary',
182     'CircControl changed to PatronLibrary'
183 );
184 is(
185     C4::Circulation::_GetCircControlBranch($item, $borrower),
186     $borrower->{branchcode},
187     '_GetCircControlBranch returned borrower branch'
188 );
189
190 # No userenv, ItemHomeLibrary
191 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
192 is(
193     C4::Context->preference('CircControl'),
194     'ItemHomeLibrary',
195     'CircControl changed to ItemHomeLibrary'
196 );
197 is(
198     $item->{$HomeOrHoldingBranch},
199     C4::Circulation::_GetCircControlBranch($item, $borrower),
200     '_GetCircControlBranch returned item branch'
201 );
202
203 # Now, set a userenv
204 t::lib::Mocks::mock_userenv({ branchcode => $library2->{branchcode} });
205 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
206
207 # Userenv set, PickupLibrary
208 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
209 is(
210     C4::Context->preference('CircControl'),
211     'PickupLibrary',
212     'CircControl changed to PickupLibrary'
213 );
214 is(
215     C4::Circulation::_GetCircControlBranch($item, $borrower),
216     $library2->{branchcode},
217     '_GetCircControlBranch returned current branch'
218 );
219
220 # Userenv set, PatronLibrary
221 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
222 is(
223     C4::Context->preference('CircControl'),
224     'PatronLibrary',
225     'CircControl changed to PatronLibrary'
226 );
227 is(
228     C4::Circulation::_GetCircControlBranch($item, $borrower),
229     $borrower->{branchcode},
230     '_GetCircControlBranch returned borrower branch'
231 );
232
233 # Userenv set, ItemHomeLibrary
234 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
235 is(
236     C4::Context->preference('CircControl'),
237     'ItemHomeLibrary',
238     'CircControl changed to ItemHomeLibrary'
239 );
240 is(
241     C4::Circulation::_GetCircControlBranch($item, $borrower),
242     $item->{$HomeOrHoldingBranch},
243     '_GetCircControlBranch returned item branch'
244 );
245
246 # Reset initial configuration
247 t::lib::Mocks::mock_preference('CircControl', $CircControl);
248 is(
249     C4::Context->preference('CircControl'),
250     $CircControl,
251     'CircControl reset to its initial value'
252 );
253
254 # Set a simple circ policy
255 $dbh->do('DELETE FROM issuingrules');
256 Koha::CirculationRules->search()->delete();
257 $dbh->do(
258     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
259                                 issuelength, lengthunit,
260                                 renewalsallowed, renewalperiod,
261                                 norenewalbefore, auto_renew,
262                                 fine, chargeperiod)
263       VALUES (?, ?, ?, ?,
264               ?, ?,
265               ?, ?,
266               ?, ?,
267               ?, ?
268              )
269     },
270     {},
271     '*', '*', '*', 25,
272     14, 'days',
273     1, 7,
274     undef, 0,
275     .10, 1
276 );
277
278 my ( $reused_itemnumber_1, $reused_itemnumber_2 );
279 {
280 # CanBookBeRenewed tests
281     C4::Context->set_preference('ItemsDeniedRenewal','');
282     # Generate test biblio
283     my $biblio = $builder->build_sample_biblio();
284
285     my $branch = $library2->{branchcode};
286
287     my $item_1 = $builder->build_sample_item(
288         {
289             biblionumber     => $biblio->biblionumber,
290             library          => $branch,
291             replacementprice => 12.00,
292             itype            => $itemtype
293         }
294     );
295     $reused_itemnumber_1 = $item_1->itemnumber;
296
297     my $item_2 = $builder->build_sample_item(
298         {
299             biblionumber     => $biblio->biblionumber,
300             library          => $branch,
301             replacementprice => 23.00,
302             itype            => $itemtype
303         }
304     );
305     $reused_itemnumber_2 = $item_2->itemnumber;
306
307     my $item_3 = $builder->build_sample_item(
308         {
309             biblionumber     => $biblio->biblionumber,
310             library          => $branch,
311             replacementprice => 23.00,
312             itype            => $itemtype
313         }
314     );
315
316     # Create borrowers
317     my %renewing_borrower_data = (
318         firstname =>  'John',
319         surname => 'Renewal',
320         categorycode => $patron_category->{categorycode},
321         branchcode => $branch,
322     );
323
324     my %reserving_borrower_data = (
325         firstname =>  'Katrin',
326         surname => 'Reservation',
327         categorycode => $patron_category->{categorycode},
328         branchcode => $branch,
329     );
330
331     my %hold_waiting_borrower_data = (
332         firstname =>  'Kyle',
333         surname => 'Reservation',
334         categorycode => $patron_category->{categorycode},
335         branchcode => $branch,
336     );
337
338     my %restricted_borrower_data = (
339         firstname =>  'Alice',
340         surname => 'Reservation',
341         categorycode => $patron_category->{categorycode},
342         debarred => '3228-01-01',
343         branchcode => $branch,
344     );
345
346     my %expired_borrower_data = (
347         firstname =>  'Ça',
348         surname => 'Glisse',
349         categorycode => $patron_category->{categorycode},
350         branchcode => $branch,
351         dateexpiry => dt_from_string->subtract( months => 1 ),
352     );
353
354     my $renewing_borrowernumber = Koha::Patron->new(\%renewing_borrower_data)->store->borrowernumber;
355     my $reserving_borrowernumber = Koha::Patron->new(\%reserving_borrower_data)->store->borrowernumber;
356     my $hold_waiting_borrowernumber = Koha::Patron->new(\%hold_waiting_borrower_data)->store->borrowernumber;
357     my $restricted_borrowernumber = Koha::Patron->new(\%restricted_borrower_data)->store->borrowernumber;
358     my $expired_borrowernumber = Koha::Patron->new(\%expired_borrower_data)->store->borrowernumber;
359
360     my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
361     my $restricted_borrower = Koha::Patrons->find( $restricted_borrowernumber )->unblessed;
362     my $expired_borrower = Koha::Patrons->find( $expired_borrowernumber )->unblessed;
363
364     my $bibitems       = '';
365     my $priority       = '1';
366     my $resdate        = undef;
367     my $expdate        = undef;
368     my $notes          = '';
369     my $checkitem      = undef;
370     my $found          = undef;
371
372     my $issue = AddIssue( $renewing_borrower, $item_1->barcode);
373     my $datedue = dt_from_string( $issue->date_due() );
374     is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
375
376     my $issue2 = AddIssue( $renewing_borrower, $item_2->barcode);
377     $datedue = dt_from_string( $issue->date_due() );
378     is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
379
380
381     my $borrowing_borrowernumber = Koha::Checkouts->find( { itemnumber => $item_1->itemnumber } )->borrowernumber;
382     is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
383
384     my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
385     is( $renewokay, 1, 'Can renew, no holds for this title or item');
386
387
388     # Biblio-level hold, renewal test
389     AddReserve(
390         $branch, $reserving_borrowernumber, $biblio->biblionumber,
391         $bibitems,  $priority, $resdate, $expdate, $notes,
392         'a title', $checkitem, $found
393     );
394
395     # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
396     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
397     t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
398     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
399     is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
400     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
401     is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
402
403     # Now let's add an item level hold, we should no longer be able to renew the item
404     my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
405         {
406             borrowernumber => $hold_waiting_borrowernumber,
407             biblionumber   => $biblio->biblionumber,
408             itemnumber     => $item_1->itemnumber,
409             branchcode     => $branch,
410             priority       => 3,
411         }
412     );
413     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
414     is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
415     $hold->delete();
416
417     # Now let's add a waiting hold on the 3rd item, it's no longer available tp check out by just anyone, so we should no longer
418     # be able to renew these items
419     $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
420         {
421             borrowernumber => $hold_waiting_borrowernumber,
422             biblionumber   => $biblio->biblionumber,
423             itemnumber     => $item_3->itemnumber,
424             branchcode     => $branch,
425             priority       => 0,
426             found          => 'W'
427         }
428     );
429     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
430     is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
431     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
432     is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
433     t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
434
435     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
436     is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
437     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
438
439     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
440     is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
441     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
442
443     my $reserveid = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next->reserve_id;
444     my $reserving_borrower = Koha::Patrons->find( $reserving_borrowernumber )->unblessed;
445     AddIssue($reserving_borrower, $item_3->barcode);
446     my $reserve = $dbh->selectrow_hashref(
447         'SELECT * FROM old_reserves WHERE reserve_id = ?',
448         { Slice => {} },
449         $reserveid
450     );
451     is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
452
453     # Item-level hold, renewal test
454     AddReserve(
455         $branch, $reserving_borrowernumber, $biblio->biblionumber,
456         $bibitems,  $priority, $resdate, $expdate, $notes,
457         'a title', $item_1->itemnumber, $found
458     );
459
460     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
461     is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
462     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
463
464     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber, 1);
465     is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
466
467     # Items can't fill hold for reasons
468     ModItem({ notforloan => 1 }, $biblio->biblionumber, $item_1->itemnumber);
469     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
470     is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
471     ModItem({ notforloan => 0, itype => $itemtype }, $biblio->biblionumber, $item_1->itemnumber);
472
473     # FIXME: Add more for itemtype not for loan etc.
474
475     # Restricted users cannot renew when RestrictionBlockRenewing is enabled
476     my $item_5 = $builder->build_sample_item(
477         {
478             biblionumber     => $biblio->biblionumber,
479             library          => $branch,
480             replacementprice => 23.00,
481             itype            => $itemtype,
482         }
483     );
484     my $datedue5 = AddIssue($restricted_borrower, $item_5->barcode);
485     is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
486
487     t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
488     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_2->itemnumber);
489     is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
490     ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $item_5->itemnumber);
491     is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
492
493     # Users cannot renew an overdue item
494     my $item_6 = $builder->build_sample_item(
495         {
496             biblionumber     => $biblio->biblionumber,
497             library          => $branch,
498             replacementprice => 23.00,
499             itype            => $itemtype,
500         }
501     );
502
503     my $item_7 = $builder->build_sample_item(
504         {
505             biblionumber     => $biblio->biblionumber,
506             library          => $branch,
507             replacementprice => 23.00,
508             itype            => $itemtype,
509         }
510     );
511
512     my $datedue6 = AddIssue( $renewing_borrower, $item_6->barcode);
513     is (defined $datedue6, 1, "Item 2 checked out, due date: ".$datedue6->date_due);
514
515     my $now = dt_from_string();
516     my $five_weeks = DateTime::Duration->new(weeks => 5);
517     my $five_weeks_ago = $now - $five_weeks;
518     t::lib::Mocks::mock_preference('finesMode', 'production');
519
520     my $passeddatedue1 = AddIssue($renewing_borrower, $item_7->barcode, $five_weeks_ago);
521     is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
522
523     my ( $fine ) = CalcFine( $item_7->unblessed, $renewing_borrower->{categorycode}, $branch, $five_weeks_ago, $now );
524     C4::Overdues::UpdateFine(
525         {
526             issue_id       => $passeddatedue1->id(),
527             itemnumber     => $item_7->itemnumber,
528             borrowernumber => $renewing_borrower->{borrowernumber},
529             amount         => $fine,
530             due            => Koha::DateUtils::output_pref($five_weeks_ago)
531         }
532     );
533
534     t::lib::Mocks::mock_preference('RenewalLog', 0);
535     my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
536     my %params_renewal = (
537         timestamp => { -like => $date . "%" },
538         module => "CIRCULATION",
539         action => "RENEWAL",
540     );
541     my %params_issue = (
542         timestamp => { -like => $date . "%" },
543         module => "CIRCULATION",
544         action => "ISSUE"
545     );
546     my $old_log_size = Koha::ActionLogs->count( \%params_renewal );
547     my $dt = dt_from_string();
548     Time::Fake->offset( $dt->epoch );
549     my $datedue1 = AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
550     my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
551     is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
552     isnt (DateTime->compare($datedue1, $dt), 0, "AddRenewal returned a good duedate");
553     Time::Fake->reset;
554
555     t::lib::Mocks::mock_preference('RenewalLog', 1);
556     $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
557     $old_log_size = Koha::ActionLogs->count( \%params_renewal );
558     AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
559     $new_log_size = Koha::ActionLogs->count( \%params_renewal );
560     is ($new_log_size, $old_log_size + 1, 'renew log successfully added');
561
562     my $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $item_7->itemnumber } );
563     is( $fines->count, 2 );
564     isnt( $fines->next->status, 'UNRETURNED', 'Fine on renewed item is closed out properly' );
565     isnt( $fines->next->status, 'UNRETURNED', 'Fine on renewed item is closed out properly' );
566     $fines->delete();
567
568
569     my $old_issue_log_size = Koha::ActionLogs->count( \%params_issue );
570     my $old_renew_log_size = Koha::ActionLogs->count( \%params_renewal );
571     AddIssue( $renewing_borrower,$item_7->barcode,Koha::DateUtils::output_pref({str=>$datedue6->date_due, dateformat =>'iso'}),0,$date, 0, undef );
572     $new_log_size = Koha::ActionLogs->count( \%params_renewal );
573     is ($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
574     $new_log_size = Koha::ActionLogs->count( \%params_issue );
575     is ($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
576
577     $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $item_7->itemnumber } );
578     $fines->delete();
579
580     t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
581     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_6->itemnumber);
582     is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
583     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_7->itemnumber);
584     is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
585
586
587     $hold = Koha::Holds->search({ biblionumber => $biblio->biblionumber, borrowernumber => $reserving_borrowernumber })->next;
588     $hold->cancel;
589
590     # Bug 14101
591     # Test automatic renewal before value for "norenewalbefore" in policy is set
592     # In this case automatic renewal is not permitted prior to due date
593     my $item_4 = $builder->build_sample_item(
594         {
595             biblionumber     => $biblio->biblionumber,
596             library          => $branch,
597             replacementprice => 16.00,
598             itype            => $itemtype,
599         }
600     );
601
602     $issue = AddIssue( $renewing_borrower, $item_4->barcode, undef, undef, undef, undef, { auto_renew => 1 } );
603     ( $renewokay, $error ) =
604       CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
605     is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
606     is( $error, 'auto_too_soon',
607         'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
608
609     # Bug 7413
610     # Test premature manual renewal
611     $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
612
613     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
614     is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
615     is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
616
617     # Bug 14395
618     # Test 'exact time' setting for syspref NoRenewalBeforePrecision
619     t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
620     is(
621         GetSoonestRenewDate( $renewing_borrowernumber, $item_1->itemnumber ),
622         $datedue->clone->add( days => -7 ),
623         'Bug 14395: Renewals permitted 7 days before due date, as expected'
624     );
625
626     # Bug 14395
627     # Test 'date' setting for syspref NoRenewalBeforePrecision
628     t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
629     is(
630         GetSoonestRenewDate( $renewing_borrowernumber, $item_1->itemnumber ),
631         $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
632         'Bug 14395: Renewals permitted 7 days before due date, as expected'
633     );
634
635     # Bug 14101
636     # Test premature automatic renewal
637     ( $renewokay, $error ) =
638       CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
639     is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
640     is( $error, 'auto_too_soon',
641         'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
642     );
643
644     # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
645     # and test automatic renewal again
646     $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
647     ( $renewokay, $error ) =
648       CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
649     is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
650     is( $error, 'auto_too_soon',
651         'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
652     );
653
654     # Change policy so that loans can be renewed 99 days prior to the due date
655     # and test automatic renewal again
656     $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
657     ( $renewokay, $error ) =
658       CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
659     is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
660     is( $error, 'auto_renew',
661         'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
662     );
663
664     subtest "too_late_renewal / no_auto_renewal_after" => sub {
665         plan tests => 14;
666         my $item_to_auto_renew = $builder->build(
667             {   source => 'Item',
668                 value  => {
669                     biblionumber  => $biblio->biblionumber,
670                     homebranch    => $branch,
671                     holdingbranch => $branch,
672                 }
673             }
674         );
675
676         my $ten_days_before = dt_from_string->add( days => -10 );
677         my $ten_days_ahead  = dt_from_string->add( days => 10 );
678         AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
679
680         $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 9');
681         ( $renewokay, $error ) =
682           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
683         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
684         is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
685
686         $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 10');
687         ( $renewokay, $error ) =
688           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
689         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
690         is( $error, 'auto_too_late', 'Cannot auto renew, too late - no_auto_renewal_after is inclusive(returned code is auto_too_late)' );
691
692         $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 11');
693         ( $renewokay, $error ) =
694           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
695         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
696         is( $error, 'auto_too_soon', 'Cannot auto renew, too soon - no_auto_renewal_after is defined(returned code is auto_too_soon)' );
697
698         $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
699         ( $renewokay, $error ) =
700           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
701         is( $renewokay, 0,            'Do not renew, renewal is automatic' );
702         is( $error,     'auto_renew', 'Cannot renew, renew is automatic' );
703
704         $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string->add( days => -1 ) );
705         ( $renewokay, $error ) =
706           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
707         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
708         is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
709
710         $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string->add( days => -1 ) );
711         ( $renewokay, $error ) =
712           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
713         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
714         is( $error, 'auto_too_late', 'Cannot renew, too late(returned code is auto_too_late)' );
715
716         $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string->add( days => 1 ) );
717         ( $renewokay, $error ) =
718           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
719         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
720         is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
721     };
722
723     subtest "auto_too_much_oweing | OPACFineNoRenewalsBlockAutoRenew" => sub {
724         plan tests => 6;
725         my $item_to_auto_renew = $builder->build({
726             source => 'Item',
727             value => {
728                 biblionumber => $biblio->biblionumber,
729                 homebranch       => $branch,
730                 holdingbranch    => $branch,
731             }
732         });
733
734         my $ten_days_before = dt_from_string->add( days => -10 );
735         my $ten_days_ahead = dt_from_string->add( days => 10 );
736         AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
737
738         $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
739         C4::Context->set_preference('OPACFineNoRenewalsBlockAutoRenew','1');
740         C4::Context->set_preference('OPACFineNoRenewals','10');
741         my $fines_amount = 5;
742         my $account = Koha::Account->new({patron_id => $renewing_borrowernumber});
743         $account->add_debit(
744             {
745                 amount      => $fines_amount,
746                 interface   => 'test',
747                 type        => 'overdue',
748                 item_id     => $item_to_auto_renew->{itemnumber},
749                 description => "Some fines"
750             }
751         )->status('RETURNED')->store;
752         ( $renewokay, $error ) =
753           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
754         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
755         is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 5' );
756
757         $account->add_debit(
758             {
759                 amount      => $fines_amount,
760                 interface   => 'test',
761                 type        => 'overdue',
762                 item_id     => $item_to_auto_renew->{itemnumber},
763                 description => "Some fines"
764             }
765         )->status('RETURNED')->store;
766         ( $renewokay, $error ) =
767           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
768         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
769         is( $error, 'auto_renew', 'Can auto renew, OPACFineNoRenewals=10, patron has 10' );
770
771         $account->add_debit(
772             {
773                 amount      => $fines_amount,
774                 interface   => 'test',
775                 type        => 'overdue',
776                 item_id     => $item_to_auto_renew->{itemnumber},
777                 description => "Some fines"
778             }
779         )->status('RETURNED')->store;
780         ( $renewokay, $error ) =
781           CanBookBeRenewed( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
782         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
783         is( $error, 'auto_too_much_oweing', 'Cannot auto renew, OPACFineNoRenewals=10, patron has 15' );
784
785         $dbh->do('DELETE FROM accountlines WHERE borrowernumber=?', undef, $renewing_borrowernumber);
786     };
787
788     subtest "auto_account_expired | BlockExpiredPatronOpacActions" => sub {
789         plan tests => 6;
790         my $item_to_auto_renew = $builder->build({
791             source => 'Item',
792             value => {
793                 biblionumber => $biblio->biblionumber,
794                 homebranch       => $branch,
795                 holdingbranch    => $branch,
796             }
797         });
798
799         $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 11');
800
801         my $ten_days_before = dt_from_string->add( days => -10 );
802         my $ten_days_ahead = dt_from_string->add( days => 10 );
803
804         # Patron is expired and BlockExpiredPatronOpacActions=0
805         # => auto renew is allowed
806         t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 0);
807         my $patron = $expired_borrower;
808         my $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
809         ( $renewokay, $error ) =
810           CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
811         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
812         is( $error, 'auto_renew', 'Can auto renew, patron is expired but BlockExpiredPatronOpacActions=0' );
813         Koha::Checkouts->find( $checkout->issue_id )->delete;
814
815
816         # Patron is expired and BlockExpiredPatronOpacActions=1
817         # => auto renew is not allowed
818         t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
819         $patron = $expired_borrower;
820         $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
821         ( $renewokay, $error ) =
822           CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
823         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
824         is( $error, 'auto_account_expired', 'Can not auto renew, lockExpiredPatronOpacActions=1 and patron is expired' );
825         Koha::Checkouts->find( $checkout->issue_id )->delete;
826
827
828         # Patron is not expired and BlockExpiredPatronOpacActions=1
829         # => auto renew is allowed
830         t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
831         $patron = $renewing_borrower;
832         $checkout = AddIssue( $patron, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
833         ( $renewokay, $error ) =
834           CanBookBeRenewed( $patron->{borrowernumber}, $item_to_auto_renew->{itemnumber} );
835         is( $renewokay, 0, 'Do not renew, renewal is automatic' );
836         is( $error, 'auto_renew', 'Can auto renew, BlockExpiredPatronOpacActions=1 but patron is not expired' );
837         Koha::Checkouts->find( $checkout->issue_id )->delete;
838     };
839
840     subtest "GetLatestAutoRenewDate" => sub {
841         plan tests => 5;
842         my $item_to_auto_renew = $builder->build(
843             {   source => 'Item',
844                 value  => {
845                     biblionumber  => $biblio->biblionumber,
846                     homebranch    => $branch,
847                     holdingbranch => $branch,
848                 }
849             }
850         );
851
852         my $ten_days_before = dt_from_string->add( days => -10 );
853         my $ten_days_ahead  = dt_from_string->add( days => 10 );
854         AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
855         $dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = NULL');
856         my $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
857         is( $latest_auto_renew_date, undef, 'GetLatestAutoRenewDate should return undef if no_auto_renewal_after or no_auto_renewal_after_hard_limit are not defined' );
858         my $five_days_before = dt_from_string->add( days => -5 );
859         $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 5, no_auto_renewal_after_hard_limit = NULL');
860         $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
861         is( $latest_auto_renew_date->truncate( to => 'minute' ),
862             $five_days_before->truncate( to => 'minute' ),
863             'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
864         );
865         my $five_days_ahead = dt_from_string->add( days => 5 );
866         $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = NULL');
867         $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
868         is( $latest_auto_renew_date->truncate( to => 'minute' ),
869             $five_days_ahead->truncate( to => 'minute' ),
870             'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
871         );
872         my $two_days_ahead = dt_from_string->add( days => 2 );
873         $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = NULL, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string->add( days => 2 ) );
874         $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
875         is( $latest_auto_renew_date->truncate( to => 'day' ),
876             $two_days_ahead->truncate( to => 'day' ),
877             'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is defined and not no_auto_renewal_after'
878         );
879         $dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15, no_auto_renewal_after_hard_limit = ?', undef, dt_from_string->add( days => 2 ) );
880         $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
881         is( $latest_auto_renew_date->truncate( to => 'day' ),
882             $two_days_ahead->truncate( to => 'day' ),
883             'GetLatestAutoRenewDate should return +2 days if no_auto_renewal_after_hard_limit is < no_auto_renewal_after'
884         );
885
886     };
887
888     # Too many renewals
889
890     # set policy to forbid renewals
891     $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
892
893     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
894     is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
895     is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
896
897     # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
898     t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
899     t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
900
901     C4::Overdues::UpdateFine(
902         {
903             issue_id       => $issue->id(),
904             itemnumber     => $item_1->itemnumber,
905             borrowernumber => $renewing_borrower->{borrowernumber},
906             amount         => 15.00,
907             type           => q{},
908             due            => Koha::DateUtils::output_pref($datedue)
909         }
910     );
911
912     my $line = Koha::Account::Lines->search({ borrowernumber => $renewing_borrower->{borrowernumber} })->next();
913     is( $line->accounttype, 'OVERDUE', 'Account line type is OVERDUE' );
914     is( $line->status, 'UNRETURNED', 'Account line status is UNRETURNED' );
915     is( $line->amountoutstanding, '15.000000', 'Account line amount outstanding is 15.00' );
916     is( $line->amount, '15.000000', 'Account line amount is 15.00' );
917     is( $line->issue_id, $issue->id, 'Account line issue id matches' );
918
919     my $offset = Koha::Account::Offsets->search({ debit_id => $line->id })->next();
920     is( $offset->type, 'OVERDUE', 'Account offset type is Fine' );
921     is( $offset->amount, '15.000000', 'Account offset amount is 15.00' );
922
923     t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
924     t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
925
926     LostItem( $item_1->itemnumber, 'test', 1 );
927
928     $line = Koha::Account::Lines->find($line->id);
929     is( $line->accounttype, 'OVERDUE', 'Account type remains as OVERDUE' );
930     isnt( $line->status, 'UNRETURNED', 'Account status correctly changed from UNRETURNED to RETURNED' );
931
932     my $item = Koha::Items->find($item_1->itemnumber);
933     ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
934     my $checkout = Koha::Checkouts->find({ itemnumber => $item_1->itemnumber });
935     is( $checkout, undef, 'LostItem called with forced return has checked in the item' );
936
937     my $total_due = $dbh->selectrow_array(
938         'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
939         undef, $renewing_borrower->{borrowernumber}
940     );
941
942     is( $total_due, '15.000000', 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
943
944     C4::Context->dbh->do("DELETE FROM accountlines");
945
946     C4::Overdues::UpdateFine(
947         {
948             issue_id       => $issue2->id(),
949             itemnumber     => $item_2->itemnumber,
950             borrowernumber => $renewing_borrower->{borrowernumber},
951             amount         => 15.00,
952             type           => q{},
953             due            => Koha::DateUtils::output_pref($datedue)
954         }
955     );
956
957     LostItem( $item_2->itemnumber, 'test', 0 );
958
959     my $item2 = Koha::Items->find($item_2->itemnumber);
960     ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
961     ok( Koha::Checkouts->find({ itemnumber => $item_2->itemnumber }), 'LostItem called without forced return has checked in the item' );
962
963     $total_due = $dbh->selectrow_array(
964         'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
965         undef, $renewing_borrower->{borrowernumber}
966     );
967
968     ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
969
970     my $future = dt_from_string();
971     $future->add( days => 7 );
972     my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
973     ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
974
975     # Users cannot renew any item if there is an overdue item
976     t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
977     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_6->itemnumber);
978     is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
979     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_7->itemnumber);
980     is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
981
982     my $manager = $builder->build_object({ class => "Koha::Patrons" });
983     t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
984     t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
985     $checkout = Koha::Checkouts->find( { itemnumber => $item_3->itemnumber } );
986     LostItem( $item_3->itemnumber, 'test', 0 );
987     my $accountline = Koha::Account::Lines->find( { itemnumber => $item_3->itemnumber } );
988     is( $accountline->issue_id, $checkout->id, "Issue id added for lost replacement fee charge" );
989     is(
990         $accountline->description,
991         sprintf( "%s %s %s",
992             $item_3->biblio->title  || '',
993             $item_3->barcode        || '',
994             $item_3->itemcallnumber || '' ),
995         "Account line description must not contain 'Lost Items ', but be title, barcode, itemcallnumber"
996     );
997   }
998
999 {
1000     # GetUpcomingDueIssues tests
1001     my $branch   = $library2->{branchcode};
1002
1003     #Create another record
1004     my $biblio2 = $builder->build_sample_biblio();
1005
1006     #Create third item
1007     my $item_1 = Koha::Items->find($reused_itemnumber_1);
1008     my $item_2 = Koha::Items->find($reused_itemnumber_2);
1009     my $item_3 = $builder->build_sample_item(
1010         {
1011             biblionumber     => $biblio2->biblionumber,
1012             library          => $branch,
1013             itype            => $itemtype,
1014         }
1015     );
1016
1017
1018     # Create a borrower
1019     my %a_borrower_data = (
1020         firstname =>  'Fridolyn',
1021         surname => 'SOMERS',
1022         categorycode => $patron_category->{categorycode},
1023         branchcode => $branch,
1024     );
1025
1026     my $a_borrower_borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
1027     my $a_borrower = Koha::Patrons->find( $a_borrower_borrowernumber )->unblessed;
1028
1029     my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
1030     my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
1031     my $today = DateTime->today(time_zone => C4::Context->tz());
1032
1033     my $issue = AddIssue( $a_borrower, $item_1->barcode, $yesterday );
1034     my $datedue = dt_from_string( $issue->date_due() );
1035     my $issue2 = AddIssue( $a_borrower, $item_2->barcode, $two_days_ahead );
1036     my $datedue2 = dt_from_string( $issue->date_due() );
1037
1038     my $upcoming_dues;
1039
1040     # GetUpcomingDueIssues tests
1041     for my $i(0..1) {
1042         $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
1043         is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
1044     }
1045
1046     #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
1047     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
1048     is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
1049
1050     for my $i(3..5) {
1051         $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
1052         is ( scalar( @$upcoming_dues ), 1,
1053             "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
1054     }
1055
1056     # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
1057
1058     my $issue3 = AddIssue( $a_borrower, $item_3->barcode, $today );
1059
1060     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
1061     is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
1062
1063     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
1064     is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
1065
1066     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
1067     is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
1068
1069     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 }  );
1070     is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
1071
1072     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
1073     is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
1074
1075     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
1076     is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
1077
1078 }
1079
1080 {
1081     my $branch   = $library2->{branchcode};
1082
1083     my $biblio = $builder->build_sample_biblio();
1084
1085     #Create third item
1086     my $item = $builder->build_sample_item(
1087         {
1088             biblionumber     => $biblio->biblionumber,
1089             library          => $branch,
1090             itype            => $itemtype,
1091         }
1092     );
1093
1094     # Create a borrower
1095     my %a_borrower_data = (
1096         firstname =>  'Kyle',
1097         surname => 'Hall',
1098         categorycode => $patron_category->{categorycode},
1099         branchcode => $branch,
1100     );
1101
1102     my $borrowernumber = Koha::Patron->new(\%a_borrower_data)->store->borrowernumber;
1103
1104     my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1105     my $issue = AddIssue( $borrower, $item->barcode );
1106     UpdateFine(
1107         {
1108             issue_id       => $issue->id(),
1109             itemnumber     => $item->itemnumber,
1110             borrowernumber => $borrowernumber,
1111             amount         => 0,
1112             type           => q{}
1113         }
1114     );
1115
1116     my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $item->itemnumber );
1117     my $count = $hr->{count};
1118
1119     is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
1120 }
1121
1122 {
1123     $dbh->do('DELETE FROM issues');
1124     $dbh->do('DELETE FROM items');
1125     $dbh->do('DELETE FROM issuingrules');
1126     Koha::CirculationRules->search()->delete();
1127     $dbh->do(
1128         q{
1129         INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, issuelength, lengthunit, renewalsallowed, renewalperiod,
1130                     norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
1131         },
1132         {},
1133         '*', '*', '*', 25,
1134         14,  'days',
1135         1,   7,
1136         undef,  0,
1137         .10, 1
1138     );
1139     Koha::CirculationRules->set_rules(
1140         {
1141             categorycode => '*',
1142             itemtype     => '*',
1143             branchcode   => '*',
1144             rules        => {
1145                 maxissueqty => 20
1146             }
1147         }
1148     );
1149     my $biblio = $builder->build_sample_biblio();
1150
1151     my $item_1 = $builder->build_sample_item(
1152         {
1153             biblionumber     => $biblio->biblionumber,
1154             library          => $library2->{branchcode},
1155             itype            => $itemtype,
1156         }
1157     );
1158
1159     my $item_2= $builder->build_sample_item(
1160         {
1161             biblionumber     => $biblio->biblionumber,
1162             library          => $library2->{branchcode},
1163             itype            => $itemtype,
1164         }
1165     );
1166
1167     my $borrowernumber1 = Koha::Patron->new({
1168         firstname    => 'Kyle',
1169         surname      => 'Hall',
1170         categorycode => $patron_category->{categorycode},
1171         branchcode   => $library2->{branchcode},
1172     })->store->borrowernumber;
1173     my $borrowernumber2 = Koha::Patron->new({
1174         firstname    => 'Chelsea',
1175         surname      => 'Hall',
1176         categorycode => $patron_category->{categorycode},
1177         branchcode   => $library2->{branchcode},
1178     })->store->borrowernumber;
1179
1180     my $borrower1 = Koha::Patrons->find( $borrowernumber1 )->unblessed;
1181     my $borrower2 = Koha::Patrons->find( $borrowernumber2 )->unblessed;
1182
1183     my $issue = AddIssue( $borrower1, $item_1->barcode );
1184
1185     my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1186     is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
1187
1188     AddReserve(
1189         $library2->{branchcode}, $borrowernumber2, $biblio->biblionumber,
1190         '',  1, undef, undef, '',
1191         undef, undef, undef
1192     );
1193
1194     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1195     t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1196     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1197     is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
1198
1199     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
1200     t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1201     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1202     is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
1203
1204     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1205     t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
1206     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1207     is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
1208
1209     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
1210     t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
1211     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1212     is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
1213
1214     # Setting item not checked out to be not for loan but holdable
1215     ModItem({ notforloan => -1 }, $biblio->biblionumber, $item_2->itemnumber);
1216
1217     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $item_1->itemnumber );
1218     is( $renewokay, 0, 'Bug 14337 - Verify the borrower can not renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled but the only available item is notforloan' );
1219 }
1220
1221 {
1222     # Don't allow renewing onsite checkout
1223     my $branch   = $library->{branchcode};
1224
1225     #Create another record
1226     my $biblio = $builder->build_sample_biblio();
1227
1228     my $item = $builder->build_sample_item(
1229         {
1230             biblionumber     => $biblio->biblionumber,
1231             library          => $branch,
1232             itype            => $itemtype,
1233         }
1234     );
1235
1236     my $borrowernumber = Koha::Patron->new({
1237         firstname =>  'fn',
1238         surname => 'dn',
1239         categorycode => $patron_category->{categorycode},
1240         branchcode => $branch,
1241     })->store->borrowernumber;
1242
1243     my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
1244
1245     my $issue = AddIssue( $borrower, $item->barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
1246     my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $item->itemnumber );
1247     is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
1248     is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
1249 }
1250
1251 {
1252     my $library = $builder->build({ source => 'Branch' });
1253
1254     my $biblio = $builder->build_sample_biblio();
1255
1256     my $item = $builder->build_sample_item(
1257         {
1258             biblionumber     => $biblio->biblionumber,
1259             library          => $library->{branchcode},
1260             itype            => $itemtype,
1261         }
1262     );
1263
1264     my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
1265
1266     my $issue = AddIssue( $patron, $item->barcode );
1267     UpdateFine(
1268         {
1269             issue_id       => $issue->id(),
1270             itemnumber     => $item->itemnumber,
1271             borrowernumber => $patron->{borrowernumber},
1272             amount         => 1,
1273             type           => q{}
1274         }
1275     );
1276     UpdateFine(
1277         {
1278             issue_id       => $issue->id(),
1279             itemnumber     => $item->itemnumber,
1280             borrowernumber => $patron->{borrowernumber},
1281             amount         => 2,
1282             type           => q{}
1283         }
1284     );
1285     is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
1286 }
1287
1288 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
1289     plan tests => 24;
1290
1291     my $homebranch    = $builder->build( { source => 'Branch' } );
1292     my $holdingbranch = $builder->build( { source => 'Branch' } );
1293     my $otherbranch   = $builder->build( { source => 'Branch' } );
1294     my $patron_1      = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1295     my $patron_2      = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1296
1297     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1298     my $item = $builder->build(
1299         {   source => 'Item',
1300             value  => {
1301                 homebranch    => $homebranch->{branchcode},
1302                 holdingbranch => $holdingbranch->{branchcode},
1303                 biblionumber  => $biblioitem->{biblionumber}
1304             }
1305         }
1306     );
1307
1308     set_userenv($holdingbranch);
1309
1310     my $issue = AddIssue( $patron_1->unblessed, $item->{barcode} );
1311     is( ref($issue), 'Koha::Checkout', 'AddIssue should return a Koha::Checkout object' );
1312
1313     my ( $error, $question, $alerts );
1314
1315     # AllowReturnToBranch == anywhere
1316     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1317     ## Test that unknown barcodes don't generate internal server errors
1318     set_userenv($homebranch);
1319     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, 'KohaIsAwesome' );
1320     ok( $error->{UNKNOWN_BARCODE}, '"KohaIsAwesome" is not a valid barcode as expected.' );
1321     ## Can be issued from homebranch
1322     set_userenv($homebranch);
1323     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1324     is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1325     is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1326     ## Can be issued from holdingbranch
1327     set_userenv($holdingbranch);
1328     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1329     is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1330     is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1331     ## Can be issued from another branch
1332     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1333     is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1334     is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1335
1336     # AllowReturnToBranch == holdingbranch
1337     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1338     ## Cannot be issued from homebranch
1339     set_userenv($homebranch);
1340     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1341     is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1342     is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1343     is( $error->{branch_to_return},         $holdingbranch->{branchcode} );
1344     ## Can be issued from holdinbranch
1345     set_userenv($holdingbranch);
1346     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1347     is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1348     is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1349     ## Cannot be issued from another branch
1350     set_userenv($otherbranch);
1351     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1352     is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1353     is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1354     is( $error->{branch_to_return},         $holdingbranch->{branchcode} );
1355
1356     # AllowReturnToBranch == homebranch
1357     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1358     ## Can be issued from holdinbranch
1359     set_userenv($homebranch);
1360     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1361     is( keys(%$error) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1362     is( exists $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER must be set' );
1363     ## Cannot be issued from holdinbranch
1364     set_userenv($holdingbranch);
1365     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1366     is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1367     is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1368     is( $error->{branch_to_return},         $homebranch->{branchcode} );
1369     ## Cannot be issued from holdinbranch
1370     set_userenv($otherbranch);
1371     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1372     is( keys(%$question) + keys(%$alerts), 0, 'There should not be any errors or alerts (impossible)' . str($error, $question, $alerts) );
1373     is( exists $error->{RETURN_IMPOSSIBLE}, 1, 'RETURN_IMPOSSIBLE must be set' );
1374     is( $error->{branch_to_return},         $homebranch->{branchcode} );
1375
1376     # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1377 };
1378
1379 subtest 'AddIssue & AllowReturnToBranch' => sub {
1380     plan tests => 9;
1381
1382     my $homebranch    = $builder->build( { source => 'Branch' } );
1383     my $holdingbranch = $builder->build( { source => 'Branch' } );
1384     my $otherbranch   = $builder->build( { source => 'Branch' } );
1385     my $patron_1      = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1386     my $patron_2      = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1387
1388     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1389     my $item = $builder->build(
1390         {   source => 'Item',
1391             value  => {
1392                 homebranch    => $homebranch->{branchcode},
1393                 holdingbranch => $holdingbranch->{branchcode},
1394                 notforloan    => 0,
1395                 itemlost      => 0,
1396                 withdrawn     => 0,
1397                 biblionumber  => $biblioitem->{biblionumber}
1398             }
1399         }
1400     );
1401
1402     set_userenv($holdingbranch);
1403
1404     my $ref_issue = 'Koha::Checkout';
1405     my $issue = AddIssue( $patron_1, $item->{barcode} );
1406
1407     my ( $error, $question, $alerts );
1408
1409     # AllowReturnToBranch == homebranch
1410     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1411     ## Can be issued from homebranch
1412     set_userenv($homebranch);
1413     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1414     set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1415     ## Can be issued from holdinbranch
1416     set_userenv($holdingbranch);
1417     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1418     set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1419     ## Can be issued from another branch
1420     set_userenv($otherbranch);
1421     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1422     set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1423
1424     # AllowReturnToBranch == holdinbranch
1425     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1426     ## Cannot be issued from homebranch
1427     set_userenv($homebranch);
1428     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1429     ## Can be issued from holdingbranch
1430     set_userenv($holdingbranch);
1431     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1432     set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1433     ## Cannot be issued from another branch
1434     set_userenv($otherbranch);
1435     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1436
1437     # AllowReturnToBranch == homebranch
1438     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1439     ## Can be issued from homebranch
1440     set_userenv($homebranch);
1441     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1442     set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1443     ## Cannot be issued from holdinbranch
1444     set_userenv($holdingbranch);
1445     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1446     ## Cannot be issued from another branch
1447     set_userenv($otherbranch);
1448     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1449     # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1450 };
1451
1452 subtest 'CanBookBeIssued + Koha::Patron->is_debarred|has_overdues' => sub {
1453     plan tests => 8;
1454
1455     my $library = $builder->build( { source => 'Branch' } );
1456     my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1457
1458     my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1459     my $item_1 = $builder->build(
1460         {   source => 'Item',
1461             value  => {
1462                 homebranch    => $library->{branchcode},
1463                 holdingbranch => $library->{branchcode},
1464                 biblionumber  => $biblioitem_1->{biblionumber}
1465             }
1466         }
1467     );
1468     my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1469     my $item_2 = $builder->build(
1470         {   source => 'Item',
1471             value  => {
1472                 homebranch    => $library->{branchcode},
1473                 holdingbranch => $library->{branchcode},
1474                 biblionumber  => $biblioitem_2->{biblionumber}
1475             }
1476         }
1477     );
1478
1479     my ( $error, $question, $alerts );
1480
1481     # Patron cannot issue item_1, they have overdues
1482     my $yesterday = DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 );
1483     my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, $yesterday );    # Add an overdue
1484
1485     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'confirmation' );
1486     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1487     is( keys(%$error) + keys(%$alerts),  0, 'No key for error and alert' . str($error, $question, $alerts) );
1488     is( $question->{USERBLOCKEDOVERDUE}, 1, 'OverduesBlockCirc=confirmation, USERBLOCKEDOVERDUE should be set for question' );
1489
1490     t::lib::Mocks::mock_preference( 'OverduesBlockCirc', 'block' );
1491     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1492     is( keys(%$question) + keys(%$alerts),  0, 'No key for question and alert ' . str($error, $question, $alerts) );
1493     is( $error->{USERBLOCKEDOVERDUE},      1, 'OverduesBlockCirc=block, USERBLOCKEDOVERDUE should be set for error' );
1494
1495     # Patron cannot issue item_1, they are debarred
1496     my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
1497     Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber, expiration => $tomorrow } );
1498     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1499     is( keys(%$question) + keys(%$alerts),  0, 'No key for question and alert ' . str($error, $question, $alerts) );
1500     is( $error->{USERBLOCKEDWITHENDDATE}, output_pref( { dt => $tomorrow, dateformat => 'sql', dateonly => 1 } ), 'USERBLOCKEDWITHENDDATE should be tomorrow' );
1501
1502     Koha::Patron::Debarments::AddDebarment( { borrowernumber => $patron->borrowernumber } );
1503     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1504     is( keys(%$question) + keys(%$alerts),  0, 'No key for question and alert ' . str($error, $question, $alerts) );
1505     is( $error->{USERBLOCKEDNOENDDATE},    '9999-12-31', 'USERBLOCKEDNOENDDATE should be 9999-12-31 for unlimited debarments' );
1506 };
1507
1508 subtest 'CanBookBeIssued + Statistic patrons "X"' => sub {
1509     plan tests => 1;
1510
1511     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
1512     my $patron_category_x = $builder->build_object(
1513         {
1514             class => 'Koha::Patron::Categories',
1515             value => { category_type => 'X' }
1516         }
1517     );
1518     my $patron = $builder->build_object(
1519         {
1520             class => 'Koha::Patrons',
1521             value => {
1522                 categorycode  => $patron_category_x->categorycode,
1523                 gonenoaddress => undef,
1524                 lost          => undef,
1525                 debarred      => undef,
1526                 borrowernotes => ""
1527             }
1528         }
1529     );
1530     my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1531     my $item_1 = $builder->build(
1532         {
1533             source => 'Item',
1534             value  => {
1535                 homebranch    => $library->branchcode,
1536                 holdingbranch => $library->branchcode,
1537                 biblionumber  => $biblioitem_1->{biblionumber}
1538             }
1539         }
1540     );
1541
1542     my ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_1->{barcode} );
1543     is( $error->{STATS}, 1, '"Error" flag "STATS" must be set if CanBookBeIssued is called with a statistic patron (category_type=X)' );
1544
1545     # TODO There are other tests to provide here
1546 };
1547
1548 subtest 'MultipleReserves' => sub {
1549     plan tests => 3;
1550
1551     my $biblio = $builder->build_sample_biblio();
1552
1553     my $branch = $library2->{branchcode};
1554
1555     my $item_1 = $builder->build_sample_item(
1556         {
1557             biblionumber     => $biblio->biblionumber,
1558             library          => $branch,
1559             replacementprice => 12.00,
1560             itype            => $itemtype,
1561         }
1562     );
1563
1564     my $item_2 = $builder->build_sample_item(
1565         {
1566             biblionumber     => $biblio->biblionumber,
1567             library          => $branch,
1568             replacementprice => 12.00,
1569             itype            => $itemtype,
1570         }
1571     );
1572
1573     my $bibitems       = '';
1574     my $priority       = '1';
1575     my $resdate        = undef;
1576     my $expdate        = undef;
1577     my $notes          = '';
1578     my $checkitem      = undef;
1579     my $found          = undef;
1580
1581     my %renewing_borrower_data = (
1582         firstname =>  'John',
1583         surname => 'Renewal',
1584         categorycode => $patron_category->{categorycode},
1585         branchcode => $branch,
1586     );
1587     my $renewing_borrowernumber = Koha::Patron->new(\%renewing_borrower_data)->store->borrowernumber;
1588     my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
1589     my $issue = AddIssue( $renewing_borrower, $item_1->barcode);
1590     my $datedue = dt_from_string( $issue->date_due() );
1591     is (defined $issue->date_due(), 1, "item 1 checked out");
1592     my $borrowing_borrowernumber = Koha::Checkouts->find({ itemnumber => $item_1->itemnumber })->borrowernumber;
1593
1594     my %reserving_borrower_data1 = (
1595         firstname =>  'Katrin',
1596         surname => 'Reservation',
1597         categorycode => $patron_category->{categorycode},
1598         branchcode => $branch,
1599     );
1600     my $reserving_borrowernumber1 = Koha::Patron->new(\%reserving_borrower_data1)->store->borrowernumber;
1601     AddReserve(
1602         $branch, $reserving_borrowernumber1, $biblio->biblionumber,
1603         $bibitems,  $priority, $resdate, $expdate, $notes,
1604         'a title', $checkitem, $found
1605     );
1606
1607     my %reserving_borrower_data2 = (
1608         firstname =>  'Kirk',
1609         surname => 'Reservation',
1610         categorycode => $patron_category->{categorycode},
1611         branchcode => $branch,
1612     );
1613     my $reserving_borrowernumber2 = Koha::Patron->new(\%reserving_borrower_data2)->store->borrowernumber;
1614     AddReserve(
1615         $branch, $reserving_borrowernumber2, $biblio->biblionumber,
1616         $bibitems,  $priority, $resdate, $expdate, $notes,
1617         'a title', $checkitem, $found
1618     );
1619
1620     {
1621         my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
1622         is($renewokay, 0, 'Bug 17941 - should cover the case where 2 books are both reserved, so failing');
1623     }
1624
1625     my $item_3 = $builder->build_sample_item(
1626         {
1627             biblionumber     => $biblio->biblionumber,
1628             library          => $branch,
1629             replacementprice => 12.00,
1630             itype            => $itemtype,
1631         }
1632     );
1633
1634     {
1635         my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber, 1);
1636         is($renewokay, 1, 'Bug 17941 - should cover the case where 2 books are reserved, but a third one is available');
1637     }
1638 };
1639
1640 subtest 'CanBookBeIssued + AllowMultipleIssuesOnABiblio' => sub {
1641     plan tests => 5;
1642
1643     my $library = $builder->build( { source => 'Branch' } );
1644     my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
1645
1646     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1647     my $biblionumber = $biblioitem->{biblionumber};
1648     my $item_1 = $builder->build(
1649         {   source => 'Item',
1650             value  => {
1651                 homebranch    => $library->{branchcode},
1652                 holdingbranch => $library->{branchcode},
1653                 biblionumber  => $biblionumber,
1654             }
1655         }
1656     );
1657     my $item_2 = $builder->build(
1658         {   source => 'Item',
1659             value  => {
1660                 homebranch    => $library->{branchcode},
1661                 holdingbranch => $library->{branchcode},
1662                 biblionumber  => $biblionumber,
1663             }
1664         }
1665     );
1666
1667     my ( $error, $question, $alerts );
1668     my $issue = AddIssue( $patron->unblessed, $item_1->{barcode}, dt_from_string->add( days => 1 ) );
1669
1670     t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1671     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1672     is( keys(%$error) + keys(%$alerts),  0, 'No error or alert should be raised' . str($error, $question, $alerts) );
1673     is( $question->{BIBLIO_ALREADY_ISSUED}, 1, 'BIBLIO_ALREADY_ISSUED question flag should be set if AllowMultipleIssuesOnABiblio=0 and issue already exists' . str($error, $question, $alerts) );
1674
1675     t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1676     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1677     is( keys(%$error) + keys(%$question) + keys(%$alerts),  0, 'No BIBLIO_ALREADY_ISSUED flag should be set if AllowMultipleIssuesOnABiblio=1' . str($error, $question, $alerts) );
1678
1679     # Add a subscription
1680     Koha::Subscription->new({ biblionumber => $biblionumber })->store;
1681
1682     t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 0);
1683     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1684     is( keys(%$error) + keys(%$question) + keys(%$alerts),  0, 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription' . str($error, $question, $alerts) );
1685
1686     t::lib::Mocks::mock_preference('AllowMultipleIssuesOnABiblio', 1);
1687     ( $error, $question, $alerts ) = CanBookBeIssued( $patron, $item_2->{barcode} );
1688     is( keys(%$error) + keys(%$question) + keys(%$alerts),  0, 'No BIBLIO_ALREADY_ISSUED flag should be set if it is a subscription' . str($error, $question, $alerts) );
1689 };
1690
1691 subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
1692     plan tests => 8;
1693
1694     my $library = $builder->build( { source => 'Branch' } );
1695     my $patron  = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1696
1697     # Add 2 items
1698     my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1699     my $item_1 = $builder->build(
1700         {
1701             source => 'Item',
1702             value  => {
1703                 homebranch    => $library->{branchcode},
1704                 holdingbranch => $library->{branchcode},
1705                 notforloan    => 0,
1706                 itemlost      => 0,
1707                 withdrawn     => 0,
1708                 biblionumber  => $biblioitem_1->{biblionumber}
1709             }
1710         }
1711     );
1712     my $biblioitem_2 = $builder->build( { source => 'Biblioitem' } );
1713     my $item_2 = $builder->build(
1714         {
1715             source => 'Item',
1716             value  => {
1717                 homebranch    => $library->{branchcode},
1718                 holdingbranch => $library->{branchcode},
1719                 notforloan    => 0,
1720                 itemlost      => 0,
1721                 withdrawn     => 0,
1722                 biblionumber  => $biblioitem_2->{biblionumber}
1723             }
1724         }
1725     );
1726
1727     # And the issuing rule
1728     Koha::IssuingRules->search->delete;
1729     my $rule = Koha::IssuingRule->new(
1730         {
1731             categorycode => '*',
1732             itemtype     => '*',
1733             branchcode   => '*',
1734             issuelength  => 1,
1735             firstremind  => 1,        # 1 day of grace
1736             finedays     => 2,        # 2 days of fine per day of overdue
1737             lengthunit   => 'days',
1738         }
1739     );
1740     $rule->store();
1741
1742     # Patron cannot issue item_1, they have overdues
1743     my $five_days_ago = dt_from_string->subtract( days => 5 );
1744     my $ten_days_ago  = dt_from_string->subtract( days => 10 );
1745     AddIssue( $patron, $item_1->{barcode}, $five_days_ago );    # Add an overdue
1746     AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1747       ;    # Add another overdue
1748
1749     t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '0' );
1750     AddReturn( $item_1->{barcode}, $library->{branchcode}, undef, dt_from_string );
1751     my $debarments = Koha::Patron::Debarments::GetDebarments(
1752         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1753     is( scalar(@$debarments), 1 );
1754
1755     # FIXME Is it right? I'd have expected 5 * 2 - 1 instead
1756     # Same for the others
1757     my $expected_expiration = output_pref(
1758         {
1759             dt         => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1760             dateformat => 'sql',
1761             dateonly   => 1
1762         }
1763     );
1764     is( $debarments->[0]->{expiration}, $expected_expiration );
1765
1766     AddReturn( $item_2->{barcode}, $library->{branchcode}, undef, dt_from_string );
1767     $debarments = Koha::Patron::Debarments::GetDebarments(
1768         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1769     is( scalar(@$debarments), 1 );
1770     $expected_expiration = output_pref(
1771         {
1772             dt         => dt_from_string->add( days => ( 10 - 1 ) * 2 ),
1773             dateformat => 'sql',
1774             dateonly   => 1
1775         }
1776     );
1777     is( $debarments->[0]->{expiration}, $expected_expiration );
1778
1779     Koha::Patron::Debarments::DelUniqueDebarment(
1780         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1781
1782     t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '1' );
1783     AddIssue( $patron, $item_1->{barcode}, $five_days_ago );    # Add an overdue
1784     AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
1785       ;    # Add another overdue
1786     AddReturn( $item_1->{barcode}, $library->{branchcode}, undef, dt_from_string );
1787     $debarments = Koha::Patron::Debarments::GetDebarments(
1788         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1789     is( scalar(@$debarments), 1 );
1790     $expected_expiration = output_pref(
1791         {
1792             dt         => dt_from_string->add( days => ( 5 - 1 ) * 2 ),
1793             dateformat => 'sql',
1794             dateonly   => 1
1795         }
1796     );
1797     is( $debarments->[0]->{expiration}, $expected_expiration );
1798
1799     AddReturn( $item_2->{barcode}, $library->{branchcode}, undef, dt_from_string );
1800     $debarments = Koha::Patron::Debarments::GetDebarments(
1801         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
1802     is( scalar(@$debarments), 1 );
1803     $expected_expiration = output_pref(
1804         {
1805             dt => dt_from_string->add( days => ( 5 - 1 ) * 2 + ( 10 - 1 ) * 2 ),
1806             dateformat => 'sql',
1807             dateonly   => 1
1808         }
1809     );
1810     is( $debarments->[0]->{expiration}, $expected_expiration );
1811 };
1812
1813 subtest 'AddReturn + suspension_chargeperiod' => sub {
1814     plan tests => 21;
1815
1816     my $library = $builder->build( { source => 'Branch' } );
1817     my $patron  = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
1818
1819     # Add 2 items
1820     my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
1821     my $item_1 = $builder->build(
1822         {
1823             source => 'Item',
1824             value  => {
1825                 homebranch    => $library->{branchcode},
1826                 holdingbranch => $library->{branchcode},
1827                 notforloan    => 0,
1828                 itemlost      => 0,
1829                 withdrawn     => 0,
1830                 biblionumber  => $biblioitem_1->{biblionumber}
1831             }
1832         }
1833     );
1834
1835     # And the issuing rule
1836     Koha::IssuingRules->search->delete;
1837     my $rule = Koha::IssuingRule->new(
1838         {
1839             categorycode => '*',
1840             itemtype     => '*',
1841             branchcode   => '*',
1842             issuelength  => 1,
1843             firstremind  => 0,        # 0 day of grace
1844             finedays     => 2,        # 2 days of fine per day of overdue
1845             suspension_chargeperiod => 1,
1846             lengthunit   => 'days',
1847         }
1848     );
1849     $rule->store();
1850
1851     my $five_days_ago = dt_from_string->subtract( days => 5 );
1852     # We want to charge 2 days every day, without grace
1853     # With 5 days of overdue: 5 * Z
1854     my $expected_expiration = dt_from_string->add( days => ( 5 * 2 ) / 1 );
1855     test_debarment_on_checkout(
1856         {
1857             item            => $item_1,
1858             library         => $library,
1859             patron          => $patron,
1860             due_date        => $five_days_ago,
1861             expiration_date => $expected_expiration,
1862         }
1863     );
1864
1865     # We want to charge 2 days every 2 days, without grace
1866     # With 5 days of overdue: (5 * 2) / 2
1867     $rule->suspension_chargeperiod(2)->store;
1868     $expected_expiration = dt_from_string->add( days => floor( 5 * 2 ) / 2 );
1869     test_debarment_on_checkout(
1870         {
1871             item            => $item_1,
1872             library         => $library,
1873             patron          => $patron,
1874             due_date        => $five_days_ago,
1875             expiration_date => $expected_expiration,
1876         }
1877     );
1878
1879     # We want to charge 2 days every 3 days, with 1 day of grace
1880     # With 5 days of overdue: ((5-1) / 3 ) * 2
1881     $rule->suspension_chargeperiod(3)->store;
1882     $rule->firstremind(1)->store;
1883     $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 1 ) / 3 ) * 2 ) );
1884     test_debarment_on_checkout(
1885         {
1886             item            => $item_1,
1887             library         => $library,
1888             patron          => $patron,
1889             due_date        => $five_days_ago,
1890             expiration_date => $expected_expiration,
1891         }
1892     );
1893
1894     # Use finesCalendar to know if holiday must be skipped to calculate the due date
1895     # We want to charge 2 days every days, with 0 day of grace (to not burn brains)
1896     $rule->finedays(2)->store;
1897     $rule->suspension_chargeperiod(1)->store;
1898     $rule->firstremind(0)->store;
1899     t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
1900
1901     # Adding a holiday 2 days ago
1902     my $calendar = C4::Calendar->new(branchcode => $library->{branchcode});
1903     my $two_days_ago = dt_from_string->subtract( days => 2 );
1904     $calendar->insert_single_holiday(
1905         day             => $two_days_ago->day,
1906         month           => $two_days_ago->month,
1907         year            => $two_days_ago->year,
1908         title           => 'holidayTest-2d',
1909         description     => 'holidayDesc 2 days ago'
1910     );
1911     # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
1912     $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
1913     test_debarment_on_checkout(
1914         {
1915             item            => $item_1,
1916             library         => $library,
1917             patron          => $patron,
1918             due_date        => $five_days_ago,
1919             expiration_date => $expected_expiration,
1920         }
1921     );
1922
1923     # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
1924     my $two_days_ahead = dt_from_string->add( days => 2 );
1925     $calendar->insert_single_holiday(
1926         day             => $two_days_ahead->day,
1927         month           => $two_days_ahead->month,
1928         year            => $two_days_ahead->year,
1929         title           => 'holidayTest+2d',
1930         description     => 'holidayDesc 2 days ahead'
1931     );
1932
1933     # Same as above, but we should skip D+2
1934     $expected_expiration = dt_from_string->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
1935     test_debarment_on_checkout(
1936         {
1937             item            => $item_1,
1938             library         => $library,
1939             patron          => $patron,
1940             due_date        => $five_days_ago,
1941             expiration_date => $expected_expiration,
1942         }
1943     );
1944
1945     # Adding another holiday, day of expiration date
1946     my $expected_expiration_dt = dt_from_string($expected_expiration);
1947     $calendar->insert_single_holiday(
1948         day             => $expected_expiration_dt->day,
1949         month           => $expected_expiration_dt->month,
1950         year            => $expected_expiration_dt->year,
1951         title           => 'holidayTest_exp',
1952         description     => 'holidayDesc on expiration date'
1953     );
1954     # Expiration date will be the day after
1955     test_debarment_on_checkout(
1956         {
1957             item            => $item_1,
1958             library         => $library,
1959             patron          => $patron,
1960             due_date        => $five_days_ago,
1961             expiration_date => $expected_expiration_dt->clone->add( days => 1 ),
1962         }
1963     );
1964
1965     test_debarment_on_checkout(
1966         {
1967             item            => $item_1,
1968             library         => $library,
1969             patron          => $patron,
1970             return_date     => dt_from_string->add(days => 5),
1971             expiration_date => dt_from_string->add(days => 5 + (5 * 2 - 1) ),
1972         }
1973     );
1974 };
1975
1976 subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
1977     plan tests => 2;
1978
1979     my $library = $builder->build( { source => 'Branch' } );
1980     my $patron1 = $builder->build_object(
1981         {
1982             class => 'Koha::Patrons',
1983             value  => {
1984                 branchcode => $library->{branchcode},
1985                 firstname => "Happy",
1986                 surname => "Gilmore",
1987             }
1988         }
1989     );
1990     my $patron2 = $builder->build_object(
1991         {
1992             class => 'Koha::Patrons',
1993             value  => {
1994                 branchcode => $library->{branchcode},
1995                 firstname => "Billy",
1996                 surname => "Madison",
1997             }
1998         }
1999     );
2000
2001     C4::Context->_new_userenv('xxx');
2002     C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Random Library', '', '', '');
2003
2004     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2005     my $biblionumber = $biblioitem->{biblionumber};
2006     my $item = $builder->build(
2007         {   source => 'Item',
2008             value  => {
2009                 homebranch    => $library->{branchcode},
2010                 holdingbranch => $library->{branchcode},
2011                 notforloan    => 0,
2012                 itemlost      => 0,
2013                 withdrawn     => 0,
2014                 biblionumber  => $biblionumber,
2015             }
2016         }
2017     );
2018
2019     my ( $error, $question, $alerts );
2020     my $issue = AddIssue( $patron1->unblessed, $item->{barcode} );
2021
2022     t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
2023     ( $error, $question, $alerts ) = CanBookBeIssued( $patron2, $item->{barcode} );
2024     is( $question->{ISSUED_TO_ANOTHER}, 1, 'ISSUED_TO_ANOTHER question flag should be set if AutoReturnCheckedOutItems is disabled and item is checked out to another' );
2025
2026     t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 1);
2027     ( $error, $question, $alerts ) = CanBookBeIssued( $patron2, $item->{barcode} );
2028     is( $alerts->{RETURNED_FROM_ANOTHER}->{patron}->borrowernumber, $patron1->borrowernumber, 'RETURNED_FROM_ANOTHER alert flag should be set if AutoReturnCheckedOutItems is enabled and item is checked out to another' );
2029
2030     t::lib::Mocks::mock_preference('AutoReturnCheckedOutItems', 0);
2031 };
2032
2033
2034 subtest 'AddReturn | is_overdue' => sub {
2035     plan tests => 5;
2036
2037     t::lib::Mocks::mock_preference('CalculateFinesOnReturn', 1);
2038     t::lib::Mocks::mock_preference('finesMode', 'production');
2039     t::lib::Mocks::mock_preference('MaxFine', '100');
2040
2041     my $library = $builder->build( { source => 'Branch' } );
2042     my $patron  = $builder->build( { source => 'Borrower', value => { categorycode => $patron_category->{categorycode} } } );
2043     my $manager = $builder->build_object({ class => "Koha::Patrons" });
2044     t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $manager->branchcode });
2045
2046     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2047     my $item_type          = $builder->build_object(
2048         {   class => 'Koha::ItemTypes',
2049             value => {
2050                 notforloan         => undef,
2051                 rentalcharge       => 0,
2052                 defaultreplacecost => undef,
2053                 processfee         => 0,
2054                 rentalcharge_daily => 0,
2055             }
2056         }
2057     );
2058     my $item = $builder->build(
2059         {
2060             source => 'Item',
2061             value  => {
2062                 homebranch    => $library->{branchcode},
2063                 holdingbranch => $library->{branchcode},
2064                 notforloan    => 0,
2065                 itemlost      => 0,
2066                 withdrawn     => 0,
2067                 biblionumber  => $biblioitem->{biblionumber},
2068                 replacementprice => 7,
2069                 itype         => $item_type->itemtype
2070             }
2071         }
2072     );
2073
2074     Koha::IssuingRules->search->delete;
2075     my $rule = Koha::IssuingRule->new(
2076         {
2077             categorycode => '*',
2078             itemtype     => '*',
2079             branchcode   => '*',
2080             issuelength  => 6,
2081             lengthunit   => 'days',
2082             fine         => 1, # Charge 1 every day of overdue
2083             chargeperiod => 1,
2084         }
2085     );
2086     $rule->store();
2087
2088     my $now   = dt_from_string;
2089     my $one_day_ago   = dt_from_string->subtract( days => 1 );
2090     my $five_days_ago = dt_from_string->subtract( days => 5 );
2091     my $ten_days_ago  = dt_from_string->subtract( days => 10 );
2092     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
2093
2094     # No return date specified, today will be used => 10 days overdue charged
2095     AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
2096     AddReturn( $item->{barcode}, $library->{branchcode} );
2097     is( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
2098     Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2099
2100     # specify return date 5 days before => no overdue charged
2101     AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
2102     AddReturn( $item->{barcode}, $library->{branchcode}, undef, $ten_days_ago );
2103     is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
2104     Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2105
2106     # specify return date 5 days later => 5 days overdue charged
2107     AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
2108     AddReturn( $item->{barcode}, $library->{branchcode}, undef, $five_days_ago );
2109     is( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
2110     Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2111
2112     # specify return date 5 days later, specify exemptfine => no overdue charge
2113     AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
2114     AddReturn( $item->{barcode}, $library->{branchcode}, 1, $five_days_ago );
2115     is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
2116     Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
2117
2118     subtest 'bug 22877' => sub {
2119
2120         plan tests => 3;
2121
2122         my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago );    # date due was 10d ago
2123
2124         # Fake fines cronjob on this checkout
2125         my ($fine) =
2126           CalcFine( $item, $patron->categorycode, $library->{branchcode},
2127             $ten_days_ago, $now );
2128         UpdateFine(
2129             {
2130                 issue_id       => $issue->issue_id,
2131                 itemnumber     => $item->{itemnumber},
2132                 borrowernumber => $patron->borrowernumber,
2133                 amount         => $fine,
2134                 due            => output_pref($ten_days_ago)
2135             }
2136         );
2137         is( int( $patron->account->balance() ),
2138             10, "Overdue fine of 10 days overdue" );
2139
2140         # Fake longoverdue with charge and not marking returned
2141         LostItem( $item->{itemnumber}, 'cronjob', 0 );
2142         is( int( $patron->account->balance() ),
2143             17, "Lost fine of 7 plus 10 days overdue" );
2144
2145         # Now we return it today
2146         AddReturn( $item->{barcode}, $library->{branchcode} );
2147         is( int( $patron->account->balance() ),
2148             17, "Should have a single 10 days overdue fine and lost charge" );
2149       }
2150 };
2151
2152 subtest '_FixAccountForLostAndReturned' => sub {
2153
2154     plan tests => 5;
2155
2156     t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
2157     t::lib::Mocks::mock_preference( 'WhenLostForgiveFine',          0 );
2158
2159     my $processfee_amount  = 20;
2160     my $replacement_amount = 99.00;
2161     my $item_type          = $builder->build_object(
2162         {   class => 'Koha::ItemTypes',
2163             value => {
2164                 notforloan         => undef,
2165                 rentalcharge       => 0,
2166                 defaultreplacecost => undef,
2167                 processfee         => $processfee_amount,
2168                 rentalcharge_daily => 0,
2169             }
2170         }
2171     );
2172     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
2173
2174     my $biblio = $builder->build_sample_biblio({ author => 'Hall, Daria' });
2175
2176     subtest 'Full write-off tests' => sub {
2177
2178         plan tests => 10;
2179
2180         my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2181         my $manager = $builder->build_object({ class => "Koha::Patrons" });
2182         t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
2183
2184         my $item = $builder->build_sample_item(
2185             {
2186                 biblionumber     => $biblio->biblionumber,
2187                 library          => $library->branchcode,
2188                 replacementprice => $replacement_amount,
2189                 itype            => $item_type->itemtype,
2190             }
2191         );
2192
2193         AddIssue( $patron->unblessed, $item->barcode );
2194
2195         # Simulate item marked as lost
2196         ModItem( { itemlost => 3 }, $biblio->biblionumber, $item->itemnumber );
2197         LostItem( $item->itemnumber, 1 );
2198
2199         my $processing_fee_lines = Koha::Account::Lines->search(
2200             { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2201         is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2202         my $processing_fee_line = $processing_fee_lines->next;
2203         is( $processing_fee_line->amount + 0,
2204             $processfee_amount, 'The right PF amount is generated' );
2205         is( $processing_fee_line->amountoutstanding + 0,
2206             $processfee_amount, 'The right PF amountoutstanding is generated' );
2207
2208         my $lost_fee_lines = Koha::Account::Lines->search(
2209             { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2210         is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2211         my $lost_fee_line = $lost_fee_lines->next;
2212         is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2213         is( $lost_fee_line->amountoutstanding + 0,
2214             $replacement_amount, 'The right L amountoutstanding is generated' );
2215
2216         my $account = $patron->account;
2217         my $debts   = $account->outstanding_debits;
2218
2219         # Write off the debt
2220         my $credit = $account->add_credit(
2221             {   amount => $account->balance,
2222                 type   => 'writeoff',
2223                 interface => 'test',
2224             }
2225         );
2226         $credit->apply( { debits => $debts, offset_type => 'Writeoff' } );
2227
2228         my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2229         is( $credit_return_id, undef, 'No CR account line added' );
2230
2231         $lost_fee_line->discard_changes; # reload from DB
2232         is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2233         is( $lost_fee_line->accounttype,
2234             'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2235
2236         is( $patron->account->balance, -0, 'The patron balance is 0, everything was written off' );
2237     };
2238
2239     subtest 'Full payment tests' => sub {
2240
2241         plan tests => 12;
2242
2243         my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2244
2245         my $item = $builder->build_sample_item(
2246             {
2247                 biblionumber     => $biblio->biblionumber,
2248                 library          => $library->branchcode,
2249                 replacementprice => $replacement_amount,
2250                 itype            => $item_type->itemtype
2251             }
2252         );
2253
2254         AddIssue( $patron->unblessed, $item->barcode );
2255
2256         # Simulate item marked as lost
2257         ModItem( { itemlost => 1 }, $biblio->biblionumber, $item->itemnumber );
2258         LostItem( $item->itemnumber, 1 );
2259
2260         my $processing_fee_lines = Koha::Account::Lines->search(
2261             { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2262         is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2263         my $processing_fee_line = $processing_fee_lines->next;
2264         is( $processing_fee_line->amount + 0,
2265             $processfee_amount, 'The right PF amount is generated' );
2266         is( $processing_fee_line->amountoutstanding + 0,
2267             $processfee_amount, 'The right PF amountoutstanding is generated' );
2268
2269         my $lost_fee_lines = Koha::Account::Lines->search(
2270             { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2271         is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2272         my $lost_fee_line = $lost_fee_lines->next;
2273         is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2274         is( $lost_fee_line->amountoutstanding + 0,
2275             $replacement_amount, 'The right L amountountstanding is generated' );
2276
2277         my $account = $patron->account;
2278         my $debts   = $account->outstanding_debits;
2279
2280         # Write off the debt
2281         my $credit = $account->add_credit(
2282             {   amount => $account->balance,
2283                 type   => 'payment',
2284                 interface => 'test',
2285             }
2286         );
2287         $credit->apply( { debits => $debts, offset_type => 'Payment' } );
2288
2289         my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2290         my $credit_return = Koha::Account::Lines->find($credit_return_id);
2291
2292         is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2293         is( $credit_return->amount + 0,
2294             -99.00, 'The account line of type CR has an amount of -99' );
2295         is( $credit_return->amountoutstanding + 0,
2296             -99.00, 'The account line of type CR has an amountoutstanding of -99' );
2297
2298         $lost_fee_line->discard_changes;
2299         is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2300         is( $lost_fee_line->accounttype,
2301             'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2302
2303         is( $patron->account->balance,
2304             -99, 'The patron balance is -99, a credit that equals the lost fee payment' );
2305     };
2306
2307     subtest 'Test without payment or write off' => sub {
2308
2309         plan tests => 12;
2310
2311         my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2312
2313         my $item = $builder->build_sample_item(
2314             {
2315                 biblionumber     => $biblio->biblionumber,
2316                 library          => $library->branchcode,
2317                 replacementprice => 23.00,
2318                 replacementprice => $replacement_amount,
2319                 itype            => $item_type->itemtype
2320             }
2321         );
2322
2323         AddIssue( $patron->unblessed, $item->barcode );
2324
2325         # Simulate item marked as lost
2326         ModItem( { itemlost => 3 }, $biblio->biblionumber, $item->itemnumber );
2327         LostItem( $item->itemnumber, 1 );
2328
2329         my $processing_fee_lines = Koha::Account::Lines->search(
2330             { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2331         is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2332         my $processing_fee_line = $processing_fee_lines->next;
2333         is( $processing_fee_line->amount + 0,
2334             $processfee_amount, 'The right PF amount is generated' );
2335         is( $processing_fee_line->amountoutstanding + 0,
2336             $processfee_amount, 'The right PF amountoutstanding is generated' );
2337
2338         my $lost_fee_lines = Koha::Account::Lines->search(
2339             { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2340         is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2341         my $lost_fee_line = $lost_fee_lines->next;
2342         is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2343         is( $lost_fee_line->amountoutstanding + 0,
2344             $replacement_amount, 'The right L amountountstanding is generated' );
2345
2346         my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2347         my $credit_return = Koha::Account::Lines->find($credit_return_id);
2348
2349         is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2350         is( $credit_return->amount + 0, -99.00, 'The account line of type CR has an amount of -99' );
2351         is( $credit_return->amountoutstanding + 0, 0, 'The account line of type CR has an amountoutstanding of 0' );
2352
2353         $lost_fee_line->discard_changes;
2354         is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2355         is( $lost_fee_line->accounttype, 'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2356
2357         is( $patron->account->balance, 20, 'The patron balance is 20, still owes the processing fee' );
2358     };
2359
2360     subtest 'Test with partial payement and write off, and remaining debt' => sub {
2361
2362         plan tests => 15;
2363
2364         my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2365         my $item = $builder->build_sample_item(
2366             {
2367                 biblionumber     => $biblio->biblionumber,
2368                 library          => $library->branchcode,
2369                 replacementprice => $replacement_amount,
2370                 itype            => $item_type->itemtype
2371             }
2372         );
2373
2374         AddIssue( $patron->unblessed, $item->barcode );
2375
2376         # Simulate item marked as lost
2377         ModItem( { itemlost => 1 }, $biblio->biblionumber, $item->itemnumber );
2378         LostItem( $item->itemnumber, 1 );
2379
2380         my $processing_fee_lines = Koha::Account::Lines->search(
2381             { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'PF' } );
2382         is( $processing_fee_lines->count, 1, 'Only one processing fee produced' );
2383         my $processing_fee_line = $processing_fee_lines->next;
2384         is( $processing_fee_line->amount + 0,
2385             $processfee_amount, 'The right PF amount is generated' );
2386         is( $processing_fee_line->amountoutstanding + 0,
2387             $processfee_amount, 'The right PF amountoutstanding is generated' );
2388
2389         my $lost_fee_lines = Koha::Account::Lines->search(
2390             { borrowernumber => $patron->id, itemnumber => $item->itemnumber, accounttype => 'L' } );
2391         is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2392         my $lost_fee_line = $lost_fee_lines->next;
2393         is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2394         is( $lost_fee_line->amountoutstanding + 0,
2395             $replacement_amount, 'The right L amountountstanding is generated' );
2396
2397         my $account = $patron->account;
2398         is( $account->balance, $processfee_amount + $replacement_amount, 'Balance is PF + L' );
2399
2400         # Partially pay fee
2401         my $payment_amount = 27;
2402         my $payment        = $account->add_credit(
2403             {   amount => $payment_amount,
2404                 type   => 'payment',
2405                 interface => 'test',
2406             }
2407         );
2408
2409         $payment->apply( { debits => $lost_fee_lines->reset, offset_type => 'Payment' } );
2410
2411         # Partially write off fee
2412         my $write_off_amount = 25;
2413         my $write_off        = $account->add_credit(
2414             {   amount => $write_off_amount,
2415                 type   => 'writeoff',
2416                 interface => 'test',
2417             }
2418         );
2419         $write_off->apply( { debits => $lost_fee_lines->reset, offset_type => 'Writeoff' } );
2420
2421         is( $account->balance,
2422             $processfee_amount + $replacement_amount - $payment_amount - $write_off_amount,
2423             'Payment and write off applied'
2424         );
2425
2426         # Store the amountoutstanding value
2427         $lost_fee_line->discard_changes;
2428         my $outstanding = $lost_fee_line->amountoutstanding;
2429
2430         my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item->itemnumber, $patron->id );
2431         my $credit_return = Koha::Account::Lines->find($credit_return_id);
2432
2433         is( $account->balance, $processfee_amount - $payment_amount, 'Balance is PF - payment (CR)' );
2434
2435         $lost_fee_line->discard_changes;
2436         is( $lost_fee_line->amountoutstanding + 0, 0, 'Lost fee has no outstanding amount' );
2437         is( $lost_fee_line->accounttype,
2438             'LR', 'Lost fee now has account type of LR ( Lost Returned )' );
2439
2440         is( $credit_return->accounttype, 'CR', 'An account line of type CR is added' );
2441         is( $credit_return->amount + 0,
2442             ($payment_amount + $outstanding ) * -1,
2443             'The account line of type CR has an amount equal to the payment + outstanding'
2444         );
2445         is( $credit_return->amountoutstanding + 0,
2446             $payment_amount * -1,
2447             'The account line of type CR has an amountoutstanding equal to the payment'
2448         );
2449
2450         is( $account->balance,
2451             $processfee_amount - $payment_amount,
2452             'The patron balance is the difference between the PF and the credit'
2453         );
2454     };
2455
2456     subtest 'Partial payement, existing debits and AccountAutoReconcile' => sub {
2457
2458         plan tests => 8;
2459
2460         my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
2461         my $barcode = 'KD123456793';
2462         my $replacement_amount = 100;
2463         my $processfee_amount  = 20;
2464
2465         my $item_type          = $builder->build_object(
2466             {   class => 'Koha::ItemTypes',
2467                 value => {
2468                     notforloan         => undef,
2469                     rentalcharge       => 0,
2470                     defaultreplacecost => undef,
2471                     processfee         => 0,
2472                     rentalcharge_daily => 0,
2473                 }
2474             }
2475         );
2476         my ( undef, undef, $item_id ) = AddItem(
2477             {   homebranch       => $library->branchcode,
2478                 holdingbranch    => $library->branchcode,
2479                 barcode          => $barcode,
2480                 replacementprice => $replacement_amount,
2481                 itype            => $item_type->itemtype
2482             },
2483             $biblio->biblionumber
2484         );
2485
2486         AddIssue( $patron->unblessed, $barcode );
2487
2488         # Simulate item marked as lost
2489         ModItem( { itemlost => 1 }, $biblio->biblionumber, $item_id );
2490         LostItem( $item_id, 1 );
2491
2492         my $lost_fee_lines = Koha::Account::Lines->search(
2493             { borrowernumber => $patron->id, itemnumber => $item_id, accounttype => 'L' } );
2494         is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
2495         my $lost_fee_line = $lost_fee_lines->next;
2496         is( $lost_fee_line->amount + 0, $replacement_amount, 'The right L amount is generated' );
2497         is( $lost_fee_line->amountoutstanding + 0,
2498             $replacement_amount, 'The right L amountountstanding is generated' );
2499
2500         my $account = $patron->account;
2501         is( $account->balance, $replacement_amount, 'Balance is L' );
2502
2503         # Partially pay fee
2504         my $payment_amount = 27;
2505         my $payment        = $account->add_credit(
2506             {   amount => $payment_amount,
2507                 type   => 'payment',
2508                 interface => 'test',
2509             }
2510         );
2511         $payment->apply({ debits => $lost_fee_lines->reset, offset_type => 'Payment' });
2512
2513         is( $account->balance,
2514             $replacement_amount - $payment_amount,
2515             'Payment applied'
2516         );
2517
2518         my $manual_debit_amount = 80;
2519         $account->add_debit( { amount => $manual_debit_amount, type => 'overdue', interface =>'test' } );
2520
2521         is( $account->balance, $manual_debit_amount + $replacement_amount - $payment_amount, 'Manual debit applied' );
2522
2523         t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
2524
2525         my $credit_return_id = C4::Circulation::_FixAccountForLostAndReturned( $item_id, $patron->id );
2526         my $credit_return = Koha::Account::Lines->find($credit_return_id);
2527
2528         is( $account->balance, $manual_debit_amount - $payment_amount, 'Balance is PF - payment (CR)' );
2529
2530         my $manual_debit = Koha::Account::Lines->search({ borrowernumber => $patron->id, accounttype => 'OVERDUE', status => 'UNRETURNED' })->next;
2531         is( $manual_debit->amountoutstanding + 0, $manual_debit_amount - $payment_amount, 'reconcile_balance was called' );
2532     };
2533 };
2534
2535 subtest '_FixOverduesOnReturn' => sub {
2536     plan tests => 9;
2537
2538     my $manager = $builder->build_object({ class => "Koha::Patrons" });
2539     t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $manager->branchcode });
2540
2541     my $biblio = $builder->build_sample_biblio({ author => 'Hall, Kylie' });
2542
2543     my $branchcode  = $library2->{branchcode};
2544
2545     my $item = $builder->build_sample_item(
2546         {
2547             biblionumber     => $biblio->biblionumber,
2548             library          => $branchcode,
2549             replacementprice => 99.00,
2550             itype            => $itemtype,
2551         }
2552     );
2553
2554     my $patron = $builder->build( { source => 'Borrower' } );
2555
2556     ## Start with basic call, should just close out the open fine
2557     my $accountline = Koha::Account::Line->new(
2558         {
2559             borrowernumber => $patron->{borrowernumber},
2560             accounttype    => 'OVERDUE',
2561             status         => 'UNRETURNED',
2562             itemnumber     => $item->itemnumber,
2563             amount => 99.00,
2564             amountoutstanding => 99.00,
2565             interface => 'test',
2566         }
2567     )->store();
2568
2569     C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber );
2570
2571     $accountline->_result()->discard_changes();
2572
2573     is( $accountline->amountoutstanding, '99.000000', 'Fine has the same amount outstanding as previously' );
2574     is( $accountline->status, 'RETURNED', 'Open fine ( account type OVERDUE ) has been closed out ( status RETURNED )');
2575
2576     ## Run again, with exemptfine enabled
2577     $accountline->set(
2578         {
2579             accounttype    => 'OVERDUE',
2580             status         => 'UNRETURNED',
2581             amountoutstanding => 99.00,
2582         }
2583     )->store();
2584
2585     C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, 1 );
2586
2587     $accountline->_result()->discard_changes();
2588     my $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Forgiven' })->next();
2589
2590     is( $accountline->amountoutstanding + 0, 0, 'Fine has been reduced to 0' );
2591     is( $accountline->status, 'FORGIVEN', 'Open fine ( account type OVERDUE ) has been set to fine forgiven ( status FORGIVEN )');
2592     is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via forgiveness" );
2593     is( $offset->amount, '-99.000000', "Amount of offset is correct" );
2594     my $credit = $offset->credit;
2595     is( ref $credit, "Koha::Account::Line", "Found matching credit for fine forgiveness" );
2596     is( $credit->amount, '-99.000000', "Credit amount is set correctly" );
2597     is( $credit->amountoutstanding + 0, 0, "Credit amountoutstanding is correctly set to 0" );
2598 };
2599
2600 subtest 'Set waiting flag' => sub {
2601     plan tests => 4;
2602
2603     my $library_1 = $builder->build( { source => 'Branch' } );
2604     my $patron_1  = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2605     my $library_2 = $builder->build( { source => 'Branch' } );
2606     my $patron_2  = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2607
2608     my $biblio = $builder->build( { source => 'Biblio' } );
2609     my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2610
2611     my $item = $builder->build(
2612         {
2613             source => 'Item',
2614             value  => {
2615                 homebranch    => $library_1->{branchcode},
2616                 holdingbranch => $library_1->{branchcode},
2617                 notforloan    => 0,
2618                 itemlost      => 0,
2619                 withdrawn     => 0,
2620                 biblionumber  => $biblioitem->{biblionumber},
2621             }
2622         }
2623     );
2624
2625     set_userenv( $library_2 );
2626     my $reserve_id = AddReserve(
2627         $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber},
2628         '', 1, undef, undef, '', undef, $item->{itemnumber},
2629     );
2630
2631     set_userenv( $library_1 );
2632     my $do_transfer = 1;
2633     my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2634     ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2635     my $hold = Koha::Holds->find( $reserve_id );
2636     is( $hold->found, 'T', 'Hold is in transit' );
2637
2638     my ( $status ) = CheckReserves($item->{itemnumber});
2639     is( $status, 'Reserved', 'Hold is not waiting yet');
2640
2641     set_userenv( $library_2 );
2642     $do_transfer = 0;
2643     AddReturn( $item->{barcode}, $library_2->{branchcode} );
2644     ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2645     $hold = Koha::Holds->find( $reserve_id );
2646     is( $hold->found, 'W', 'Hold is waiting' );
2647     ( $status ) = CheckReserves($item->{itemnumber});
2648     is( $status, 'Waiting', 'Now the hold is waiting');
2649 };
2650
2651 subtest 'Cancel transfers on lost items' => sub {
2652     plan tests => 5;
2653     my $library_1 = $builder->build( { source => 'Branch' } );
2654     my $patron_1 = $builder->build( { source => 'Borrower', value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2655     my $library_2 = $builder->build( { source => 'Branch' } );
2656     my $patron_2  = $builder->build( { source => 'Borrower', value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} } } );
2657     my $biblio = $builder->build( { source => 'Biblio' } );
2658     my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } );
2659     my $item = $builder->build(
2660         {
2661             source => 'Item',
2662             value => {
2663                 homebranch => $library_1->{branchcode},
2664                 holdingbranch => $library_1->{branchcode},
2665                 notforloan => 0,
2666                 itemlost => 0,
2667                 withdrawn => 0,
2668                 biblionumber => $biblioitem->{biblionumber},
2669             }
2670         }
2671     );
2672
2673     set_userenv( $library_2 );
2674     my $reserve_id = AddReserve(
2675         $library_2->{branchcode}, $patron_2->{borrowernumber}, $biblioitem->{biblionumber}, '', 1, undef, undef, '', undef, $item->{itemnumber},
2676     );
2677
2678     #Return book and add transfer
2679     set_userenv( $library_1 );
2680     my $do_transfer = 1;
2681     my ( $res, $rr ) = AddReturn( $item->{barcode}, $library_1->{branchcode} );
2682     ModReserveAffect( $item->{itemnumber}, undef, $do_transfer, $reserve_id );
2683     C4::Circulation::transferbook( $library_2->{branchcode}, $item->{barcode} );
2684     my $hold = Koha::Holds->find( $reserve_id );
2685     is( $hold->found, 'T', 'Hold is in transit' );
2686
2687     #Check transfer exists and the items holding branch is the transfer destination branch before marking it as lost
2688     my ($datesent,$frombranch,$tobranch) = GetTransfers($item->{itemnumber});
2689     is( $tobranch, $library_2->{branchcode}, 'The transfer record exists in the branchtransfers table');
2690     my $itemcheck = Koha::Items->find($item->{itemnumber});
2691     is( $itemcheck->holdingbranch, $library_2->{branchcode}, 'Items holding branch is the transfers destination branch before it is marked as lost' );
2692
2693     #Simulate item being marked as lost and confirm the transfer is deleted and the items holding branch is the transfers source branch
2694     ModItem( { itemlost => 1 }, $biblio->{biblionumber}, $item->{itemnumber} );
2695     LostItem( $item->{itemnumber}, 'test', 1 );
2696     ($datesent,$frombranch,$tobranch) = GetTransfers($item->{itemnumber});
2697     is( $tobranch, undef, 'The transfer on the lost item has been deleted as the LostItemCancelOutstandingTransfer is enabled');
2698     $itemcheck = Koha::Items->find($item->{itemnumber});
2699     is( $itemcheck->holdingbranch, $library_1->{branchcode}, 'Lost item with cancelled hold has holding branch equallying the transfers source branch' );
2700 };
2701
2702 subtest 'CanBookBeIssued | is_overdue' => sub {
2703     plan tests => 3;
2704
2705     # Set a simple circ policy
2706     $dbh->do('DELETE FROM issuingrules');
2707     $dbh->do(
2708     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
2709                                     issuelength, lengthunit,
2710                                     renewalsallowed, renewalperiod,
2711                                     norenewalbefore, auto_renew,
2712                                     fine, chargeperiod)
2713           VALUES (?, ?, ?, ?,
2714                   ?, ?,
2715                   ?, ?,
2716                   ?, ?,
2717                   ?, ?
2718                  )
2719         },
2720         {},
2721         '*',   '*', '*', 25,
2722         14,  'days',
2723         1,     7,
2724         undef, 0,
2725         .10,   1
2726     );
2727
2728     my $five_days_go = output_pref({ dt => dt_from_string->add( days => 5 ), dateonly => 1});
2729     my $ten_days_go  = output_pref({ dt => dt_from_string->add( days => 10), dateonly => 1 });
2730     my $library = $builder->build( { source => 'Branch' } );
2731     my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
2732
2733     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2734     my $item = $builder->build(
2735         {
2736             source => 'Item',
2737             value  => {
2738                 homebranch    => $library->{branchcode},
2739                 holdingbranch => $library->{branchcode},
2740                 notforloan    => 0,
2741                 itemlost      => 0,
2742                 withdrawn     => 0,
2743                 biblionumber  => $biblioitem->{biblionumber},
2744             }
2745         }
2746     );
2747
2748     my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $five_days_go ); # date due was 10d ago
2749     my $actualissue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
2750     is( output_pref({ str => $actualissue->date_due, dateonly => 1}), $five_days_go, "First issue works");
2751     my ($issuingimpossible, $needsconfirmation) = CanBookBeIssued($patron,$item->{barcode},$ten_days_go, undef, undef, undef);
2752     is( $needsconfirmation->{RENEW_ISSUE}, 1, "This is a renewal");
2753     is( $needsconfirmation->{TOO_MANY}, undef, "Not too many, is a renewal");
2754 };
2755
2756 subtest 'ItemsDeniedRenewal preference' => sub {
2757     plan tests => 18;
2758
2759     C4::Context->set_preference('ItemsDeniedRenewal','');
2760
2761     my $idr_lib = $builder->build_object({ class => 'Koha::Libraries'});
2762     $dbh->do(
2763         q{
2764         INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, issuelength, lengthunit, renewalsallowed, renewalperiod,
2765                     norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
2766         },
2767         {},
2768         '*', $idr_lib->branchcode, '*', 25,
2769         14,  'days',
2770         10,   7,
2771         undef,  0,
2772         .10, 1
2773     );
2774
2775     my $deny_book = $builder->build_object({ class => 'Koha::Items', value => {
2776         homebranch => $idr_lib->branchcode,
2777         withdrawn => 1,
2778         itype => 'HIDE',
2779         location => 'PROC',
2780         itemcallnumber => undef,
2781         itemnotes => "",
2782         }
2783     });
2784     my $allow_book = $builder->build_object({ class => 'Koha::Items', value => {
2785         homebranch => $idr_lib->branchcode,
2786         withdrawn => 0,
2787         itype => 'NOHIDE',
2788         location => 'NOPROC'
2789         }
2790     });
2791
2792     my $idr_borrower = $builder->build_object({ class => 'Koha::Patrons', value=> {
2793         branchcode => $idr_lib->branchcode,
2794         }
2795     });
2796     my $future = dt_from_string->add( days => 1 );
2797     my $deny_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2798         returndate => undef,
2799         renewals => 0,
2800         auto_renew => 0,
2801         borrowernumber => $idr_borrower->borrowernumber,
2802         itemnumber => $deny_book->itemnumber,
2803         onsite_checkout => 0,
2804         date_due => $future,
2805         }
2806     });
2807     my $allow_issue = $builder->build_object({ class => 'Koha::Checkouts', value => {
2808         returndate => undef,
2809         renewals => 0,
2810         auto_renew => 0,
2811         borrowernumber => $idr_borrower->borrowernumber,
2812         itemnumber => $allow_book->itemnumber,
2813         onsite_checkout => 0,
2814         date_due => $future,
2815         }
2816     });
2817
2818     my $idr_rules;
2819
2820     my ( $idr_mayrenew, $idr_error ) =
2821     CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2822     is( $idr_mayrenew, 1, 'Renewal allowed when no rules' );
2823     is( $idr_error, undef, 'Renewal allowed when no rules' );
2824
2825     $idr_rules="withdrawn: [1]";
2826
2827     C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2828     ( $idr_mayrenew, $idr_error ) =
2829     CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2830     is( $idr_mayrenew, 0, 'Renewal blocked when 1 rules (withdrawn)' );
2831     is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 1 rule (withdrawn)' );
2832     ( $idr_mayrenew, $idr_error ) =
2833     CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2834     is( $idr_mayrenew, 1, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2835     is( $idr_error, undef, 'Renewal allowed when 1 rules not matched (withdrawn)' );
2836
2837     $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]";
2838
2839     C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2840     ( $idr_mayrenew, $idr_error ) =
2841     CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2842     is( $idr_mayrenew, 0, 'Renewal blocked when 2 rules matched (withdrawn, itype)' );
2843     is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 2 rules matched (withdrawn,itype)' );
2844     ( $idr_mayrenew, $idr_error ) =
2845     CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2846     is( $idr_mayrenew, 1, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2847     is( $idr_error, undef, 'Renewal allowed when 2 rules not matched (withdrawn, itype)' );
2848
2849     $idr_rules="withdrawn: [1]\nitype: [HIDE,INVISIBLE]\nlocation: [PROC]";
2850
2851     C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2852     ( $idr_mayrenew, $idr_error ) =
2853     CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2854     is( $idr_mayrenew, 0, 'Renewal blocked when 3 rules matched (withdrawn, itype, location)' );
2855     is( $idr_error, 'item_denied_renewal', 'Renewal blocked when 3 rules matched (withdrawn,itype, location)' );
2856     ( $idr_mayrenew, $idr_error ) =
2857     CanBookBeRenewed( $idr_borrower->borrowernumber, $allow_issue->itemnumber );
2858     is( $idr_mayrenew, 1, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2859     is( $idr_error, undef, 'Renewal allowed when 3 rules not matched (withdrawn, itype, location)' );
2860
2861     $idr_rules="itemcallnumber: [NULL]";
2862     C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2863     ( $idr_mayrenew, $idr_error ) =
2864     CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2865     is( $idr_mayrenew, 0, 'Renewal blocked for undef when NULL in pref' );
2866     $idr_rules="itemcallnumber: ['']";
2867     C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2868     ( $idr_mayrenew, $idr_error ) =
2869     CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2870     is( $idr_mayrenew, 1, 'Renewal not blocked for undef when "" in pref' );
2871
2872     $idr_rules="itemnotes: [NULL]";
2873     C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2874     ( $idr_mayrenew, $idr_error ) =
2875     CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2876     is( $idr_mayrenew, 1, 'Renewal not blocked for "" when NULL in pref' );
2877     $idr_rules="itemnotes: ['']";
2878     C4::Context->set_preference('ItemsDeniedRenewal',$idr_rules);
2879     ( $idr_mayrenew, $idr_error ) =
2880     CanBookBeRenewed( $idr_borrower->borrowernumber, $deny_issue->itemnumber );
2881     is( $idr_mayrenew, 0, 'Renewal blocked for empty string when "" in pref' );
2882 };
2883
2884 subtest 'CanBookBeIssued | item-level_itypes=biblio' => sub {
2885     plan tests => 2;
2886
2887     t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2888     my $library = $builder->build( { source => 'Branch' } );
2889     my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2890
2891     my $itemtype = $builder->build(
2892         {
2893             source => 'Itemtype',
2894             value  => { notforloan => undef, }
2895         }
2896     );
2897
2898     my $biblioitem = $builder->build( { source => 'Biblioitem', value => { itemtype => $itemtype->{itemtype} } } );
2899     my $item = $builder->build_object(
2900         {
2901             class => 'Koha::Items',
2902             value  => {
2903                 homebranch    => $library->{branchcode},
2904                 holdingbranch => $library->{branchcode},
2905                 notforloan    => 0,
2906                 itemlost      => 0,
2907                 withdrawn     => 0,
2908                 biblionumber  => $biblioitem->{biblionumber},
2909                 biblioitemnumber => $biblioitem->{biblioitemnumber},
2910             }
2911         }
2912     )->store;
2913
2914     my ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2915     is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2916     is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2917 };
2918
2919 subtest 'CanBookBeIssued | notforloan' => sub {
2920     plan tests => 2;
2921
2922     t::lib::Mocks::mock_preference('AllowNotForLoanOverride', 0);
2923
2924     my $library = $builder->build( { source => 'Branch' } );
2925     my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
2926
2927     my $itemtype = $builder->build(
2928         {
2929             source => 'Itemtype',
2930             value  => { notforloan => undef, }
2931         }
2932     );
2933
2934     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
2935     my $item = $builder->build_object(
2936         {
2937             class => 'Koha::Items',
2938             value  => {
2939                 homebranch    => $library->{branchcode},
2940                 holdingbranch => $library->{branchcode},
2941                 notforloan    => 0,
2942                 itemlost      => 0,
2943                 withdrawn     => 0,
2944                 itype         => $itemtype->{itemtype},
2945                 biblionumber  => $biblioitem->{biblionumber},
2946                 biblioitemnumber => $biblioitem->{biblioitemnumber},
2947             }
2948         }
2949     )->store;
2950
2951     my ( $issuingimpossible, $needsconfirmation );
2952
2953
2954     subtest 'item-level_itypes = 1' => sub {
2955         plan tests => 6;
2956
2957         t::lib::Mocks::mock_preference('item-level_itypes', 1); # item
2958         # Is for loan at item type and item level
2959         ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2960         is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
2961         is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
2962
2963         # not for loan at item type level
2964         Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
2965         ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2966         is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2967         is_deeply(
2968             $issuingimpossible,
2969             { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
2970             'Item can not be issued, not for loan at item type level'
2971         );
2972
2973         # not for loan at item level
2974         Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
2975         $item->notforloan( 1 )->store;
2976         ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
2977         is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
2978         is_deeply(
2979             $issuingimpossible,
2980             { NOT_FOR_LOAN => 1, item_notforloan => 1 },
2981             'Item can not be issued, not for loan at item type level'
2982         );
2983     };
2984
2985     subtest 'item-level_itypes = 0' => sub {
2986         plan tests => 6;
2987
2988         t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
2989
2990         # We set another itemtype for biblioitem
2991         my $itemtype = $builder->build(
2992             {
2993                 source => 'Itemtype',
2994                 value  => { notforloan => undef, }
2995             }
2996         );
2997
2998         # for loan at item type and item level
2999         $item->notforloan(0)->store;
3000         $item->biblioitem->itemtype($itemtype->{itemtype})->store;
3001         ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3002         is_deeply( $needsconfirmation, {}, 'Item can be issued to this patron' );
3003         is_deeply( $issuingimpossible, {}, 'Item can be issued to this patron' );
3004
3005         # not for loan at item type level
3006         Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(1)->store;
3007         ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3008         is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3009         is_deeply(
3010             $issuingimpossible,
3011             { NOT_FOR_LOAN => 1, itemtype_notforloan => $itemtype->{itemtype} },
3012             'Item can not be issued, not for loan at item type level'
3013         );
3014
3015         # not for loan at item level
3016         Koha::ItemTypes->find( $itemtype->{itemtype} )->notforloan(undef)->store;
3017         $item->notforloan( 1 )->store;
3018         ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, undef, undef, undef, undef );
3019         is_deeply( $needsconfirmation, {}, 'No confirmation needed, AllowNotForLoanOverride=0' );
3020         is_deeply(
3021             $issuingimpossible,
3022             { NOT_FOR_LOAN => 1, item_notforloan => 1 },
3023             'Item can not be issued, not for loan at item type level'
3024         );
3025     };
3026
3027     # TODO test with AllowNotForLoanOverride = 1
3028 };
3029
3030 subtest 'AddReturn should clear items.onloan for unissued items' => sub {
3031     plan tests => 1;
3032
3033     t::lib::Mocks::mock_preference( "AllowReturnToBranch", 'anywhere' );
3034     my $item = $builder->build_object({ class => 'Koha::Items', value  => { onloan => '2018-01-01' }});
3035     AddReturn( $item->barcode, $item->homebranch );
3036     $item->discard_changes; # refresh
3037     is( $item->onloan, undef, 'AddReturn did clear items.onloan' );
3038 };
3039
3040
3041 subtest 'AddRenewal and AddIssuingCharge tests' => sub {
3042
3043     plan tests => 13;
3044
3045
3046     t::lib::Mocks::mock_preference('item-level_itypes', 1);
3047
3048     my $issuing_charges = 15;
3049     my $title   = 'A title';
3050     my $author  = 'Author, An';
3051     my $barcode = 'WHATARETHEODDS';
3052
3053     my $circ = Test::MockModule->new('C4::Circulation');
3054     $circ->mock(
3055         'GetIssuingCharges',
3056         sub {
3057             return $issuing_charges;
3058         }
3059     );
3060
3061     my $library  = $builder->build_object({ class => 'Koha::Libraries' });
3062     my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes', value => { rentalcharge_daily => 0.00 }});
3063     my $patron   = $builder->build_object({
3064         class => 'Koha::Patrons',
3065         value => { branchcode => $library->id }
3066     });
3067
3068     my $biblio = $builder->build_sample_biblio({ title=> $title, author => $author });
3069     my ( undef, undef, $item_id ) = AddItem(
3070         {
3071             homebranch       => $library->id,
3072             holdingbranch    => $library->id,
3073             barcode          => $barcode,
3074             replacementprice => 23.00,
3075             itype            => $itemtype->id
3076         },
3077         $biblio->biblionumber
3078     );
3079     my $item = Koha::Items->find( $item_id );
3080
3081     my $context = Test::MockModule->new('C4::Context');
3082     $context->mock( userenv => { branch => $library->id } );
3083
3084     # Check the item out
3085     AddIssue( $patron->unblessed, $item->barcode );
3086     t::lib::Mocks::mock_preference( 'RenewalLog', 0 );
3087     my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
3088     my %params_renewal = (
3089         timestamp => { -like => $date . "%" },
3090         module => "CIRCULATION",
3091         action => "RENEWAL",
3092     );
3093     my $old_log_size = Koha::ActionLogs->count( \%params_renewal );;
3094     AddRenewal( $patron->id, $item->id, $library->id );
3095     my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
3096     is( $new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog' );
3097
3098     my $checkouts = $patron->checkouts;
3099     # The following will fail if run on 00:00:00
3100     unlike ( $checkouts->next->lastreneweddate, qr/00:00:00/, 'AddRenewal should set the renewal date with the time part');
3101
3102     t::lib::Mocks::mock_preference( 'RenewalLog', 1 );
3103     $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
3104     $old_log_size = Koha::ActionLogs->count( \%params_renewal );
3105     AddRenewal( $patron->id, $item->id, $library->id );
3106     $new_log_size = Koha::ActionLogs->count( \%params_renewal );
3107     is( $new_log_size, $old_log_size + 1, 'renew log successfully added' );
3108
3109     my $lines = Koha::Account::Lines->search({
3110         borrowernumber => $patron->id,
3111         itemnumber     => $item->id
3112     });
3113
3114     is( $lines->count, 3 );
3115
3116     my $line = $lines->next;
3117     is( $line->accounttype, 'Rent',       'The issuing charge generates an accountline' );
3118     is( $line->branchcode,  $library->id, 'AddIssuingCharge correctly sets branchcode' );
3119     is( $line->description, 'Rental',     'AddIssuingCharge set a hardcoded description for the accountline' );
3120
3121     $line = $lines->next;
3122     is( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' );
3123     is( $line->branchcode,  $library->id, 'AddRenewal correctly sets branchcode' );
3124     is( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' );
3125
3126     $line = $lines->next;
3127     is( $line->accounttype, 'Rent', 'Fine on renewed item is closed out properly' );
3128     is( $line->branchcode,  $library->id, 'AddRenewal correctly sets branchcode' );
3129     is( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' );
3130
3131 };
3132
3133 subtest 'ProcessOfflinePayment() tests' => sub {
3134
3135     plan tests => 4;
3136
3137
3138     my $amount = 123;
3139
3140     my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
3141     my $library = $builder->build_object({ class => 'Koha::Libraries' });
3142     my $result  = C4::Circulation::ProcessOfflinePayment({ cardnumber => $patron->cardnumber, amount => $amount, branchcode => $library->id });
3143
3144     is( $result, 'Success.', 'The right string is returned' );
3145
3146     my $lines = $patron->account->lines;
3147     is( $lines->count, 1, 'line created correctly');
3148
3149     my $line = $lines->next;
3150     is( $line->amount+0, $amount * -1, 'amount picked from params' );
3151     is( $line->branchcode, $library->id, 'branchcode set correctly' );
3152
3153 };
3154
3155 subtest 'Incremented fee tests' => sub {
3156     plan tests => 11;
3157
3158     t::lib::Mocks::mock_preference('item-level_itypes', 1);
3159
3160     my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store;
3161
3162     my $module = new Test::MockModule('C4::Context');
3163     $module->mock('userenv', sub { { branch => $library->id } });
3164
3165     my $patron = $builder->build_object(
3166         {
3167             class => 'Koha::Patrons',
3168             value => { categorycode => $patron_category->{categorycode} }
3169         }
3170     )->store;
3171
3172     my $itemtype = $builder->build_object(
3173         {
3174             class => 'Koha::ItemTypes',
3175             value  => {
3176                 notforloan          => undef,
3177                 rentalcharge        => 0,
3178                 rentalcharge_daily => 1.000000
3179             }
3180         }
3181     )->store;
3182
3183     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
3184     my $item = $builder->build_object(
3185         {
3186             class => 'Koha::Items',
3187             value => {
3188                 homebranch       => $library->id,
3189                 holdingbranch    => $library->id,
3190                 notforloan       => 0,
3191                 itemlost         => 0,
3192                 withdrawn        => 0,
3193                 itype            => $itemtype->id,
3194                 biblionumber     => $biblioitem->{biblionumber},
3195                 biblioitemnumber => $biblioitem->{biblioitemnumber},
3196             }
3197         }
3198     )->store;
3199
3200     is( $itemtype->rentalcharge_daily, '1.000000', 'Daily rental charge stored and retreived correctly' );
3201     is( $item->effective_itemtype, $itemtype->id, "Itemtype set correctly for item");
3202
3203     my $dt_from = dt_from_string();
3204     my $dt_to = dt_from_string()->add( days => 7 );
3205     my $dt_to_renew = dt_from_string()->add( days => 13 );
3206
3207     t::lib::Mocks::mock_preference('finesCalendar', 'ignoreCalendar');
3208     my $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3209     my $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3210     is( $accountline->amount, '7.000000', "Daily rental charge calculated correctly with finesCalendar = ignoreCalendar" );
3211     $accountline->delete();
3212     AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3213     $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3214     is( $accountline->amount, '6.000000', "Daily rental charge calculated correctly with finesCalendar = ignoreCalendar, for renewal" );
3215     $accountline->delete();
3216     $issue->delete();
3217
3218     t::lib::Mocks::mock_preference('finesCalendar', 'noFinesWhenClosed');
3219     $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3220     $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3221     is( $accountline->amount, '7.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed" );
3222     $accountline->delete();
3223     AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3224     $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3225     is( $accountline->amount, '6.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed, for renewal" );
3226     $accountline->delete();
3227     $issue->delete();
3228
3229     my $calendar = C4::Calendar->new( branchcode => $library->id );
3230     $calendar->insert_week_day_holiday(
3231         weekday     => 3,
3232         title       => 'Test holiday',
3233         description => 'Test holiday'
3234     );
3235     $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
3236     $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3237     is( $accountline->amount, '6.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed Wednesdays" );
3238     $accountline->delete();
3239     AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3240     $accountline = Koha::Account::Lines->find({ itemnumber => $item->id });
3241     is( $accountline->amount, '5.000000', "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed Wednesdays, for renewal" );
3242     $accountline->delete();
3243     $issue->delete();
3244
3245     $itemtype->rentalcharge('2.000000')->store;
3246     is( $itemtype->rentalcharge, '2.000000', 'Rental charge updated and retreived correctly' );
3247     $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from);
3248     my $accountlines = Koha::Account::Lines->search({ itemnumber => $item->id });
3249     is( $accountlines->count, '2', "Fixed charge and accrued charge recorded distinctly");
3250     $accountlines->delete();
3251     AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
3252     $accountlines = Koha::Account::Lines->search({ itemnumber => $item->id });
3253     is( $accountlines->count, '2', "Fixed charge and accrued charge recorded distinctly, for renewal");
3254     $accountlines->delete();
3255     $issue->delete();
3256 };
3257
3258 subtest 'CanBookBeIssued & RentalFeesCheckoutConfirmation' => sub {
3259     plan tests => 2;
3260
3261     t::lib::Mocks::mock_preference('RentalFeesCheckoutConfirmation', 1);
3262     t::lib::Mocks::mock_preference('item-level_itypes', 1);
3263
3264     my $library =
3265       $builder->build_object( { class => 'Koha::Libraries' } )->store;
3266     my $patron = $builder->build_object(
3267         {
3268             class => 'Koha::Patrons',
3269             value => { categorycode => $patron_category->{categorycode} }
3270         }
3271     )->store;
3272
3273     my $itemtype = $builder->build_object(
3274         {
3275             class => 'Koha::ItemTypes',
3276             value => {
3277                 notforloan             => 0,
3278                 rentalcharge           => 0,
3279                 rentalcharge_daily => 0
3280             }
3281         }
3282     );
3283
3284     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
3285     my $item = $builder->build_object(
3286         {
3287             class => 'Koha::Items',
3288             value  => {
3289                 homebranch    => $library->id,
3290                 holdingbranch => $library->id,
3291                 notforloan    => 0,
3292                 itemlost      => 0,
3293                 withdrawn     => 0,
3294                 itype         => $itemtype->id,
3295                 biblionumber  => $biblioitem->{biblionumber},
3296                 biblioitemnumber => $biblioitem->{biblioitemnumber},
3297             }
3298         }
3299     )->store;
3300
3301     my ( $issuingimpossible, $needsconfirmation );
3302     my $dt_from = dt_from_string();
3303     my $dt_due = dt_from_string()->add( days => 3 );
3304
3305     $itemtype->rentalcharge('1.000000')->store;
3306     ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
3307     is_deeply( $needsconfirmation, { RENTALCHARGE => '1' }, 'Item needs rentalcharge confirmation to be issued' );
3308     $itemtype->rentalcharge('0')->store;
3309     $itemtype->rentalcharge_daily('1.000000')->store;
3310     ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
3311     is_deeply( $needsconfirmation, { RENTALCHARGE => '3' }, 'Item needs rentalcharge confirmation to be issued, increment' );
3312     $itemtype->rentalcharge_daily('0')->store;
3313 };
3314
3315 $schema->storage->txn_rollback;
3316 C4::Context->clear_syspref_cache();
3317 $cache->clear_from_cache('single_holidays');