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;
31 use Koha::DateUtils qw(dt_from_string);
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',
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',
64 rentalcharge_daily => '0.00',
65 rentalcharge_daily_calendar => 1,
66 rentalcharge_hourly => '0.00',
67 rentalcharge_hourly_calendar => 1,
68 rentalcharge => '0.00',
70 defaultreplacecost => '0.00',
74 my $item = $builder->build_object(
76 class => 'Koha::Items',
78 biblionumber => $biblio->id,
79 homebranch => $library->id,
80 holdingbranch => $library->id,
81 itype => $itemtype->id,
85 my $patron = $builder->build_object(
87 class => 'Koha::Patrons',
89 dateexpiry => '9999-12-31',
90 categorycode => $patron_category->id,
95 my $now = dt_from_string()->set_time_zone('floating');
96 Time::Fake->offset( $now->epoch );
98 my $dt_from = $now->clone->subtract( days => 2 );
99 my $dt_to = $now->clone->add( days => 4 );
101 subtest 'new' => sub {
104 # Mandatory parameters missing
106 Koha::Charges::Fees->new(
114 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for patron';
116 Koha::Charges::Fees->new(
124 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for library';
126 Koha::Charges::Fees->new(
134 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for item';
136 Koha::Charges::Fees->new(
144 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for to_date';
146 # Mandatory parameter bad
148 Koha::Charges::Fees->new(
157 'dies for bad patron';
159 Koha::Charges::Fees->new(
168 'dies for bad library';
170 Koha::Charges::Fees->new(
181 Koha::Charges::Fees->new(
190 'dies for bad to_date';
193 my $fees = Koha::Charges::Fees->new(
201 is( $fees->from_date, dt_from_string(),
202 'from_date default set correctly to today' );
205 subtest 'patron accessor' => sub {
208 my $fees = Koha::Charges::Fees->new(
218 $fees->patron->isa('Koha::Patron'),
219 'patron accessor returns a Koha::Patron'
221 warning_is { $fees->patron('12345') }
223 "Setting 'patron' to something other than a Koha::Patron is not supported!"
224 }, "Warning thrown when attempting to set patron to string";
228 subtest 'library accessor' => sub {
231 my $fees = Koha::Charges::Fees->new(
241 $fees->library->isa('Koha::Library'),
242 'library accessor returns a Koha::Library'
244 warning_is { $fees->library('12345') }
246 "Setting 'library' to something other than a Koha::Library is not supported!"
247 }, "Warning thrown when attempting to set library to string";
250 subtest 'item accessor' => sub {
253 my $fees = Koha::Charges::Fees->new(
262 ok( $fees->item->isa('Koha::Item'), 'item accessor returns a Koha::Item' );
263 warning_is { $fees->item('12345') }
265 "Setting 'item' to something other than a Koha::Item is not supported!"
266 }, "Warning thrown when attempting to set item to string";
269 subtest 'to_date accessor' => sub {
272 my $fees = Koha::Charges::Fees->new(
281 ok( $fees->to_date->isa('DateTime'),
282 'to_date accessor returns a DateTime' );
283 warning_is { $fees->to_date(12345) }
285 "Setting 'to_date' to something other than a DateTime is not supported!"
286 }, "Warning thrown when attempting to set to_date to integer";
289 subtest 'from_date accessor' => sub {
292 my $fees = Koha::Charges::Fees->new(
302 $fees->from_date->isa('DateTime'),
303 'from_date accessor returns a DateTime'
305 warning_is { $fees->from_date(12345) }
307 "Setting 'from_date' to something other than a DateTime is not supported!"
308 }, "Warning thrown when attempting to set from_date to integer";
311 subtest 'accumulate_rentalcharge tests' => sub {
314 my $fees = Koha::Charges::Fees->new(
320 from_date => $dt_from,
325 Koha::CirculationRules->set_rules({
326 categorycode => $patron->categorycode,
327 itemtype => $itemtype->id,
328 branchcode => $library->id,
330 lengthunit => 'days',
335 $itemtype->rentalcharge_daily(1.00);
337 is( $itemtype->rentalcharge_daily,
338 1.00, 'Daily return charge stored correctly' );
340 t::lib::Mocks::mock_preference( 'finesCalendar', 'ignoreCalendar' );
341 my $charge = $fees->accumulate_rentalcharge();
343 'Daily rental charge calculated correctly with finesCalendar = ignoreCalendar'
346 t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
347 $charge = $fees->accumulate_rentalcharge();
349 'Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed'
352 $itemtype->rentalcharge_daily_calendar(0)->store();
353 $charge = $fees->accumulate_rentalcharge();
355 'Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and rentalcharge_daily_calendar = 0'
357 $itemtype->rentalcharge_daily_calendar(1)->store();
359 my $calendar = C4::Calendar->new( branchcode => $library->id );
360 # DateTime 1..7 (Mon..Sun), C4::Calender 0..6 (Sun..Sat)
362 ( $dt_from->day_of_week == 6 ) ? 0
363 : ( $dt_from->day_of_week == 7 ) ? 1
364 : $dt_from->day_of_week + 1;
365 $calendar->insert_week_day_holiday(
366 weekday => $closed_day,
367 title => 'Test holiday',
368 description => 'Test holiday'
370 $charge = $fees->accumulate_rentalcharge();
380 my $dayname = $day_names->{$closed_day};
382 "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname"
386 Koha::CirculationRules->set_rules({
387 categorycode => $patron->categorycode,
388 itemtype => $itemtype->id,
389 branchcode => $library->id,
391 lengthunit => 'hours',
397 $itemtype->rentalcharge_hourly("0.25");
400 $dt_to = $dt_from->clone->add( hours => 96 );
401 $fees = Koha::Charges::Fees->new(
407 from_date => $dt_from,
411 t::lib::Mocks::mock_preference( 'finesCalendar', 'ignoreCalendar' );
412 $charge = $fees->accumulate_rentalcharge();
413 is( $charge, 24.00, 'Hourly rental charge calculated correctly (96h * 0.25u)' );
415 t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
416 $charge = $fees->accumulate_rentalcharge();
418 "Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname (96h - 24h * 0.25u)"
421 $itemtype->rentalcharge_hourly_calendar(0)->store();
422 $charge = $fees->accumulate_rentalcharge();
424 "Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname (96h - 24h * 0.25u) and rentalcharge_hourly_calendar = 0"
426 $itemtype->rentalcharge_hourly_calendar(1)->store();
428 $calendar->delete_holiday( weekday => $closed_day );
429 $charge = $fees->accumulate_rentalcharge();
430 is( $charge, 24.00, 'Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed (96h - 0h * 0.25u)' );
433 $schema->storage->txn_rollback;