Bug 8236: (QA followup) Tweak language, fix whitespace error
[koha.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
20 use DateTime;
21 use C4::Biblio;
22 use C4::Branch;
23 use C4::Items;
24 use C4::Members;
25 use C4::Reserves;
26 use C4::Overdues qw(UpdateFine);
27 use Koha::DateUtils;
28 use Koha::Database;
29
30 use Test::More tests => 70 ;
31
32 BEGIN {
33     use_ok('C4::Circulation');
34 }
35
36 my $dbh = C4::Context->dbh;
37 my $schema = Koha::Database->new()->schema();
38
39 # Start transaction
40 $dbh->{RaiseError} = 1;
41 $schema->storage->txn_begin();
42
43 # Start with a clean slate
44 $dbh->do('DELETE FROM issues');
45
46 my $CircControl = C4::Context->preference('CircControl');
47 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
48
49 my $item = {
50     homebranch => 'MPL',
51     holdingbranch => 'MPL'
52 };
53
54 my $borrower = {
55     branchcode => 'MPL'
56 };
57
58 # No userenv, PickupLibrary
59 C4::Context->set_preference('CircControl', 'PickupLibrary');
60 is(
61     C4::Context->preference('CircControl'),
62     'PickupLibrary',
63     'CircControl changed to PickupLibrary'
64 );
65 is(
66     C4::Circulation::_GetCircControlBranch($item, $borrower),
67     $item->{$HomeOrHoldingBranch},
68     '_GetCircControlBranch returned item branch (no userenv defined)'
69 );
70
71 # No userenv, PatronLibrary
72 C4::Context->set_preference('CircControl', 'PatronLibrary');
73 is(
74     C4::Context->preference('CircControl'),
75     'PatronLibrary',
76     'CircControl changed to PatronLibrary'
77 );
78 is(
79     C4::Circulation::_GetCircControlBranch($item, $borrower),
80     $borrower->{branchcode},
81     '_GetCircControlBranch returned borrower branch'
82 );
83
84 # No userenv, ItemHomeLibrary
85 C4::Context->set_preference('CircControl', 'ItemHomeLibrary');
86 is(
87     C4::Context->preference('CircControl'),
88     'ItemHomeLibrary',
89     'CircControl changed to ItemHomeLibrary'
90 );
91 is(
92     $item->{$HomeOrHoldingBranch},
93     C4::Circulation::_GetCircControlBranch($item, $borrower),
94     '_GetCircControlBranch returned item branch'
95 );
96
97 # Now, set a userenv
98 C4::Context->_new_userenv('xxx');
99 C4::Context->set_userenv(0,0,0,'firstname','surname', 'MPL', 'Midway Public Library', '', '', '');
100 is(C4::Context->userenv->{branch}, 'MPL', 'userenv set');
101
102 # Userenv set, PickupLibrary
103 C4::Context->set_preference('CircControl', 'PickupLibrary');
104 is(
105     C4::Context->preference('CircControl'),
106     'PickupLibrary',
107     'CircControl changed to PickupLibrary'
108 );
109 is(
110     C4::Circulation::_GetCircControlBranch($item, $borrower),
111     'MPL',
112     '_GetCircControlBranch returned current branch'
113 );
114
115 # Userenv set, PatronLibrary
116 C4::Context->set_preference('CircControl', 'PatronLibrary');
117 is(
118     C4::Context->preference('CircControl'),
119     'PatronLibrary',
120     'CircControl changed to PatronLibrary'
121 );
122 is(
123     C4::Circulation::_GetCircControlBranch($item, $borrower),
124     $borrower->{branchcode},
125     '_GetCircControlBranch returned borrower branch'
126 );
127
128 # Userenv set, ItemHomeLibrary
129 C4::Context->set_preference('CircControl', 'ItemHomeLibrary');
130 is(
131     C4::Context->preference('CircControl'),
132     'ItemHomeLibrary',
133     'CircControl changed to ItemHomeLibrary'
134 );
135 is(
136     C4::Circulation::_GetCircControlBranch($item, $borrower),
137     $item->{$HomeOrHoldingBranch},
138     '_GetCircControlBranch returned item branch'
139 );
140
141 # Reset initial configuration
142 C4::Context->set_preference('CircControl', $CircControl);
143 is(
144     C4::Context->preference('CircControl'),
145     $CircControl,
146     'CircControl reset to its initial value'
147 );
148
149 # Set a simple circ policy
150 $dbh->do('DELETE FROM issuingrules');
151 $dbh->do(
152     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
153                                 maxissueqty, issuelength, lengthunit,
154                                 renewalsallowed, renewalperiod,
155                                 norenewalbefore, auto_renew,
156                                 fine, chargeperiod)
157       VALUES (?, ?, ?, ?,
158               ?, ?, ?,
159               ?, ?,
160               ?, ?,
161               ?, ?
162              )
163     },
164     {},
165     '*', '*', '*', 25,
166     20, 14, 'days',
167     1, 7,
168     '', 0,
169     .10, 1
170 );
171
172 # Test C4::Circulation::ProcessOfflinePayment
173 my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
174 $sth->execute();
175 my ( $original_count ) = $sth->fetchrow_array();
176
177 C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', 'S', 'MPL' )");
178
179 C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' });
180
181 $sth->execute();
182 my ( $new_count ) = $sth->fetchrow_array();
183
184 ok( $new_count == $original_count  + 1, 'ProcessOfflinePayment makes payment correctly' );
185
186 C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
187 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
188 C4::Context->dbh->do("DELETE FROM accountlines");
189 {
190 # CanBookBeRenewed tests
191
192     # Generate test biblio
193     my $biblio = MARC::Record->new();
194     my $title = 'Silence in the library';
195     $biblio->append_fields(
196         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
197         MARC::Field->new('245', ' ', ' ', a => $title),
198     );
199
200     my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
201
202     my $barcode = 'R00000342';
203     my $branch = 'MPL';
204
205     my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
206         {
207             homebranch       => $branch,
208             holdingbranch    => $branch,
209             barcode          => $barcode,
210             replacementprice => 12.00
211         },
212         $biblionumber
213     );
214
215     my $barcode2 = 'R00000343';
216     my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
217         {
218             homebranch       => $branch,
219             holdingbranch    => $branch,
220             barcode          => $barcode2,
221             replacementprice => 23.00
222         },
223         $biblionumber
224     );
225
226     my $barcode3 = 'R00000346';
227     my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
228         {
229             homebranch       => $branch,
230             holdingbranch    => $branch,
231             barcode          => $barcode3,
232             replacementprice => 23.00
233         },
234         $biblionumber
235     );
236
237
238
239
240     # Create borrowers
241     my %renewing_borrower_data = (
242         firstname =>  'John',
243         surname => 'Renewal',
244         categorycode => 'S',
245         branchcode => $branch,
246     );
247
248     my %reserving_borrower_data = (
249         firstname =>  'Katrin',
250         surname => 'Reservation',
251         categorycode => 'S',
252         branchcode => $branch,
253     );
254
255     my %hold_waiting_borrower_data = (
256         firstname =>  'Kyle',
257         surname => 'Reservation',
258         categorycode => 'S',
259         branchcode => $branch,
260     );
261
262     my %restricted_borrower_data = (
263         firstname =>  'Alice',
264         surname => 'Reservation',
265         categorycode => 'S',
266         debarred => '3228-01-01',
267         branchcode => $branch,
268     );
269
270     my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
271     my $reserving_borrowernumber = AddMember(%reserving_borrower_data);
272     my $hold_waiting_borrowernumber = AddMember(%hold_waiting_borrower_data);
273     my $restricted_borrowernumber = AddMember(%restricted_borrower_data);
274
275     my $renewing_borrower = GetMember( borrowernumber => $renewing_borrowernumber );
276     my $restricted_borrower = GetMember( borrowernumber => $restricted_borrowernumber );
277
278     my $bibitems       = '';
279     my $priority       = '1';
280     my $resdate        = undef;
281     my $expdate        = undef;
282     my $notes          = '';
283     my $checkitem      = undef;
284     my $found          = undef;
285
286     my $issue = AddIssue( $renewing_borrower, $barcode);
287     my $datedue = dt_from_string( $issue->date_due() );
288     is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
289
290     my $issue2 = AddIssue( $renewing_borrower, $barcode2);
291     $datedue = dt_from_string( $issue->date_due() );
292     is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
293
294
295     my $borrowing_borrowernumber = GetItemIssue($itemnumber)->{borrowernumber};
296     is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
297
298     my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
299     is( $renewokay, 1, 'Can renew, no holds for this title or item');
300
301
302     # Biblio-level hold, renewal test
303     AddReserve(
304         $branch, $reserving_borrowernumber, $biblionumber,
305         $bibitems,  $priority, $resdate, $expdate, $notes,
306         $title, $checkitem, $found
307     );
308
309     # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
310     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
311     C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 1 );
312     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
313     is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
314     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
315     is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
316
317     # Now let's add an item level hold, we should no longer be able to renew the item
318     my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
319         {
320             borrowernumber => $hold_waiting_borrowernumber,
321             biblionumber   => $biblionumber,
322             itemnumber     => $itemnumber,
323             branchcode     => $branch,
324             priority       => 3,
325         }
326     );
327     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
328     is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
329     $hold->delete();
330
331     # 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
332     # be able to renew these items
333     $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
334         {
335             borrowernumber => $hold_waiting_borrowernumber,
336             biblionumber   => $biblionumber,
337             itemnumber     => $itemnumber3,
338             branchcode     => $branch,
339             priority       => 0,
340             found          => 'W'
341         }
342     );
343     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
344     is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
345     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
346     is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
347     C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 0 );
348
349     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
350     is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
351     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
352
353     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
354     is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
355     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
356
357     my $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber});
358     my $reserving_borrower = GetMember( borrowernumber => $reserving_borrowernumber );
359     AddIssue($reserving_borrower, $barcode3);
360     my $reserve = $dbh->selectrow_hashref(
361         'SELECT * FROM old_reserves WHERE reserve_id = ?',
362         { Slice => {} },
363         $reserveid
364     );
365     is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
366
367     # Item-level hold, renewal test
368     AddReserve(
369         $branch, $reserving_borrowernumber, $biblionumber,
370         $bibitems,  $priority, $resdate, $expdate, $notes,
371         $title, $itemnumber, $found
372     );
373
374     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
375     is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
376     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
377
378     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
379     is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
380
381     # Items can't fill hold for reasons
382     ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
383     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
384     is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
385     ModItem({ notforloan => 0, itype => '' }, $biblionumber, $itemnumber,1);
386
387     # FIXME: Add more for itemtype not for loan etc.
388
389     # Restricted users cannot renew when RestrictionBlockRenewing is enabled
390     my $barcode5 = 'R00000347';
391     my ( $item_bibnum5, $item_bibitemnum5, $itemnumber5 ) = AddItem(
392         {
393             homebranch       => $branch,
394             holdingbranch    => $branch,
395             barcode          => $barcode5,
396             replacementprice => 23.00
397         },
398         $biblionumber
399     );
400     my $datedue5 = AddIssue($restricted_borrower, $barcode5);
401     is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
402
403     C4::Context->set_preference('RestrictionBlockRenewing','1');
404     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
405     is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
406     ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $itemnumber5);
407     is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
408
409     # Users cannot renew an overdue item
410     my $barcode6 = 'R00000348';
411     my ( $item_bibnum6, $item_bibitemnum6, $itemnumber6 ) = AddItem(
412         {
413             homebranch       => $branch,
414             holdingbranch    => $branch,
415             barcode          => $barcode6,
416             replacementprice => 23.00
417         },
418         $biblionumber
419     );
420
421     my $barcode7 = 'R00000349';
422     my ( $item_bibnum7, $item_bibitemnum7, $itemnumber7 ) = AddItem(
423         {
424             homebranch       => $branch,
425             holdingbranch    => $branch,
426             barcode          => $barcode7,
427             replacementprice => 23.00
428         },
429         $biblionumber
430     );
431     my $datedue6 = AddIssue( $renewing_borrower, $barcode6);
432     is (defined $datedue6, 1, "Item 2 checked out, due date: $datedue6");
433
434     my $passeddatedue1 = AddIssue($renewing_borrower, $barcode7, DateTime->from_epoch(epoch => 1));
435     is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: $passeddatedue1");
436
437
438     C4::Context->set_preference('OverduesBlockRenewing','blockitem');
439     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
440     is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
441     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
442     is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
443
444
445     $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, itemnumber => $itemnumber, borrowernumber => $reserving_borrowernumber});
446     CancelReserve({ reserve_id => $reserveid });
447
448     # Test automatic renewal before value for "norenewalbefore" in policy is set
449     my $barcode4 = '11235813';
450     my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
451         {
452             homebranch       => $branch,
453             holdingbranch    => $branch,
454             barcode          => $barcode4,
455             replacementprice => 16.00
456         },
457         $biblionumber
458     );
459
460     AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
461     ( $renewokay, $error ) =
462       CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
463     is( $renewokay, 0, 'Cannot renew, renewal is automatic' );
464     is( $error, 'auto_renew',
465         'Cannot renew, renewal is automatic (returned code is auto_renew)' );
466
467     # set policy to require that loans cannot be
468     # renewed until seven days prior to the due date
469     $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
470     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
471     is( $renewokay, 0, 'Cannot renew, renewal is premature');
472     is( $error, 'too_soon', 'Cannot renew, renewal is premature (returned code is too_soon)');
473     is(
474         GetSoonestRenewDate($renewing_borrowernumber, $itemnumber),
475         $datedue->clone->add(days => -7),
476         'renewals permitted 7 days before due date, as expected',
477     );
478
479     # Test automatic renewal again
480     ( $renewokay, $error ) =
481       CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
482     is( $renewokay, 0, 'Cannot renew, renewal is automatic and premature' );
483     is( $error, 'auto_too_soon',
484 'Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
485     );
486
487     # Too many renewals
488
489     # set policy to forbid renewals
490     $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
491
492     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
493     is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
494     is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
495
496     # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
497     C4::Context->set_preference('WhenLostForgiveFine','1');
498     C4::Context->set_preference('WhenLostChargeReplacementFee','1');
499
500     C4::Overdues::UpdateFine( $itemnumber, $renewing_borrower->{borrowernumber},
501         15.00, q{}, Koha::DateUtils::output_pref($datedue) );
502
503     LostItem( $itemnumber, 1 );
504
505     my $item = $schema->resultset('Item')->find( $itemnumber );
506     ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
507
508     my $total_due = $dbh->selectrow_array(
509         'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
510         undef, $renewing_borrower->{borrowernumber}
511     );
512
513     ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
514
515     C4::Context->dbh->do("DELETE FROM accountlines");
516
517     C4::Context->set_preference('WhenLostForgiveFine','0');
518     C4::Context->set_preference('WhenLostChargeReplacementFee','0');
519
520     C4::Overdues::UpdateFine( $itemnumber2, $renewing_borrower->{borrowernumber},
521         15.00, q{}, Koha::DateUtils::output_pref($datedue) );
522
523     LostItem( $itemnumber2, 0 );
524
525     my $item2 = $schema->resultset('Item')->find( $itemnumber2 );
526     ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
527
528     $total_due = $dbh->selectrow_array(
529         'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
530         undef, $renewing_borrower->{borrowernumber}
531     );
532
533     ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
534
535     my $now = dt_from_string();
536     my $future = dt_from_string();
537     $future->add( days => 7 );
538     my $units = C4::Overdues::get_chargeable_units('days', $future, $now, 'MPL');
539     ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
540
541     # Users cannot renew any item if there is an overdue item
542     C4::Context->set_preference('OverduesBlockRenewing','block');
543     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
544     is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
545     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
546     is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
547
548   }
549
550 {
551     # GetUpcomingDueIssues tests
552     my $barcode  = 'R00000342';
553     my $barcode2 = 'R00000343';
554     my $barcode3 = 'R00000344';
555     my $branch   = 'MPL';
556
557     #Create another record
558     my $biblio2 = MARC::Record->new();
559     my $title2 = 'Something is worng here';
560     $biblio2->append_fields(
561         MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
562         MARC::Field->new('245', ' ', ' ', a => $title2),
563     );
564     my ($biblionumber2, $biblioitemnumber2) = AddBiblio($biblio2, '');
565
566     #Create third item
567     AddItem(
568         {
569             homebranch       => $branch,
570             holdingbranch    => $branch,
571             barcode          => $barcode3
572         },
573         $biblionumber2
574     );
575
576     # Create a borrower
577     my %a_borrower_data = (
578         firstname =>  'Fridolyn',
579         surname => 'SOMERS',
580         categorycode => 'S',
581         branchcode => $branch,
582     );
583
584     my $a_borrower_borrowernumber = AddMember(%a_borrower_data);
585     my $a_borrower = GetMember( borrowernumber => $a_borrower_borrowernumber );
586
587     my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
588     my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
589     my $today = DateTime->today(time_zone => C4::Context->tz());
590
591     my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
592     my $datedue = dt_from_string( $issue->date_due() );
593     my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
594     my $datedue2 = dt_from_string( $issue->date_due() );
595
596     my $upcoming_dues;
597
598     # GetUpcomingDueIssues tests
599     for my $i(0..1) {
600         $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
601         is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
602     }
603
604     #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
605     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
606     is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
607
608     for my $i(3..5) {
609         $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
610         is ( scalar( @$upcoming_dues ), 1,
611             "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
612     }
613
614     # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
615
616     my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
617
618     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
619     is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
620
621     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
622     is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
623
624     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
625     is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
626
627     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 }  );
628     is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
629
630     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
631     is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
632
633     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
634     is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
635
636 }
637
638 {
639     my $barcode  = '1234567890';
640     my $branch   = 'MPL';
641
642     my $biblio = MARC::Record->new();
643     my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
644
645     #Create third item
646     my ( undef, undef, $itemnumber ) = AddItem(
647         {
648             homebranch       => $branch,
649             holdingbranch    => $branch,
650             barcode          => $barcode
651         },
652         $biblionumber
653     );
654
655     # Create a borrower
656     my %a_borrower_data = (
657         firstname =>  'Kyle',
658         surname => 'Hall',
659         categorycode => 'S',
660         branchcode => $branch,
661     );
662
663     my $borrowernumber = AddMember(%a_borrower_data);
664
665     UpdateFine( $itemnumber, $borrowernumber, 0 );
666
667     my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
668     my $count = $hr->{count};
669
670     is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
671 }
672
673 {
674     $dbh->do('DELETE FROM issues');
675     $dbh->do('DELETE FROM items');
676     $dbh->do('DELETE FROM issuingrules');
677     $dbh->do(
678         q{
679         INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
680                     norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
681         },
682         {},
683         '*', '*', '*', 25,
684         20,  14,  'days',
685         1,   7,
686         '',  0,
687         .10, 1
688     );
689     my $biblio = MARC::Record->new();
690     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
691
692     my $barcode1 = '1234';
693     my ( undef, undef, $itemnumber1 ) = AddItem(
694         {
695             homebranch    => 'MPL',
696             holdingbranch => 'MPL',
697             barcode       => $barcode1,
698         },
699         $biblionumber
700     );
701     my $barcode2 = '4321';
702     my ( undef, undef, $itemnumber2 ) = AddItem(
703         {
704             homebranch    => 'MPL',
705             holdingbranch => 'MPL',
706             barcode       => $barcode2,
707         },
708         $biblionumber
709     );
710
711     my $borrowernumber1 = AddMember(
712         firstname    => 'Kyle',
713         surname      => 'Hall',
714         categorycode => 'S',
715         branchcode   => 'MPL',
716     );
717     my $borrowernumber2 = AddMember(
718         firstname    => 'Chelsea',
719         surname      => 'Hall',
720         categorycode => 'S',
721         branchcode   => 'MPL',
722     );
723
724     my $borrower1 = GetMember( borrowernumber => $borrowernumber1 );
725     my $borrower2 = GetMember( borrowernumber => $borrowernumber2 );
726
727     my $issue = AddIssue( $borrower1, $barcode1 );
728
729     my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
730     is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
731
732     AddReserve(
733         'MPL', $borrowernumber2, $biblionumber,
734         '',  1, undef, undef, '',
735         undef, undef, undef
736     );
737
738     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
739     C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
740     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
741     is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
742
743     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
744     C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
745     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
746     is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
747
748     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
749     C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
750     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
751     is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
752
753     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
754     C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
755     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
756     is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
757
758     # Setting item not checked out to be not for loan but holdable
759     ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
760
761     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
762     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' );
763 }
764
765 {
766     # Don't allow renewing onsite checkout
767     my $barcode  = 'R00000XXX';
768     my $branch   = 'CPL';
769
770     #Create another record
771     my $biblio = MARC::Record->new();
772     $biblio->append_fields(
773         MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
774         MARC::Field->new('245', ' ', ' ', a => 'A title'),
775     );
776     my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
777
778     my (undef, undef, $itemnumber) = AddItem(
779         {
780             homebranch       => $branch,
781             holdingbranch    => $branch,
782             barcode          => $barcode,
783         },
784         $biblionumber
785     );
786
787     my $borrowernumber = AddMember(
788         firstname =>  'fn',
789         surname => 'dn',
790         categorycode => 'S',
791         branchcode => $branch,
792     );
793
794     my $borrower = GetMember( borrowernumber => $borrowernumber );
795     my $issue = AddIssue( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
796     my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
797     is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
798     is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
799 }
800
801 $schema->storage->txn_rollback();
802 1;