|
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 = 'X'; |
| 85 |
my $itemtype = 'MINUTES'; |
| 86 |
my $borrower = {categorycode => 'X', dateexpiry => $dateexpiry}; |
| 87 |
$dbh->do("INSERT INTO itemtypes (itemtype, parent_type) VALUES('$itemtype', NULL)"); |
| 88 |
$dbh->do("INSERT INTO categories (categorycode) VALUES('$categorycode')"); |
| 89 |
$dbh->do("INSERT INTO circulation_rules (branchcode, categorycode, itemtype, rule_name, rule_value) VALUES('$branch', '$categorycode', '$itemtype', 'issuelength', '$issuelength')"); |
| 90 |
$dbh->do("INSERT INTO circulation_rules (branchcode, categorycode, itemtype, rule_name, rule_value) VALUES('$branch', '$categorycode', '$itemtype', 'lengthunit', '$lengthunit')"); |
| 91 |
|
| 92 |
|
| 93 |
my $date = C4::Circulation::CalcDateDue( $today, $itemtype, $branch, $borrower ); |
| 94 |
is($date, DateTime->today->add(minutes => $issuelength), "Due date calculated correctly"); |
| 95 |
|
| 96 |
#passed closing hour |
| 97 |
$issuelength = 1300; |
| 98 |
Koha::CirculationRules->set_rules( |
| 99 |
{ |
| 100 |
categorycode => $categorycode, |
| 101 |
branchcode => $branch, |
| 102 |
itemtype => $itemtype, |
| 103 |
rules => { |
| 104 |
issuelength => $issuelength, |
| 105 |
} |
| 106 |
} |
| 107 |
); |
| 108 |
|
| 109 |
$date = C4::Circulation::CalcDateDue( $today, $itemtype, $branch, $borrower ); |
| 110 |
is($date, DateTime->today->add(hours =>17), "Due date passed close hour, item due at close hour"); |
| 111 |
|
| 112 |
############################# |
| 113 |
# Chargeable minutes between |
| 114 |
############################ |
| 115 |
|
| 116 |
$issuelength = 25; |
| 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 |
my $date_due = DateTime->today; |
| 129 |
my $date_returned = DateTime->today->add(minutes => 40); |
| 130 |
|
| 131 |
my $chargeable_units = C4::Overdues::get_chargeable_units('minutes', $date_due, $date_returned, $branch); |
| 132 |
is($chargeable_units, 40, "40 minutes of use"); |
| 133 |
|
| 134 |
###################### |
| 135 |
# Fines calculation |
| 136 |
##################### |
| 137 |
|
| 138 |
my $category = $builder->build( |
| 139 |
{ |
| 140 |
source => 'Category', |
| 141 |
} |
| 142 |
); |
| 143 |
|
| 144 |
my $patron = $builder->build( |
| 145 |
{ |
| 146 |
source => 'Borrower', |
| 147 |
value => { |
| 148 |
categorycode => $category->{categorycode}, |
| 149 |
branchcode => $branch, |
| 150 |
}, |
| 151 |
} |
| 152 |
); |
| 153 |
|
| 154 |
my $biblio = $builder->build_sample_biblio(); |
| 155 |
|
| 156 |
my $item = $builder->build( |
| 157 |
{ |
| 158 |
source => 'Item', |
| 159 |
value => { |
| 160 |
biblionumber => $biblio->biblionumber, |
| 161 |
homebranch => $branch, |
| 162 |
holdingbranch => $branch, |
| 163 |
replacementprice => '5.00', |
| 164 |
}, |
| 165 |
} |
| 166 |
); |
| 167 |
my $rule = $builder->schema->resultset('CirculationRule')->search({ |
| 168 |
branchcode => undef, |
| 169 |
categorycode => undef, |
| 170 |
itemtype => undef, |
| 171 |
}); |
| 172 |
$rule->delete_all if $rule; |
| 173 |
# FinesIncludeGracePeriod included |
| 174 |
t::lib::Mocks::mock_preference('FinesIncludeGracePeriod', 1); |
| 175 |
$builder->build( |
| 176 |
{ |
| 177 |
source => 'CirculationRule', |
| 178 |
value => { |
| 179 |
branchcode => undef, |
| 180 |
categorycode => undef, |
| 181 |
itemtype => undef, |
| 182 |
rule_name => 'fine', |
| 183 |
rule_value => '1', |
| 184 |
}, |
| 185 |
} |
| 186 |
); |
| 187 |
$builder->build( |
| 188 |
{ |
| 189 |
source => 'CirculationRule', |
| 190 |
value => { |
| 191 |
branchcode => undef, |
| 192 |
categorycode => undef, |
| 193 |
itemtype => undef, |
| 194 |
rule_name => 'lengthunit', |
| 195 |
rule_value => 'minutes', |
| 196 |
}, |
| 197 |
} |
| 198 |
); |
| 199 |
$builder->build( |
| 200 |
{ |
| 201 |
source => 'CirculationRule', |
| 202 |
value => { |
| 203 |
branchcode => undef, |
| 204 |
categorycode => undef, |
| 205 |
itemtype => undef, |
| 206 |
rule_name => 'finedays', |
| 207 |
rule_value => 0, |
| 208 |
}, |
| 209 |
} |
| 210 |
); |
| 211 |
$builder->build( |
| 212 |
{ |
| 213 |
source => 'CirculationRule', |
| 214 |
value => { |
| 215 |
branchcode => undef, |
| 216 |
categorycode => undef, |
| 217 |
itemtype => undef, |
| 218 |
rule_name => 'firstremind', |
| 219 |
rule_value => 5, |
| 220 |
}, |
| 221 |
} |
| 222 |
); |
| 223 |
$builder->build( |
| 224 |
{ |
| 225 |
source => 'CirculationRule', |
| 226 |
value => { |
| 227 |
branchcode => undef, |
| 228 |
categorycode => undef, |
| 229 |
itemtype => undef, |
| 230 |
rule_name => 'chargeperiod', |
| 231 |
rule_value => 1, |
| 232 |
}, |
| 233 |
} |
| 234 |
); |
| 235 |
$builder->build( |
| 236 |
{ |
| 237 |
source => 'CirculationRule', |
| 238 |
value => { |
| 239 |
branchcode => undef, |
| 240 |
categorycode => undef, |
| 241 |
itemtype => undef, |
| 242 |
rule_name => 'overduefinescap', |
| 243 |
rule_value => 9000, |
| 244 |
}, |
| 245 |
} |
| 246 |
); |
| 247 |
$builder->build( |
| 248 |
{ |
| 249 |
source => 'CirculationRule', |
| 250 |
value => { |
| 251 |
branchcode => undef, |
| 252 |
categorycode => undef, |
| 253 |
itemtype => undef, |
| 254 |
rule_name => 'cap_fine_to_replacement_price', |
| 255 |
rule_value => 0, |
| 256 |
}, |
| 257 |
} |
| 258 |
); |
| 259 |
|
| 260 |
my $start_dt = dt_from_string->set( hour => 15, minute => 0, second => 0); |
| 261 |
my $end_dt = dt_from_string->set( hour => 15, minute => 4, second => 0); |
| 262 |
|
| 263 |
my @amount = C4::Overdues::CalcFine( $item, $patron->{categorycode}, $branch, $start_dt, $end_dt ); |
| 264 |
is($amount[0], 0, "No fine when under the fine grace period"); |
| 265 |
|
| 266 |
$end_dt = dt_from_string->set( hour => 15, minute => 6, second => 0); |
| 267 |
@amount = C4::Overdues::CalcFine( $item, $patron->{categorycode}, $branch, $start_dt, $end_dt ); |
| 268 |
is($amount[0], 6, "6\$ fine for 6 minutes delay, fine grace period included"); |
| 269 |
|
| 270 |
# FinesIncludeGracePeriod not included |
| 271 |
t::lib::Mocks::mock_preference('FinesIncludeGracePeriod', 0); |
| 272 |
|
| 273 |
@amount = C4::Overdues::CalcFine( $item, $patron->{categorycode}, $branch, $start_dt, $end_dt ); |
| 274 |
is($amount[0], 1, "1\$ fine for 6 minutes delay, fine grace period not included"); |
| 275 |
|
| 276 |
$schema->storage->txn_rollback; |
| 277 |
|
| 278 |
1; |