Bug 14337: Add Unit Tests
[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 => 65;
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     # Create borrowers
238     my %renewing_borrower_data = (
239         firstname =>  'John',
240         surname => 'Renewal',
241         categorycode => 'S',
242         branchcode => $branch,
243     );
244
245     my %reserving_borrower_data = (
246         firstname =>  'Katrin',
247         surname => 'Reservation',
248         categorycode => 'S',
249         branchcode => $branch,
250     );
251
252     my %hold_waiting_borrower_data = (
253         firstname =>  'Kyle',
254         surname => 'Reservation',
255         categorycode => 'S',
256         branchcode => $branch,
257     );
258
259     my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
260     my $reserving_borrowernumber = AddMember(%reserving_borrower_data);
261     my $hold_waiting_borrowernumber = AddMember(%hold_waiting_borrower_data);
262
263     my $renewing_borrower = GetMember( borrowernumber => $renewing_borrowernumber );
264
265     my $bibitems       = '';
266     my $priority       = '1';
267     my $resdate        = undef;
268     my $expdate        = undef;
269     my $notes          = '';
270     my $checkitem      = undef;
271     my $found          = undef;
272
273     my $issue = AddIssue( $renewing_borrower, $barcode);
274     my $datedue = dt_from_string( $issue->date_due() );
275     is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
276
277     my $issue2 = AddIssue( $renewing_borrower, $barcode2);
278     $datedue = dt_from_string( $issue->date_due() );
279     is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
280
281     my $borrowing_borrowernumber = GetItemIssue($itemnumber)->{borrowernumber};
282     is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
283
284     my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
285     is( $renewokay, 1, 'Can renew, no holds for this title or item');
286
287
288     # Biblio-level hold, renewal test
289     AddReserve(
290         $branch, $reserving_borrowernumber, $biblionumber,
291         $bibitems,  $priority, $resdate, $expdate, $notes,
292         $title, $checkitem, $found
293     );
294
295     # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
296     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
297     C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 1 );
298     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
299     is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
300     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
301     is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
302
303     # Now let's add an item level hold, we should no longer be able to renew the item
304     my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
305         {
306             borrowernumber => $hold_waiting_borrowernumber,
307             biblionumber   => $biblionumber,
308             itemnumber     => $itemnumber,
309             branchcode     => $branch,
310             priority       => 3,
311         }
312     );
313     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
314     is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
315     $hold->delete();
316
317     # 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
318     # be able to renew these items
319     $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
320         {
321             borrowernumber => $hold_waiting_borrowernumber,
322             biblionumber   => $biblionumber,
323             itemnumber     => $itemnumber3,
324             branchcode     => $branch,
325             priority       => 0,
326             found          => 'W'
327         }
328     );
329     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
330     is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
331     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
332     is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
333     C4::Context->set_preference('AllowRenewalIfOtherItemsAvailable', 0 );
334
335     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
336     is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
337     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
338
339     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
340     is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
341     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
342
343     my $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber});
344     my $reserving_borrower = GetMember( borrowernumber => $reserving_borrowernumber );
345     AddIssue($reserving_borrower, $barcode3);
346     my $reserve = $dbh->selectrow_hashref(
347         'SELECT * FROM old_reserves WHERE reserve_id = ?',
348         { Slice => {} },
349         $reserveid
350     );
351     is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
352
353     # Item-level hold, renewal test
354     AddReserve(
355         $branch, $reserving_borrowernumber, $biblionumber,
356         $bibitems,  $priority, $resdate, $expdate, $notes,
357         $title, $itemnumber, $found
358     );
359
360     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
361     is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
362     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
363
364     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
365     is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
366
367
368     # Items can't fill hold for reasons
369     ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
370     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
371     is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
372     ModItem({ notforloan => 0, itype => '' }, $biblionumber, $itemnumber,1);
373
374     # FIXME: Add more for itemtype not for loan etc.
375
376     $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, itemnumber => $itemnumber, borrowernumber => $reserving_borrowernumber});
377     CancelReserve({ reserve_id => $reserveid });
378
379     # Test automatic renewal before value for "norenewalbefore" in policy is set
380     my $barcode4 = '11235813';
381     my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
382         {
383             homebranch       => $branch,
384             holdingbranch    => $branch,
385             barcode          => $barcode4,
386             replacementprice => 16.00
387         },
388         $biblionumber
389     );
390
391     AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
392     ( $renewokay, $error ) =
393       CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
394     is( $renewokay, 0, 'Cannot renew, renewal is automatic' );
395     is( $error, 'auto_renew',
396         'Cannot renew, renewal is automatic (returned code is auto_renew)' );
397
398     # set policy to require that loans cannot be
399     # renewed until seven days prior to the due date
400     $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
401     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
402     is( $renewokay, 0, 'Cannot renew, renewal is premature');
403     is( $error, 'too_soon', 'Cannot renew, renewal is premature (returned code is too_soon)');
404     is(
405         GetSoonestRenewDate($renewing_borrowernumber, $itemnumber),
406         $datedue->clone->add(days => -7),
407         'renewals permitted 7 days before due date, as expected',
408     );
409
410     # Test automatic renewal again
411     ( $renewokay, $error ) =
412       CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
413     is( $renewokay, 0, 'Cannot renew, renewal is automatic and premature' );
414     is( $error, 'auto_too_soon',
415 'Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
416     );
417
418     # Too many renewals
419
420     # set policy to forbid renewals
421     $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
422
423     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
424     is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
425     is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
426
427     # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
428     C4::Context->set_preference('WhenLostForgiveFine','1');
429     C4::Context->set_preference('WhenLostChargeReplacementFee','1');
430
431     C4::Overdues::UpdateFine( $itemnumber, $renewing_borrower->{borrowernumber},
432         15.00, q{}, Koha::DateUtils::output_pref($datedue) );
433
434     LostItem( $itemnumber, 1 );
435
436     my $item = $schema->resultset('Item')->find( $itemnumber );
437     ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
438
439     my $total_due = $dbh->selectrow_array(
440         'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
441         undef, $renewing_borrower->{borrowernumber}
442     );
443
444     ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
445
446     C4::Context->dbh->do("DELETE FROM accountlines");
447
448     C4::Context->set_preference('WhenLostForgiveFine','0');
449     C4::Context->set_preference('WhenLostChargeReplacementFee','0');
450
451     C4::Overdues::UpdateFine( $itemnumber2, $renewing_borrower->{borrowernumber},
452         15.00, q{}, Koha::DateUtils::output_pref($datedue) );
453
454     LostItem( $itemnumber2, 0 );
455
456     my $item2 = $schema->resultset('Item')->find( $itemnumber2 );
457     ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
458
459     $total_due = $dbh->selectrow_array(
460         'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
461         undef, $renewing_borrower->{borrowernumber}
462     );
463
464     ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
465
466     my $now = dt_from_string();
467     my $future = dt_from_string();
468     $future->add( days => 7 );
469     my $units = C4::Overdues::get_chargeable_units('days', $future, $now, 'MPL');
470     ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
471 }
472
473 {
474     # GetUpcomingDueIssues tests
475     my $barcode  = 'R00000342';
476     my $barcode2 = 'R00000343';
477     my $barcode3 = 'R00000344';
478     my $branch   = 'MPL';
479
480     #Create another record
481     my $biblio2 = MARC::Record->new();
482     my $title2 = 'Something is worng here';
483     $biblio2->append_fields(
484         MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
485         MARC::Field->new('245', ' ', ' ', a => $title2),
486     );
487     my ($biblionumber2, $biblioitemnumber2) = AddBiblio($biblio2, '');
488
489     #Create third item
490     AddItem(
491         {
492             homebranch       => $branch,
493             holdingbranch    => $branch,
494             barcode          => $barcode3
495         },
496         $biblionumber2
497     );
498
499     # Create a borrower
500     my %a_borrower_data = (
501         firstname =>  'Fridolyn',
502         surname => 'SOMERS',
503         categorycode => 'S',
504         branchcode => $branch,
505     );
506
507     my $a_borrower_borrowernumber = AddMember(%a_borrower_data);
508     my $a_borrower = GetMember( borrowernumber => $a_borrower_borrowernumber );
509
510     my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
511     my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
512     my $today = DateTime->today(time_zone => C4::Context->tz());
513
514     my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
515     my $datedue = dt_from_string( $issue->date_due() );
516     my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
517     my $datedue2 = dt_from_string( $issue->date_due() );
518
519     my $upcoming_dues;
520
521     # GetUpcomingDueIssues tests
522     for my $i(0..1) {
523         $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
524         is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
525     }
526
527     #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
528     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
529     is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
530
531     for my $i(3..5) {
532         $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
533         is ( scalar( @$upcoming_dues ), 1,
534             "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
535     }
536
537     # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
538
539     my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
540
541     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
542     is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
543
544     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
545     is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
546
547     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
548     is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
549
550     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 }  );
551     is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
552
553     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
554     is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
555
556     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
557     is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
558
559 }
560
561 {
562     my $barcode  = '1234567890';
563     my $branch   = 'MPL';
564
565     my $biblio = MARC::Record->new();
566     my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
567
568     #Create third item
569     my ( undef, undef, $itemnumber ) = AddItem(
570         {
571             homebranch       => $branch,
572             holdingbranch    => $branch,
573             barcode          => $barcode
574         },
575         $biblionumber
576     );
577
578     # Create a borrower
579     my %a_borrower_data = (
580         firstname =>  'Kyle',
581         surname => 'Hall',
582         categorycode => 'S',
583         branchcode => $branch,
584     );
585
586     my $borrowernumber = AddMember(%a_borrower_data);
587
588     UpdateFine( $itemnumber, $borrowernumber, 0 );
589
590     my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
591     my $count = $hr->{count};
592
593     is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
594 }
595
596 {
597     $dbh->do('DELETE FROM issues');
598     $dbh->do('DELETE FROM items');
599     $dbh->do('DELETE FROM issuingrules');
600     $dbh->do(
601         q{
602         INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
603                     norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
604         },
605         {},
606         '*', '*', '*', 25,
607         20,  14,  'days',
608         1,   7,
609         '',  0,
610         .10, 1
611     );
612     my $biblio = MARC::Record->new();
613     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
614
615     my $barcode1 = '1234';
616     my ( undef, undef, $itemnumber1 ) = AddItem(
617         {
618             homebranch    => 'MPL',
619             holdingbranch => 'MPL',
620             barcode       => $barcode1,
621         },
622         $biblionumber
623     );
624     my $barcode2 = '4321';
625     my ( undef, undef, $itemnumber2 ) = AddItem(
626         {
627             homebranch    => 'MPL',
628             holdingbranch => 'MPL',
629             barcode       => $barcode2,
630         },
631         $biblionumber
632     );
633
634     my $borrowernumber1 = AddMember(
635         firstname    => 'Kyle',
636         surname      => 'Hall',
637         categorycode => 'S',
638         branchcode   => 'MPL',
639     );
640     my $borrowernumber2 = AddMember(
641         firstname    => 'Chelsea',
642         surname      => 'Hall',
643         categorycode => 'S',
644         branchcode   => 'MPL',
645     );
646
647     my $borrower1 = GetMember( borrowernumber => $borrowernumber1 );
648     my $borrower2 = GetMember( borrowernumber => $borrowernumber2 );
649
650     my $issue = AddIssue( $borrower1, $barcode1 );
651
652     my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
653     is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
654
655     AddReserve(
656         'MPL', $borrowernumber2, $biblionumber,
657         'a', '',  1, undef, undef, '',
658         undef, undef, undef
659     );
660
661     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
662     is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record' );
663
664     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
665     C4::Context->set_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
666
667     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
668     is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled' );
669
670     # Setting item not checked out to be not for loan but holdable
671     ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
672
673     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
674     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' );
675 }
676
677 $schema->storage->txn_rollback();
678 1;