Bug 32336: (QA follow-up) Use $metadata->schema
[srvgit] / C4 / Calendar.pm
index 92eb6b3..257119c 100644 (file)
@@ -17,13 +17,13 @@ package C4::Calendar;
 
 use strict;
 use warnings;
-use vars qw($VERSION @EXPORT);
+use vars qw(@EXPORT);
 
-use Carp;
-use Date::Calc qw( Date_to_Days Today);
+use Carp qw( croak );
+use Date::Calc qw( Today );
 
 use C4::Context;
-use Koha::Cache;
+use Koha::Caches;
 
 use constant ISO_DATE_FORMAT => "%04d-%02d-%02d";
 
@@ -201,7 +201,7 @@ sub insert_week_day_holiday {
     croak "Invalid weekday $weekday" unless $weekday =~ m/^[0-6]$/;
 
     my $dbh = C4::Context->dbh();
-    my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )"); 
+    my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (branchcode,weekday,day,month,title,description) values ( ?,?,NULL,NULL,?,? )");
        $insertHoliday->execute( $self->{branchcode}, $weekday, $options{title}, $options{description});
     $self->{'week_days_holidays'}->{$weekday}{title} = $options{title};
     $self->{'week_days_holidays'}->{$weekday}{description} = $options{description};
@@ -232,7 +232,7 @@ sub insert_day_month_holiday {
     my %options = @_;
 
     my $dbh = C4::Context->dbh();
-    my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )");
+    my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (branchcode,weekday,day,month,title,description) values (?, NULL, ?, ?, ?,? )");
        $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description});
     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
@@ -269,15 +269,16 @@ sub insert_single_holiday {
 
        my $dbh = C4::Context->dbh();
     my $isexception = 0;
-    my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
+    my $insertHoliday = $dbh->prepare("insert into special_holidays (branchcode,day,month,year,isexception,title,description) values (?,?,?,?,?,?,?)");
        $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
 
 
     # changed the 'single_holidays' table, lets force/reset its cache
-    my $cache = Koha::Cache->get_instance();
-    $cache->clear_from_cache( 'single_holidays') ;
+    my $cache = Koha::Caches->get_instance();
+    my $key   = $self->{branchcode} . "_holidays";
+    $cache->clear_from_cache($key);
 
     return $self;
 
@@ -314,14 +315,15 @@ sub insert_exception_holiday {
 
     my $dbh = C4::Context->dbh();
     my $isexception = 1;
-    my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
+    my $insertException = $dbh->prepare("insert into special_holidays (branchcode,day,month,year,isexception,title,description) values (?,?,?,?,?,?,?)");
        $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
 
     # changed the 'single_holidays' table, lets force/reset its cache
-    my $cache = Koha::Cache->get_instance();
-    $cache->clear_from_cache( 'single_holidays') ;
+    my $cache = Koha::Caches->get_instance();
+    my $key   = $self->{branchcode} . "_holidays";
+    $cache->clear_from_cache($key);
 
     return $self;
 }
@@ -420,8 +422,9 @@ UPDATE special_holidays SET title = ?, description = ?
     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
 
     # changed the 'single_holidays' table, lets force/reset its cache
-    my $cache = Koha::Cache->get_instance();
-    $cache->clear_from_cache( 'single_holidays') ;
+    my $cache = Koha::Caches->get_instance();
+    my $key   = $self->{branchcode} . "_holidays";
+    $cache->clear_from_cache($key);
 
     return $self;
 }
@@ -462,8 +465,9 @@ UPDATE special_holidays SET title = ?, description = ?
     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
 
     # changed the 'single_holidays' table, lets force/reset its cache
-    my $cache = Koha::Cache->get_instance();
-    $cache->clear_from_cache( 'single_holidays') ;
+    my $cache = Koha::Caches->get_instance();
+    my $key   = $self->{branchcode} . "_holidays";
+    $cache->clear_from_cache($key);
 
     return $self;
 }
@@ -542,8 +546,9 @@ sub delete_holiday {
     }
 
     # changed the 'single_holidays' table, lets force/reset its cache
-    my $cache = Koha::Cache->get_instance();
-    $cache->clear_from_cache( 'single_holidays') ;
+    my $cache = Koha::Caches->get_instance();
+    my $key   = $self->{branchcode} . "_holidays";
+    $cache->clear_from_cache($key);
 
     return $self;
 }
@@ -572,8 +577,9 @@ sub delete_holiday_range {
     $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
 
     # changed the 'single_holidays' table, lets force/reset its cache
-    my $cache = Koha::Cache->get_instance();
-    $cache->clear_from_cache( 'single_holidays') ;
+    my $cache = Koha::Caches->get_instance();
+    my $key   = $self->{branchcode} . "_holidays";
+    $cache->clear_from_cache($key);
 
 }
 
@@ -625,8 +631,9 @@ sub delete_exception_holiday_range {
     $sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
 
     # changed the 'single_holidays' table, lets force/reset its cache
-    my $cache = Koha::Cache->get_instance();
-    $cache->clear_from_cache( 'single_holidays') ;
+    my $cache = Koha::Caches->get_instance();
+    my $key   = $self->{branchcode} . "_holidays";
+    $cache->clear_from_cache($key);
 }
 
 =head2 isHoliday
@@ -683,55 +690,38 @@ sub copy_to_branch {
     my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d;
 
     my $wdh = $self->get_week_days_holidays;
-    $target_calendar->insert_week_day_holiday( weekday => $_, %{ $wdh->{$_} } )
-      foreach keys %$wdh;
-    $target_calendar->insert_day_month_holiday(%$_)
-      foreach values %{ $self->get_day_month_holidays };
-    $target_calendar->insert_exception_holiday(%$_)
-      foreach grep { $_->{date} gt $today } values %{ $self->get_exception_holidays };
-    $target_calendar->insert_single_holiday(%$_)
-      foreach grep { $_->{date} gt $today } values %{ $self->get_single_holidays };
-
-    return 1;
-}
-
-=head2 addDate
-
-    my ($day, $month, $year) = $calendar->addDate($date, $offset)
-
-C<$startdate> is the starting date of the interval.
-
-C<$offset> Is the number of days that this function has to count from $date.
-
-=cut
+    my $target_wdh = $target_calendar->get_week_days_holidays;
+    foreach my $key (keys %$wdh) {
+        unless (grep { $_ eq $key } keys %$target_wdh) {
+            $target_calendar->insert_week_day_holiday( weekday => $key, %{ $wdh->{$key} } )
+        }
+    }
 
-sub addDate {
-    my ($self, $startdate, $offset) = @_;
-    $startdate = eval { output_pref( { dt => $startdate, dateonly => 1, dateformat => 'iso' } ); };
-    my ( $year, $month, $day) = split( "-", $startdate );
-       my $daystep = 1;
-       if ($offset < 0) { # In case $offset is negative
-       # $offset = $offset*(-1);
-               $daystep = -1;
+    my $dmh = $self->get_day_month_holidays;
+    my $target_dmh = $target_calendar->get_day_month_holidays;
+    foreach my $values (values %$dmh) {
+        unless (grep { $_->{day} eq $values->{day} && $_->{month} eq $values->{month} } values %$target_dmh) {
+            $target_calendar->insert_day_month_holiday(%{ $values });
+        }
     }
-       my $daysMode = C4::Context->preference('useDaysMode');
-    if ($daysMode eq 'Datedue') {
-        ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
-               while ($self->isHoliday($day, $month, $year)) {
-            ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
+
+    my $exception_holidays = $self->get_exception_holidays;
+    my $target_exceptions = $target_calendar->get_exception_holidays;
+    foreach my $values ( grep {$_->{date} gt $today} values %{ $exception_holidays }) {
+        unless ( grep { $_->{date} eq $values->{date} } values %$target_exceptions) {
+            $target_calendar->insert_exception_holiday(%{ $values });
         }
-    } elsif($daysMode eq 'Calendar') {
-        while ($offset !=  0) {
-            ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
-            if (!($self->isHoliday($day, $month, $year))) {
-                $offset = $offset - $daystep;
-                       }
+    }
+
+    my $single_holidays = $self->get_single_holidays;
+    my $target_singles = $target_calendar->get_single_holidays;
+    foreach my $values ( grep {$_->{date} gt $today} values %{ $single_holidays }) {
+        unless ( grep { $_->{date} eq $values->{date} } values %$target_singles){
+            $target_calendar->insert_single_holiday(%{ $values });
         }
-       } else { ## ($daysMode eq 'Days') 
-        ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
     }
-    my $date_ret = sprintf(ISO_DATE_FORMAT,$year,$month,$day);
-    return($date_ret);
+
+    return 1;
 }
 
 1;
@@ -743,3 +733,4 @@ __END__
 Koha Physics Library UNLP <matias_veleda@hotmail.com>
 
 =cut
+