Bug 5549 : Due Date calculation in issue needs to be HH:MM aware
authorColin Campbell <colin.campbell@ptfs-europe.com>
Wed, 8 Jun 2011 13:03:33 +0000 (14:03 +0100)
committerChris Cormack <chrisc@catalyst.net.nz>
Mon, 19 Mar 2012 23:12:09 +0000 (12:12 +1300)
C4/Circulation.pm
Koha/Calendar.pm [new file with mode: 0644]
Koha/DateUtils.pm
circ/circulation.pl

index 999f617..fc05e84 100644 (file)
@@ -50,6 +50,7 @@ use C4::Branch; # GetBranches
 use C4::Log; # logaction
 
 use Data::Dumper;
+use Koha::DateUtils;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
@@ -691,21 +692,28 @@ sub CanBookBeIssued {
     #
     # DUE DATE is OK ? -- should already have checked.
     #
+    if ($duedate && ref $duedate ne 'DateTime') {
+        $duedate = dt_from_string($duedate);
+    }
+    my $now = DateTime->now( timezone => C4::Context->tz() );
     unless ( $duedate ) {
-        my $issuedate = strftime( "%Y-%m-%d", localtime );
+        my $issuedate = $now->clone();
 
         my $branch = _GetCircControlBranch($item,$borrower);
         my $itype = ( C4::Context->preference('item-level_itypes') ) ? $item->{'itype'} : $biblioitem->{'itemtype'};
-        $duedate = CalcDateDue( C4::Dates->new( $issuedate, 'iso' ), $itype, $branch, $borrower );
+        $duedate = CalcDateDue( $issuedate, $itype, $branch, $borrower );
 
         # Offline circ calls AddIssue directly, doesn't run through here
         #  So issuingimpossible should be ok.
     }
     if ($duedate) {
-        $needsconfirmation{INVALID_DATE} = $duedate->output('syspref')
-          unless $duedate->output('iso') ge C4::Dates->today('iso');
+        my $today = $now->clone();
+        $today->truncate( to => 'minutes');
+        if (DateTime->compare($duedate,$today) == -1 ) { # duedate cannot be before now
+            $needsconfirmation{INVALID_DATE} = output_pref($duedate);
+        }
     } else {
-        $issuingimpossible{INVALID_DATE} = $duedate->output('syspref');
+            $issuingimpossible{INVALID_DATE} = output_pref($duedate);
     }
 
     #
@@ -726,13 +734,25 @@ sub CanBookBeIssued {
     if ( $borrower->{flags}->{'DBARRED'} ) {
         $issuingimpossible{DEBARRED} = 1;
     }
-    if ( $borrower->{'dateexpiry'} eq '0000-00-00') {
+    if ( !defined $borrower->{dateexpiry} || $borrower->{'dateexpiry'} eq '0000-00-00') {
         $issuingimpossible{EXPIRED} = 1;
     } else {
-        my @expirydate=  split /-/,$borrower->{'dateexpiry'};
-        if($expirydate[0]==0 || $expirydate[1]==0|| $expirydate[2]==0 ||
-            Date_to_Days(Today) > Date_to_Days( @expirydate )) {
-            $issuingimpossible{EXPIRED} = 1;                                   
+        my ($y, $m, $d) =  split /-/,$borrower->{'dateexpiry'};
+        if ($y && $m && $d) { # are we really writing oinvalid dates to borrs
+            my $expiry_dt = DateTime->new(
+                year => $y,
+                month => $m,
+                day   => $d,
+                time_zone => C4::Context->tz,
+            );
+            $expiry_dt->truncate( to => 'days');
+            my $today = $now->clone()->truncate(to => 'days');
+            if (DateTime->compare($today, $expiry_dt) == 1) {
+                $issuingimpossible{EXPIRED} = 1;
+            }
+        } else {
+            carp("Invalid expity date in borr");
+            $issuingimpossible{EXPIRED} = 1;
         }
     }
     #
@@ -741,7 +761,7 @@ sub CanBookBeIssued {
 
     # DEBTS
     my ($amount) =
-      C4::Members::GetMemberAccountRecords( $borrower->{'borrowernumber'}, '' && $duedate->output('iso') );
+      C4::Members::GetMemberAccountRecords( $borrower->{'borrowernumber'}, '' && $duedate->ymd() );
     my $amountlimit = C4::Context->preference("noissuescharge");
     my $allowfineoverride = C4::Context->preference("AllowFineOverride");
     my $allfinesneedoverride = C4::Context->preference("AllFinesNeedOverride");
@@ -958,13 +978,20 @@ sub AddIssue {
     my ( $borrower, $barcode, $datedue, $cancelreserve, $issuedate, $sipmode) = @_;
     my $dbh = C4::Context->dbh;
        my $barcodecheck=CheckValidBarcode($barcode);
+    if ($datedue && ref $datedue ne 'DateTime') {
+        $datedue = dt_from_string($datedue);
+    }
     # $issuedate defaults to today.
     if ( ! defined $issuedate ) {
-        $issuedate = strftime( "%Y-%m-%d", localtime );
-        # TODO: for hourly circ, this will need to be a C4::Dates object
-        # and all calls to AddIssue including issuedate will need to pass a Dates object.
+        $issuedate = DateTime->now(time_zone => C4::Context->tz());
     }
-       if ($borrower and $barcode and $barcodecheck ne '0'){
+    else {
+        if ( ref $issuedate ne 'DateTime') {
+            $issuedate = dt_from_string($issuedate);
+
+        }
+    }
+       if ($borrower and $barcode and $barcodecheck ne '0'){#??? wtf
                # find which item we issue
                my $item = GetItem('', $barcode) or return undef;       # if we don't get an Item, abort.
                my $branch = _GetCircControlBranch($item,$borrower);
@@ -1024,17 +1051,17 @@ sub AddIssue {
           );
         unless ($datedue) {
             my $itype = ( C4::Context->preference('item-level_itypes') ) ? $biblio->{'itype'} : $biblio->{'itemtype'};
-            $datedue = CalcDateDue( C4::Dates->new( $issuedate, 'iso' ), $itype, $branch, $borrower );
+            $datedue = CalcDateDue( $issuedate, $itype, $branch, $borrower );
 
         }
+        $datedue->truncate( to => 'minutes');
         $sth->execute(
             $borrower->{'borrowernumber'},      # borrowernumber
             $item->{'itemnumber'},              # itemnumber
-            $issuedate,                         # issuedate
-            $datedue->output('iso'),            # date_due
+            $issuedate->strftime('%Y-%m-%d %H:%M:00'), # issuedate
+            $datedue->strftime('%Y-%m-%d %H:%M:00'),   # date_due
             C4::Context->userenv->{'branch'}    # branchcode
         );
-        $sth->finish;
         if ( C4::Context->preference('ReturnToShelvingCart') ) { ## ReturnToShelvingCart is on, anything issued should be taken off the cart.
           CartToShelf( $item->{'itemnumber'} );
         }
@@ -1049,7 +1076,7 @@ sub AddIssue {
                   holdingbranch    => C4::Context->userenv->{'branch'},
                   itemlost         => 0,
                   datelastborrowed => C4::Dates->new()->output('iso'),
-                  onloan           => $datedue->output('iso'),
+                  onloan           => $datedue->ymd(),
                 }, $item->{'biblionumber'}, $item->{'itemnumber'});
         ModDateLastSeen( $item->{'itemnumber'} );
 
@@ -1111,53 +1138,57 @@ sub GetLoanLength {
     my $dbh = C4::Context->dbh;
     my $sth =
       $dbh->prepare(
-"select issuelength from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null"
+'select issuelength, lengthunit from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null'
       );
 # warn "in get loan lenght $borrowertype $itemtype $branchcode ";
 # try to find issuelength & return the 1st available.
 # check with borrowertype, itemtype and branchcode, then without one of those parameters
     $sth->execute( $borrowertype, $itemtype, $branchcode );
     my $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+    return $loanlength
+      if defined($loanlength) && $loanlength->{issuelength};
 
-    $sth->execute( $borrowertype, "*", $branchcode );
+    $sth->execute( $borrowertype, '*', $branchcode );
     $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+    return $loanlength
+      if defined($loanlength) && $loanlength->{issuelength};
 
-    $sth->execute( "*", $itemtype, $branchcode );
+    $sth->execute( '*', $itemtype, $branchcode );
     $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+    return $loanlength
+      if defined($loanlength) && $loanlength->{issuelength};
 
-    $sth->execute( "*", "*", $branchcode );
+    $sth->execute( '*', '*', $branchcode );
     $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+    return $loanlength
+      if defined($loanlength) && $loanlength->{issuelength};
 
-    $sth->execute( $borrowertype, $itemtype, "*" );
+    $sth->execute( $borrowertype, $itemtype, '*' );
     $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+    return $loanlength
+      if defined($loanlength) && $loanlength->{issuelength};
 
-    $sth->execute( $borrowertype, "*", "*" );
+    $sth->execute( $borrowertype, '*', '*' );
     $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+    return $loanlength
+      if defined($loanlength) && $loanlength->{issuelength};
 
-    $sth->execute( "*", $itemtype, "*" );
+    $sth->execute( '*', $itemtype, '*' );
     $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+    return $loanlength
+      if defined($loanlength) && $loanlength->{issuelength};
 
-    $sth->execute( "*", "*", "*" );
+    $sth->execute( '*', '*', '*' );
     $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
+    return $loanlength
+      if defined($loanlength) && $loanlength->{issuelength};
 
     # if no rule is set => 21 days (hardcoded)
-    return 21;
+    return {
+        issuelength => 21,
+        lengthunit => 'days',
+    };
+
 }
 
 
@@ -1178,43 +1209,43 @@ sub GetHardDueDate {
       );
     $sth->execute( $borrowertype, $itemtype, $branchcode );
     my $results = $sth->fetchrow_hashref;
-    return (C4::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
-      if defined($results) && $results->{hardduedate} ne 'NULL';
+    return (dt_from_string($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
+      if defined($results) && $results->{hardduedate};
 
     $sth->execute( $borrowertype, "*", $branchcode );
     $results = $sth->fetchrow_hashref;
-    return (C4::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
-      if defined($results) && $results->{hardduedate} ne 'NULL';
+    return (dt_from_string($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
+      if defined($results) && $results->{hardduedate};
 
     $sth->execute( "*", $itemtype, $branchcode );
     $results = $sth->fetchrow_hashref;
-    return (C4::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
-      if defined($results) && $results->{hardduedate} ne 'NULL';
+    return (dt_from_string($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
+      if defined($results) && $results->{hardduedate};
 
     $sth->execute( "*", "*", $branchcode );
     $results = $sth->fetchrow_hashref;
-    return (C4::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
-      if defined($results) && $results->{hardduedate} ne 'NULL';
+    return (dt_from_string($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
+      if defined($results) && $results->{hardduedate};
 
     $sth->execute( $borrowertype, $itemtype, "*" );
     $results = $sth->fetchrow_hashref;
-    return (C4::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
-      if defined($results) && $results->{hardduedate} ne 'NULL';
+    return (dt_from_string($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
+      if defined($results) && $results->{hardduedate};
 
     $sth->execute( $borrowertype, "*", "*" );
     $results = $sth->fetchrow_hashref;
-    return (C4::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
-      if defined($results) && $results->{hardduedate} ne 'NULL';
+    return (dt_from_string($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
+      if defined($results) && $results->{hardduedate};
 
     $sth->execute( "*", $itemtype, "*" );
     $results = $sth->fetchrow_hashref;
-    return (C4::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
-      if defined($results) && $results->{hardduedate} ne 'NULL';
+    return (dt_from_string($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
+      if defined($results) && $results->{hardduedate};
 
     $sth->execute( "*", "*", "*" );
     $results = $sth->fetchrow_hashref;
-    return (C4::Dates->new($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
-      if defined($results) && $results->{hardduedate} ne 'NULL';
+    return (dt_from_string($results->{hardduedate}, 'iso'),$results->{hardduedatecompare})
+      if defined($results) && $results->{hardduedate};
 
     # if no rule is set => return undefined
     return (undef, undef);
@@ -2759,50 +2790,85 @@ C<$borrower> = Borrower object
 
 =cut
 
-sub CalcDateDue { 
-       my ($startdate,$itemtype,$branch,$borrower) = @_;
-       my $datedue;
-        my $loanlength = GetLoanLength($borrower->{'categorycode'},$itemtype, $branch);
+sub CalcDateDue {
+    my ( $startdate, $itemtype, $branch, $borrower ) = @_;
 
-       # if globalDueDate ON the datedue is set to that date
-       if ( C4::Context->preference('globalDueDate')
-             && ( C4::Context->preference('globalDueDate') =~ C4::Dates->regexp('syspref') ) ) {
-            $datedue = C4::Dates->new( C4::Context->preference('globalDueDate') );
-       } else {
-       # otherwise, calculate the datedue as normal
-               if(C4::Context->preference('useDaysMode') eq 'Days') {  # ignoring calendar
-                       my $timedue = time + ($loanlength) * 86400;
-               #FIXME - assumes now even though we take a startdate 
-                       my @datearr  = localtime($timedue);
-                       $datedue = C4::Dates->new( sprintf("%04d-%02d-%02d", 1900 + $datearr[5], $datearr[4] + 1, $datearr[3]), 'iso');
-               } else {
-                       my $calendar = C4::Calendar->new(  branchcode => $branch );
-                       $datedue = $calendar->addDate($startdate, $loanlength);
-               }
-       }
+    # loanlength now a href
+    my $loanlength =
+      GetLoanLength( $borrower->{'categorycode'}, $itemtype, $branch );
+
+    my $datedue;
+
+    # if globalDueDate ON the datedue is set to that date
+    if (C4::Context->preference('globalDueDate')
+        && ( C4::Context->preference('globalDueDate') =~
+            C4::Dates->regexp('syspref') )
+      ) {
+        $datedue = dt_from_string(
+            C4::Context->preference('globalDueDate'),
+            C4::Context->preference('dateformat')
+        );
+    } else {
 
-       # if Hard Due Dates are used, retreive them and apply as necessary
-        my ($hardduedate, $hardduedatecompare) = GetHardDueDate($borrower->{'categorycode'},$itemtype, $branch);
-       if ( $hardduedate && $hardduedate->output('iso') && $hardduedate->output('iso') ne '0000-00-00') {
-            # if the calculated due date is after the 'before' Hard Due Date (ceiling), override
-            if ( $datedue->output( 'iso' ) gt $hardduedate->output( 'iso' ) && $hardduedatecompare == -1) {
-                $datedue = $hardduedate;
-            # if the calculated date is before the 'after' Hard Due Date (floor), override
-            } elsif ( $datedue->output( 'iso' ) lt $hardduedate->output( 'iso' ) && $hardduedatecompare == 1) {
-                $datedue = $hardduedate;               
-            # if the hard due date is set to 'exactly', overrride
-            } elsif ( $hardduedatecompare == 0) {
-                $datedue = $hardduedate;
+        # otherwise, calculate the datedue as normal
+        if ( C4::Context->preference('useDaysMode') eq 'Days' )
+        {    # ignoring calendar
+            my $dt =
+              DateTime->now( time_zone => C4::Context->tz() )
+              ->truncate( to => 'minute' );
+            if ( $loanlength->{lengthunit} eq 'hours' ) {
+                $dt->add( hours => $loanlength->{issuelength} );
+                return $dt;
+            } else {    # days
+                $dt->add( days => $loanlength->{issuelength} );
+                $dt->set_hour(23);
+                $dt->set_minute(59);
+                return $dt;
             }
-            # in all other cases, keep the date due as it is
-       }
+        } else {
+            my $dur;
+            if ($loanlength->{lengthunit} eq 'hours') {
+                $dur = DateTime::Duration->new( hours => $loanlength->{issuelength});
+            }
+            else { # days
+                $dur = DateTime::Duration->new( days => $loanlength->{issuelength});
+            }
+            if (ref $startdate ne 'DateTime' ) {
+                $startdate = dt_from_string($startdate);
+            }
+            my $calendar = Koha::Calendar->new( branchcode => $branch );
+            $datedue = $calendar->addDate( $startdate, $dur, $loanlength->{lengthunit} );
+        }
+    }
 
-       # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
-       if ( C4::Context->preference('ReturnBeforeExpiry') && $datedue->output('iso') gt $borrower->{dateexpiry} ) {
-           $datedue = C4::Dates->new( $borrower->{dateexpiry}, 'iso' );
-       }
+    # if Hard Due Dates are used, retreive them and apply as necessary
+    my ( $hardduedate, $hardduedatecompare ) =
+      GetHardDueDate( $borrower->{'categorycode'}, $itemtype, $branch );
+    if ($hardduedate) {    # hardduedates are currently dates
+        $hardduedate->truncate( to => 'minute' );
+        $hardduedate->set_hour(23);
+        $hardduedate->set_minute(59);
+        my $cmp = DateTime->compare( $hardduedate, $datedue );
+
+# if the calculated due date is after the 'before' Hard Due Date (ceiling), override
+# if the calculated date is before the 'after' Hard Due Date (floor), override
+# if the hard due date is set to 'exactly', overrride
+        if ( $hardduedatecompare == 0 || $hardduedatecompare == $cmp ) {
+            $datedue = $hardduedate->clone;
+        }
 
-       return $datedue;
+        # in all other cases, keep the date due as it is
+    }
+
+    # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate
+    if ( C4::Context->preference('ReturnBeforeExpiry') ) {
+        my $expiry_dt = dt_from_string( $borrower->{dateexpiry}, 'iso' );
+        if ( DateTime->compare( $datedue, $expiry_dt ) == 1 ) {
+            $datedue = $expiry_dt->clone;
+        }
+    }
+
+    return $datedue;
 }
 
 =head2 CheckValidDatedue
diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm
new file mode 100644 (file)
index 0000000..b901326
--- /dev/null
@@ -0,0 +1,229 @@
+package Koha::Calendar;
+use strict;
+use warnings;
+use 5.010;
+
+use DateTime;
+use C4::Context;
+use Carp;
+use Readonly;
+
+sub new {
+    my ( $classname, %options ) = @_;
+    my $self = {};
+    for my $o_name ( keys %options ) {
+        my $o = lc $o_name;
+        $self->{$o} = $options{$o_name};
+    }
+    if ( !defined $self->{branchcode} ) {
+        croak 'No branchcode argument passed to Koha::Calendar->new';
+    }
+    bless $self, $classname;
+    $self->_init();
+    return $self;
+}
+
+sub _init {
+    my $self       = shift;
+    my $branch     = $self->{branchcode};
+    my $dbh        = C4::Context->dbh();
+    my $repeat_sth = $dbh->prepare(
+'SELECT * from repeatable_holidays WHERE branchcode = ? AND ISNULL(weekday) = ?'
+    );
+    $repeat_sth->execute( $branch, 0 );
+    $self->{weekly_closed_days} = [];
+    Readonly::Scalar my $sunday => 7;
+    while ( my $tuple = $repeat_sth->fetchrow_hashref ) {
+        my $day = $tuple->{weekday} == 0 ? $sunday : $tuple->{weekday};
+        push @{ $self->{weekly_closed_days} }, $day;
+    }
+    $repeat_sth->execute( $branch, 1 );
+    $self->{day_month_closed_days} = [];
+    while ( my $tuple = $repeat_sth->fetchrow_hashref ) {
+        push @{ $self->{day_month_closed_days} },
+          { day => $tuple->{day}, month => $tuple->{month}, };
+    }
+    my $special = $dbh->prepare(
+'SELECT day, month, year, title, description FROM special_holidays WHERE ( branchcode = ? ) AND (isexception = ?)'
+    );
+    $special->execute( $branch, 1 );
+    my $dates = [];
+    while ( my ( $day, $month, $year, $title, $description ) =
+        $special->fetchrow ) {
+        push @{$dates},
+          DateTime->new(
+            day       => $day,
+            month     => $month,
+            year      => $year,
+            time_zone => C4::Context->tz()
+          )->truncate( to => 'day' );
+    }
+    $self->{exception_holidays} =
+      DateTime::Set->from_datetimes( dates => $dates );
+    $special->execute( $branch, 1 );
+    $dates = [];
+    while ( my ( $day, $month, $year, $title, $description ) =
+        $special->fetchrow ) {
+        push @{$dates},
+          DateTime->new(
+            day       => $day,
+            month     => $month,
+            year      => $year,
+            time_zone => C4::Context->tz()
+          )->truncate( to => 'day' );
+    }
+    $self->{single_holidays} = DateTime::Set->from_datetimes( dates => $dates );
+    return;
+}
+
+sub addDate {
+    my ( $self, $base_date, $add_duration, $unit ) = @_;
+    my $days_mode = C4::Context->preference('useDaysMode');
+    Readonly::Scalar my $return_by_hour => 10;
+    if ( $days_mode eq 'Datedue' ) {
+
+        my $dt = $base_date + $add_duration;
+        while ( $self->is_holiday($dt) ) {
+
+            # TODOP if hours set to 10 am
+            $dt->add( days => 1 );
+            if ( $unit eq 'hours' ) {
+                $dt->set_hour($return_by_hour);    # Staffs specific
+            }
+        }
+        return $dt;
+    } elsif ( $days_mode eq 'Calendar' ) {
+        my $days = $add_duration->in_units('days');
+        while ($days) {
+            $base_date->add( days => 1 );
+            if ( $self->is_holiday($base_date) ) {
+                next;
+            } else {
+                --$days;
+            }
+        }
+        if ( $unit eq 'hours' ) {
+            my $dt = $base_date->clone()->subtract( days => 1 );
+            if ( $self->is_holiday($dt) ) {
+                $base_date->set_hour($return_by_hour);    # Staffs specific
+            }
+        }
+        return $base_date;
+    } else {    # Days
+        return $base_date + $add_duration;
+    }
+}
+
+sub is_holiday {
+    my ( $self, $dt ) = @_;
+    my $dow = $dt->day_of_week;
+    my @matches = grep { $_ == $dow } @{ $self->{weekly_closed_days} };
+    if (@matches) {
+        return 1;
+    }
+    $dt->truncate('days');
+    my $day   = $dt->day;
+    my $month = $dt->month;
+    for my $dm ( @{ $self->{day_month_closed_days} } ) {
+        if ( $month == $dm->{month} && $day == $dm->{day} ) {
+            return 1;
+        }
+    }
+    if ( $self->{exception_holidays}->contains($dt) ) {
+        return 1;
+    }
+    if ( $self->{single_holidays}->contains($dt) ) {
+        return 1;
+    }
+
+    # damn have to go to work after all
+    return 0;
+}
+
+1;
+__END__
+
+=head1 NAME
+
+Koha::Calendar - Object containing a branches calendar
+
+=head1 VERSION
+
+This documentation refers to Koha::Calendar version 0.0.1
+
+=head1 SYNOPSIS
+
+  use Koha::Calendat
+
+  my $c = Koha::Calender->new( branchcode => 'MAIN' );
+  my $dt = DateTime->now();
+
+  # are we open
+  $open = $c->is_holiday($dt);
+  # when will item be due if loan period = $dur (a DateTime::Duration object)
+  $duedate = $c->addDate($dt,$dur,'days');
+
+
+=head1 DESCRIPTION
+
+  Implements those features of C4::Calendar needed for Staffs Rolling Loans
+
+=head1 METHODS
+
+=head2 new : Create a calendar object
+
+my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
+
+The option branchcode is required
+
+
+=head2 addDate
+
+    my $dt = $calendar->addDate($date, $dur, $unit)
+
+C<$date> is a DateTime object representing the starting date of the interval.
+
+C<$offset> is a DateTime::Duration to add to it
+
+C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
+
+Currently unit is only used to invoke Staffs return Monday at 10 am rule this
+parameter will be removed when issuingrules properly cope with that
+
+
+=head2 is_holiday
+
+$yesno = $calendar->is_holiday($dt);
+
+passed at DateTime object returns 1 if it is a closed day
+0 if not according to the calendar
+
+=head1 DIAGNOSTICS
+
+Will croak if not passed a branchcode in new
+
+=head1 BUGS AND LIMITATIONS
+
+This only contains a limited subset of the functionality in C4::Calendar
+Only enough to support Staffs Rolling loans
+
+=head1 AUTHOR
+
+Colin Campbell colin.campbell@ptfs-europe.com
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright (c) 2011 PTFS-Europe Ltd All rights reserved
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
index 99dac96..94ea731 100644 (file)
@@ -26,7 +26,7 @@ use C4::Context;
 use base 'Exporter';
 use version; our $VERSION = qv('1.0.0');
 
-our @EXPORT = (qw( dt_from_string ));
+our @EXPORT = (qw( dt_from_string output_pref));
 
 =head1 DateUtils
 
@@ -69,6 +69,9 @@ sub dt_from_string {
         } else {
             if ( $date_format eq 'iso' ) {
                 $date_string =~ s/-00/-01/;
+                if ( $date_string =~ m/^0000-00/ ) {
+                    return;               # invalid date in db
+                }
             } elsif ( $date_format eq 'us' ) {
                 $date_string =~ s[-00-][-01-];
             } elsif ( $date_format eq 'sql' ) {
@@ -82,4 +85,26 @@ s/(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})/$1-$2-$3T$4:$5:$6/;
     return DateTime->now( time_zone => $tz );
 
 }
+
+sub output_pref {
+    my $dt   = shift;
+    my $pref = C4::Context->preference('dateformat');
+    given ($pref) {
+        when (/^iso/) {
+            return $dt->strftime('%Y-%m-%d $H:%M');
+        }
+        when (/^metric/) {
+            return $dt->strftime('%d/%m/%Y $H:%M');
+        }
+        when (/^us/) {
+            return $dt->strftime('%m/%d/%Y $H:%M');
+        }
+        default {
+            return $dt->strftime('%Y-%m-%d $H:%M');
+        }
+
+    }
+    return;
+}
+
 1;
index 6928966..11b40a4 100755 (executable)
@@ -21,7 +21,7 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 use strict;
-#use warnings; FIXME - Bug 2505
+use warnings;
 use CGI;
 use C4::Output;
 use C4::Auth qw/:DEFAULT get_session/;
@@ -145,13 +145,7 @@ if($duedatespec_allow){
     if ($duedatespec) {
         if ($duedatespec =~ C4::Dates->regexp('syspref')) {
             my $tempdate = C4::Dates->new($duedatespec);
-#           if ($tempdate and $tempdate->output('iso') gt C4::Dates->new()->output('iso')) {
-#               # i.e., it has to be later than today/now
                 $datedue = $tempdate;
-#           } else {
-#               $invalidduedate = 1;
-#               $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);
-#           }
         } else {
             $invalidduedate = 1;
             $template->param(IMPOSSIBLE=>1, INVALID_DATE=>$duedatespec);