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

(-)a/C4/Circulation.pm (-11 / +29 lines)
Lines 1158-1167 AddIssue does the following things : Link Here
1158
sub AddIssue {
1158
sub AddIssue {
1159
    my ( $borrower, $barcode, $datedue, $cancelreserve, $issuedate, $sipmode) = @_;
1159
    my ( $borrower, $barcode, $datedue, $cancelreserve, $issuedate, $sipmode) = @_;
1160
    my $dbh = C4::Context->dbh;
1160
    my $dbh = C4::Context->dbh;
1161
	my $barcodecheck=CheckValidBarcode($barcode);
1161
1162
    if ($datedue && ref $datedue ne 'DateTime') {
1162
    my $barcodecheck=CheckValidBarcode($barcode);
1163
        $datedue = dt_from_string($datedue);
1163
1164
    }
1165
    # $issuedate defaults to today.
1164
    # $issuedate defaults to today.
1166
    if ( ! defined $issuedate ) {
1165
    if ( ! defined $issuedate ) {
1167
        $issuedate = DateTime->now(time_zone => C4::Context->tz());
1166
        $issuedate = DateTime->now(time_zone => C4::Context->tz());
Lines 1176-1182 sub AddIssue { Link Here
1176
		# find which item we issue
1175
		# find which item we issue
1177
		my $item = GetItem('', $barcode) or return;	# if we don't get an Item, abort.
1176
		my $item = GetItem('', $barcode) or return;	# if we don't get an Item, abort.
1178
		my $branch = _GetCircControlBranch($item,$borrower);
1177
		my $branch = _GetCircControlBranch($item,$borrower);
1179
		
1178
1179
                # $datedue was passed as a parameter, check validity
1180
                # and adjust if mandated by sysprefs
1181
                if ($datedue && ref $datedue ne 'DateTime') {
1182
                    $datedue = dt_from_string($datedue);
1183
                    my $calendar = Koha::Calendar->new( branchcode => $branch );
1184
                    if ( C4::Context->preference('useDaysMode') eq 'Datedue'
1185
                       || C4::Context->preference('useDaysMode') eq 'Calendar' )
1186
                    {
1187
                        # use the calendar to push the due date to the next open day
1188
                        $datedue = $calendar->next_open_day($datedue);
1189
                    }
1190
		}
1191
1180
		# get actual issuing if there is one
1192
		# get actual issuing if there is one
1181
		my $actualissue = GetItemIssue( $item->{itemnumber});
1193
		my $actualissue = GetItemIssue( $item->{itemnumber});
1182
		
1194
		
Lines 3004-3025 sub CalcDateDue { Link Here
3004
            C4::Context->preference('dateformat')
3016
            C4::Context->preference('dateformat')
3005
        );
3017
        );
3006
    } else {
3018
    } else {
3007
3008
        # otherwise, calculate the datedue as normal
3019
        # otherwise, calculate the datedue as normal
3009
        if ( C4::Context->preference('useDaysMode') eq 'Days' )
3020
        my $calendar = Koha::Calendar->new( branchcode => $branch );
3010
        {    # ignoring calendar
3021
3022
        if ( C4::Context->preference('useDaysMode') ne 'Calendar' )
3023
        {   # ignoring calendar to calculate date due
3011
            my $dt =
3024
            my $dt =
3012
              DateTime->now( time_zone => C4::Context->tz() )
3025
              DateTime->now( time_zone => C4::Context->tz() )
3013
              ->truncate( to => 'minute' );
3026
              ->truncate( to => 'minute' );
3014
            if ( $loanlength->{lengthunit} eq 'hours' ) {
3027
            if ( $loanlength->{lengthunit} eq 'hours' ) {
3015
                $dt->add( hours => $loanlength->{issuelength} );
3028
                $dt->add( hours => $loanlength->{issuelength} );
3016
                return $dt;
3017
            } else {    # days
3029
            } else {    # days
3018
                $dt->add( days => $loanlength->{issuelength} );
3030
                $dt->add( days => $loanlength->{issuelength} );
3019
                $dt->set_hour(23);
3031
                $dt->set_hour(23);
3020
                $dt->set_minute(59);
3032
                $dt->set_minute(59);
3021
                return $dt;
3022
            }
3033
            }
3034
            
3035
            if ( C4::Context->preference('useDaysMode') eq 'Datedue' ) {
3036
                # use the calendar to push the due date to the next open day
3037
                $dt = $calendar->next_open_day($dt);
3038
            }
3039
3040
            return $dt;
3041
3023
        } else {
3042
        } else {
3024
            my $dur;
3043
            my $dur;
3025
            if ($loanlength->{lengthunit} eq 'hours') {
3044
            if ($loanlength->{lengthunit} eq 'hours') {
Lines 3031-3037 sub CalcDateDue { Link Here
3031
            if (ref $startdate ne 'DateTime' ) {
3050
            if (ref $startdate ne 'DateTime' ) {
3032
                $startdate = dt_from_string($startdate);
3051
                $startdate = dt_from_string($startdate);
3033
            }
3052
            }
3034
            my $calendar = Koha::Calendar->new( branchcode => $branch );
3035
            $datedue = $calendar->addDate( $startdate, $dur, $loanlength->{lengthunit} );
3053
            $datedue = $calendar->addDate( $startdate, $dur, $loanlength->{lengthunit} );
3036
            if ($loanlength->{lengthunit} eq 'days') {
3054
            if ($loanlength->{lengthunit} eq 'days') {
3037
                $datedue->set_hour(23);
3055
                $datedue->set_hour(23);
(-)a/Koha/Calendar.pm (-3 / +23 lines)
Lines 164-169 sub is_holiday { Link Here
164
    return 0;
164
    return 0;
165
}
165
}
166
166
167
sub next_open_day {
168
    my ( $self, $dt ) = @_;
169
170
    while ($self->is_holiday($dt)) {
171
        $dt->add(days => 1);
172
    }
173
174
    return $dt;
175
}
176
167
sub days_between {
177
sub days_between {
168
    my $self     = shift;
178
    my $self     = shift;
169
    my $start_dt = shift;
179
    my $start_dt = shift;
Lines 273-281 This documentation refers to Koha::Calendar version 0.0.1 Link Here
273
283
274
=head1 SYNOPSIS
284
=head1 SYNOPSIS
275
285
276
  use Koha::Calendat
286
  use Koha::Calendar
277
287
278
  my $c = Koha::Calender->new( branchcode => 'MAIN' );
288
  my $c = Koha::Calendar->new( branchcode => 'MAIN' );
279
  my $dt = DateTime->now();
289
  my $dt = DateTime->now();
280
290
281
  # are we open
291
  # are we open
Lines 315-321 parameter will be removed when issuingrules properly cope with that Link Here
315
325
316
$yesno = $calendar->is_holiday($dt);
326
$yesno = $calendar->is_holiday($dt);
317
327
318
passed at DateTime object returns 1 if it is a closed day
328
passed a DateTime object returns 1 if it is a closed day
319
0 if not according to the calendar
329
0 if not according to the calendar
320
330
321
=head2 days_between
331
=head2 days_between
Lines 326-331 Passed two dates returns a DateTime::Duration object measuring the length betwee Link Here
326
ignoring closed days. Always returns a positive number irrespective of the
336
ignoring closed days. Always returns a positive number irrespective of the
327
relative order of the parameters
337
relative order of the parameters
328
338
339
=head2 next_open_day
340
341
$datetime = $calendar->next_open_day($duedate_dt)
342
343
Passed a Datetime returns another Datetime representing the next open day. It is
344
intended for use to calculate the due date when useDaysMode syspref is set to either
345
'Datedue' or 'Calendar'.
346
347
Note: if is_holiday($duedate_dt) = 0 then it should return the same date.
348
329
=head2 set_daysmode
349
=head2 set_daysmode
330
350
331
For testing only allows the calling script to change days mode
351
For testing only allows the calling script to change days mode
(-)a/t/Calendar.t (-2 / +5 lines)
Lines 3-9 Link Here
3
use strict;
3
use strict;
4
use warnings;
4
use warnings;
5
use DateTime;
5
use DateTime;
6
use Test::More tests => 21;
6
use Test::More tests => 23;
7
use Koha::DateUtils;
7
use Koha::DateUtils;
8
8
9
BEGIN {
9
BEGIN {
Lines 127-129 $cal->add_holiday( dt_from_string('2012-07-07','iso') ); Link Here
127
$daycount = $cal->days_between( dt_from_string("2012-07-01",'iso'),
127
$daycount = $cal->days_between( dt_from_string("2012-07-01",'iso'),
128
    dt_from_string("2012-07-15",'iso') )->in_units('days');
128
    dt_from_string("2012-07-15",'iso') )->in_units('days');
129
cmp_ok( $daycount, '==', 12, 'multiple holidays correctly recognized' );
129
cmp_ok( $daycount, '==', 12, 'multiple holidays correctly recognized' );
130
- 
130
$dt = dt_from_string('2012-07-07','iso');
131
cmp_ok( $dt->add(days => 1), '==', $cal->next_open_day(dt_from_string('2012-07-07','iso')), 'Next open day correctly calculated' );
132
$dt = dt_from_string('2012-07-08','iso');
133
cmp_ok( $dt, '==', $cal->next_open_day(dt_from_string('2012-07-08','iso')), 'Next open day correctly calculated (idempotent version)' );

Return to bug 8800