85f38ea42e3a5f7cb9437fa2ec731fd88d52e4db
[srvgit] / t / db_dependent / Circulation / CalcFine.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 3;
6
7 use C4::Context;
8 use C4::Overdues;
9
10 use Koha::DateUtils qw( dt_from_string );
11
12 use t::lib::TestBuilder;
13 use t::lib::Mocks;
14
15 my $schema = Koha::Database->schema;
16 $schema->storage->txn_begin;
17
18 our $dbh = C4::Context->dbh;
19 $dbh->do(q|DELETE FROM issues|);
20
21 t::lib::Mocks::mock_preference('item-level_itypes', '1');
22
23 my $builder = t::lib::TestBuilder->new();
24
25 my $branch = $builder->build(
26     {
27         source => 'Branch',
28     }
29 );
30
31 my $category = $builder->build(
32     {
33         source => 'Category',
34     }
35 );
36
37 my $patron = $builder->build(
38     {
39         source => 'Borrower',
40         value  => {
41             categorycode => $category->{categorycode},
42             branchcode   => $branch->{branchcode},
43         },
44     }
45 );
46
47 my $itemtype = $builder->build(
48     {
49         source => 'Itemtype',
50         value => {
51             defaultreplacecost => 6,
52         },
53     }
54 );
55
56 my $item = $builder->build_sample_item(
57     {
58         library          => $branch->{branchcode},
59         replacementprice => '5.00',
60         itype            => $itemtype->{itemtype},
61     }
62 );
63
64 subtest 'Test basic functionality' => sub {
65     plan tests => 1;
66
67     Koha::CirculationRules->set_rules(
68         {
69             branchcode   => undef,
70             categorycode => undef,
71             itemtype     => undef,
72             rules        => {
73                 fine                          => '1.00',
74                 lengthunit                    => 'days',
75                 finedays                      => 0,
76                 firstremind                   => 0,
77                 chargeperiod                  => 1,
78                 overduefinescap               => undef,
79                 cap_fine_to_replacement_price => 0,
80             }
81         },
82     );
83
84     my $start_dt = DateTime->new(
85         year       => 2000,
86         month      => 1,
87         day        => 1,
88     );
89
90     my $end_dt = DateTime->new(
91         year       => 2000,
92         month      => 1,
93         day        => 30,
94     );
95
96     my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
97
98     is( $amount, 29, 'Amount is calculated correctly' );
99
100     teardown();
101 };
102
103 subtest 'Test cap_fine_to_replacement_price' => sub {
104     plan tests => 2;
105
106     t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
107     Koha::CirculationRules->set_rules(
108         {
109             branchcode   => undef,
110             categorycode => undef,
111             itemtype     => undef,
112             rules        => {
113                 fine                          => '1.00',
114                 lengthunit                    => 'days',
115                 finedays                      => 0,
116                 firstremind                   => 0,
117                 chargeperiod                  => 1,
118                 overduefinescap               => undef,
119                 cap_fine_to_replacement_price => 1,
120             },
121         }
122     );
123
124     my $start_dt = DateTime->new(
125         year       => 2000,
126         month      => 1,
127         day        => 1,
128     );
129
130     my $end_dt = DateTime->new(
131         year       => 2000,
132         month      => 1,
133         day        => 30,
134     );
135
136     my $item = $builder->build_sample_item(
137         {
138             library          => $branch->{branchcode},
139             replacementprice => 5,
140             itype            => $itemtype->{itemtype},
141         }
142     );
143
144     my ($amount) = CalcFine( $item->unblessed, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
145
146     is( int($amount), 5, 'Amount is calculated correctly' );
147
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' );
152
153     teardown();
154 };
155
156 subtest 'Test cap_fine_to_replacement_pricew with overduefinescap' => sub {
157     plan tests => 2;
158
159     t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1');
160     Koha::CirculationRules->set_rules(
161         {
162             branchcode   => undef,
163             categorycode => undef,
164             itemtype     => undef,
165             rules        => {
166                 fine                          => '1.00',
167                 lengthunit                    => 'days',
168                 finedays                      => 0,
169                 firstremind                   => 0,
170                 chargeperiod                  => 1,
171                 overduefinescap               => 3,
172                 cap_fine_to_replacement_price => 1,
173             },
174         }
175     );
176
177     my $start_dt = DateTime->new(
178         year       => 2000,
179         month      => 1,
180         day        => 1,
181     );
182
183     my $end_dt = DateTime->new(
184         year       => 2000,
185         month      => 1,
186         day        => 30,
187     );
188
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' );
191
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' );
195
196     teardown();
197 };
198
199 sub teardown {
200     $dbh->do(q|DELETE FROM circulation_rules|);
201 }