3 # This file is part of Koha
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.
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.
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>.
20 use Test::More tests => 4;
24 use Koha::CurbsidePickups;
25 use Koha::CurbsidePickupPolicies;
27 use Koha::DateUtils qw( dt_from_string );
29 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
37 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
38 my $library_disabled = $builder->build_object( { class => 'Koha::Libraries' } );
39 my $logged_in_patron = $builder->build_object(
41 class => 'Koha::Patrons',
42 value => { branchcode => $library->branchcode }
45 t::lib::Mocks::mock_userenv( { patron => $logged_in_patron } );
46 my $patron = $builder->build_object(
48 class => 'Koha::Patrons',
49 value => { branchcode => $library->branchcode }
53 my $policy = Koha::CurbsidePickupPolicy->new(
55 branchcode => $library->branchcode,
57 pickup_interval => 30,
58 patrons_per_interval => 2,
59 patron_scheduled_pickup => 1
62 my $policy_disabled = Koha::CurbsidePickupPolicy->new(
64 branchcode => $library_disabled->branchcode,
66 pickup_interval => 30,
67 patrons_per_interval => 2,
68 patron_scheduled_pickup => 1
72 # Open Mondays from 12 to 18
73 $policy->add_opening_slot('1-12:00-18:00');
75 my $today = dt_from_string;
77 subtest 'Create a pickup' => sub {
80 # Day and datetime are ok
82 $today->clone->add( days => ( 1 - $today->day_of_week ) % 7 );
84 $next_monday->set_hour(15)->set_minute(00)->set_second(00);
85 my $cp = Koha::CurbsidePickup->new(
87 branchcode => $library->branchcode,
88 borrowernumber => $patron->borrowernumber,
89 scheduled_pickup_datetime => $schedule_dt,
90 notes => 'just a note'
93 is( $cp->status, 'to-be-staged' );
96 Koha::CurbsidePickup->new(
98 branchcode => $library->branchcode,
99 borrowernumber => $patron->borrowernumber,
100 scheduled_pickup_datetime => $schedule_dt,
101 notes => 'just a note'
105 'Koha::Exceptions::CurbsidePickup::TooManyPickups',
106 'Cannot create 2 pickups for the same patron';
112 $today->clone->add( days => ( 2 - $today->day_of_week ) % 7 );
113 $schedule_dt = $next_tuesday->set_hour(15)->set_minute(00)->set_second(00);
115 Koha::CurbsidePickup->new(
117 branchcode => $library->branchcode,
118 borrowernumber => $patron->borrowernumber,
119 scheduled_pickup_datetime => $schedule_dt,
120 notes => 'just a note'
124 'Koha::Exceptions::CurbsidePickup::NoMatchingSlots',
125 'Cannot create a pickup on a day without opening slots defined';
127 # Day ok but datetime not ok
128 $schedule_dt = $next_monday->set_hour(19)->set_minute(00)->set_second(00);
130 Koha::CurbsidePickup->new(
132 branchcode => $library->branchcode,
133 borrowernumber => $patron->borrowernumber,
134 scheduled_pickup_datetime => $schedule_dt,
135 notes => 'just a note'
139 'Koha::Exceptions::CurbsidePickup::NoMatchingSlots',
140 'Cannot create a pickup on a time without opening slots defined';
142 # Day ok, datetime inside the opening slot, but wrong (15:15 for instance)
143 $schedule_dt = $next_monday->set_hour(15)->set_minute(15)->set_second(00);
145 Koha::CurbsidePickup->new(
147 branchcode => $library->branchcode,
148 borrowernumber => $patron->borrowernumber,
149 scheduled_pickup_datetime => $schedule_dt,
150 notes => 'just a note'
154 'Koha::Exceptions::CurbsidePickup::NoMatchingSlots',
155 'Cannot create a pickup on a time that is not matching the start of an interval';
159 subtest 'workflow' => sub {
163 Koha::CurbsidePickups->search( { branchcode => $library->branchcode } );
166 $today->clone->add( days => ( 1 - $today->day_of_week ) % 7 );
168 $next_monday->set_hour(15)->set_minute(00)->set_second(00);
169 my $cp = Koha::CurbsidePickup->new(
171 branchcode => $library->branchcode,
172 borrowernumber => $patron->borrowernumber,
173 scheduled_pickup_datetime => $schedule_dt,
174 notes => 'just a note'
177 is( $cp->status, 'to-be-staged' );
178 is( $pickups->filter_by_to_be_staged->count, 1 );
181 is( $cp->status, 'staged-and-ready' );
182 is( $pickups->filter_by_staged_and_ready->count, 1 );
184 $cp->mark_as_unstaged;
185 is( $cp->status, 'to-be-staged' );
189 $cp->mark_patron_has_arrived;
190 is( $cp->status, 'patron-is-outside' );
191 is( $pickups->filter_by_patron_outside->count, 1 );
193 $cp->mark_as_delivered;
194 is( $cp->status, 'delivered' );
195 is( $pickups->filter_by_delivered->count, 1 );
200 subtest 'mark_as_delivered' => sub {
203 my $item = $builder->build_sample_item({ library => $library->branchcode });
204 my $reserve_id = C4::Reserves::AddReserve(
206 branchcode => $library->branchcode,
207 borrowernumber => $patron->borrowernumber,
208 biblionumber => $item->biblionumber,
210 itemnumber => $item->itemnumber,
213 my $hold = Koha::Holds->find($reserve_id);
217 $today->clone->add( days => ( 1 - $today->day_of_week ) % 7 );
219 $next_monday->set_hour(15)->set_minute(00)->set_second(00);
220 my $cp = Koha::CurbsidePickup->new(
222 branchcode => $library->branchcode,
223 borrowernumber => $patron->borrowernumber,
224 scheduled_pickup_datetime => $schedule_dt,
225 notes => 'just a note'
229 $cp->mark_as_delivered;
230 $cp->discard_changes;
231 is( t::lib::Dates::compare( $cp->arrival_datetime, dt_from_string), 0, 'Arrival time has been set to now' );
233 is( $hold->get_from_storage, undef, 'Hold has been filled' );
234 my $checkout = Koha::Checkouts->find({ itemnumber => $item->itemnumber });
235 is( $checkout->borrowernumber, $patron->borrowernumber, 'Item has correctly been checked out' );
240 subtest 'notify_new_pickup' => sub {
244 $builder->build_sample_item( { library => $library->branchcode } );
245 my $reserve_id = C4::Reserves::AddReserve(
247 branchcode => $library->branchcode,
248 borrowernumber => $patron->borrowernumber,
249 biblionumber => $item->biblionumber,
251 itemnumber => $item->itemnumber,
254 my $hold = Koha::Holds->find($reserve_id);
258 $today->clone->add( days => ( 1 - $today->day_of_week ) % 7 );
260 $next_monday->set_hour(15)->set_minute(00)->set_second(00);
261 my $cp = Koha::CurbsidePickup->new(
263 branchcode => $library->branchcode,
264 borrowernumber => $patron->borrowernumber,
265 scheduled_pickup_datetime => $schedule_dt,
266 notes => 'just a note'
270 $patron->set( { email => 'test@example.org' } )->store;
271 my $dbh = C4::Context->dbh;
272 $dbh->do( q|INSERT INTO borrower_message_preferences( borrowernumber, message_attribute_id ) VALUES ( ?, ?)|,
273 undef, $patron->borrowernumber, 4
275 my $borrower_message_preference_id =
276 $dbh->last_insert_id( undef, undef, "borrower_message_preferences", undef );
278 q|INSERT INTO borrower_message_transport_preferences( borrower_message_preference_id, message_transport_type) VALUES ( ?, ? )|,
279 undef, $borrower_message_preference_id, 'email'
282 $cp->notify_new_pickup;
284 my $messages = C4::Letters::GetQueuedMessages(
285 { borrowernumber => $patron->borrowernumber } );
287 $messages->[0]->{subject},
288 sprintf ("You have schedule a curbside pickup for %s.", $library->branchname),
289 "Notice correctly generated"
291 my $biblio_title = $item->biblio->title;
292 like( $messages->[0]->{content},
293 qr{$biblio_title}, "Content contains the list of waiting holds" );
296 $schema->storage->txn_rollback;