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