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

(-)a/C4/Circulation.pm (-1 / +20 lines)
Lines 3710-3720 sub CalcDateDue { Link Here
3710
        }
3710
        }
3711
    );
3711
    );
3712
3712
3713
    my $calendar = Koha::DiscreteCalendar->new({ branchcode => $branch });
3713
    # calculate the datedue as normal
3714
    # calculate the datedue as normal
3714
    if ( $daysmode eq 'Days' )
3715
    if ( $daysmode eq 'Days' )
3715
    {    # ignoring calendar
3716
    {    # ignoring calendar
3716
        if ( $loanlength->{lengthunit} eq 'hours' ) {
3717
        if ( $loanlength->{lengthunit} eq 'hours' ) {
3717
            $datedue->add( hours => $loanlength->{$length_key} );
3718
            $datedue->add( hours => $loanlength->{$length_key} );
3719
	    } elsif ( $loanlength->{lengthunit} eq 'minutes' ) {
3720
	        $datedue->add( minutes => $loanlength->{$length_key} );
3718
        } else {    # days
3721
        } else {    # days
3719
            $datedue->add( days => $loanlength->{$length_key} );
3722
            $datedue->add( days => $loanlength->{$length_key} );
3720
            $datedue->set_hour(23);
3723
            $datedue->set_hour(23);
Lines 3725-3734 sub CalcDateDue { Link Here
3725
        if ($loanlength->{lengthunit} eq 'hours') {
3728
        if ($loanlength->{lengthunit} eq 'hours') {
3726
            $dur = DateTime::Duration->new( hours => $loanlength->{$length_key});
3729
            $dur = DateTime::Duration->new( hours => $loanlength->{$length_key});
3727
        }
3730
        }
3731
	    elsif ($loanlength->{lengthunit} eq 'minutes') {
3732
	        $dur = DateTime::Duration->new( minutes => $loanlength->{$length_key});
3733
	    }
3728
        else { # days
3734
        else { # days
3729
            $dur = DateTime::Duration->new( days => $loanlength->{$length_key});
3735
            $dur = DateTime::Duration->new( days => $loanlength->{$length_key});
3730
        }
3736
        }
3731
        my $calendar = Koha::DiscreteCalendar->new({ branchcode => $branch, days_mode => $daysmode });
3737
	##my $calendar = Koha::DiscreteCalendar->new({ branchcode => $branch, days_mode => $daysmode });
3732
        $datedue = $calendar->addDuration( $datedue, $dur, $loanlength->{lengthunit} );
3738
        $datedue = $calendar->addDuration( $datedue, $dur, $loanlength->{lengthunit} );
3733
        if ($loanlength->{lengthunit} eq 'days') {
3739
        if ($loanlength->{lengthunit} eq 'days') {
3734
            $datedue->set_hour(23);
3740
            $datedue->set_hour(23);
Lines 3774-3779 sub CalcDateDue { Link Here
3774
          }
3780
          }
3775
        }
3781
        }
3776
    }
3782
    }
3783
3784
    #check if it's minutes or hourly loan and set due date to close hour, if the current due date is passed closing hours.
3785
    if ($loanlength->{lengthunit} eq 'hours' || $loanlength->{lengthunit} eq 'minutes') {
3786
	    my $dateInfo = $calendar->get_date_info($datedue);
3787
	    my $close = dt_from_string($dateInfo->{date} ." ". $dateInfo->{close_hour}, 'iso', C4::Context->tz());
3788
3789
	    my $close_datetime = $datedue->clone()->set(hour => $close->hour(), minute=> $close->minute());
3790
3791
	    if(DateTime->compare($datedue, $close_datetime) > 0) {
3792
	        $datedue = $close_datetime->clone();
3793
	    }
3794
    }
3795
3777
    return $datedue;
3796
    return $datedue;
3778
}
3797
}
3779
3798
(-)a/C4/Overdues.pm (+12 lines)
Lines 331-336 sub get_chargeable_units { Link Here
331
        }
331
        }
332
        return $charge_duration->in_units('hours');
332
        return $charge_duration->in_units('hours');
333
    }
333
    }
334
    elsif($unit eq 'minutes'){
335
	    if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
336
	        my $calendar = Koha::DiscreteCalendar->new({ branchcode => $branchcode });
337
	        $charge_duration = $calendar->open_minutes_between( $date_due, $date_returned );
338
	    } else {
339
	        $charge_duration = $date_returned->delta_ms( $date_due );
340
        }
341
	    if($charge_duration == 0 && $charge_duration * 60 > 0) {
342
	        return 1;
343
    	}
344
	    return $charge_duration;
345
    }
334
    else { # days
346
    else { # days
335
        if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
347
        if(C4::Context->preference('finesCalendar') eq 'noFinesWhenClosed') {
336
            my $calendar = Koha::DiscreteCalendar->new({ branchcode => $branchcode });
348
            my $calendar = Koha::DiscreteCalendar->new({ branchcode => $branchcode });
(-)a/Koha/DiscreteCalendar.pm (+96 lines)
Lines 1246-1251 sub open_hours_between { Link Here
1246
    return ($working_hours_between->next()->get_column('hours_between') - $not_used_hours->next()->get_column('not_used_hours'));
1246
    return ($working_hours_between->next()->get_column('hours_between') - $not_used_hours->next()->get_column('not_used_hours'));
1247
}
1247
}
1248
1248
1249
=head2 open_minutes_between
1250
1251
  $minutes = $calendar->open_minutes_between($start_date, $end_date)
1252
1253
Returns the number of minutes between C<$start_date> and C<$end_date>, taking into
1254
account the opening hours of the library.
1255
1256
=cut
1257
1258
sub open_minutes_between {
1259
    my ($self, $start_date, $end_date) = @_;
1260
    my $branchcode = $self->{branchcode};
1261
    my $schema = Koha::Database->new->schema;
1262
    my $dtf = DateTime::Format::Strptime->new(pattern => "%F %T");
1263
    $start_date = $dtf->format_datetime($start_date);
1264
    $end_date = $dtf->format_datetime($end_date);
1265
    
1266
    my $working_minutes_between = $schema->resultset('DiscreteCalendar')->search(
1267
	    {
1268
	        branchcode  => $branchcode,
1269
	        is_opened   => 1,
1270
        },
1271
	    {
1272
	        select  => \['sum(time_to_sec(timediff(close_hour, open_hour)) / 60)'],
1273
	        as      => [qw /minutes_between/],
1274
	        where   => \['date BETWEEN DATE(?) AND DATE(?)', $start_date, $end_date]
1275
        }
1276
    );
1277
1278
    my $loan_day = $schema->resultset('DiscreteCalendar')->search(
1279
	    {
1280
	        branchcode  => $branchcode,
1281
	    },
1282
	    {
1283
	        order_by => \[ 'ABS(DATEDIFF(date, ?))', $start_date ],
1284
	        rows => 1,
1285
	    }
1286
    );
1287
1288
    my $return_day = $schema->resultset('DiscreteCalendar')->search(
1289
	    {
1290
	        branchcode  => $branchcode,
1291
	    },
1292
	    {
1293
	        order_by => \[ 'ABS(DATEDIFF(date, ?))', $end_date ],
1294
	        rows => 1,
1295
        }
1296
    );
1297
1298
    #Capture the time portion of the date
1299
    $start_date =~ /\s(.*)/;
1300
    my $loan_date_time = $1;
1301
    $end_date =~ /\s(.*)/;
1302
    my $return_date_time = $1;
1303
1304
    my $not_used_minutes = $schema->resultset('DiscreteCalendar')->search(
1305
	    {
1306
	        branchcode  => $branchcode,
1307
	        is_opened    => 1,
1308
	    },
1309
	    {
1310
	        select  => \[ '(time_to_sec(timediff(?, ?)) + time_to_sec(timediff(?, ?)) ) / 60', $return_day->next()->close_hour(), $return_date_time, $loan_date_time, $loan_day->next()->open_hour()],
1311
	        as      => [qw /not_used_minutes/],
1312
	    }
1313
    );
1314
1315
    return ($working_minutes_between->next()->get_column('minutes_between') - $not_used_minutes->next()->get_column('not_used_minutes'));
1316
}
1317
1249
=head2 addDuration
1318
=head2 addDuration
1250
1319
1251
  my $dt = $calendar->addDuration($date, $dur, $unit)
1320
  my $dt = $calendar->addDuration($date, $dur, $unit)
Lines 1272-1277 sub addDuration { Link Here
1272
        my $return_by_hour = 10;
1341
        my $return_by_hour = 10;
1273
1342
1274
        $dt = $self->addHours($startdate, $add_duration, $return_by_hour);
1343
        $dt = $self->addHours($startdate, $add_duration, $return_by_hour);
1344
    } elsif($unit eq 'minutes') {
1345
	    $dt = $self->addMinutes($startdate, $add_duration);
1275
    } else {
1346
    } else {
1276
        # days
1347
        # days
1277
        $dt = $self->addDays($startdate, $add_duration);
1348
        $dt = $self->addDays($startdate, $add_duration);
Lines 1314-1319 sub addHours { Link Here
1314
    return $base_date;
1385
    return $base_date;
1315
}
1386
}
1316
1387
1388
=head2 addMinutes
1389
1390
  $end = $calendar->addMinutes($date, $dur)
1391
1392
Add C<$dur> minutes to C<$start> date.
1393
1394
=cut
1395
1396
sub addMinutes {
1397
    my ( $self, $startdate, $minutes_duration) = @_;
1398
    my $base_date = $startdate->clone();
1399
1400
    $base_date->add_duration($minutes_duration);
1401
1402
    my $dateInfo = $self->get_date_info($base_date);
1403
1404
    my $close_datetime = dt_from_string($dateInfo->{date} ." ". $dateInfo->{close_hour}, 'iso');
1405
1406
    if(DateTime->compare($base_date, $close_datetime) > 0) {
1407
	    return $close_datetime;
1408
    }
1409
1410
    return $base_date;
1411
}
1412
1317
=head2 addDays
1413
=head2 addDays
1318
1414
1319
  $date = $calendar->addDays($start, $duration)
1415
  $date = $calendar->addDays($start, $duration)
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smart-rules.tt (+3 lines)
Lines 256-261 Link Here
256
                                            Days
256
                                            Days
257
                                        [% ELSIF ( lengthunit == 'hours') %]
257
                                        [% ELSIF ( lengthunit == 'hours') %]
258
                                            Hours
258
                                            Hours
259
					[% ELSIF ( lengthunit == 'minutes') %]
260
					    Minutes
259
                                        [% ELSE %]
261
                                        [% ELSE %]
260
                                            Undefined
262
                                            Undefined
261
                                        [% END %]
263
                                        [% END %]
Lines 432-437 Link Here
432
                      <select name="lengthunit" id="lengthunit">
434
                      <select name="lengthunit" id="lengthunit">
433
                        <option value="days" selected="selected">Days</option>
435
                        <option value="days" selected="selected">Days</option>
434
                        <option value="hours">Hours</option>
436
                        <option value="hours">Hours</option>
437
			<option value="minutes">Minutes</option>
435
                      </select>
438
                      </select>
436
                    </td>
439
                    </td>
437
                    <td>
440
                    <td>
(-)a/t/db_dependent/MinuteLoans.t (-1 / +283 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 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;

Return to bug 17983