5 use Test::More tests => 5;
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 'Test with fine amount empty' => sub {
107 Koha::CirculationRules->set_rules(
110 categorycode => undef,
114 lengthunit => 'days',
118 overduefinescap => undef,
119 cap_fine_to_replacement_price => 1,
124 my $start_dt = DateTime->new(
130 my $end_dt = DateTime->new(
137 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
139 undef, "No warning when fine amount is ''";
144 subtest 'Test cap_fine_to_replacement_price' => sub {
147 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
148 Koha::CirculationRules->set_rules(
151 categorycode => undef,
155 lengthunit => 'days',
159 overduefinescap => undef,
160 cap_fine_to_replacement_price => 1,
165 my $start_dt = DateTime->new(
171 my $end_dt = DateTime->new(
177 my $item = $builder->build_sample_item(
179 library => $branch->{branchcode},
180 replacementprice => 5,
181 itype => $itemtype->{itemtype},
185 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
187 is( int($amount), 5, 'Amount is calculated correctly' );
189 # Use default replacement cost (useDefaultReplacementCost) is item's replacement price is 0
190 $item->replacementprice(0)->store;
191 ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
192 is( int($amount), 6, 'Amount is calculated correctly' );
197 subtest 'Test cap_fine_to_replacement_pricew with overduefinescap' => sub {
200 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
201 Koha::CirculationRules->set_rules(
204 categorycode => undef,
208 lengthunit => 'days',
212 overduefinescap => 3,
213 cap_fine_to_replacement_price => 1,
218 my $start_dt = DateTime->new(
224 my $end_dt = DateTime->new(
230 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
231 is( int($amount), 3, 'Got the lesser of overduefinescap and replacement price where overduefinescap < replacement price' );
233 Koha::CirculationRules->set_rule({ rule_name => 'overduefinescap', rule_value => 6, branchcode => undef, categorycode => undef, itemtype => undef });
234 ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
235 is( int($amount), 5, 'Get the lesser of overduefinescap and replacement price where overduefinescap > replacement price' );
240 subtest 'Recall overdue fines' => sub {
243 Koha::CirculationRules->set_rules(
246 categorycode => undef,
250 lengthunit => 'days',
254 recall_overdue_fine => '5.00',
259 my $start_dt = DateTime->new(
265 my $end_dt = DateTime->new(
271 my $recall = Koha::Recall->new({
272 patron_id => $patron->{borrowernumber},
273 created_date => dt_from_string,
274 biblio_id => $item->biblionumber,
275 pickup_library_id => $branch->{branchcode},
276 item_id => $item->itemnumber,
277 expiration_date => undef,
280 $recall->set_overdue;
282 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
283 is( int($amount), 25, 'Use recall fine amount specified in circulation rules' );
285 $recall->set_fulfilled;
286 ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
287 is( int($amount), 5, 'With no recall, use normal fine amount' );
294 $dbh->do(q|DELETE FROM circulation_rules|);