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 => 10;
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 'patron' => sub {
115 my $patron = $builder->build_object(
117 class => 'Koha::Patrons',
118 value => { branchcode => $library->{branchcode} }
122 my $item = $builder->build_sample_item;
123 my $checkout = Koha::Checkout->new(
125 borrowernumber => $patron->borrowernumber,
126 itemnumber => $item->itemnumber,
127 branchcode => $library->{branchcode},
131 my $p = $checkout->patron;
132 is( ref($p), 'Koha::Patron',
133 'Koha::Checkout->patron should return a Koha::Patron' );
134 is( $p->borrowernumber, $patron->borrowernumber,
135 'Koha::Checkout->patron should return the correct patron' );
137 # Testing Koha::Old::Checkout->patron now
138 my $issue_id = $checkout->issue_id;
139 C4::Circulation::MarkIssueReturned( $p->borrowernumber,
140 $checkout->itemnumber );
142 my $old_issue = Koha::Old::Checkouts->find($issue_id);
143 is( $old_issue->patron, undef,
144 'Koha::Checkout->patron should return undef if the patron record has been deleted'
148 $retrieved_checkout_1->delete;
150 Koha::Checkouts->search->count,
151 $nb_of_checkouts + 1,
152 'Delete should have deleted the checkout'
155 subtest 'issuer' => sub {
157 my $patron = $builder->build_object(
159 class => 'Koha::Patrons',
160 value => { branchcode => $library->{branchcode} }
163 my $issuer = $builder->build_object(
165 class => 'Koha::Patrons',
166 value => { branchcode => $library->{branchcode} }
170 my $item = $builder->build_sample_item;
171 my $checkout = Koha::Checkout->new(
173 borrowernumber => $patron->borrowernumber,
174 issuer_id => $issuer->borrowernumber,
175 itemnumber => $item->itemnumber,
176 branchcode => $library->{branchcode},
180 my $i = $checkout->issuer;
181 is( ref($i), 'Koha::Patron',
182 'Koha::Checkout->issuer should return a Koha::Patron' );
183 is( $i->borrowernumber, $issuer->borrowernumber,
184 'Koha::Checkout->issuer should return the correct patron' );
186 # Testing Koha::Old::Checkout->patron now
187 my $issue_id = $checkout->issue_id;
188 C4::Circulation::MarkIssueReturned( $patron->borrowernumber,
189 $checkout->itemnumber );
191 my $old_issue = Koha::Old::Checkouts->find($issue_id);
192 is( $old_issue->issuer_id, undef,
193 'Koha::Checkout->issuer_id should return undef if the patron record has been deleted'
198 subtest 'Koha::Old::Checkouts->filter_by_todays_checkins' => sub {
202 # We will create 7 checkins for a given patron
203 # 3 checked in today - 2 days, and 4 checked in today
204 my $librarian = $builder->build_object(
206 class => 'Koha::Patrons',
207 value => { branchcode => $library->{branchcode} }
210 t::lib::Mocks::mock_userenv( { patron => $librarian } );
211 my $patron = $builder->build_object(
213 class => 'Koha::Patrons',
214 value => { branchcode => $library->{branchcode} }
221 my $item = $builder->build_sample_item;
225 borrowernumber => $patron->borrowernumber,
226 itemnumber => $item->itemnumber,
227 branchcode => $library->{branchcode},
232 # Checkin 3 today - 2 days
233 my $not_today = dt_from_string->add( days => -2 );
234 for my $i ( 0 .. 2 ) {
235 my $checkout = $checkouts[$i];
236 C4::Circulation::AddReturn(
237 $checkout->item->barcode, $library->{branchcode},
238 undef, $not_today->set_hour( int( rand(24) ) )
242 my $today = dt_from_string;
243 for my $i ( 3 .. 6 ) {
244 my $checkout = $checkouts[$i];
245 C4::Circulation::AddReturn(
246 $checkout->item->barcode, $library->{branchcode},
247 undef, $today->set_hour( int( rand(24) ) )
251 my $old_checkouts = $patron->old_checkouts;
252 is( $old_checkouts->count, 7, 'There should be 7 old checkouts' );
253 my $todays_checkins = $old_checkouts->filter_by_todays_checkins;
254 is( $todays_checkins->count, 4, 'There should be 4 checkins today' );
256 [ $todays_checkins->get_column('itemnumber') ],
257 [ map { $_->itemnumber } @checkouts[ 3 .. 6 ] ],
258 q{Correct list of today's checkins}
262 $schema->storage->txn_rollback;
264 subtest 'automatic_checkin' => sub {
268 $schema->storage->txn_begin;
270 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
273 $builder->build_sample_item(
274 { homebranch => $patron->branchcode, itemlost => 0 } );
276 $builder->build_sample_item(
277 { homebranch => $patron->branchcode, itemlost => 0 } );
279 $builder->build_sample_item(
280 { homebranch => $patron->branchcode, itemlost => 0 } );
282 $builder->build_sample_item(
283 { homebranch => $patron->branchcode, itemlost => 0 } );
285 $due_ac_item->itemtype->automatic_checkin(1)->store;
286 $odue_ac_item->itemtype->automatic_checkin(1)->store;
287 $ac_item->itemtype->automatic_checkin(1)->store;
288 $normal_item->itemtype->automatic_checkin(0)->store;
290 my $today = dt_from_string;
291 my $tomorrow = dt_from_string->add( days => 1 );
292 my $yesterday = dt_from_string->subtract( days => 1 );
294 # Checkout do for automatic checkin
295 my $checkout_due_aci = Koha::Checkout->new(
297 borrowernumber => $patron->borrowernumber,
298 itemnumber => $due_ac_item->itemnumber,
299 branchcode => $patron->branchcode,
304 # Checkout not due for automatic checkin
305 my $checkout_odue_aci = Koha::Checkout->new(
307 borrowernumber => $patron->borrowernumber,
308 itemnumber => $odue_ac_item->itemnumber,
309 branchcode => $patron->branchcode,
310 date_due => $yesterday
314 # Checkout not due for automatic checkin
315 my $checkout_aci = Koha::Checkout->new(
317 borrowernumber => $patron->borrowernumber,
318 itemnumber => $ac_item->itemnumber,
319 branchcode => $patron->branchcode,
320 date_due => $tomorrow
324 # due checkout for nomal itemtype
325 my $checkout_ni = Koha::Checkout->new(
327 borrowernumber => $patron->borrowernumber,
328 itemnumber => $normal_item->itemnumber,
329 branchcode => $patron->branchcode,
334 my $searched = Koha::Checkouts->find( $checkout_ni->issue_id );
335 is( $searched->issue_id, $checkout_ni->issue_id,
336 'checkout for normal_item exists' );
338 $searched = Koha::Checkouts->find( $checkout_aci->issue_id );
339 is( $searched->issue_id, $checkout_aci->issue_id,
340 'checkout for ac_item exists' );
342 $searched = Koha::Checkouts->find( $checkout_due_aci->issue_id );
345 $checkout_due_aci->issue_id,
346 'checkout for due_ac_item exists'
349 $searched = Koha::Checkouts->find( $checkout_odue_aci->issue_id );
352 $checkout_odue_aci->issue_id,
353 'checkout for odue_ac_item exists'
356 Koha::Checkouts->automatic_checkin;
358 $searched = Koha::Checkouts->find( $checkout_ni->issue_id );
359 is( $searched->issue_id, $checkout_ni->issue_id,
360 'checkout for normal_item still exists' );
362 $searched = Koha::Checkouts->find( $checkout_aci->issue_id );
363 is( $searched->issue_id, $checkout_aci->issue_id,
364 'checkout for ac_item still exists' );
366 $searched = Koha::Checkouts->find( $checkout_due_aci->issue_id );
367 is( $searched, undef, 'checkout for due_ac_item doesn\'t exist anymore' );
369 $searched = Koha::Checkouts->find( $checkout_odue_aci->issue_id );
370 is( $searched, undef, 'checkout for odue_ac_item doesn\'t exist anymore' );
372 $searched = Koha::Old::Checkouts->find( $checkout_odue_aci->issue_id );
373 is( dt_from_string($searched->returndate), $yesterday, 'old checkout for odue_ac_item has the right return date' );
375 $schema->storage->txn_rollback;