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