Bug 23382: (follow-up) Ensure closed day is inside charge window
[koha.git] / t / db_dependent / Koha / Charges / Fees.t
blobb67b552f42eff281257d613ec81946174609f78b
1 #!/usr/bin/perl
3 # Copyright 2018 ByWater Solutions
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
22 use Test::More tests => 8;
23 use Test::Exception;
24 use Test::Warn;
26 use t::lib::Mocks;
27 use t::lib::TestBuilder;
29 use Time::Fake;
30 use C4::Calendar;
31 use Koha::DateUtils qw(dt_from_string);
33 BEGIN {
34 use_ok('Koha::Charges::Fees');
37 my $schema = Koha::Database->new->schema;
38 my $builder = t::lib::TestBuilder->new();
39 $schema->storage->txn_begin;
41 my $patron_category = $builder->build_object(
43 class => 'Koha::Patron::Categories',
44 value => {
45 category_type => 'P',
46 enrolmentfee => 0,
50 my $library = $builder->build_object(
52 class => 'Koha::Libraries',
55 my $biblio = $builder->build_object(
57 class => 'Koha::Biblios',
60 my $itemtype = $builder->build_object(
62 class => 'Koha::ItemTypes',
63 value => {
64 rentalcharge_daily => '0.00',
65 rentalcharge_hourly => '0.00',
66 rentalcharge => '0.00',
67 processfee => '0.00',
68 defaultreplacecost => '0.00',
72 my $item = $builder->build_object(
74 class => 'Koha::Items',
75 value => {
76 biblionumber => $biblio->id,
77 homebranch => $library->id,
78 holdingbranch => $library->id,
79 itype => $itemtype->id,
83 my $patron = $builder->build_object(
85 class => 'Koha::Patrons',
86 value => {
87 dateexpiry => '9999-12-31',
88 categorycode => $patron_category->id,
93 my $dt = dt_from_string();
94 Time::Fake->offset( $dt->epoch );
96 my $dt_from = dt_from_string()->subtract( days => 2 );
97 my $dt_to = dt_from_string()->add( days => 4 );
99 subtest 'new' => sub {
100 plan tests => 9;
102 # Mandatory parameters missing
103 throws_ok {
104 Koha::Charges::Fees->new(
106 library => $library,
107 item => $item,
108 to_date => $dt_to,
112 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for patron';
113 throws_ok {
114 Koha::Charges::Fees->new(
116 patron => $patron,
117 item => $item,
118 to_date => $dt_to,
122 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for library';
123 throws_ok {
124 Koha::Charges::Fees->new(
126 patron => $patron,
127 library => $library,
128 to_date => $dt_to,
132 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for item';
133 throws_ok {
134 Koha::Charges::Fees->new(
136 patron => $patron,
137 library => $library,
138 item => $item,
142 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for to_date';
144 # Mandatory parameter bad
145 dies_ok {
146 Koha::Charges::Fees->new(
148 patron => '12345',
149 library => $library,
150 item => $item,
151 to_date => $dt_to,
155 'dies for bad patron';
156 dies_ok {
157 Koha::Charges::Fees->new(
159 patron => $patron,
160 library => '12345',
161 item => $item,
162 to_date => $dt_to,
166 'dies for bad library';
167 dies_ok {
168 Koha::Charges::Fees->new(
170 patron => $patron,
171 library => $library,
172 item => '12345',
173 to_date => $dt_to,
177 'dies for bad item';
178 dies_ok {
179 Koha::Charges::Fees->new(
181 patron => $patron,
182 library => $library,
183 item => $item,
184 to_date => 12345
188 'dies for bad to_date';
190 # Defaults
191 my $fees = Koha::Charges::Fees->new(
193 patron => $patron,
194 library => $library,
195 item => $item,
196 to_date => $dt_to,
199 is( $fees->from_date, dt_from_string(),
200 'from_date default set correctly to today' );
203 subtest 'patron accessor' => sub {
204 plan tests => 2;
206 my $fees = Koha::Charges::Fees->new(
208 patron => $patron,
209 library => $library,
210 item => $item,
211 to_date => $dt_to,
216 $fees->patron->isa('Koha::Patron'),
217 'patron accessor returns a Koha::Patron'
219 warning_is { $fees->patron('12345') }
220 { carped =>
221 "Setting 'patron' to something other than a Koha::Patron is not supported!"
222 }, "Warning thrown when attempting to set patron to string";
226 subtest 'library accessor' => sub {
227 plan tests => 2;
229 my $fees = Koha::Charges::Fees->new(
231 patron => $patron,
232 library => $library,
233 item => $item,
234 to_date => $dt_to,
239 $fees->library->isa('Koha::Library'),
240 'library accessor returns a Koha::Library'
242 warning_is { $fees->library('12345') }
243 { carped =>
244 "Setting 'library' to something other than a Koha::Library is not supported!"
245 }, "Warning thrown when attempting to set library to string";
248 subtest 'item accessor' => sub {
249 plan tests => 2;
251 my $fees = Koha::Charges::Fees->new(
253 patron => $patron,
254 library => $library,
255 item => $item,
256 to_date => $dt_to,
260 ok( $fees->item->isa('Koha::Item'), 'item accessor returns a Koha::Item' );
261 warning_is { $fees->item('12345') }
262 { carped =>
263 "Setting 'item' to something other than a Koha::Item is not supported!"
264 }, "Warning thrown when attempting to set item to string";
267 subtest 'to_date accessor' => sub {
268 plan tests => 2;
270 my $fees = Koha::Charges::Fees->new(
272 patron => $patron,
273 library => $library,
274 item => $item,
275 to_date => $dt_to,
279 ok( $fees->to_date->isa('DateTime'),
280 'to_date accessor returns a DateTime' );
281 warning_is { $fees->to_date(12345) }
282 { carped =>
283 "Setting 'to_date' to something other than a DateTime is not supported!"
284 }, "Warning thrown when attempting to set to_date to integer";
287 subtest 'from_date accessor' => sub {
288 plan tests => 2;
290 my $fees = Koha::Charges::Fees->new(
292 patron => $patron,
293 library => $library,
294 item => $item,
295 to_date => $dt_to,
300 $fees->from_date->isa('DateTime'),
301 'from_date accessor returns a DateTime'
303 warning_is { $fees->from_date(12345) }
304 { carped =>
305 "Setting 'from_date' to something other than a DateTime is not supported!"
306 }, "Warning thrown when attempting to set from_date to integer";
309 subtest 'accumulate_rentalcharge tests' => sub {
310 plan tests => 7;
312 my $fees = Koha::Charges::Fees->new(
314 patron => $patron,
315 library => $library,
316 item => $item,
317 to_date => $dt_to,
318 from_date => $dt_from,
322 # Daily tests
323 $itemtype->rentalcharge_daily(1.00);
324 $itemtype->store();
325 is( $itemtype->rentalcharge_daily,
326 1.00, 'Daily return charge stored correctly' );
328 t::lib::Mocks::mock_preference( 'finesCalendar', 'ignoreCalendar' );
329 my $charge = $fees->accumulate_rentalcharge();
330 is( $charge, 6.00,
331 'Daily rental charge calculated correctly with finesCalendar = ignoreCalendar'
334 t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
335 $charge = $fees->accumulate_rentalcharge();
336 is( $charge, 6.00,
337 'Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed'
340 my $calendar = C4::Calendar->new( branchcode => $library->id );
341 my $day = $dt_from->day_of_week + 1;
342 $calendar->insert_week_day_holiday(
343 weekday => $day,
344 title => 'Test holiday',
345 description => 'Test holiday'
347 $charge = $fees->accumulate_rentalcharge();
348 my $dayname = $dt_from->clone->add( days => 1 )->day_name;
349 is( $charge, 5.00,
350 "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname"
353 # Hourly tests
354 my $issuingrule = Koha::IssuingRules->get_effective_issuing_rule(
356 categorycode => $patron->categorycode,
357 itemtype => $itemtype->id,
358 branchcode => $library->id
361 $issuingrule->lengthunit('hours');
362 $issuingrule->store();
363 $itemtype->rentalcharge_hourly("0.25");
364 $itemtype->store();
366 $dt_to = $dt_from->clone->add( hours => 96 );
367 $fees = Koha::Charges::Fees->new(
369 patron => $patron,
370 library => $library,
371 item => $item,
372 to_date => $dt_to,
373 from_date => $dt_from,
377 t::lib::Mocks::mock_preference( 'finesCalendar', 'ignoreCalendar' );
378 $charge = $fees->accumulate_rentalcharge();
379 is( $charge, 24.00, 'Hourly rental charge calculated correctly (96h * 0.25u)' );
381 t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
382 $charge = $fees->accumulate_rentalcharge();
383 is( $charge, 18.00,
384 "Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname (96h - 24h * 0.25u)"
387 $calendar->delete_holiday( weekday => $day );
388 $charge = $fees->accumulate_rentalcharge();
389 is( $charge, 24.00, 'Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed (96h - 0h * 0.25u)' );
392 $schema->storage->txn_rollback;
393 Time::Fake->reset;