Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use DateTime; |
7 |
use DateTime::Duration; |
8 |
use Test::More tests => 11; |
9 |
use Test::MockModule; |
10 |
use Test::NoWarnings; |
11 |
|
12 |
use C4::Circulation; |
13 |
use C4::Overdues; |
14 |
use Koha::DateUtils qw( dt_from_string ); |
15 |
use Koha::DiscreteCalendar; |
16 |
|
17 |
use t::lib::Mocks; |
18 |
use t::lib::TestBuilder; |
19 |
|
20 |
my $dbh = C4::Context->dbh(); |
21 |
my $today = DateTime->today; |
22 |
my $schema = Koha::Database->new->schema; |
23 |
$schema->storage->txn_begin; |
24 |
|
25 |
my $builder = t::lib::TestBuilder->new(); |
26 |
my $branch = $builder->build( { source => 'Branch' } )->{branchcode}; |
27 |
|
28 |
my $calendar = Koha::DiscreteCalendar->new( { branchcode => $branch } ); |
29 |
|
30 |
####################################### |
31 |
# Add minutes and open minutes between |
32 |
####################################### |
33 |
|
34 |
my $start_date = dt_from_string->set( hour => 14, minute => 15, second => 0 ); |
35 |
my $end_date = dt_from_string->set( hour => 14, minute => 20, second => 0 ); |
36 |
|
37 |
is( $calendar->open_minutes_between( $start_date, $end_date ), 5, "Item returned 5 minutes late" ); |
38 |
|
39 |
#Adding 10 minutes |
40 |
my $ten_mins_duration = DateTime::Duration->new( minutes => 10 ); |
41 |
is( |
42 |
$calendar->addDuration( $start_date, $ten_mins_duration, 'minutes' ), |
43 |
dt_from_string->set( hour => 14, minute => 25, second => 0 ), |
44 |
'Added 10 minutes to loan' |
45 |
); |
46 |
|
47 |
#Adding 10 minutes, passed closing hour |
48 |
$start_date = dt_from_string->set( hour => 16, minute => 51, second => 0 ); |
49 |
is( |
50 |
$calendar->addDuration( $start_date, $ten_mins_duration, 'minutes' ), |
51 |
dt_from_string->set( hour => 17, minute => 0, second => 0 ), |
52 |
'Added 10 minutes, due date passed closing hours, set due date to same day at close hour' |
53 |
); |
54 |
|
55 |
#Item returned next open day after a holiday. |
56 |
my $open_minutes_today = DateTime->today->add( hours => 11, minutes => 10 ); |
57 |
my $open_minutes_tomorrow = DateTime->today->add( days => 1 ); |
58 |
my $open_minutes_day_after_tomorrow = DateTime->today->add( days => 2, hours => 11 ); |
59 |
|
60 |
$calendar->edit_holiday( |
61 |
{ |
62 |
title => "Single holiday Today", |
63 |
holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, |
64 |
start_date => $open_minutes_tomorrow, |
65 |
end_date => $open_minutes_tomorrow |
66 |
} |
67 |
); |
68 |
|
69 |
is( |
70 |
$calendar->open_minutes_between( $open_minutes_today, $open_minutes_day_after_tomorrow ), |
71 |
530, |
72 |
"Correct open minutes, with a holiday in between" |
73 |
); |
74 |
|
75 |
###################### |
76 |
# DueDate calculation |
77 |
###################### |
78 |
|
79 |
#Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days' |
80 |
t::lib::Mocks::mock_preference( 'ReturnBeforeExpiry', 1 ); |
81 |
t::lib::Mocks::mock_preference( 'useDaysMode', 'Days' ); |
82 |
|
83 |
my $issuelength = 25; |
84 |
my $renewalperiod = 5; |
85 |
my $lengthunit = 'minutes'; |
86 |
my $dateexpiry = DateTime->today->add( years => 1 ); |
87 |
|
88 |
my $mock_loan_length = [ |
89 |
[ 'issuelength', 'renewalperiod', 'lengthunit' ], |
90 |
[ $issuelength, $renewalperiod, $lengthunit ] |
91 |
]; |
92 |
|
93 |
my $categorycode = $builder->build( { source => 'Category' } )->{categorycode}; |
94 |
my $patron = $builder->build_object( |
95 |
{ |
96 |
class => 'Koha::Patrons', |
97 |
value => { |
98 |
categorycode => $categorycode, |
99 |
branchcode => $branch, |
100 |
dateexpiry => $dateexpiry, |
101 |
}, |
102 |
} |
103 |
); |
104 |
my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } )->itemtype; |
105 |
$dbh->do( |
106 |
"INSERT INTO circulation_rules (branchcode, categorycode, itemtype, rule_name, rule_value) VALUES('$branch', '$categorycode', '$itemtype', 'issuelength', '$issuelength')" |
107 |
); |
108 |
$dbh->do( |
109 |
"INSERT INTO circulation_rules (branchcode, categorycode, itemtype, rule_name, rule_value) VALUES('$branch', '$categorycode', '$itemtype', 'lengthunit', '$lengthunit')" |
110 |
); |
111 |
|
112 |
my $date = C4::Circulation::CalcDateDue( $today, $itemtype, $branch, $patron ); |
113 |
is( $date, DateTime->today->add( minutes => $issuelength ), "Due date calculated correctly" ); |
114 |
|
115 |
#passed closing hour |
116 |
$issuelength = 1300; |
117 |
Koha::CirculationRules->set_rules( |
118 |
{ |
119 |
categorycode => $categorycode, |
120 |
branchcode => $branch, |
121 |
itemtype => $itemtype, |
122 |
rules => { |
123 |
issuelength => $issuelength, |
124 |
} |
125 |
} |
126 |
); |
127 |
|
128 |
$date = C4::Circulation::CalcDateDue( $today, $itemtype, $branch, $patron ); |
129 |
is( $date, DateTime->today->add( hours => 17 ), "Due date passed close hour, item due at close hour" ); |
130 |
|
131 |
############################# |
132 |
# Chargeable minutes between |
133 |
############################ |
134 |
|
135 |
$issuelength = 25; |
136 |
Koha::CirculationRules->set_rules( |
137 |
{ |
138 |
categorycode => $categorycode, |
139 |
branchcode => $branch, |
140 |
itemtype => $itemtype, |
141 |
rules => { |
142 |
issuelength => $issuelength, |
143 |
} |
144 |
} |
145 |
); |
146 |
|
147 |
my $date_due = DateTime->today; |
148 |
my $date_returned = DateTime->today->add( minutes => 40 ); |
149 |
|
150 |
my $chargeable_units = C4::Overdues::get_chargeable_units( 'minutes', $date_due, $date_returned, $branch ); |
151 |
is( $chargeable_units, 40, "40 minutes of use" ); |
152 |
|
153 |
###################### |
154 |
# Fines calculation |
155 |
##################### |
156 |
|
157 |
my $biblio = $builder->build_sample_biblio(); |
158 |
|
159 |
my $item = $builder->build( |
160 |
{ |
161 |
source => 'Item', |
162 |
value => { |
163 |
biblionumber => $biblio->biblionumber, |
164 |
homebranch => $branch, |
165 |
holdingbranch => $branch, |
166 |
replacementprice => '5.00', |
167 |
}, |
168 |
} |
169 |
); |
170 |
my $rule = $builder->schema->resultset('CirculationRule')->search( |
171 |
{ |
172 |
branchcode => undef, |
173 |
categorycode => undef, |
174 |
itemtype => undef, |
175 |
} |
176 |
); |
177 |
$rule->delete_all if $rule; |
178 |
|
179 |
# FinesIncludeGracePeriod included |
180 |
t::lib::Mocks::mock_preference( 'FinesIncludeGracePeriod', 1 ); |
181 |
$builder->build( |
182 |
{ |
183 |
source => 'CirculationRule', |
184 |
value => { |
185 |
branchcode => undef, |
186 |
categorycode => undef, |
187 |
itemtype => undef, |
188 |
rule_name => 'fine', |
189 |
rule_value => '1', |
190 |
}, |
191 |
} |
192 |
); |
193 |
$builder->build( |
194 |
{ |
195 |
source => 'CirculationRule', |
196 |
value => { |
197 |
branchcode => undef, |
198 |
categorycode => undef, |
199 |
itemtype => undef, |
200 |
rule_name => 'lengthunit', |
201 |
rule_value => 'minutes', |
202 |
}, |
203 |
} |
204 |
); |
205 |
$builder->build( |
206 |
{ |
207 |
source => 'CirculationRule', |
208 |
value => { |
209 |
branchcode => undef, |
210 |
categorycode => undef, |
211 |
itemtype => undef, |
212 |
rule_name => 'finedays', |
213 |
rule_value => 0, |
214 |
}, |
215 |
} |
216 |
); |
217 |
$builder->build( |
218 |
{ |
219 |
source => 'CirculationRule', |
220 |
value => { |
221 |
branchcode => undef, |
222 |
categorycode => undef, |
223 |
itemtype => undef, |
224 |
rule_name => 'firstremind', |
225 |
rule_value => 5, |
226 |
}, |
227 |
} |
228 |
); |
229 |
$builder->build( |
230 |
{ |
231 |
source => 'CirculationRule', |
232 |
value => { |
233 |
branchcode => undef, |
234 |
categorycode => undef, |
235 |
itemtype => undef, |
236 |
rule_name => 'chargeperiod', |
237 |
rule_value => 1, |
238 |
}, |
239 |
} |
240 |
); |
241 |
$builder->build( |
242 |
{ |
243 |
source => 'CirculationRule', |
244 |
value => { |
245 |
branchcode => undef, |
246 |
categorycode => undef, |
247 |
itemtype => undef, |
248 |
rule_name => 'overduefinescap', |
249 |
rule_value => 9000, |
250 |
}, |
251 |
} |
252 |
); |
253 |
$builder->build( |
254 |
{ |
255 |
source => 'CirculationRule', |
256 |
value => { |
257 |
branchcode => undef, |
258 |
categorycode => undef, |
259 |
itemtype => undef, |
260 |
rule_name => 'cap_fine_to_replacement_price', |
261 |
rule_value => 0, |
262 |
}, |
263 |
} |
264 |
); |
265 |
|
266 |
my $start_dt = dt_from_string->set( hour => 15, minute => 0, second => 0 ); |
267 |
my $end_dt = dt_from_string->set( hour => 15, minute => 4, second => 0 ); |
268 |
|
269 |
my @amount = C4::Overdues::CalcFine( $item, $patron->{categorycode}, $branch, $start_dt, $end_dt ); |
270 |
is( $amount[0], 0, "No fine when under the fine grace period" ); |
271 |
|
272 |
$end_dt = dt_from_string->set( hour => 15, minute => 6, second => 0 ); |
273 |
@amount = C4::Overdues::CalcFine( $item, $patron->{categorycode}, $branch, $start_dt, $end_dt ); |
274 |
is( $amount[0], 6, "6\$ fine for 6 minutes delay, fine grace period included" ); |
275 |
|
276 |
# FinesIncludeGracePeriod not included |
277 |
t::lib::Mocks::mock_preference( 'FinesIncludeGracePeriod', 0 ); |
278 |
|
279 |
@amount = C4::Overdues::CalcFine( $item, $patron->{categorycode}, $branch, $start_dt, $end_dt ); |
280 |
is( $amount[0], 1, "1\$ fine for 6 minutes delay, fine grace period not included" ); |
281 |
|
282 |
$schema->storage->txn_rollback; |
283 |
|
284 |
1; |