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
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 => 8;
27 use t::lib::TestBuilder;
32 use Koha::DateUtils qw(dt_from_string);
35 use_ok('Koha::Charges::Fees');
38 my $schema = Koha::Database->new->schema;
39 my $builder = t::lib::TestBuilder->new();
40 $schema->storage->txn_begin;
42 my $patron_category = $builder->build_object(
44 class => 'Koha::Patron::Categories',
51 my $library = $builder->build_object(
53 class => 'Koha::Libraries',
56 my $biblio = $builder->build_object(
58 class => 'Koha::Biblios',
61 my $itemtype = $builder->build_object(
63 class => 'Koha::ItemTypes',
65 rentalcharge_daily => '0.00',
66 rentalcharge_daily_calendar => 1,
67 rentalcharge_hourly => '0.00',
68 rentalcharge_hourly_calendar => 1,
69 rentalcharge => '0.00',
71 defaultreplacecost => '0.00',
75 my $item = $builder->build_object(
77 class => 'Koha::Items',
79 biblionumber => $biblio->id,
80 homebranch => $library->id,
81 holdingbranch => $library->id,
82 itype => $itemtype->id,
86 my $patron = $builder->build_object(
88 class => 'Koha::Patrons',
90 dateexpiry => '9999-12-31',
91 categorycode => $patron_category->id,
96 my $now = dt_from_string()->set_time_zone('floating');
97 Time::Fake->offset( $now->epoch );
99 my $dt_from = $now->clone->subtract( days => 2 );
100 my $dt_to = $now->clone->add( days => 4 );
102 subtest 'new' => sub {
105 # Mandatory parameters missing
107 Koha::Charges::Fees->new(
115 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for patron';
117 Koha::Charges::Fees->new(
125 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for library';
127 Koha::Charges::Fees->new(
135 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for item';
137 Koha::Charges::Fees->new(
145 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for to_date';
147 # Mandatory parameter bad
149 Koha::Charges::Fees->new(
158 'dies for bad patron';
160 Koha::Charges::Fees->new(
169 'dies for bad library';
171 Koha::Charges::Fees->new(
182 Koha::Charges::Fees->new(
191 'dies for bad to_date';
194 my $fees = Koha::Charges::Fees->new(
202 is( t::lib::Dates::compare($fees->from_date, dt_from_string()), 0,
203 'from_date default set correctly to today' );
206 subtest 'patron accessor' => sub {
209 my $fees = Koha::Charges::Fees->new(
219 $fees->patron->isa('Koha::Patron'),
220 'patron accessor returns a Koha::Patron'
222 warning_is { $fees->patron('12345') }
224 "Setting 'patron' to something other than a Koha::Patron is not supported!"
225 }, "Warning thrown when attempting to set patron to string";
229 subtest 'library accessor' => sub {
232 my $fees = Koha::Charges::Fees->new(
242 $fees->library->isa('Koha::Library'),
243 'library accessor returns a Koha::Library'
245 warning_is { $fees->library('12345') }
247 "Setting 'library' to something other than a Koha::Library is not supported!"
248 }, "Warning thrown when attempting to set library to string";
251 subtest 'item accessor' => sub {
254 my $fees = Koha::Charges::Fees->new(
263 ok( $fees->item->isa('Koha::Item'), 'item accessor returns a Koha::Item' );
264 warning_is { $fees->item('12345') }
266 "Setting 'item' to something other than a Koha::Item is not supported!"
267 }, "Warning thrown when attempting to set item to string";
270 subtest 'to_date accessor' => sub {
273 my $fees = Koha::Charges::Fees->new(
282 ok( $fees->to_date->isa('DateTime'),
283 'to_date accessor returns a DateTime' );
284 warning_is { $fees->to_date(12345) }
286 "Setting 'to_date' to something other than a DateTime is not supported!"
287 }, "Warning thrown when attempting to set to_date to integer";
290 subtest 'from_date accessor' => sub {
293 my $fees = Koha::Charges::Fees->new(
303 $fees->from_date->isa('DateTime'),
304 'from_date accessor returns a DateTime'
306 warning_is { $fees->from_date(12345) }
308 "Setting 'from_date' to something other than a DateTime is not supported!"
309 }, "Warning thrown when attempting to set from_date to integer";
312 subtest 'accumulate_rentalcharge tests' => sub {
315 my $fees = Koha::Charges::Fees->new(
321 from_date => $dt_from,
326 Koha::CirculationRules->set_rules({
327 categorycode => $patron->categorycode,
328 itemtype => $itemtype->id,
329 branchcode => $library->id,
331 lengthunit => 'days',
336 $itemtype->rentalcharge_daily(1.00);
338 is( $itemtype->rentalcharge_daily,
339 1.00, 'Daily return charge stored correctly' );
341 t::lib::Mocks::mock_preference( 'finesCalendar', 'ignoreCalendar' );
342 my $charge = $fees->accumulate_rentalcharge();
344 'Daily rental charge calculated correctly with finesCalendar = ignoreCalendar'
347 t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
348 $charge = $fees->accumulate_rentalcharge();
350 'Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed'
353 $itemtype->rentalcharge_daily_calendar(0)->store();
354 $charge = $fees->accumulate_rentalcharge();
356 'Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and rentalcharge_daily_calendar = 0'
358 $itemtype->rentalcharge_daily_calendar(1)->store();
360 my $calendar = C4::Calendar->new( branchcode => $library->id );
361 # DateTime 1..7 (Mon..Sun), C4::Calender 0..6 (Sun..Sat)
363 ( $dt_from->day_of_week == 6 ) ? 0
364 : ( $dt_from->day_of_week == 7 ) ? 1
365 : $dt_from->day_of_week + 1;
366 $calendar->insert_week_day_holiday(
367 weekday => $closed_day,
368 title => 'Test holiday',
369 description => 'Test holiday'
371 $charge = $fees->accumulate_rentalcharge();
381 my $dayname = $day_names->{$closed_day};
383 "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname"
387 Koha::CirculationRules->set_rules({
388 categorycode => $patron->categorycode,
389 itemtype => $itemtype->id,
390 branchcode => $library->id,
392 lengthunit => 'hours',
398 $itemtype->rentalcharge_hourly("0.25");
401 $dt_to = $dt_from->clone->add( hours => 96 );
402 $fees = Koha::Charges::Fees->new(
408 from_date => $dt_from,
412 $itemtype->rentalcharge_hourly_calendar(0)->store();
413 $charge = $fees->accumulate_rentalcharge();
414 is( $charge, 24.00, 'Hourly rental charge calculated correctly (96h * 0.25u)' );
416 $itemtype->rentalcharge_hourly_calendar(1)->store();
417 $charge = $fees->accumulate_rentalcharge();
419 "Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname (96h - 24h * 0.25u)"
422 $itemtype->rentalcharge_hourly_calendar(0)->store();
423 $charge = $fees->accumulate_rentalcharge();
425 "Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname (96h - 24h * 0.25u) and rentalcharge_hourly_calendar = 0"
428 $itemtype->rentalcharge_hourly_calendar(1)->store();
429 $calendar->delete_holiday( weekday => $closed_day );
430 $charge = $fees->accumulate_rentalcharge();
431 is( $charge, 24.00, 'Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed (96h - 0h * 0.25u)' );
434 $schema->storage->txn_rollback;