5 use Test::More tests => 3;
8 use C4::Overdues qw( CalcFine );
10 use Koha::DateUtils qw( dt_from_string );
12 use t::lib::TestBuilder;
15 my $schema = Koha::Database->schema;
16 $schema->storage->txn_begin;
18 our $dbh = C4::Context->dbh;
19 $dbh->do(q|DELETE FROM issues|);
21 t::lib::Mocks::mock_preference('item-level_itypes', '1');
23 my $builder = t::lib::TestBuilder->new();
25 my $branch = $builder->build(
31 my $category = $builder->build(
37 my $patron = $builder->build(
41 categorycode => $category->{categorycode},
42 branchcode => $branch->{branchcode},
47 my $itemtype = $builder->build(
51 defaultreplacecost => 6,
56 my $item = $builder->build_sample_item(
58 library => $branch->{branchcode},
59 replacementprice => '5.00',
60 itype => $itemtype->{itemtype},
64 subtest 'Test basic functionality' => sub {
67 Koha::CirculationRules->set_rules(
70 categorycode => undef,
78 overduefinescap => undef,
79 cap_fine_to_replacement_price => 0,
84 my $start_dt = DateTime->new(
90 my $end_dt = DateTime->new(
96 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
98 is( $amount, 29, 'Amount is calculated correctly' );
103 subtest 'Test cap_fine_to_replacement_price' => sub {
106 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
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(
136 my $item = $builder->build_sample_item(
138 library => $branch->{branchcode},
139 replacementprice => 5,
140 itype => $itemtype->{itemtype},
144 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
146 is( int($amount), 5, 'Amount is calculated correctly' );
148 # Use default replacement cost (useDefaultReplacementCost) is item's replacement price is 0
149 $item->replacementprice(0)->store;
150 ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
151 is( int($amount), 6, 'Amount is calculated correctly' );
156 subtest 'Test cap_fine_to_replacement_pricew with overduefinescap' => sub {
159 t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
160 Koha::CirculationRules->set_rules(
163 categorycode => undef,
167 lengthunit => 'days',
171 overduefinescap => 3,
172 cap_fine_to_replacement_price => 1,
177 my $start_dt = DateTime->new(
183 my $end_dt = DateTime->new(
189 my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
190 is( int($amount), 3, 'Got the lesser of overduefinescap and replacement price where overduefinescap < replacement price' );
192 Koha::CirculationRules->set_rule({ rule_name => 'overduefinescap', rule_value => 6, branchcode => undef, categorycode => undef, itemtype => undef });
193 ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
194 is( int($amount), 5, 'Get the lesser of overduefinescap and replacement price where overduefinescap > replacement price' );
200 $dbh->do(q|DELETE FROM circulation_rules|);