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 => 9;
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( { source => 'Borrower', value => { branchcode => $library->{branchcode} } } );
38 my $item_1 = $builder->build_sample_item;
39 my $item_2 = $builder->build_sample_item;
40 my $nb_of_checkouts = Koha::Checkouts->search->count;
41 my $new_checkout_1 = Koha::Checkout->new(
42 { borrowernumber => $patron->{borrowernumber},
43 itemnumber => $item_1->itemnumber,
44 branchcode => $library->{branchcode},
47 my $new_checkout_2 = Koha::Checkout->new(
48 { borrowernumber => $patron->{borrowernumber},
49 itemnumber => $item_2->itemnumber,
50 branchcode => $library->{branchcode},
54 like( $new_checkout_1->issue_id, qr|^\d+$|, 'Adding a new checkout should have set the issue_id' );
55 is( Koha::Checkouts->search->count, $nb_of_checkouts + 2, 'The 2 checkouts should have been added' );
57 my $retrieved_checkout_1 = Koha::Checkouts->find( $new_checkout_1->issue_id );
58 is( $retrieved_checkout_1->itemnumber, $new_checkout_1->itemnumber, 'Find a checkout by id should return the correct checkout' );
60 subtest 'is_overdue' => sub {
62 my $ten_days_ago = dt_from_string->add( days => -10 );
63 my $ten_days_later = dt_from_string->add( days => 10 );
64 my $yesterday = dt_from_string->add( days => -1 );
65 my $tomorrow = dt_from_string->add( days => 1 );
67 $retrieved_checkout_1->date_due($ten_days_ago)->store;
68 is( $retrieved_checkout_1->is_overdue,
69 1, 'The item should have been returned 10 days ago' );
71 $retrieved_checkout_1->date_due($ten_days_later)->store;
72 is( $retrieved_checkout_1->is_overdue, 0, 'The item is due in 10 days' );
74 $retrieved_checkout_1->date_due($tomorrow)->store;
75 is( $retrieved_checkout_1->is_overdue($ten_days_later),
76 1, 'The item should have been returned yesterday' );
78 $retrieved_checkout_1->date_due($yesterday)->store;
79 is( $retrieved_checkout_1->is_overdue($ten_days_ago),
80 0, 'Ten days ago the item due yesterday was not late' );
82 $retrieved_checkout_1->date_due($tomorrow)->store;
83 is( $retrieved_checkout_1->is_overdue($ten_days_later),
84 1, 'In Ten days, the item due tomorrow will be late' );
86 $retrieved_checkout_1->date_due($yesterday)->store;
87 is( $retrieved_checkout_1->is_overdue($ten_days_ago),
88 0, 'In Ten days, the item due yesterday will still be late' );
91 subtest 'item' => sub {
93 my $item = $retrieved_checkout_1->item;
94 is( ref( $item ), 'Koha::Item', 'Koha::Checkout->item should return a Koha::Item' );
95 is( $item->itemnumber, $item_1->itemnumber, 'Koha::Checkout->item should return the correct item' );
98 subtest 'patron' => sub {
100 my $patron = $builder->build_object({class=>'Koha::Patrons', value => {branchcode => $library->{branchcode}}});
102 my $item = $builder->build_sample_item;
103 my $checkout = Koha::Checkout->new(
104 { borrowernumber => $patron->borrowernumber,
105 itemnumber => $item->itemnumber,
106 branchcode => $library->{branchcode},
110 my $p = $checkout->patron;
111 is( ref($p), 'Koha::Patron',
112 'Koha::Checkout->patron should return a Koha::Patron' );
113 is( $p->borrowernumber, $patron->borrowernumber,
114 'Koha::Checkout->patron should return the correct patron' );
116 # Testing Koha::Old::Checkout->patron now
117 my $issue_id = $checkout->issue_id;
118 C4::Circulation::MarkIssueReturned( $p->borrowernumber, $checkout->itemnumber );
120 my $old_issue = Koha::Old::Checkouts->find($issue_id);
121 is( $old_issue->patron, undef,
122 'Koha::Checkout->patron should return undef if the patron record has been deleted'
126 $retrieved_checkout_1->delete;
127 is( Koha::Checkouts->search->count, $nb_of_checkouts + 1, 'Delete should have deleted the checkout' );
129 subtest 'issuer' => sub {
131 my $patron = $builder->build_object({class=>'Koha::Patrons', value => {branchcode => $library->{branchcode}}});
132 my $issuer = $builder->build_object({class=>'Koha::Patrons', value => {branchcode => $library->{branchcode}}});
134 my $item = $builder->build_sample_item;
135 my $checkout = Koha::Checkout->new({
136 borrowernumber => $patron->borrowernumber,
137 issuer_id => $issuer->borrowernumber,
138 itemnumber => $item->itemnumber,
139 branchcode => $library->{branchcode},
142 my $i = $checkout->issuer;
143 is( ref($i), 'Koha::Patron',
144 'Koha::Checkout->issuer should return a Koha::Patron' );
145 is( $i->borrowernumber, $issuer->borrowernumber,
146 'Koha::Checkout->issuer should return the correct patron' );
148 # Testing Koha::Old::Checkout->patron now
149 my $issue_id = $checkout->issue_id;
150 C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $checkout->itemnumber );
152 my $old_issue = Koha::Old::Checkouts->find($issue_id);
153 is( $old_issue->issuer_id, undef,
154 'Koha::Checkout->issuer_id should return undef if the patron record has been deleted'
159 subtest 'Koha::Old::Checkouts->filter_by_todays_checkins' => sub {
163 # We will create 7 checkins for a given patron
164 # 3 checked in today - 2 days, and 4 checked in today
165 my $librarian = $builder->build_object(
167 class => 'Koha::Patrons',
168 value => { branchcode => $library->{branchcode} }
171 t::lib::Mocks::mock_userenv( { patron => $librarian } );
172 my $patron = $builder->build_object(
174 class => 'Koha::Patrons',
175 value => { branchcode => $library->{branchcode} }
182 my $item = $builder->build_sample_item;
186 borrowernumber => $patron->borrowernumber,
187 itemnumber => $item->itemnumber,
188 branchcode => $library->{branchcode},
193 # Checkin 3 today - 2 days
194 my $not_today = dt_from_string->add( days => -2 );
195 for my $i ( 0 .. 2 ) {
196 my $checkout = $checkouts[$i];
197 C4::Circulation::AddReturn(
198 $checkout->item->barcode, $library->{branchcode},
199 undef, $not_today->set_hour( int( rand(24) ) )
203 my $today = dt_from_string;
204 for my $i ( 3 .. 6 ) {
205 my $checkout = $checkouts[$i];
206 C4::Circulation::AddReturn(
207 $checkout->item->barcode, $library->{branchcode},
208 undef, $today->set_hour( int( rand(24) ) )
212 my $old_checkouts = $patron->old_checkouts;
213 is( $old_checkouts->count, 7, 'There should be 7 old checkouts' );
214 my $todays_checkins = $old_checkouts->filter_by_todays_checkins;
215 is( $todays_checkins->count, 4, 'There should be 4 checkins today' );
217 [ $todays_checkins->get_column('itemnumber') ],
218 [ map { $_->itemnumber } @checkouts[ 3 .. 6 ] ],
219 q{Correct list of today's checkins}
223 $schema->storage->txn_rollback;