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
;
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_hourly
=> '0.00',
66 rentalcharge
=> '0.00',
68 defaultreplacecost
=> '0.00',
72 my $item = $builder->build_object(
74 class => 'Koha::Items',
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',
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 {
102 # Mandatory parameters missing
104 Koha
::Charges
::Fees
->new(
112 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for patron';
114 Koha
::Charges
::Fees
->new(
122 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for library';
124 Koha
::Charges
::Fees
->new(
132 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for item';
134 Koha
::Charges
::Fees
->new(
142 'Koha::Exceptions::MissingParameter', 'MissingParameter thrown for to_date';
144 # Mandatory parameter bad
146 Koha
::Charges
::Fees
->new(
155 'dies for bad patron';
157 Koha
::Charges
::Fees
->new(
166 'dies for bad library';
168 Koha
::Charges
::Fees
->new(
179 Koha
::Charges
::Fees
->new(
188 'dies for bad to_date';
191 my $fees = Koha
::Charges
::Fees
->new(
199 is
( $fees->from_date, dt_from_string
(),
200 'from_date default set correctly to today' );
203 subtest
'patron accessor' => sub {
206 my $fees = Koha
::Charges
::Fees
->new(
216 $fees->patron->isa('Koha::Patron'),
217 'patron accessor returns a Koha::Patron'
219 warning_is
{ $fees->patron('12345') }
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 {
229 my $fees = Koha
::Charges
::Fees
->new(
239 $fees->library->isa('Koha::Library'),
240 'library accessor returns a Koha::Library'
242 warning_is
{ $fees->library('12345') }
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 {
251 my $fees = Koha
::Charges
::Fees
->new(
260 ok
( $fees->item->isa('Koha::Item'), 'item accessor returns a Koha::Item' );
261 warning_is
{ $fees->item('12345') }
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 {
270 my $fees = Koha
::Charges
::Fees
->new(
279 ok
( $fees->to_date->isa('DateTime'),
280 'to_date accessor returns a DateTime' );
281 warning_is
{ $fees->to_date(12345) }
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 {
290 my $fees = Koha
::Charges
::Fees
->new(
300 $fees->from_date->isa('DateTime'),
301 'from_date accessor returns a DateTime'
303 warning_is
{ $fees->from_date(12345) }
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 {
312 my $fees = Koha
::Charges
::Fees
->new(
318 from_date
=> $dt_from,
323 $itemtype->rentalcharge_daily(1.00);
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();
331 'Daily rental charge calculated correctly with finesCalendar = ignoreCalendar'
334 t
::lib
::Mocks
::mock_preference
( 'finesCalendar', 'noFinesWhenClosed' );
335 $charge = $fees->accumulate_rentalcharge();
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(
344 title
=> 'Test holiday',
345 description
=> 'Test holiday'
347 $charge = $fees->accumulate_rentalcharge();
348 my $dayname = $dt_from->clone->add( days
=> 1 )->day_name;
350 "Daily rental charge calculated correctly with finesCalendar = noFinesWhenClosed and closed $dayname"
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");
366 $dt_to = $dt_from->clone->add( hours
=> 96 );
367 $fees = Koha
::Charges
::Fees
->new(
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();
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;