Bug 14504: Changes missed while fixing patches
[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 t::lib::Mocks;
22 use C4::Biblio;
23 use C4::Branch;
24 use C4::Items;
25 use C4::Members;
26 use C4::Reserves;
27 use C4::Overdues qw(UpdateFine CalcFine);
28 use Koha::DateUtils;
29 use Koha::Database;
30
31 use t::lib::TestBuilder;
32
33 use Test::More tests => 87;
34
35 BEGIN {
36     use_ok('C4::Circulation');
37 }
38
39 my $schema = Koha::Database->schema;
40 $schema->storage->txn_begin;
41 my $builder = t::lib::TestBuilder->new;
42 my $dbh = C4::Context->dbh;
43
44 # Start transaction
45 $dbh->{RaiseError} = 1;
46
47 # Start with a clean slate
48 $dbh->do('DELETE FROM issues');
49
50 my $library = $builder->build({
51     source => 'Branch',
52 });
53 my $library2 = $builder->build({
54     source => 'Branch',
55 });
56
57 my $CircControl = C4::Context->preference('CircControl');
58 my $HomeOrHoldingBranch = C4::Context->preference('HomeOrHoldingBranch');
59
60 my $item = {
61     homebranch => $library2->{branchcode},
62     holdingbranch => $library2->{branchcode}
63 };
64
65 my $borrower = {
66     branchcode => $library2->{branchcode}
67 };
68
69 # No userenv, PickupLibrary
70 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
71 is(
72     C4::Context->preference('CircControl'),
73     'PickupLibrary',
74     'CircControl changed to PickupLibrary'
75 );
76 is(
77     C4::Circulation::_GetCircControlBranch($item, $borrower),
78     $item->{$HomeOrHoldingBranch},
79     '_GetCircControlBranch returned item branch (no userenv defined)'
80 );
81
82 # No userenv, PatronLibrary
83 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
84 is(
85     C4::Context->preference('CircControl'),
86     'PatronLibrary',
87     'CircControl changed to PatronLibrary'
88 );
89 is(
90     C4::Circulation::_GetCircControlBranch($item, $borrower),
91     $borrower->{branchcode},
92     '_GetCircControlBranch returned borrower branch'
93 );
94
95 # No userenv, ItemHomeLibrary
96 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
97 is(
98     C4::Context->preference('CircControl'),
99     'ItemHomeLibrary',
100     'CircControl changed to ItemHomeLibrary'
101 );
102 is(
103     $item->{$HomeOrHoldingBranch},
104     C4::Circulation::_GetCircControlBranch($item, $borrower),
105     '_GetCircControlBranch returned item branch'
106 );
107
108 # Now, set a userenv
109 C4::Context->_new_userenv('xxx');
110 C4::Context->set_userenv(0,0,0,'firstname','surname', $library2->{branchcode}, 'Midway Public Library', '', '', '');
111 is(C4::Context->userenv->{branch}, $library2->{branchcode}, 'userenv set');
112
113 # Userenv set, PickupLibrary
114 t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
115 is(
116     C4::Context->preference('CircControl'),
117     'PickupLibrary',
118     'CircControl changed to PickupLibrary'
119 );
120 is(
121     C4::Circulation::_GetCircControlBranch($item, $borrower),
122     $library2->{branchcode},
123     '_GetCircControlBranch returned current branch'
124 );
125
126 # Userenv set, PatronLibrary
127 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
128 is(
129     C4::Context->preference('CircControl'),
130     'PatronLibrary',
131     'CircControl changed to PatronLibrary'
132 );
133 is(
134     C4::Circulation::_GetCircControlBranch($item, $borrower),
135     $borrower->{branchcode},
136     '_GetCircControlBranch returned borrower branch'
137 );
138
139 # Userenv set, ItemHomeLibrary
140 t::lib::Mocks::mock_preference('CircControl', 'ItemHomeLibrary');
141 is(
142     C4::Context->preference('CircControl'),
143     'ItemHomeLibrary',
144     'CircControl changed to ItemHomeLibrary'
145 );
146 is(
147     C4::Circulation::_GetCircControlBranch($item, $borrower),
148     $item->{$HomeOrHoldingBranch},
149     '_GetCircControlBranch returned item branch'
150 );
151
152 # Reset initial configuration
153 t::lib::Mocks::mock_preference('CircControl', $CircControl);
154 is(
155     C4::Context->preference('CircControl'),
156     $CircControl,
157     'CircControl reset to its initial value'
158 );
159
160 # Set a simple circ policy
161 $dbh->do('DELETE FROM issuingrules');
162 $dbh->do(
163     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed,
164                                 maxissueqty, issuelength, lengthunit,
165                                 renewalsallowed, renewalperiod,
166                                 norenewalbefore, auto_renew,
167                                 fine, chargeperiod)
168       VALUES (?, ?, ?, ?,
169               ?, ?, ?,
170               ?, ?,
171               ?, ?,
172               ?, ?
173              )
174     },
175     {},
176     '*', '*', '*', 25,
177     20, 14, 'days',
178     1, 7,
179     undef, 0,
180     .10, 1
181 );
182
183 # Test C4::Circulation::ProcessOfflinePayment
184 my $sth = C4::Context->dbh->prepare("SELECT COUNT(*) FROM accountlines WHERE amount = '-123.45' AND accounttype = 'Pay'");
185 $sth->execute();
186 my ( $original_count ) = $sth->fetchrow_array();
187
188 C4::Context->dbh->do("INSERT INTO borrowers ( cardnumber, surname, firstname, categorycode, branchcode ) VALUES ( '99999999999', 'Hall', 'Kyle', 'S', ? )", undef, $library2->{branchcode} );
189
190 C4::Circulation::ProcessOfflinePayment({ cardnumber => '99999999999', amount => '123.45' });
191
192 $sth->execute();
193 my ( $new_count ) = $sth->fetchrow_array();
194
195 ok( $new_count == $original_count  + 1, 'ProcessOfflinePayment makes payment correctly' );
196
197 C4::Context->dbh->do("DELETE FROM accountlines WHERE borrowernumber IN ( SELECT borrowernumber FROM borrowers WHERE cardnumber = '99999999999' )");
198 C4::Context->dbh->do("DELETE FROM borrowers WHERE cardnumber = '99999999999'");
199 C4::Context->dbh->do("DELETE FROM accountlines");
200 {
201 # CanBookBeRenewed tests
202
203     # Generate test biblio
204     my $biblio = MARC::Record->new();
205     my $title = 'Silence in the library';
206     $biblio->append_fields(
207         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
208         MARC::Field->new('245', ' ', ' ', a => $title),
209     );
210
211     my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
212
213     my $barcode = 'R00000342';
214     my $branch = $library2->{branchcode};
215
216     my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
217         {
218             homebranch       => $branch,
219             holdingbranch    => $branch,
220             barcode          => $barcode,
221             replacementprice => 12.00
222         },
223         $biblionumber
224     );
225
226     my $barcode2 = 'R00000343';
227     my ( $item_bibnum2, $item_bibitemnum2, $itemnumber2 ) = AddItem(
228         {
229             homebranch       => $branch,
230             holdingbranch    => $branch,
231             barcode          => $barcode2,
232             replacementprice => 23.00
233         },
234         $biblionumber
235     );
236
237     my $barcode3 = 'R00000346';
238     my ( $item_bibnum3, $item_bibitemnum3, $itemnumber3 ) = AddItem(
239         {
240             homebranch       => $branch,
241             holdingbranch    => $branch,
242             barcode          => $barcode3,
243             replacementprice => 23.00
244         },
245         $biblionumber
246     );
247
248
249
250
251     # Create borrowers
252     my %renewing_borrower_data = (
253         firstname =>  'John',
254         surname => 'Renewal',
255         categorycode => 'S',
256         branchcode => $branch,
257     );
258
259     my %reserving_borrower_data = (
260         firstname =>  'Katrin',
261         surname => 'Reservation',
262         categorycode => 'S',
263         branchcode => $branch,
264     );
265
266     my %hold_waiting_borrower_data = (
267         firstname =>  'Kyle',
268         surname => 'Reservation',
269         categorycode => 'S',
270         branchcode => $branch,
271     );
272
273     my %restricted_borrower_data = (
274         firstname =>  'Alice',
275         surname => 'Reservation',
276         categorycode => 'S',
277         debarred => '3228-01-01',
278         branchcode => $branch,
279     );
280
281     my $renewing_borrowernumber = AddMember(%renewing_borrower_data);
282     my $reserving_borrowernumber = AddMember(%reserving_borrower_data);
283     my $hold_waiting_borrowernumber = AddMember(%hold_waiting_borrower_data);
284     my $restricted_borrowernumber = AddMember(%restricted_borrower_data);
285
286     my $renewing_borrower = GetMember( borrowernumber => $renewing_borrowernumber );
287     my $restricted_borrower = GetMember( borrowernumber => $restricted_borrowernumber );
288
289     my $bibitems       = '';
290     my $priority       = '1';
291     my $resdate        = undef;
292     my $expdate        = undef;
293     my $notes          = '';
294     my $checkitem      = undef;
295     my $found          = undef;
296
297     my $issue = AddIssue( $renewing_borrower, $barcode);
298     my $datedue = dt_from_string( $issue->date_due() );
299     is (defined $issue->date_due(), 1, "Item 1 checked out, due date: " . $issue->date_due() );
300
301     my $issue2 = AddIssue( $renewing_borrower, $barcode2);
302     $datedue = dt_from_string( $issue->date_due() );
303     is (defined $issue2, 1, "Item 2 checked out, due date: " . $issue2->date_due());
304
305
306     my $borrowing_borrowernumber = GetItemIssue($itemnumber)->{borrowernumber};
307     is ($borrowing_borrowernumber, $renewing_borrowernumber, "Item checked out to $renewing_borrower->{firstname} $renewing_borrower->{surname}");
308
309     my ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
310     is( $renewokay, 1, 'Can renew, no holds for this title or item');
311
312
313     # Biblio-level hold, renewal test
314     AddReserve(
315         $branch, $reserving_borrowernumber, $biblionumber,
316         $bibitems,  $priority, $resdate, $expdate, $notes,
317         $title, $checkitem, $found
318     );
319
320     # Testing of feature to allow the renewal of reserved items if other items on the record can fill all needed holds
321     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
322     t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 1 );
323     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
324     is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
325     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
326     is( $renewokay, 1, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
327
328     # Now let's add an item level hold, we should no longer be able to renew the item
329     my $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
330         {
331             borrowernumber => $hold_waiting_borrowernumber,
332             biblionumber   => $biblionumber,
333             itemnumber     => $itemnumber,
334             branchcode     => $branch,
335             priority       => 3,
336         }
337     );
338     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
339     is( $renewokay, 0, 'Bug 13919 - Renewal possible with item level hold on item');
340     $hold->delete();
341
342     # 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
343     # be able to renew these items
344     $hold = Koha::Database->new()->schema()->resultset('Reserve')->create(
345         {
346             borrowernumber => $hold_waiting_borrowernumber,
347             biblionumber   => $biblionumber,
348             itemnumber     => $itemnumber3,
349             branchcode     => $branch,
350             priority       => 0,
351             found          => 'W'
352         }
353     );
354     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
355     is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
356     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
357     is( $renewokay, 0, 'Bug 11634 - Allow renewal of item with unfilled holds if other available items can fill those holds');
358     t::lib::Mocks::mock_preference('AllowRenewalIfOtherItemsAvailable', 0 );
359
360     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
361     is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
362     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
363
364     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
365     is( $renewokay, 0, '(Bug 10663) Cannot renew, reserved');
366     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, reserved (returned error is on_reserve)');
367
368     my $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, borrowernumber => $reserving_borrowernumber});
369     my $reserving_borrower = GetMember( borrowernumber => $reserving_borrowernumber );
370     AddIssue($reserving_borrower, $barcode3);
371     my $reserve = $dbh->selectrow_hashref(
372         'SELECT * FROM old_reserves WHERE reserve_id = ?',
373         { Slice => {} },
374         $reserveid
375     );
376     is($reserve->{found}, 'F', 'hold marked completed when checking out item that fills it');
377
378     # Item-level hold, renewal test
379     AddReserve(
380         $branch, $reserving_borrowernumber, $biblionumber,
381         $bibitems,  $priority, $resdate, $expdate, $notes,
382         $title, $itemnumber, $found
383     );
384
385     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
386     is( $renewokay, 0, '(Bug 10663) Cannot renew, item reserved');
387     is( $error, 'on_reserve', '(Bug 10663) Cannot renew, item reserved (returned error is on_reserve)');
388
389     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2, 1);
390     is( $renewokay, 1, 'Can renew item 2, item-level hold is on item 1');
391
392     # Items can't fill hold for reasons
393     ModItem({ notforloan => 1 }, $biblionumber, $itemnumber);
394     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber, 1);
395     is( $renewokay, 1, 'Can renew, item is marked not for loan, hold does not block');
396     ModItem({ notforloan => 0, itype => '' }, $biblionumber, $itemnumber,1);
397
398     # FIXME: Add more for itemtype not for loan etc.
399
400     # Restricted users cannot renew when RestrictionBlockRenewing is enabled
401     my $barcode5 = 'R00000347';
402     my ( $item_bibnum5, $item_bibitemnum5, $itemnumber5 ) = AddItem(
403         {
404             homebranch       => $branch,
405             holdingbranch    => $branch,
406             barcode          => $barcode5,
407             replacementprice => 23.00
408         },
409         $biblionumber
410     );
411     my $datedue5 = AddIssue($restricted_borrower, $barcode5);
412     is (defined $datedue5, 1, "Item with date due checked out, due date: $datedue5");
413
414     t::lib::Mocks::mock_preference('RestrictionBlockRenewing','1');
415     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber2);
416     is( $renewokay, 1, '(Bug 8236), Can renew, user is not restricted');
417     ( $renewokay, $error ) = CanBookBeRenewed($restricted_borrowernumber, $itemnumber5);
418     is( $renewokay, 0, '(Bug 8236), Cannot renew, user is restricted');
419
420     # Users cannot renew an overdue item
421     my $barcode6 = 'R00000348';
422     my ( $item_bibnum6, $item_bibitemnum6, $itemnumber6 ) = AddItem(
423         {
424             homebranch       => $branch,
425             holdingbranch    => $branch,
426             barcode          => $barcode6,
427             replacementprice => 23.00
428         },
429         $biblionumber
430     );
431
432     my $barcode7 = 'R00000349';
433     my ( $item_bibnum7, $item_bibitemnum7, $itemnumber7 ) = AddItem(
434         {
435             homebranch       => $branch,
436             holdingbranch    => $branch,
437             barcode          => $barcode7,
438             replacementprice => 23.00
439         },
440         $biblionumber
441     );
442     my $datedue6 = AddIssue( $renewing_borrower, $barcode6);
443     is (defined $datedue6, 1, "Item 2 checked out, due date: $datedue6");
444
445     my $now = dt_from_string();
446     my $five_weeks = DateTime::Duration->new(weeks => 5);
447     my $five_weeks_ago = $now - $five_weeks;
448
449     my $passeddatedue1 = AddIssue($renewing_borrower, $barcode7, $five_weeks_ago);
450     is (defined $passeddatedue1, 1, "Item with passed date due checked out, due date: " . $passeddatedue1->date_due);
451
452     my ( $fine ) = CalcFine( GetItem(undef, $barcode7), $renewing_borrower->{categorycode}, $branch, $five_weeks_ago, $now );
453     C4::Overdues::UpdateFine(
454         {
455             issue_id       => $passeddatedue1->id(),
456             itemnumber     => $itemnumber7,
457             borrowernumber => $renewing_borrower->{borrowernumber},
458             amount         => $fine,
459             type           => 'FU',
460             due            => Koha::DateUtils::output_pref($five_weeks_ago)
461         }
462     );
463     AddRenewal( $renewing_borrower->{borrowernumber}, $itemnumber7, $branch );
464     $fine = $schema->resultset('Accountline')->single( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $itemnumber7 } );
465     is( $fine->accounttype, 'F', 'Fine on renewed item is closed out properly' );
466     $fine->delete();
467
468     t::lib::Mocks::mock_preference('OverduesBlockRenewing','blockitem');
469     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
470     is( $renewokay, 1, '(Bug 8236), Can renew, this item is not overdue');
471     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
472     is( $renewokay, 0, '(Bug 8236), Cannot renew, this item is overdue');
473
474
475     $reserveid = C4::Reserves::GetReserveId({ biblionumber => $biblionumber, itemnumber => $itemnumber, borrowernumber => $reserving_borrowernumber});
476     CancelReserve({ reserve_id => $reserveid });
477
478     # Bug 14101
479     # Test automatic renewal before value for "norenewalbefore" in policy is set
480     # In this case automatic renewal is not permitted prior to due date
481     my $barcode4 = '11235813';
482     my ( $item_bibnum4, $item_bibitemnum4, $itemnumber4 ) = AddItem(
483         {
484             homebranch       => $branch,
485             holdingbranch    => $branch,
486             barcode          => $barcode4,
487             replacementprice => 16.00
488         },
489         $biblionumber
490     );
491
492     $issue = AddIssue( $renewing_borrower, $barcode4, undef, undef, undef, undef, { auto_renew => 1 } );
493     ( $renewokay, $error ) =
494       CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
495     is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
496     is( $error, 'auto_too_soon',
497         'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
498
499     # Bug 7413
500     # Test premature manual renewal
501     $dbh->do('UPDATE issuingrules SET norenewalbefore = 7');
502
503     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
504     is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
505     is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
506
507     # Bug 14395
508     # Test 'exact time' setting for syspref NoRenewalBeforePrecision
509     t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
510     is(
511         GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
512         $datedue->clone->add( days => -7 ),
513         'Bug 14395: Renewals permitted 7 days before due date, as expected'
514     );
515
516     # Bug 14395
517     # Test 'date' setting for syspref NoRenewalBeforePrecision
518     t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
519     is(
520         GetSoonestRenewDate( $renewing_borrowernumber, $itemnumber ),
521         $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
522         'Bug 14395: Renewals permitted 7 days before due date, as expected'
523     );
524
525     # Bug 14101
526     # Test premature automatic renewal
527     ( $renewokay, $error ) =
528       CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
529     is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
530     is( $error, 'auto_too_soon',
531         'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
532     );
533
534     # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
535     # and test automatic renewal again
536     $dbh->do('UPDATE issuingrules SET norenewalbefore = 0');
537     ( $renewokay, $error ) =
538       CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
539     is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
540     is( $error, 'auto_too_soon',
541         'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
542     );
543
544     # Change policy so that loans can be renewed 99 days prior to the due date
545     # and test automatic renewal again
546     $dbh->do('UPDATE issuingrules SET norenewalbefore = 99');
547     ( $renewokay, $error ) =
548       CanBookBeRenewed( $renewing_borrowernumber, $itemnumber4 );
549     is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic' );
550     is( $error, 'auto_renew',
551         'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
552     );
553
554     # Too many renewals
555
556     # set policy to forbid renewals
557     $dbh->do('UPDATE issuingrules SET norenewalbefore = NULL, renewalsallowed = 0');
558
559     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber);
560     is( $renewokay, 0, 'Cannot renew, 0 renewals allowed');
561     is( $error, 'too_many', 'Cannot renew, 0 renewals allowed (returned code is too_many)');
562
563     # Test WhenLostForgiveFine and WhenLostChargeReplacementFee
564     t::lib::Mocks::mock_preference('WhenLostForgiveFine','1');
565     t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','1');
566
567     C4::Overdues::UpdateFine(
568         {
569             issue_id       => $issue->id(),
570             itemnumber     => $itemnumber,
571             borrowernumber => $renewing_borrower->{borrowernumber},
572             amount         => 15.00,
573             type           => q{},
574             due            => Koha::DateUtils::output_pref($datedue)
575         }
576     );
577
578     LostItem( $itemnumber, 1 );
579
580     my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber);
581     ok( !$item->onloan(), "Lost item marked as returned has false onloan value" );
582
583     my $total_due = $dbh->selectrow_array(
584         'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
585         undef, $renewing_borrower->{borrowernumber}
586     );
587
588     ok( $total_due == 12, 'Borrower only charged replacement fee with both WhenLostForgiveFine and WhenLostChargeReplacementFee enabled' );
589
590     C4::Context->dbh->do("DELETE FROM accountlines");
591
592     t::lib::Mocks::mock_preference('WhenLostForgiveFine','0');
593     t::lib::Mocks::mock_preference('WhenLostChargeReplacementFee','0');
594
595     C4::Overdues::UpdateFine(
596         {
597             issue_id       => $issue2->id(),
598             itemnumber     => $itemnumber2,
599             borrowernumber => $renewing_borrower->{borrowernumber},
600             amount         => 15.00,
601             type           => q{},
602             due            => Koha::DateUtils::output_pref($datedue)
603         }
604     );
605
606     LostItem( $itemnumber2, 0 );
607
608     my $item2 = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber2);
609     ok( $item2->onloan(), "Lost item *not* marked as returned has true onloan value" );
610
611     $total_due = $dbh->selectrow_array(
612         'SELECT SUM( amountoutstanding ) FROM accountlines WHERE borrowernumber = ?',
613         undef, $renewing_borrower->{borrowernumber}
614     );
615
616     ok( $total_due == 15, 'Borrower only charged fine with both WhenLostForgiveFine and WhenLostChargeReplacementFee disabled' );
617
618     my $future = dt_from_string();
619     $future->add( days => 7 );
620     my $units = C4::Overdues::get_chargeable_units('days', $future, $now, $library2->{branchcode});
621     ok( $units == 0, '_get_chargeable_units returns 0 for items not past due date (Bug 12596)' );
622
623     # Users cannot renew any item if there is an overdue item
624     t::lib::Mocks::mock_preference('OverduesBlockRenewing','block');
625     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber6);
626     is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
627     ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $itemnumber7);
628     is( $renewokay, 0, '(Bug 8236), Cannot renew, one of the items is overdue');
629
630   }
631
632 {
633     # GetUpcomingDueIssues tests
634     my $barcode  = 'R00000342';
635     my $barcode2 = 'R00000343';
636     my $barcode3 = 'R00000344';
637     my $branch   = $library2->{branchcode};
638
639     #Create another record
640     my $biblio2 = MARC::Record->new();
641     my $title2 = 'Something is worng here';
642     $biblio2->append_fields(
643         MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
644         MARC::Field->new('245', ' ', ' ', a => $title2),
645     );
646     my ($biblionumber2, $biblioitemnumber2) = AddBiblio($biblio2, '');
647
648     #Create third item
649     AddItem(
650         {
651             homebranch       => $branch,
652             holdingbranch    => $branch,
653             barcode          => $barcode3
654         },
655         $biblionumber2
656     );
657
658     # Create a borrower
659     my %a_borrower_data = (
660         firstname =>  'Fridolyn',
661         surname => 'SOMERS',
662         categorycode => 'S',
663         branchcode => $branch,
664     );
665
666     my $a_borrower_borrowernumber = AddMember(%a_borrower_data);
667     my $a_borrower = GetMember( borrowernumber => $a_borrower_borrowernumber );
668
669     my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
670     my $two_days_ahead = DateTime->today(time_zone => C4::Context->tz())->add( days => 2 );
671     my $today = DateTime->today(time_zone => C4::Context->tz());
672
673     my $issue = AddIssue( $a_borrower, $barcode, $yesterday );
674     my $datedue = dt_from_string( $issue->date_due() );
675     my $issue2 = AddIssue( $a_borrower, $barcode2, $two_days_ahead );
676     my $datedue2 = dt_from_string( $issue->date_due() );
677
678     my $upcoming_dues;
679
680     # GetUpcomingDueIssues tests
681     for my $i(0..1) {
682         $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
683         is ( scalar( @$upcoming_dues ), 0, "No items due in less than one day ($i days in advance)" );
684     }
685
686     #days_in_advance needs to be inclusive, so 1 matches items due tomorrow, 0 items due today etc.
687     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 } );
688     is ( scalar ( @$upcoming_dues), 1, "Only one item due in 2 days or less" );
689
690     for my $i(3..5) {
691         $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => $i } );
692         is ( scalar( @$upcoming_dues ), 1,
693             "Bug 9362: Only one item due in more than 2 days ($i days in advance)" );
694     }
695
696     # Bug 11218 - Due notices not generated - GetUpcomingDueIssues needs to select due today items as well
697
698     my $issue3 = AddIssue( $a_borrower, $barcode3, $today );
699
700     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => -1 } );
701     is ( scalar ( @$upcoming_dues), 0, "Overdues can not be selected" );
702
703     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 0 } );
704     is ( scalar ( @$upcoming_dues), 1, "1 item is due today" );
705
706     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 1 } );
707     is ( scalar ( @$upcoming_dues), 1, "1 item is due today, none tomorrow" );
708
709     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 2 }  );
710     is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
711
712     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues( { days_in_advance => 3 } );
713     is ( scalar ( @$upcoming_dues), 2, "2 items are due withing 2 days" );
714
715     $upcoming_dues = C4::Circulation::GetUpcomingDueIssues();
716     is ( scalar ( @$upcoming_dues), 2, "days_in_advance is 7 in GetUpcomingDueIssues if not provided" );
717
718 }
719
720 {
721     my $barcode  = '1234567890';
722     my $branch   = $library2->{branchcode};
723
724     my $biblio = MARC::Record->new();
725     my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
726
727     #Create third item
728     my ( undef, undef, $itemnumber ) = AddItem(
729         {
730             homebranch       => $branch,
731             holdingbranch    => $branch,
732             barcode          => $barcode
733         },
734         $biblionumber
735     );
736
737     # Create a borrower
738     my %a_borrower_data = (
739         firstname =>  'Kyle',
740         surname => 'Hall',
741         categorycode => 'S',
742         branchcode => $branch,
743     );
744
745     my $borrowernumber = AddMember(%a_borrower_data);
746
747     my $issue = AddIssue( GetMember( borrowernumber => $borrowernumber ), $barcode );
748     UpdateFine(
749         {
750             issue_id       => $issue->id(),
751             itemnumber     => $itemnumber,
752             borrowernumber => $borrowernumber,
753             amount         => 0
754         }
755     );
756
757     my $hr = $dbh->selectrow_hashref(q{SELECT COUNT(*) AS count FROM accountlines WHERE borrowernumber = ? AND itemnumber = ?}, undef, $borrowernumber, $itemnumber );
758     my $count = $hr->{count};
759
760     is ( $count, 0, "Calling UpdateFine on non-existant fine with an amount of 0 does not result in an empty fine" );
761 }
762
763 {
764     $dbh->do('DELETE FROM issues');
765     $dbh->do('DELETE FROM items');
766     $dbh->do('DELETE FROM issuingrules');
767     $dbh->do(
768         q{
769         INSERT INTO issuingrules ( categorycode, branchcode, itemtype, reservesallowed, maxissueqty, issuelength, lengthunit, renewalsallowed, renewalperiod,
770                     norenewalbefore, auto_renew, fine, chargeperiod ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
771         },
772         {},
773         '*', '*', '*', 25,
774         20,  14,  'days',
775         1,   7,
776         undef,  0,
777         .10, 1
778     );
779     my $biblio = MARC::Record->new();
780     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
781
782     my $barcode1 = '1234';
783     my ( undef, undef, $itemnumber1 ) = AddItem(
784         {
785             homebranch    => $library2->{branchcode},
786             holdingbranch => $library2->{branchcode},
787             barcode       => $barcode1,
788         },
789         $biblionumber
790     );
791     my $barcode2 = '4321';
792     my ( undef, undef, $itemnumber2 ) = AddItem(
793         {
794             homebranch    => $library2->{branchcode},
795             holdingbranch => $library2->{branchcode},
796             barcode       => $barcode2,
797         },
798         $biblionumber
799     );
800
801     my $borrowernumber1 = AddMember(
802         firstname    => 'Kyle',
803         surname      => 'Hall',
804         categorycode => 'S',
805         branchcode   => $library2->{branchcode},
806     );
807     my $borrowernumber2 = AddMember(
808         firstname    => 'Chelsea',
809         surname      => 'Hall',
810         categorycode => 'S',
811         branchcode   => $library2->{branchcode},
812     );
813
814     my $borrower1 = GetMember( borrowernumber => $borrowernumber1 );
815     my $borrower2 = GetMember( borrowernumber => $borrowernumber2 );
816
817     my $issue = AddIssue( $borrower1, $barcode1 );
818
819     my ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
820     is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with no hold on the record' );
821
822     AddReserve(
823         $library2->{branchcode}, $borrowernumber2, $biblionumber,
824         '',  1, undef, undef, '',
825         undef, undef, undef
826     );
827
828     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
829     t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
830     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
831     is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfholds are disabled' );
832
833     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 0");
834     t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
835     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
836     is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is enabled and onshelfholds is disabled' );
837
838     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
839     t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 0 );
840     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
841     is( $renewokay, 0, 'Bug 14337 - Verify the borrower cannot renew with a hold on the record if AllowRenewalIfOtherItemsAvailable is disabled and onshelfhold is enabled' );
842
843     C4::Context->dbh->do("UPDATE issuingrules SET onshelfholds = 1");
844     t::lib::Mocks::mock_preference( 'AllowRenewalIfOtherItemsAvailable', 1 );
845     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
846     is( $renewokay, 1, 'Bug 14337 - Verify the borrower can renew with a hold on the record if AllowRenewalIfOtherItemsAvailable and onshelfhold are enabled' );
847
848     # Setting item not checked out to be not for loan but holdable
849     ModItem({ notforloan => -1 }, $biblionumber, $itemnumber2);
850
851     ( $renewokay, $error ) = CanBookBeRenewed( $borrowernumber1, $itemnumber1 );
852     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' );
853 }
854
855 {
856     # Don't allow renewing onsite checkout
857     my $barcode  = 'R00000XXX';
858     my $branch   = $library->{branchcode};
859
860     #Create another record
861     my $biblio = MARC::Record->new();
862     $biblio->append_fields(
863         MARC::Field->new('100', ' ', ' ', a => 'Anonymous'),
864         MARC::Field->new('245', ' ', ' ', a => 'A title'),
865     );
866     my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
867
868     my (undef, undef, $itemnumber) = AddItem(
869         {
870             homebranch       => $branch,
871             holdingbranch    => $branch,
872             barcode          => $barcode,
873         },
874         $biblionumber
875     );
876
877     my $borrowernumber = AddMember(
878         firstname =>  'fn',
879         surname => 'dn',
880         categorycode => 'S',
881         branchcode => $branch,
882     );
883
884     my $borrower = GetMember( borrowernumber => $borrowernumber );
885     my $issue = AddIssue( $borrower, $barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
886     my ( $renewed, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
887     is( $renewed, 0, 'CanBookBeRenewed should not allow to renew on-site checkout' );
888     is( $error, 'onsite_checkout', 'A correct error code should be returned by CanBookBeRenewed for on-site checkout' );
889 }
890
891 {
892     my $library = $builder->build({ source => 'Branch' });
893
894     my $biblio = MARC::Record->new();
895     my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
896
897     my $barcode = 'just a barcode';
898     my ( undef, undef, $itemnumber ) = AddItem(
899         {
900             homebranch       => $library->{branchcode},
901             holdingbranch    => $library->{branchcode},
902             barcode          => $barcode,
903         },
904         $biblionumber,
905     );
906
907     my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode} } } );
908
909     my $issue = AddIssue( GetMember( borrowernumber => $patron->{borrowernumber} ), $barcode );
910     UpdateFine(
911         {
912             issue_id       => $issue->id(),
913             itemnumber     => $itemnumber,
914             borrowernumber => $patron->{borrowernumber},
915             amount         => 1,
916         }
917     );
918     UpdateFine(
919         {
920             issue_id       => $issue->id(),
921             itemnumber     => $itemnumber,
922             borrowernumber => $patron->{borrowernumber},
923             amount         => 2,
924         }
925     );
926     is( Koha::Account::Lines->search({ issue_id => $issue->id })->count, 1, 'UpdateFine should not create a new accountline when updating an existing fine');
927 }
928
929 subtest 'CanBookBeIssued & AllowReturnToBranch' => sub {
930     plan tests => 23;
931
932     my $homebranch    = $builder->build( { source => 'Branch' } );
933     my $holdingbranch = $builder->build( { source => 'Branch' } );
934     my $otherbranch   = $builder->build( { source => 'Branch' } );
935     my $patron_1      = $builder->build( { source => 'Borrower' } );
936     my $patron_2      = $builder->build( { source => 'Borrower' } );
937
938     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
939     my $item = $builder->build(
940         {   source => 'Item',
941             value  => {
942                 homebranch    => $homebranch->{branchcode},
943                 holdingbranch => $holdingbranch->{branchcode},
944                 notforloan    => 0,
945                 itemlost      => 0,
946                 withdrawn     => 0,
947                 biblionumber  => $biblioitem->{biblionumber}
948             }
949         }
950     );
951
952     set_userenv($holdingbranch);
953
954     my $issue = AddIssue( $patron_1, $item->{barcode} );
955     is( ref($issue), 'Koha::Schema::Result::Issue' );    # FIXME Should be Koha::Issue
956
957     my ( $error, $question, $alerts );
958
959     # AllowReturnToBranch == anywhere
960     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
961     ## Can be issued from homebranch
962     set_userenv($homebranch);
963     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
964     is( keys(%$error) + keys(%$alerts),        0 );
965     is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
966     ## Can be issued from holdingbranch
967     set_userenv($holdingbranch);
968     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
969     is( keys(%$error) + keys(%$alerts),        0 );
970     is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
971     ## Can be issued from another branch
972     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
973     is( keys(%$error) + keys(%$alerts),        0 );
974     is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
975
976     # AllowReturnToBranch == holdingbranch
977     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
978     ## Cannot be issued from homebranch
979     set_userenv($homebranch);
980     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
981     is( keys(%$question) + keys(%$alerts),  0 );
982     is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
983     is( $error->{branch_to_return},         $holdingbranch->{branchcode} );
984     ## Can be issued from holdinbranch
985     set_userenv($holdingbranch);
986     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
987     is( keys(%$error) + keys(%$alerts),        0 );
988     is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
989     ## Cannot be issued from another branch
990     set_userenv($otherbranch);
991     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
992     is( keys(%$question) + keys(%$alerts),  0 );
993     is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
994     is( $error->{branch_to_return},         $holdingbranch->{branchcode} );
995
996     # AllowReturnToBranch == homebranch
997     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
998     ## Can be issued from holdinbranch
999     set_userenv($homebranch);
1000     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1001     is( keys(%$error) + keys(%$alerts),        0 );
1002     is( exists $question->{ISSUED_TO_ANOTHER}, 1 );
1003     ## Cannot be issued from holdinbranch
1004     set_userenv($holdingbranch);
1005     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1006     is( keys(%$question) + keys(%$alerts),  0 );
1007     is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1008     is( $error->{branch_to_return},         $homebranch->{branchcode} );
1009     ## Cannot be issued from holdinbranch
1010     set_userenv($otherbranch);
1011     ( $error, $question, $alerts ) = CanBookBeIssued( $patron_2, $item->{barcode} );
1012     is( keys(%$question) + keys(%$alerts),  0 );
1013     is( exists $error->{RETURN_IMPOSSIBLE}, 1 );
1014     is( $error->{branch_to_return},         $homebranch->{branchcode} );
1015
1016     # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1017 };
1018
1019 subtest 'AddIssue & AllowReturnToBranch' => sub {
1020     plan tests => 9;
1021
1022     my $homebranch    = $builder->build( { source => 'Branch' } );
1023     my $holdingbranch = $builder->build( { source => 'Branch' } );
1024     my $otherbranch   = $builder->build( { source => 'Branch' } );
1025     my $patron_1      = $builder->build( { source => 'Borrower' } );
1026     my $patron_2      = $builder->build( { source => 'Borrower' } );
1027
1028     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
1029     my $item = $builder->build(
1030         {   source => 'Item',
1031             value  => {
1032                 homebranch    => $homebranch->{branchcode},
1033                 holdingbranch => $holdingbranch->{branchcode},
1034                 notforloan    => 0,
1035                 itemlost      => 0,
1036                 withdrawn     => 0,
1037                 biblionumber  => $biblioitem->{biblionumber}
1038             }
1039         }
1040     );
1041
1042     set_userenv($holdingbranch);
1043
1044     my $ref_issue = 'Koha::Schema::Result::Issue'; # FIXME Should be Koha::Issue
1045     my $issue = AddIssue( $patron_1, $item->{barcode} );
1046
1047     my ( $error, $question, $alerts );
1048
1049     # AllowReturnToBranch == homebranch
1050     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'anywhere' );
1051     ## Can be issued from homebranch
1052     set_userenv($homebranch);
1053     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1054     set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1055     ## Can be issued from holdinbranch
1056     set_userenv($holdingbranch);
1057     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1058     set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1059     ## Can be issued from another branch
1060     set_userenv($otherbranch);
1061     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1062     set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1063
1064     # AllowReturnToBranch == holdinbranch
1065     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'holdingbranch' );
1066     ## Cannot be issued from homebranch
1067     set_userenv($homebranch);
1068     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1069     ## Can be issued from holdingbranch
1070     set_userenv($holdingbranch);
1071     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1072     set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1073     ## Cannot be issued from another branch
1074     set_userenv($otherbranch);
1075     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1076
1077     # AllowReturnToBranch == homebranch
1078     t::lib::Mocks::mock_preference( 'AllowReturnToBranch', 'homebranch' );
1079     ## Can be issued from homebranch
1080     set_userenv($homebranch);
1081     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), $ref_issue );
1082     set_userenv($holdingbranch); AddIssue( $patron_1, $item->{barcode} ); # Reinsert the original issue
1083     ## Cannot be issued from holdinbranch
1084     set_userenv($holdingbranch);
1085     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1086     ## Cannot be issued from another branch
1087     set_userenv($otherbranch);
1088     is ( ref( AddIssue( $patron_2, $item->{barcode} ) ), '' );
1089     # TODO t::lib::Mocks::mock_preference('AllowReturnToBranch', 'homeorholdingbranch');
1090 };
1091
1092
1093 sub set_userenv {
1094     my ( $library ) = @_;
1095     C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', '');
1096 }
1097
1098 1;