5 use C4::Reserves qw( ModReserve ModReserveAffect );
6 use Koha::DateUtils qw( dt_from_string );
9 use t::lib::TestBuilder;
11 use Test::More tests => 11;
13 use_ok('C4::Reserves', qw( ModReserve ModReserveAffect ));
15 my $schema = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
18 my $dbh = C4::Context->dbh;
19 $dbh->do(q{DELETE FROM special_holidays});
20 $dbh->do(q{DELETE FROM repeatable_holidays});
21 $dbh->do("DELETE FROM reserves");
23 my $builder = t::lib::TestBuilder->new();
25 # Category, branch and patrons
29 categorycode => 'XYZ1',
46 my $patron1 = $builder->build({
49 categorycode => 'XYZ1',
54 my $patron2 = $builder->build({
57 categorycode => 'XYZ1',
62 my $biblio = $builder->build_sample_biblio;
63 my $biblio2 = $builder->build_sample_biblio;
64 my $biblio3 = $builder->build_sample_biblio;
65 my $biblio4 = $builder->build_sample_biblio;
67 my $item1 = $builder->build_sample_item({biblionumber => $biblio->biblionumber});
68 my $item2 = $builder->build_sample_item({biblionumber => $biblio2->biblionumber});
69 my $item3 = $builder->build_sample_item({biblionumber => $biblio3->biblionumber});
70 my $item4 = $builder->build_sample_item({biblionumber => $biblio4->biblionumber});
72 my $today = dt_from_string();
74 my $reserve1_reservedate = $today->clone;
75 $reserve1_reservedate->subtract(days => 20);
77 my $reserve1_expirationdate = $today->clone;
78 $reserve1_expirationdate->add(days => 6);
80 my $reserve1 = $builder->build({
83 borrowernumber => $patron1->{borrowernumber},
84 reservedate => $reserve1_reservedate->ymd,
85 expirationdate => undef,
86 biblionumber => $biblio->biblionumber,
93 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelay', 1);
94 t::lib::Mocks::mock_preference('ReservesMaxPickUpDelay', 6);
96 ModReserveAffect( $item1->itemnumber, $patron1->{borrowernumber});
97 my $r = Koha::Holds->find($reserve1->{reserve_id});
99 is($r->waitingdate, $today->ymd, 'Waiting date should be set to today' );
100 is($r->expirationdate, $reserve1_expirationdate->ymd, 'Expiration date should be set to today + 6' );
101 is($r->found, 'W', 'Reserve status is now "waiting"' );
102 is($r->priority, 0, 'Priority should be 0' );
103 is($r->itemnumber, $item1->itemnumber, 'Item number should be set correctly' );
105 my $reserve2 = $builder->build({
108 borrowernumber => $patron2->{borrowernumber},
109 reservedate => $reserve1_reservedate->ymd,
110 expirationdate => undef,
111 biblionumber => $biblio2->biblionumber,
112 branchcode => 'LIB1',
118 ModReserveAffect( $item2->itemnumber, $patron2->{borrowernumber}, 1);
119 my $r2 = Koha::Holds->find($reserve2->{reserve_id});
121 is($r2->found, 'T', '2nd reserve - Reserve status is now "To transfer"' );
122 is($r2->priority, 0, '2nd reserve - Priority should be 0' );
123 is($r2->itemnumber, $item2->itemnumber, '2nd reserve - Item number should be set correctly' );
125 my $reserve3 = $builder->build({
128 borrowernumber => $patron2->{borrowernumber},
129 reservedate => $reserve1_reservedate->ymd,
130 expirationdate => undef,
131 biblionumber => $biblio3->biblionumber,
132 branchcode => 'LIB1',
138 my $special_holiday1_dt = $today->clone;
139 $special_holiday1_dt->add(days => 2);
141 my $holiday = $builder->build({
142 source => 'SpecialHoliday',
144 branchcode => 'LIB1',
145 day => $special_holiday1_dt->day,
146 month => $special_holiday1_dt->month,
147 year => $special_holiday1_dt->year,
148 title => 'My special holiday',
153 my $special_holiday2_dt = $today->clone;
154 $special_holiday2_dt->add(days => 4);
156 my $holiday2 = $builder->build({
157 source => 'SpecialHoliday',
159 branchcode => 'LIB1',
160 day => $special_holiday2_dt->day,
161 month => $special_holiday2_dt->month,
162 year => $special_holiday2_dt->year,
163 title => 'My special holiday 2',
168 Koha::Caches->get_instance->flush_all;
170 t::lib::Mocks::mock_preference('ExcludeHolidaysFromMaxPickUpDelay', 1);
171 ModReserveAffect( $item3->itemnumber, $patron2->{borrowernumber});
173 # Add 6 days of pickup delay + 1 day of holiday.
174 my $expected_expiration = $today->clone;
175 $expected_expiration->add(days => 8);
177 my $r3 = Koha::Holds->find($reserve3->{reserve_id});
178 is($r3->expirationdate, $expected_expiration->ymd, 'Expiration date should be set to today + 7' );
180 my $reserve4_reservedate = $today->clone;
181 my $requested_expiredate = $today->clone()->add(days => 6);
183 my $reserve4 = $builder->build({
186 borrowernumber => $patron2->{borrowernumber},
187 reservedate => $reserve4_reservedate->ymd,
188 expirationdate => $requested_expiredate->ymd,
189 biblionumber => $biblio4->biblionumber,
190 branchcode => 'LIB1',
196 t::lib::Mocks::mock_preference('ReservesMaxPickUpDelay', 10);
197 ModReserveAffect( $item4->itemnumber, $patron2->{borrowernumber}, 0, $reserve4->{reserve_id});
199 my $r4 = Koha::Holds->find($reserve4->{reserve_id});
200 is($r4->expirationdate, $requested_expiredate->ymd, 'Requested expiration date should be kept' );
202 $schema->storage->txn_rollback;