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