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