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; |
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 $branch1 = "DFLT"; |
22 |
my $schema = Koha::Database->new->schema; |
23 |
my $calendar = Koha::DiscreteCalendar->new(branchcode => $branch1); |
24 |
|
25 |
$schema->storage->txn_begin; |
26 |
$schema->resultset('DiscreteCalendar')->search({ |
27 |
branchcode =>'DFLT' |
28 |
})->update_all({ |
29 |
isopened => 1, |
30 |
holidaytype => '', |
31 |
note => '', |
32 |
openhour => '08:00:00', |
33 |
closehour => '16:00:00' |
34 |
}); |
35 |
|
36 |
|
37 |
####################################### |
38 |
# Add minutes and open minutes between |
39 |
####################################### |
40 |
|
41 |
my $start_date = dt_from_string('2017-01-19 14:15:00', 'iso'); |
42 |
my $end_date = dt_from_string('2017-01-19 14:20:00', 'iso'); |
43 |
|
44 |
is($calendar->open_minutes_between($start_date, $end_date)->in_units('minutes'), 5, "Item returned 5 minutes late"); |
45 |
|
46 |
#Adding 10 minutes |
47 |
my $ten_mins_duration = DateTime::Duration->new( minutes => 10); |
48 |
is($calendar->addDate($start_date, $ten_mins_duration, 'minutes' ), |
49 |
dt_from_string('2017-01-19 14:25:00', 'iso'), |
50 |
'Added 10 minutes to loan' ); |
51 |
|
52 |
#Adding 10 minutes, passed closing hour |
53 |
$start_date = dt_from_string('2017-01-19 15:51:00', 'iso'); |
54 |
is($calendar->addDate($start_date, $ten_mins_duration, 'minutes' ), |
55 |
dt_from_string('2017-01-19 16:00:00', 'iso'), |
56 |
'Added 10 minutes, due date passed closing hours, set due date to same day at close hour' ); |
57 |
|
58 |
#Item returened next open day after a holiday. |
59 |
my $open_minutes_today = DateTime->today->add(hours =>10, minutes => 10); |
60 |
my $open_minutes_tomorrow = DateTime->today->add(days =>1); |
61 |
my $open_minutes_day_after_tomorrow = DateTime->today->add(days => 2, hours =>11); |
62 |
|
63 |
$calendar->edit_holiday("Single holiday Today", '', "E", '', '', $open_minutes_tomorrow, $open_minutes_tomorrow); |
64 |
|
65 |
is($calendar->open_minutes_between($open_minutes_today, $open_minutes_day_after_tomorrow)->in_units('minutes'), |
66 |
530, |
67 |
"Correct open minutes, with a holiday in between"); |
68 |
|
69 |
###################### |
70 |
# DueDate calculation |
71 |
###################### |
72 |
|
73 |
#Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days' |
74 |
t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1); |
75 |
t::lib::Mocks::mock_preference('useDaysMode', 'Days'); |
76 |
|
77 |
my $issuelength = 25; |
78 |
my $renewalperiod = 5; |
79 |
my $lengthunit = 'minutes'; |
80 |
my $dateexpiry = '2018-01-01'; |
81 |
|
82 |
my $mock_loan_length = [ |
83 |
['issuelength', 'renewalperiod', 'lengthunit'], |
84 |
[$issuelength, $renewalperiod, $lengthunit] |
85 |
]; |
86 |
|
87 |
my $categorycode = 'X'; |
88 |
my $itemtype = 'MINUTES'; |
89 |
my $borrower = {categorycode => 'X', dateexpiry => $dateexpiry}; |
90 |
$dbh->do("INSERT INTO issuingrules (categorycode, branchcode, itemtype, issuelength, lengthunit) VALUES('$categorycode', '$branch1', '$itemtype', '$issuelength', '$lengthunit')"); |
91 |
|
92 |
my $date = C4::Circulation::CalcDateDue( $today, $itemtype, $branch1, $borrower ); |
93 |
is($date, DateTime->today->add(minutes => $issuelength), "Due date calculated correctly"); |
94 |
|
95 |
#passed closing hour |
96 |
$issuelength = 1300; |
97 |
$dbh->do("UPDATE issuingrules SET issuelength=$issuelength where branchcode='$branch1' and itemtype='$itemtype'"); |
98 |
|
99 |
$date = C4::Circulation::CalcDateDue( $today, $itemtype, $branch1, $borrower ); |
100 |
is($date, DateTime->today->add(hours =>16), "Due date passed close hour, item due at close hour"); |
101 |
|
102 |
############################# |
103 |
# Chargeable minutes between |
104 |
############################ |
105 |
|
106 |
$issuelength = 25; |
107 |
$dbh->do("UPDATE issuingrules SET issuelength=$issuelength where branchcode='$branch1' and itemtype='$itemtype'"); |
108 |
|
109 |
my $date_due = DateTime->today; |
110 |
my $date_returned = DateTime->today->add(minutes => 40); |
111 |
|
112 |
my $chargeable_units = C4::Overdues::get_chargeable_units('minutes', $date_due, $date_returned, $branch1); |
113 |
is($chargeable_units, 40, "40 minutes of use"); |
114 |
|
115 |
###################### |
116 |
# Fines calculation |
117 |
##################### |
118 |
|
119 |
my $builder = t::lib::TestBuilder->new(); |
120 |
|
121 |
my $category = $builder->build( |
122 |
{ |
123 |
source => 'Category', |
124 |
} |
125 |
); |
126 |
|
127 |
my $patron = $builder->build( |
128 |
{ |
129 |
source => 'Borrower', |
130 |
value => { |
131 |
categorycode => $category->{categorycode}, |
132 |
branchcode => $branch1 |
133 |
}, |
134 |
} |
135 |
); |
136 |
|
137 |
my $biblio = $builder->build( |
138 |
{ |
139 |
source => 'Biblio', |
140 |
value => { |
141 |
branchcode => $branch1, |
142 |
}, |
143 |
} |
144 |
); |
145 |
|
146 |
my $item = $builder->build( |
147 |
{ |
148 |
source => 'Item', |
149 |
value => { |
150 |
biblionumber => $biblio->{biblionumber}, |
151 |
homebranch => $branch1, |
152 |
holdingbranch => $branch1, |
153 |
replacementprice => '5.00', |
154 |
}, |
155 |
} |
156 |
); |
157 |
|
158 |
my $rule = $builder->schema->resultset('Issuingrule')->find({ |
159 |
branchcode => '*', |
160 |
categorycode => '*', |
161 |
itemtype => '*', |
162 |
}); |
163 |
$rule->delete if $rule; |
164 |
# FinesIncludeGracePeriod included |
165 |
t::lib::Mocks::mock_preference('FinesIncludeGracePeriod', 1); |
166 |
my $issuingrule = $builder->build( |
167 |
{ |
168 |
source => 'Issuingrule', |
169 |
value => { |
170 |
branchcode => '*', |
171 |
categorycode => '*', |
172 |
itemtype => '*', |
173 |
fine => '1', |
174 |
lengthunit => 'minutes', |
175 |
finedays => 0, |
176 |
firstremind => 5, |
177 |
chargeperiod => 1, |
178 |
overduefinescap => undef, |
179 |
cap_fine_to_replacement_price => 0, |
180 |
}, |
181 |
} |
182 |
); |
183 |
my $start_dt = dt_from_string('2017-01-02 15:00:00', 'iso'); |
184 |
my $end_dt = dt_from_string('2017-01-02 15:04:00', 'iso'); |
185 |
|
186 |
my ($amount) = C4::Overdues::CalcFine( $item, $patron->{categorycode}, $branch1, $start_dt, $end_dt ); |
187 |
is($amount, 0, "No fine when under the fine grace periode"); |
188 |
|
189 |
$end_dt = dt_from_string('2017-01-02 15:06:00', 'iso'); |
190 |
($amount) = C4::Overdues::CalcFine( $item, $patron->{categorycode}, $branch1, $start_dt, $end_dt ); |
191 |
is($amount, 6, "6\$ fine for 6 minutes delay, fine grace periode included"); |
192 |
|
193 |
# FinesIncludeGracePeriod not included |
194 |
t::lib::Mocks::mock_preference('FinesIncludeGracePeriod', 0); |
195 |
|
196 |
($amount) = C4::Overdues::CalcFine( $item, $patron->{categorycode}, $branch1, $start_dt, $end_dt ); |
197 |
is($amount, 1, "1\$ fine for 6 minutes delay, fine grace periode not included"); |
198 |
|
199 |
$schema->storage->txn_rollback; |
200 |
|
201 |
1; |