Bug 32336: (QA follow-up) Use $metadata->schema
[srvgit] / Koha / Calendar.pm
index 322f34b..5ffebb4 100644 (file)
@@ -2,7 +2,7 @@ package Koha::Calendar;
 
 use Modern::Perl;
 
-use Carp;
+use Carp qw( croak );
 use DateTime;
 use DateTime::Duration;
 use C4::Context;
@@ -50,106 +50,51 @@ sub _init {
     return;
 }
 
-sub exception_holidays {
-    my ( $self ) = @_;
+sub _holidays {
+    my ($self) = @_;
 
-    my $cache              = Koha::Caches->get_instance();
-    my $exception_holidays = $cache->get_from_cache('exception_holidays');
+    my $key      = $self->{branchcode} . "_holidays";
+    my $cache    = Koha::Caches->get_instance();
+    my $holidays = $cache->get_from_cache($key);
 
-    # Populate the cache is necessary
-    unless ($exception_holidays) {
+    # $holidays looks like:
+    # {
+    #    20131122 => 1, # single_holiday
+    #    20131123 => 0, # exception_holiday
+    #    ...
+    # }
+
+    # Populate the cache if necessary
+    unless ($holidays) {
         my $dbh = C4::Context->dbh;
-        $exception_holidays = {};
+        $holidays = {};
 
-        # Push holidays for each branch
-        my $exception_holidays_sth = $dbh->prepare(
-'SELECT day, month, year, branchcode FROM special_holidays WHERE isexception = 1'
+        # Add holidays for each branch
+        my $holidays_sth = $dbh->prepare(
+'SELECT day, month, year, MAX(isexception) FROM special_holidays WHERE branchcode = ? GROUP BY day, month, year'
         );
-        $exception_holidays_sth->execute();
-        my $dates = [];
-        while ( my ( $day, $month, $year, $branch ) =
-            $exception_holidays_sth->fetchrow )
+        $holidays_sth->execute($self->{branchcode});
+
+        while ( my ( $day, $month, $year, $exception ) =
+            $holidays_sth->fetchrow )
         {
             my $datestring =
                 sprintf( "%04d", $year )
               . sprintf( "%02d", $month )
               . sprintf( "%02d", $day );
 
-            $exception_holidays->{$branch}->{$datestring} = 1;
+            $holidays->{$datestring} = $exception ? 0 : 1;
         }
-        $cache->set_in_cache( 'exception_holidays', $exception_holidays,
-            { expiry => 76800 } );
+        $cache->set_in_cache( $key, $holidays, { expiry => 76800 } );
     }
-
-    return $exception_holidays->{$self->{branchcode}} // {};
-}
-
-sub is_exception_holiday {
-    my ( $self, $date ) = @_;
-
-    return 1 if ( $self->exception_holidays->{$date} );
-    return 0;
-}
-
-sub single_holidays {
-    my ( $self, $date ) = @_;
-    my $branchcode = $self->{branchcode};
-    my $cache           = Koha::Caches->get_instance();
-    my $single_holidays = $cache->get_from_cache('single_holidays');
-
-    # $single_holidays looks like:
-    # {
-    #   CPL =>  [
-    #        [0] 20131122,
-    #         ...
-    #    ],
-    #   ...
-    # }
-
-    unless ($single_holidays) {
-        my $dbh = C4::Context->dbh;
-        $single_holidays = {};
-
-        # push holidays for each branch
-        my $branches_sth =
-          $dbh->prepare('SELECT distinct(branchcode) FROM special_holidays');
-        $branches_sth->execute();
-        while ( my $br = $branches_sth->fetchrow ) {
-            my $single_holidays_sth = $dbh->prepare(
-'SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0'
-            );
-            $single_holidays_sth->execute($br);
-
-            my @ymd_arr;
-            while ( my ( $day, $month, $year ) =
-                $single_holidays_sth->fetchrow )
-            {
-                my $dt = DateTime->new(
-                    day       => $day,
-                    month     => $month,
-                    year      => $year,
-                    time_zone => 'floating',
-                )->truncate( to => 'day' );
-                push @ymd_arr, $dt->ymd('');
-            }
-            $single_holidays->{$br} = \@ymd_arr;
-        }    # br
-        $cache->set_in_cache( 'single_holidays', $single_holidays,
-            { expiry => 76800 } )    #24 hrs ;
-    }
-
-    my $holidays  = ( $single_holidays->{$branchcode} );
-    for my $hols  (@$holidays ) {
-            return 1 if ( $date == $hols )   #match ymds;
-    }
-    return 0;
+    return $holidays // {};
 }
 
-sub addDate {
+sub addDuration {
     my ( $self, $startdate, $add_duration, $unit ) = @_;
 
 
-    Koha::Exceptions::MissingParameter->throw("Missing mandatory option for Koha:Calendar->addDate: days_mode")
+    Koha::Exceptions::MissingParameter->throw("Missing mandatory option for Koha:Calendar->addDuration: days_mode")
         unless exists $self->{days_mode};
 
     # Default to days duration (legacy support I guess)
@@ -279,17 +224,13 @@ sub is_holiday {
     my $localdt = $dt->clone();
     my $day   = $localdt->day;
     my $month = $localdt->month;
-    my $ymd   = $localdt->ymd('')  ;
+    my $ymd   = $localdt->ymd('');
 
     #Change timezone to "floating" before doing any calculations or comparisons
     $localdt->set_time_zone("floating");
     $localdt->truncate( to => 'day' );
 
-
-    if ( $self->is_exception_holiday( $ymd ) == 1 ) {
-        # exceptions are not holidays
-        return 0;
-    }
+    return $self->_holidays->{$ymd} if defined($self->_holidays->{$ymd});
 
     my $dow = $localdt->day_of_week;
     # Representation fix
@@ -307,10 +248,6 @@ sub is_holiday {
         return 1;
     }
 
-    if ($self->single_holidays(  $ymd  ) == 1 ) {
-        return 1;
-    }
-
     # damn have to go to work after all
     return 0;
 }
@@ -455,7 +392,7 @@ Koha::Calendar - Object containing a branches calendar
   # 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');
+  $duedate = $c->addDuration($dt,$dur,'days');
 
 
 =head1 DESCRIPTION
@@ -471,9 +408,9 @@ my $calendar = Koha::Calendar->new( branchcode => 'MAIN' );
 The option branchcode is required
 
 
-=head2 addDate
+=head2 addDuration
 
-    my $dt = $calendar->addDate($date, $dur, $unit)
+    my $dt = $calendar->addDuration($date, $dur, $unit)
 
 C<$date> is a DateTime object representing the starting date of the interval.
 
@@ -517,25 +454,6 @@ 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 single_holidays
-
-my $rc = $self->single_holidays(  $ymd  );
-
-Passed a $date in Ymd (yyyymmdd) format -  returns 1 if date is a single_holiday, or 0 if not.
-
-=head2 exception_holidays
-
-my $exceptions = $self->exception_holidays;
-
-Returns a hashref of exception holidays for the branch
-
-=head2 is_exception_holiday
-
-my $rc = $self->is_exception_holiday( $ymd );
-
-Passed a $date in Ymd (yyyymmdd) format - returns 1 if the date is an exception_holiday, or 0 if not.
-
 =head2 is_holiday
 
 $yesno = $calendar->is_holiday($dt);
@@ -581,6 +499,15 @@ representing the previous open day after subtracting the number of passed
 days. It is intended for use to calculate the due date when useDaysMode
 syspref is set to either 'Datedue', 'Calendar' or 'Dayweek'.
 
+=head2 days_forward
+
+$datetime = $calendar->days_forward($start_dt, $to_add)
+
+Passed a Datetime and number of days, returns another Datetime representing
+the next open day after adding the passed number of days. It is intended for
+use to calculate the due date when useDaysMode syspref is set to either
+'Datedue', 'Calendar' or 'Dayweek'.
+
 =head2 set_daysmode
 
 For testing only allows the calling script to change days mode