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
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.
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_hourly => '0.00',
67 rentalcharge => '0.00',
69 defaultreplacecost => '0.00',
73 my $item = $builder->build_object(
75 class => 'Koha::Items',
77 biblionumber => $biblio->id,
78 homebranch => $library->id,
79 holdingbranch => $library->id,
80 itype => $itemtype->id,
84 my $patron = $builder->build_object(
86 class => 'Koha::Patrons',
88 dateexpiry => '9999-12-31',
89 categorycode => $patron_category->id,
94 my $now = dt_from_string()->set_time_zone('floating');
95 Time::Fake->offset( $now->epoch );
97 my $dt_from = $now->clone->subtract( days => 2 );
98 my $dt_to = $now->clone->add( days => 4 );
100 subtest 'new' => sub {
103 # Mandatory parameters missing
105 Koha::Charges::Fees->new(
113 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for patron';
115 Koha::Charges::Fees->new(
123 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for library';
125 Koha::Charges::Fees->new(
133 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for item';
135 Koha::Charges::Fees->new(
143 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for to_date';
145 # Mandatory parameter bad
147 Koha::Charges::Fees->new(
156 'dies for bad patron';
158 Koha::Charges::Fees->new(
167 'dies for bad library';
169 Koha::Charges::Fees->new(
180 Koha::Charges::Fees->new(
189 'dies for bad to_date';
192 my $fees = Koha::Charges::Fees->new(
200 is( t::lib::Dates::compare($fees->from_date, dt_from_string()), 0,
201 'from_date default set correctly to today' );
204 subtest 'patron accessor' => sub {
207 my $fees = Koha::Charges::Fees->new(
217 $fees->patron->isa('Koha::Patron'),
218 'patron accessor returns a Koha::Patron'
220 warning_is { $fees->patron('12345') }
222 "Setting 'patron' to something other than a Koha::Patron is not supported!"
223 }, "Warning thrown when attempting to set patron to string";
227 subtest 'library accessor' => sub {
230 my $fees = Koha::Charges::Fees->new(
240 $fees->library->isa('Koha::Library'),
241 'library accessor returns a Koha::Library'
243 warning_is { $fees->library('12345') }
245 "Setting 'library' to something other than a Koha::Library is not supported!"
246 }, "Warning thrown when attempting to set library to string";
249 subtest 'item accessor' => sub {
252 my $fees = Koha::Charges::Fees->new(
261 ok( $fees->item->isa('Koha::Item'), 'item accessor returns a Koha::Item' );
262 warning_is { $fees->item('12345') }
264 "Setting 'item' to something other than a Koha::Item is not supported!"
265 }, "Warning thrown when attempting to set item to string";
268 subtest 'to_date accessor' => sub {
271 my $fees = Koha::Charges::Fees->new(
280 ok( $fees->to_date->isa('DateTime'),
281 'to_date accessor returns a DateTime' );
282 warning_is { $fees->to_date(12345) }
284 "Setting 'to_date' to something other than a DateTime is not supported!"
285 }, "Warning thrown when attempting to set to_date to integer";
288 subtest 'from_date accessor' => sub {
291 my $fees = Koha::Charges::Fees->new(
301 $fees->from_date->isa('DateTime'),
302 'from_date accessor returns a DateTime'
304 warning_is { $fees->from_date(12345) }
306 "Setting 'from_date' to something other than a DateTime is not supported!"
307 }, "Warning thrown when attempting to set from_date to integer";
310 subtest 'accumulate_rentalcharge tests' => sub {
313 my $fees = Koha::Charges::Fees->new(
319 from_date => $dt_from,
324 $itemtype->rentalcharge_daily(1.00);
326 is( $itemtype->rentalcharge_daily,
327 1.00, 'Daily return charge stored correctly' );
329 t::lib::Mocks::mock_preference( 'finesCalendar', 'ignoreCalendar' );
330 my $charge = $fees->accumulate_rentalcharge();
332 'Daily rental charge calculated correctly with finesCalendar = ignoreCalendar'
335 t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
336 $charge = $fees->accumulate_rentalcharge();
338 'Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed'
341 my $calendar = C4::Calendar->new( branchcode => $library->id );
342 # DateTime 1..7 (Mon..Sun), C4::Calender 0..6 (Sun..Sat)
344 ( $dt_from->day_of_week == 6 ) ? 0
345 : ( $dt_from->day_of_week == 7 ) ? 1
346 : $dt_from->day_of_week + 1;
347 $calendar->insert_week_day_holiday(
348 weekday => $closed_day,
349 title => 'Test holiday',
350 description => 'Test holiday'
352 $charge = $fees->accumulate_rentalcharge();
362 my $dayname = $day_names->{$closed_day};
364 "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname"
368 my $issuingrule = Koha::IssuingRules->get_effective_issuing_rule(
370 categorycode => $patron->categorycode,
371 itemtype => $itemtype->id,
372 branchcode => $library->id
375 $issuingrule->lengthunit('hours');
376 $issuingrule->store();
377 $itemtype->rentalcharge_hourly("0.25");
380 $dt_to = $dt_from->clone->add( hours => 96 );
381 $fees = Koha::Charges::Fees->new(
387 from_date => $dt_from,
391 t::lib::Mocks::mock_preference( 'finesCalendar', 'ignoreCalendar' );
392 $charge = $fees->accumulate_rentalcharge();
393 is( $charge, 24.00, 'Hourly rental charge calculated correctly (96h * 0.25u)' );
395 t::lib::Mocks::mock_preference( 'finesCalendar', 'noFinesWhenClosed' );
396 $charge = $fees->accumulate_rentalcharge();
398 "Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname (96h - 24h * 0.25u)"
401 $calendar->delete_holiday( weekday => $closed_day );
402 $charge = $fees->accumulate_rentalcharge();
403 is( $charge, 24.00, 'Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed (96h - 0h * 0.25u)' );
406 $schema->storage->txn_rollback;