5 use Test::More tests => 7;
9 use C4::Overdues qw( CalcFine );
11 use Koha::DateUtils qw( dt_from_string );
13 use t::lib::TestBuilder;
16 my $schema = Koha::Database->schema;
17 $schema->storage->txn_begin;
19 our $dbh = C4::Context->dbh;
20 $dbh->do(q|DELETE FROM issues|);
22 t::lib::Mocks::mock_preference('item-level_itypes', '1');
24 my $builder = t::lib::TestBuilder->new();
26 my $branch = $builder->build(
32 my $category = $builder->build(
38 my $patron = $builder->build(
42 categorycode => $category->{categorycode},
43 branchcode => $branch->{branchcode},
48 my $itemtype = $builder->build(
52 defaultreplacecost => 6,
57 my $item = $builder->build_sample_item(
59 library => $branch->{branchcode},
60 replacementprice => '5.00',
61 itype => $itemtype->{itemtype},
65 subtest 'Test basic functionality' => sub {
68 Koha::CirculationRules->set_rules(
71 categorycode => undef,
79 overduefinescap => undef,
80 cap_fine_to_replacement_price => 0,
85 my $start_dt = DateTime->new(
91 my $end_dt = DateTime->new(
97 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
99 is( $amount, 29, 'Amount is calculated correctly' );
104 subtest 'Overdue fines cap should be disabled when value is 0' => sub {
107 Koha::CirculationRules->set_rules(
110 categorycode => undef,
114 lengthunit => 'days',
118 overduefinescap => "0",
119 cap_fine_to_replacement_price => 0,
124 my $start_dt = DateTime->new(
130 my $end_dt = DateTime->new(
136 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
138 is( $amount, 29, 'Amount is calculated correctly' );
143 subtest 'Overdue fines cap should be disabled when value is 0.00' => sub {
146 Koha::CirculationRules->set_rules(
149 categorycode => undef,
153 lengthunit => 'days',
157 overduefinescap => "0.00",
158 cap_fine_to_replacement_price => 0,
163 my $start_dt = DateTime->new(
169 my $end_dt = DateTime->new(
175 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
177 is( $amount, 29, 'Amount is calculated correctly' );
183 subtest 'Test with fine amount empty' => sub {
186 Koha::CirculationRules->set_rules(
189 categorycode => undef,
193 lengthunit => 'days',
197 overduefinescap => undef,
198 cap_fine_to_replacement_price => 1,
203 my $start_dt = DateTime->new(
209 my $end_dt = DateTime->new(
216 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
218 undef, "No warning when fine amount is ''";
223 subtest 'Test cap_fine_to_replacement_price' => sub {
226 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
227 Koha::CirculationRules->set_rules(
230 categorycode => undef,
234 lengthunit => 'days',
238 overduefinescap => undef,
239 cap_fine_to_replacement_price => 1,
244 my $start_dt = DateTime->new(
250 my $end_dt = DateTime->new(
256 my $item = $builder->build_sample_item(
258 library => $branch->{branchcode},
259 replacementprice => 5,
260 itype => $itemtype->{itemtype},
264 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
266 is( int($amount), 5, 'Amount is calculated correctly' );
268 # Use default replacement cost (useDefaultReplacementCost) is item's replacement price is 0
269 $item->replacementprice(0)->store;
270 ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
271 is( int($amount), 6, 'Amount is calculated correctly' );
276 subtest 'Test cap_fine_to_replacement_pricew with overduefinescap' => sub {
279 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
280 Koha::CirculationRules->set_rules(
283 categorycode => undef,
287 lengthunit => 'days',
291 overduefinescap => 3,
292 cap_fine_to_replacement_price => 1,
297 my $start_dt = DateTime->new(
303 my $end_dt = DateTime->new(
309 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
310 is( int($amount), 3, 'Got the lesser of overduefinescap and replacement price where overduefinescap < replacement price' );
312 Koha::CirculationRules->set_rule({ rule_name => 'overduefinescap', rule_value => 6, branchcode => undef, categorycode => undef, itemtype => undef });
313 ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
314 is( int($amount), 5, 'Get the lesser of overduefinescap and replacement price where overduefinescap > replacement price' );
319 subtest 'Recall overdue fines' => sub {
322 Koha::CirculationRules->set_rules(
325 categorycode => undef,
329 lengthunit => 'days',
333 recall_overdue_fine => '5.00',
338 my $start_dt = DateTime->new(
344 my $end_dt = DateTime->new(
350 my $recall = Koha::Recall->new({
351 patron_id => $patron->{borrowernumber},
352 created_date => dt_from_string,
353 biblio_id => $item->biblionumber,
354 pickup_library_id => $branch->{branchcode},
355 item_id => $item->itemnumber,
356 expiration_date => undef,
359 $recall->set_overdue;
361 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
362 is( int($amount), 25, 'Use recall fine amount specified in circulation rules' );
364 $recall->set_fulfilled;
365 ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
366 is( int($amount), 5, 'With no recall, use normal fine amount' );
373 $dbh->do(q|DELETE FROM circulation_rules|);