3 # Copyright 2015 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 11;
24 use C4::Circulation qw( MarkIssueReturned AddReturn );
27 use Koha::DateUtils qw( dt_from_string );
29 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new;
36 my $library = $builder->build( { source => 'Branch' } );
37 my $patron = $builder->build(
38 { source => 'Borrower', value => { branchcode => $library->{branchcode} } }
40 my $item_1 = $builder->build_sample_item;
41 my $item_2 = $builder->build_sample_item;
42 my $nb_of_checkouts = Koha::Checkouts->search->count;
43 my $new_checkout_1 = Koha::Checkout->new(
45 borrowernumber => $patron->{borrowernumber},
46 itemnumber => $item_1->itemnumber,
47 branchcode => $library->{branchcode},
50 my $new_checkout_2 = Koha::Checkout->new(
52 borrowernumber => $patron->{borrowernumber},
53 itemnumber => $item_2->itemnumber,
54 branchcode => $library->{branchcode},
58 like( $new_checkout_1->issue_id, qr|^\d+$|,
59 'Adding a new checkout should have set the issue_id' );
61 Koha::Checkouts->search->count,
63 'The 2 checkouts should have been added'
66 my $retrieved_checkout_1 = Koha::Checkouts->find( $new_checkout_1->issue_id );
68 $retrieved_checkout_1->itemnumber,
69 $new_checkout_1->itemnumber,
70 'Find a checkout by id should return the correct checkout'
73 subtest 'is_overdue' => sub {
75 my $ten_days_ago = dt_from_string->add( days => -10 );
76 my $ten_days_later = dt_from_string->add( days => 10 );
77 my $yesterday = dt_from_string->add( days => -1 );
78 my $tomorrow = dt_from_string->add( days => 1 );
80 $retrieved_checkout_1->date_due($ten_days_ago)->store;
81 is( $retrieved_checkout_1->is_overdue,
82 1, 'The item should have been returned 10 days ago' );
84 $retrieved_checkout_1->date_due($ten_days_later)->store;
85 is( $retrieved_checkout_1->is_overdue, 0, 'The item is due in 10 days' );
87 $retrieved_checkout_1->date_due($tomorrow)->store;
88 is( $retrieved_checkout_1->is_overdue($ten_days_later),
89 1, 'The item should have been returned yesterday' );
91 $retrieved_checkout_1->date_due($yesterday)->store;
92 is( $retrieved_checkout_1->is_overdue($ten_days_ago),
93 0, 'Ten days ago the item due yesterday was not late' );
95 $retrieved_checkout_1->date_due($tomorrow)->store;
96 is( $retrieved_checkout_1->is_overdue($ten_days_later),
97 1, 'In Ten days, the item due tomorrow will be late' );
99 $retrieved_checkout_1->date_due($yesterday)->store;
100 is( $retrieved_checkout_1->is_overdue($ten_days_ago),
101 0, 'In Ten days, the item due yesterday will still be late' );
104 subtest 'item' => sub {
106 my $item = $retrieved_checkout_1->item;
107 is( ref($item), 'Koha::Item',
108 'Koha::Checkout->item should return a Koha::Item' );
109 is( $item->itemnumber, $item_1->itemnumber,
110 'Koha::Checkout->item should return the correct item' );
113 subtest 'account_lines' => sub {
116 my $accountline = Koha::Account::Line->new(
118 issue_id => $retrieved_checkout_1->id,
119 borrowernumber => $retrieved_checkout_1->borrowernumber,
120 itemnumber => $retrieved_checkout_1->itemnumber,
121 branchcode => $retrieved_checkout_1->branchcode,
123 debit_type_code => 'OVERDUE',
124 status => 'UNRETURNED',
127 amountoutstanding => '1',
131 my $account_lines = $retrieved_checkout_1->account_lines;
132 is( ref($account_lines), 'Koha::Account::Lines',
133 'Koha::Checkout->account_lines should return a Koha::Account::Lines' );
135 my $line = $account_lines->next;
136 is( ref($line), 'Koha::Account::Line',
137 'next returns a Koha::Account::Line' );
142 'Koha::Checkout->account_lines should return the correct account_lines'
146 subtest 'patron' => sub {
148 my $patron = $builder->build_object(
150 class => 'Koha::Patrons',
151 value => { branchcode => $library->{branchcode} }
155 my $item = $builder->build_sample_item;
156 my $checkout = Koha::Checkout->new(
158 borrowernumber => $patron->borrowernumber,
159 itemnumber => $item->itemnumber,
160 branchcode => $library->{branchcode},
164 my $p = $checkout->patron;
165 is( ref($p), 'Koha::Patron',
166 'Koha::Checkout->patron should return a Koha::Patron' );
167 is( $p->borrowernumber, $patron->borrowernumber,
168 'Koha::Checkout->patron should return the correct patron' );
170 # Testing Koha::Old::Checkout->patron now
171 my $issue_id = $checkout->issue_id;
172 C4::Circulation::MarkIssueReturned( $p->borrowernumber,
173 $checkout->itemnumber );
175 my $old_issue = Koha::Old::Checkouts->find($issue_id);
176 is( $old_issue->patron, undef,
177 'Koha::Checkout->patron should return undef if the patron record has been deleted'
181 $retrieved_checkout_1->delete;
183 Koha::Checkouts->search->count,
184 $nb_of_checkouts + 1,
185 'Delete should have deleted the checkout'
188 subtest 'issuer' => sub {
190 my $patron = $builder->build_object(
192 class => 'Koha::Patrons',
193 value => { branchcode => $library->{branchcode} }
196 my $issuer = $builder->build_object(
198 class => 'Koha::Patrons',
199 value => { branchcode => $library->{branchcode} }
203 my $item = $builder->build_sample_item;
204 my $checkout = Koha::Checkout->new(
206 borrowernumber => $patron->borrowernumber,
207 issuer_id => $issuer->borrowernumber,
208 itemnumber => $item->itemnumber,
209 branchcode => $library->{branchcode},
213 my $i = $checkout->issuer;
214 is( ref($i), 'Koha::Patron',
215 'Koha::Checkout->issuer should return a Koha::Patron' );
216 is( $i->borrowernumber, $issuer->borrowernumber,
217 'Koha::Checkout->issuer should return the correct patron' );
219 # Testing Koha::Old::Checkout->patron now
220 my $issue_id = $checkout->issue_id;
221 C4::Circulation::MarkIssueReturned( $patron->borrowernumber,
222 $checkout->itemnumber );
224 my $old_issue = Koha::Old::Checkouts->find($issue_id);
225 is( $old_issue->issuer_id, undef,
226 'Koha::Checkout->issuer_id should return undef if the patron record has been deleted'
231 subtest 'Koha::Old::Checkouts->filter_by_todays_checkins' => sub {
235 # We will create 7 checkins for a given patron
236 # 3 checked in today - 2 days, and 4 checked in today
237 my $librarian = $builder->build_object(
239 class => 'Koha::Patrons',
240 value => { branchcode => $library->{branchcode} }
243 t::lib::Mocks::mock_userenv( { patron => $librarian } );
244 my $patron = $builder->build_object(
246 class => 'Koha::Patrons',
247 value => { branchcode => $library->{branchcode} }
254 my $item = $builder->build_sample_item;
258 borrowernumber => $patron->borrowernumber,
259 itemnumber => $item->itemnumber,
260 branchcode => $library->{branchcode},
265 # Checkin 3 today - 2 days
266 my $not_today = dt_from_string->add( days => -2 );
267 for my $i ( 0 .. 2 ) {
268 my $checkout = $checkouts[$i];
269 C4::Circulation::AddReturn(
270 $checkout->item->barcode, $library->{branchcode},
271 undef, $not_today->set_hour( int( rand(24) ) )
275 my $today = dt_from_string;
276 for my $i ( 3 .. 6 ) {
277 my $checkout = $checkouts[$i];
278 C4::Circulation::AddReturn(
279 $checkout->item->barcode, $library->{branchcode},
280 undef, $today->set_hour( int( rand(24) ) )
284 my $old_checkouts = $patron->old_checkouts;
285 is( $old_checkouts->count, 7, 'There should be 7 old checkouts' );
286 my $todays_checkins = $old_checkouts->filter_by_todays_checkins;
287 is( $todays_checkins->count, 4, 'There should be 4 checkins today' );
289 [ $todays_checkins->get_column('itemnumber') ],
290 [ map { $_->itemnumber } @checkouts[ 3 .. 6 ] ],
291 q{Correct list of today's checkins}
295 $schema->storage->txn_rollback;
297 subtest 'automatic_checkin' => sub {
301 $schema->storage->txn_begin;
303 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
306 $builder->build_sample_item(
307 { homebranch => $patron->branchcode, itemlost => 0 } );
309 $builder->build_sample_item(
310 { homebranch => $patron->branchcode, itemlost => 0 } );
312 $builder->build_sample_item(
313 { homebranch => $patron->branchcode, itemlost => 0 } );
315 $builder->build_sample_item(
316 { homebranch => $patron->branchcode, itemlost => 0 } );
318 $due_ac_item->itemtype->automatic_checkin(1)->store;
319 $odue_ac_item->itemtype->automatic_checkin(1)->store;
320 $ac_item->itemtype->automatic_checkin(1)->store;
321 $normal_item->itemtype->automatic_checkin(0)->store;
323 my $today = dt_from_string;
324 my $tomorrow = dt_from_string->add( days => 1 );
325 my $yesterday = dt_from_string->subtract( days => 1 );
327 # Checkout do for automatic checkin
328 my $checkout_due_aci = Koha::Checkout->new(
330 borrowernumber => $patron->borrowernumber,
331 itemnumber => $due_ac_item->itemnumber,
332 branchcode => $patron->branchcode,
337 # Checkout not due for automatic checkin
338 my $checkout_odue_aci = Koha::Checkout->new(
340 borrowernumber => $patron->borrowernumber,
341 itemnumber => $odue_ac_item->itemnumber,
342 branchcode => $patron->branchcode,
343 date_due => $yesterday
347 # Checkout not due for automatic checkin
348 my $checkout_aci = Koha::Checkout->new(
350 borrowernumber => $patron->borrowernumber,
351 itemnumber => $ac_item->itemnumber,
352 branchcode => $patron->branchcode,
353 date_due => $tomorrow
357 # due checkout for nomal itemtype
358 my $checkout_ni = Koha::Checkout->new(
360 borrowernumber => $patron->borrowernumber,
361 itemnumber => $normal_item->itemnumber,
362 branchcode => $patron->branchcode,
367 my $searched = Koha::Checkouts->find( $checkout_ni->issue_id );
368 is( $searched->issue_id, $checkout_ni->issue_id,
369 'checkout for normal_item exists' );
371 $searched = Koha::Checkouts->find( $checkout_aci->issue_id );
372 is( $searched->issue_id, $checkout_aci->issue_id,
373 'checkout for ac_item exists' );
375 $searched = Koha::Checkouts->find( $checkout_due_aci->issue_id );
378 $checkout_due_aci->issue_id,
379 'checkout for due_ac_item exists'
382 $searched = Koha::Checkouts->find( $checkout_odue_aci->issue_id );
385 $checkout_odue_aci->issue_id,
386 'checkout for odue_ac_item exists'
389 Koha::Checkouts->automatic_checkin;
391 $searched = Koha::Checkouts->find( $checkout_ni->issue_id );
392 is( $searched->issue_id, $checkout_ni->issue_id,
393 'checkout for normal_item still exists' );
395 $searched = Koha::Checkouts->find( $checkout_aci->issue_id );
396 is( $searched->issue_id, $checkout_aci->issue_id,
397 'checkout for ac_item still exists' );
399 $searched = Koha::Checkouts->find( $checkout_due_aci->issue_id );
400 is( $searched, undef, 'checkout for due_ac_item doesn\'t exist anymore' );
402 $searched = Koha::Checkouts->find( $checkout_odue_aci->issue_id );
403 is( $searched, undef, 'checkout for odue_ac_item doesn\'t exist anymore' );
405 $searched = Koha::Old::Checkouts->find( $checkout_odue_aci->issue_id );
406 is( dt_from_string($searched->returndate), $yesterday, 'old checkout for odue_ac_item has the right return date' );
408 $schema->storage->txn_rollback;