View | Details | Raw Unified | Return to bug 17983
Collapse All | Expand All

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

Return to bug 17983