Bug 29523: Regression tests
[koha.git] / t / db_dependent / Koha / Checkouts.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 11;
23
24 use C4::Circulation qw( MarkIssueReturned AddReturn );
25 use C4::Reserves qw( AddReserve );
26 use Koha::Checkouts;
27 use Koha::Database;
28 use Koha::DateUtils qw( dt_from_string );
29 use Koha::Holds;
30
31 use t::lib::TestBuilder;
32 use t::lib::Mocks;
33
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36
37 my $builder = t::lib::TestBuilder->new;
38 my $library = $builder->build( { source => 'Branch' } );
39 my $patron  = $builder->build(
40     { source => 'Borrower', value => { branchcode => $library->{branchcode} } }
41 );
42 my $item_1          = $builder->build_sample_item;
43 my $item_2          = $builder->build_sample_item;
44 my $nb_of_checkouts = Koha::Checkouts->search->count;
45 my $new_checkout_1  = Koha::Checkout->new(
46     {
47         borrowernumber => $patron->{borrowernumber},
48         itemnumber     => $item_1->itemnumber,
49         branchcode     => $library->{branchcode},
50     }
51 )->store;
52 my $new_checkout_2 = Koha::Checkout->new(
53     {
54         borrowernumber => $patron->{borrowernumber},
55         itemnumber     => $item_2->itemnumber,
56         branchcode     => $library->{branchcode},
57     }
58 )->store;
59
60 like( $new_checkout_1->issue_id, qr|^\d+$|,
61     'Adding a new checkout should have set the issue_id' );
62 is(
63     Koha::Checkouts->search->count,
64     $nb_of_checkouts + 2,
65     'The 2 checkouts should have been added'
66 );
67
68 my $retrieved_checkout_1 = Koha::Checkouts->find( $new_checkout_1->issue_id );
69 is(
70     $retrieved_checkout_1->itemnumber,
71     $new_checkout_1->itemnumber,
72     'Find a checkout by id should return the correct checkout'
73 );
74
75 subtest 'is_overdue' => sub {
76     plan tests => 6;
77     my $ten_days_ago   = dt_from_string->add( days => -10 );
78     my $ten_days_later = dt_from_string->add( days => 10 );
79     my $yesterday      = dt_from_string->add( days => -1 );
80     my $tomorrow       = dt_from_string->add( days => 1 );
81
82     $retrieved_checkout_1->date_due($ten_days_ago)->store;
83     is( $retrieved_checkout_1->is_overdue,
84         1, 'The item should have been returned 10 days ago' );
85
86     $retrieved_checkout_1->date_due($ten_days_later)->store;
87     is( $retrieved_checkout_1->is_overdue, 0, 'The item is due in 10 days' );
88
89     $retrieved_checkout_1->date_due($tomorrow)->store;
90     is( $retrieved_checkout_1->is_overdue($ten_days_later),
91         1, 'The item should have been returned yesterday' );
92
93     $retrieved_checkout_1->date_due($yesterday)->store;
94     is( $retrieved_checkout_1->is_overdue($ten_days_ago),
95         0, 'Ten days ago the item due yesterday was not late' );
96
97     $retrieved_checkout_1->date_due($tomorrow)->store;
98     is( $retrieved_checkout_1->is_overdue($ten_days_later),
99         1, 'In Ten days, the item due tomorrow will be late' );
100
101     $retrieved_checkout_1->date_due($yesterday)->store;
102     is( $retrieved_checkout_1->is_overdue($ten_days_ago),
103         0, 'In Ten days, the item due yesterday will still be late' );
104 };
105
106 subtest 'item' => sub {
107     plan tests => 2;
108     my $item = $retrieved_checkout_1->item;
109     is( ref($item), 'Koha::Item',
110         'Koha::Checkout->item should return a Koha::Item' );
111     is( $item->itemnumber, $item_1->itemnumber,
112         'Koha::Checkout->item should return the correct item' );
113 };
114
115 subtest 'account_lines' => sub {
116     plan tests => 3;
117
118     my $accountline = Koha::Account::Line->new(
119         {
120             issue_id          => $retrieved_checkout_1->id,
121             borrowernumber    => $retrieved_checkout_1->borrowernumber,
122             itemnumber        => $retrieved_checkout_1->itemnumber,
123             branchcode        => $retrieved_checkout_1->branchcode,
124             date              => \'NOW()',
125             debit_type_code   => 'OVERDUE',
126             status            => 'UNRETURNED',
127             interface         => 'cli',
128             amount            => '1',
129             amountoutstanding => '1',
130         }
131     )->store();
132
133     my $account_lines = $retrieved_checkout_1->account_lines;
134     is( ref($account_lines), 'Koha::Account::Lines',
135         'Koha::Checkout->account_lines should return a Koha::Account::Lines' );
136
137     my $line = $account_lines->next;
138     is( ref($line), 'Koha::Account::Line',
139         'next returns a Koha::Account::Line' );
140
141     is(
142         $accountline->id,
143         $line->id,
144         'Koha::Checkout->account_lines should return the correct account_lines'
145     );
146 };
147
148 subtest 'patron' => sub {
149     plan tests => 3;
150     my $patron = $builder->build_object(
151         {
152             class => 'Koha::Patrons',
153             value => { branchcode => $library->{branchcode} }
154         }
155     );
156
157     my $item     = $builder->build_sample_item;
158     my $checkout = Koha::Checkout->new(
159         {
160             borrowernumber => $patron->borrowernumber,
161             itemnumber     => $item->itemnumber,
162             branchcode     => $library->{branchcode},
163         }
164     )->store;
165
166     my $p = $checkout->patron;
167     is( ref($p), 'Koha::Patron',
168         'Koha::Checkout->patron should return a Koha::Patron' );
169     is( $p->borrowernumber, $patron->borrowernumber,
170         'Koha::Checkout->patron should return the correct patron' );
171
172     # Testing Koha::Old::Checkout->patron now
173     my $issue_id = $checkout->issue_id;
174     C4::Circulation::MarkIssueReturned( $p->borrowernumber,
175         $checkout->itemnumber );
176     $p->delete;
177     my $old_issue = Koha::Old::Checkouts->find($issue_id);
178     is( $old_issue->patron, undef,
179 'Koha::Checkout->patron should return undef if the patron record has been deleted'
180     );
181 };
182
183 $retrieved_checkout_1->delete;
184 is(
185     Koha::Checkouts->search->count,
186     $nb_of_checkouts + 1,
187     'Delete should have deleted the checkout'
188 );
189
190 subtest 'issuer' => sub {
191     plan tests => 3;
192     my $patron = $builder->build_object(
193         {
194             class => 'Koha::Patrons',
195             value => { branchcode => $library->{branchcode} }
196         }
197     );
198     my $issuer = $builder->build_object(
199         {
200             class => 'Koha::Patrons',
201             value => { branchcode => $library->{branchcode} }
202         }
203     );
204
205     my $item     = $builder->build_sample_item;
206     my $checkout = Koha::Checkout->new(
207         {
208             borrowernumber => $patron->borrowernumber,
209             issuer_id      => $issuer->borrowernumber,
210             itemnumber     => $item->itemnumber,
211             branchcode     => $library->{branchcode},
212         }
213     )->store;
214
215     my $i = $checkout->issuer;
216     is( ref($i), 'Koha::Patron',
217         'Koha::Checkout->issuer should return a Koha::Patron' );
218     is( $i->borrowernumber, $issuer->borrowernumber,
219         'Koha::Checkout->issuer should return the correct patron' );
220
221     # Testing Koha::Old::Checkout->patron now
222     my $issue_id = $checkout->issue_id;
223     C4::Circulation::MarkIssueReturned( $patron->borrowernumber,
224         $checkout->itemnumber );
225     $i->delete;
226     my $old_issue = Koha::Old::Checkouts->find($issue_id);
227     is( $old_issue->issuer_id, undef,
228 'Koha::Checkout->issuer_id should return undef if the patron record has been deleted'
229     );
230
231 };
232
233 subtest 'Koha::Old::Checkouts->filter_by_todays_checkins' => sub {
234
235     plan tests => 3;
236
237     # We will create 7 checkins for a given patron
238     # 3 checked in today - 2 days, and 4 checked in today
239     my $librarian = $builder->build_object(
240         {
241             class => 'Koha::Patrons',
242             value => { branchcode => $library->{branchcode} }
243         }
244     );
245     t::lib::Mocks::mock_userenv( { patron => $librarian } );
246     my $patron = $builder->build_object(
247         {
248             class => 'Koha::Patrons',
249             value => { branchcode => $library->{branchcode} }
250         }
251     );
252
253     my @checkouts;
254     # Create 7 checkouts
255     for ( 0 .. 6 ) {
256         my $item = $builder->build_sample_item;
257         push @checkouts,
258           Koha::Checkout->new(
259             {
260                 borrowernumber => $patron->borrowernumber,
261                 itemnumber     => $item->itemnumber,
262                 branchcode     => $library->{branchcode},
263             }
264         )->store;
265     }
266
267     # Checkin 3 today - 2 days
268     my $not_today = dt_from_string->add( days => -2 );
269     for my $i ( 0 .. 2 ) {
270         my $checkout = $checkouts[$i];
271         C4::Circulation::AddReturn(
272             $checkout->item->barcode, $library->{branchcode},
273             undef, $not_today->set_hour( int( rand(24) ) )
274         );
275     }
276     # Checkin 4 today
277     my $today = dt_from_string;
278     for my $i ( 3 .. 6 ) {
279         my $checkout = $checkouts[$i];
280         C4::Circulation::AddReturn(
281             $checkout->item->barcode, $library->{branchcode},
282             undef, $today->set_hour( int( rand(24) ) )
283         );
284     }
285
286     my $old_checkouts = $patron->old_checkouts;
287     is( $old_checkouts->count, 7, 'There should be 7 old checkouts' );
288     my $todays_checkins = $old_checkouts->filter_by_todays_checkins;
289     is( $todays_checkins->count, 4, 'There should be 4 checkins today' );
290     is_deeply(
291         [ $todays_checkins->get_column('itemnumber') ],
292         [ map { $_->itemnumber } @checkouts[ 3 .. 6 ] ],
293         q{Correct list of today's checkins}
294     );
295 };
296
297 $schema->storage->txn_rollback;
298
299 subtest 'automatic_checkin' => sub {
300
301     plan tests => 10;
302
303     $schema->storage->txn_begin;
304
305     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
306
307     my $due_ac_item =
308       $builder->build_sample_item(
309         { homebranch => $patron->branchcode, itemlost => 0 } );
310     my $ac_item =
311       $builder->build_sample_item(
312         { homebranch => $patron->branchcode, itemlost => 0 } );
313     my $odue_ac_item =
314       $builder->build_sample_item(
315         { homebranch => $patron->branchcode, itemlost => 0 } );
316     my $normal_item =
317       $builder->build_sample_item(
318         { homebranch => $patron->branchcode, itemlost => 0 } );
319
320     $due_ac_item->itemtype->automatic_checkin(1)->store;
321     $odue_ac_item->itemtype->automatic_checkin(1)->store;
322     $ac_item->itemtype->automatic_checkin(1)->store;
323     $normal_item->itemtype->automatic_checkin(0)->store;
324
325     my $today     = dt_from_string;
326     my $tomorrow  = dt_from_string->add( days => 1 );
327     my $yesterday = dt_from_string->subtract( days => 1 );
328
329     # Checkout do for automatic checkin
330     my $checkout_due_aci = Koha::Checkout->new(
331         {
332             borrowernumber => $patron->borrowernumber,
333             itemnumber     => $due_ac_item->itemnumber,
334             branchcode     => $patron->branchcode,
335             date_due       => $today,
336         }
337     )->store;
338
339     # Checkout not due for automatic checkin
340     my $checkout_odue_aci = Koha::Checkout->new(
341         {
342             borrowernumber => $patron->borrowernumber,
343             itemnumber     => $odue_ac_item->itemnumber,
344             branchcode     => $patron->branchcode,
345             date_due       => $yesterday
346         }
347     )->store;
348
349     # Checkout not due for automatic checkin
350     my $checkout_aci = Koha::Checkout->new(
351         {
352             borrowernumber => $patron->borrowernumber,
353             itemnumber     => $ac_item->itemnumber,
354             branchcode     => $patron->branchcode,
355             date_due       => $tomorrow
356         }
357     )->store;
358
359     # due checkout for nomal itemtype
360     my $checkout_ni = Koha::Checkout->new(
361         {
362             borrowernumber => $patron->borrowernumber,
363             itemnumber     => $normal_item->itemnumber,
364             branchcode     => $patron->branchcode,
365             date_due       => $today,
366         }
367     )->store;
368
369     my $searched = Koha::Checkouts->find( $checkout_ni->issue_id );
370     is( $searched->issue_id, $checkout_ni->issue_id,
371         'checkout for normal_item exists' );
372
373     $searched = Koha::Checkouts->find( $checkout_aci->issue_id );
374     is( $searched->issue_id, $checkout_aci->issue_id,
375         'checkout for ac_item exists' );
376
377     $searched = Koha::Checkouts->find( $checkout_due_aci->issue_id );
378     is(
379         $searched->issue_id,
380         $checkout_due_aci->issue_id,
381         'checkout for due_ac_item exists'
382     );
383
384     $searched = Koha::Checkouts->find( $checkout_odue_aci->issue_id );
385     is(
386         $searched->issue_id,
387         $checkout_odue_aci->issue_id,
388         'checkout for odue_ac_item exists'
389     );
390
391     Koha::Checkouts->automatic_checkin;
392
393     $searched = Koha::Checkouts->find( $checkout_ni->issue_id );
394     is( $searched->issue_id, $checkout_ni->issue_id,
395         'checkout for normal_item still exists' );
396
397     $searched = Koha::Checkouts->find( $checkout_aci->issue_id );
398     is( $searched->issue_id, $checkout_aci->issue_id,
399         'checkout for ac_item still exists' );
400
401     $searched = Koha::Checkouts->find( $checkout_due_aci->issue_id );
402     is( $searched, undef, 'checkout for due_ac_item doesn\'t exist anymore' );
403
404     $searched = Koha::Checkouts->find( $checkout_odue_aci->issue_id );
405     is( $searched, undef, 'checkout for odue_ac_item doesn\'t exist anymore' );
406
407     $searched = Koha::Old::Checkouts->find( $checkout_odue_aci->issue_id );
408     is( dt_from_string($searched->returndate), $yesterday, 'old checkout for odue_ac_item has the right return date' );
409
410
411     subtest 'automatic_checkin AutomaticCheckinAutoFill tests' => sub {
412
413         plan tests => 3;
414
415         my $checkout_2_due_ac = Koha::Checkout->new(
416             {
417                 borrowernumber => $patron->borrowernumber,
418                 itemnumber     => $due_ac_item->itemnumber,
419                 branchcode     => $patron->branchcode,
420                 date_due       => $today
421             }
422         )->store;
423
424         my $patron_2 =
425             $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $patron->branchcode } } );
426         my $reserveid = AddReserve(
427             {
428                 branchcode     => $patron->branchcode,
429                 borrowernumber => $patron_2->id,
430                 biblionumber   => $due_ac_item->biblionumber,
431                 priority       => 1
432             }
433         );
434
435         t::lib::Mocks::mock_preference( 'AutomaticCheckinAutoFill', '0' );
436
437         Koha::Checkouts->automatic_checkin;
438         my $reserve = Koha::Holds->find($reserveid);
439
440         is( $reserve->found, undef, "Hold was not filled when AutomaticCheckinAutoFill disabled" );
441
442         my $checkout_3_due_ac = Koha::Checkout->new(
443             {
444                 borrowernumber => $patron->borrowernumber,
445                 itemnumber     => $due_ac_item->itemnumber,
446                 branchcode     => $patron->branchcode,
447                 date_due       => $today
448             }
449         )->store;
450         t::lib::Mocks::mock_preference( 'AutomaticCheckinAutoFill', '1' );
451
452         Koha::Checkouts->automatic_checkin;
453         $reserve->discard_changes;
454
455         is( $reserve->found, 'W', "Hold was filled when AutomaticCheckinAutoFill enabled" );
456
457         my $checkout_2_odue_ac = Koha::Checkout->new(
458             {
459                 borrowernumber => $patron->borrowernumber,
460                 itemnumber     => $odue_ac_item->itemnumber,
461                 branchcode     => $patron->branchcode,
462                 date_due       => $today
463             }
464         )->store;
465         my $branch2    = $builder->build_object( { class => "Koha::Libraries" } );
466         my $reserve2id = AddReserve(
467             {
468                 branchcode     => $branch2->branchcode,
469                 borrowernumber => $patron_2->id,
470                 biblionumber   => $odue_ac_item->biblionumber,
471                 priority       => 1
472             }
473         );
474         Koha::Checkouts->automatic_checkin;
475
476         my $reserve2 = Koha::Holds->find($reserve2id);
477         is(
478             $reserve2->found, 'T',
479             "Hold was filled when AutomaticCheckinAutoFill enabled and transfer was initiated when branches didn't match"
480         );
481     };
482
483     $schema->storage->txn_rollback;
484 }