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' );
182 subtest 'Test with fine amount empty' => sub {
185 Koha::CirculationRules->set_rules(
188 categorycode => undef,
192 lengthunit => 'days',
196 overduefinescap => undef,
197 cap_fine_to_replacement_price => 1,
202 my $start_dt = DateTime->new(
208 my $end_dt = DateTime->new(
215 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
217 undef, "No warning when fine amount is ''";
222 subtest 'Test cap_fine_to_replacement_price' => sub {
225 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
226 Koha::CirculationRules->set_rules(
229 categorycode => undef,
233 lengthunit => 'days',
237 overduefinescap => undef,
238 cap_fine_to_replacement_price => 1,
243 my $start_dt = DateTime->new(
249 my $end_dt = DateTime->new(
255 my $item = $builder->build_sample_item(
257 library => $branch->{branchcode},
258 replacementprice => 5,
259 itype => $itemtype->{itemtype},
263 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
265 is( int($amount), 5, 'Amount is calculated correctly' );
267 # Use default replacement cost (useDefaultReplacementCost) is item's replacement price is 0
268 $item->replacementprice(0)->store;
269 ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
270 is( int($amount), 6, 'Amount is calculated correctly' );
275 subtest 'Test cap_fine_to_replacement_pricew with overduefinescap' => sub {
278 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
279 Koha::CirculationRules->set_rules(
282 categorycode => undef,
286 lengthunit => 'days',
290 overduefinescap => 3,
291 cap_fine_to_replacement_price => 1,
296 my $start_dt = DateTime->new(
302 my $end_dt = DateTime->new(
308 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
309 is( int($amount), 3, 'Got the lesser of overduefinescap and replacement price where overduefinescap < replacement price' );
311 Koha::CirculationRules->set_rule({ rule_name => 'overduefinescap', rule_value => 6, branchcode => undef, categorycode => undef, itemtype => undef });
312 ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
313 is( int($amount), 5, 'Get the lesser of overduefinescap and replacement price where overduefinescap > replacement price' );
318 subtest 'Recall overdue fines' => sub {
321 Koha::CirculationRules->set_rules(
324 categorycode => undef,
328 lengthunit => 'days',
332 recall_overdue_fine => '5.00',
337 my $start_dt = DateTime->new(
343 my $end_dt = DateTime->new(
349 my $recall = Koha::Recall->new({
350 patron_id => $patron->{borrowernumber},
351 created_date => dt_from_string,
352 biblio_id => $item->biblionumber,
353 pickup_library_id => $branch->{branchcode},
354 item_id => $item->itemnumber,
355 expiration_date => undef,
358 $recall->set_overdue;
360 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
361 is( int($amount), 25, 'Use recall fine amount specified in circulation rules' );
363 $recall->set_fulfilled;
364 ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
365 is( int($amount), 5, 'With no recall, use normal fine amount' );
372 $dbh->do(q|DELETE FROM circulation_rules|);