diff --git a/C4/Overdues.pm b/C4/Overdues.pm index 71d74e76c5..79bae5ea24 100644 --- a/C4/Overdues.pm +++ b/C4/Overdues.pm @@ -37,8 +37,6 @@ BEGIN { ); } -use Date::Calc qw( Today ); -use Date::Manip qw( UnixDate ); use List::MoreUtils qw( uniq ); use POSIX qw( ceil floor ); use Locale::Currency::Format 1.28 qw( currency_format FMT_SYMBOL ); @@ -370,131 +368,6 @@ sub get_chargeable_units { } } -=head2 GetSpecialHolidays - - &GetSpecialHolidays($date_dues,$itemnumber); - -return number of special days between date of the day and date due - -C<$date_dues> is the envisaged date of book return. - -C<$itemnumber> is the book's item number. - -=cut - -sub GetSpecialHolidays { - my ( $date_dues, $itemnumber ) = @_; - - # calcul the today date - my $today = join "-", &Today(); - - # return the holdingbranch - my $iteminfo = GetIssuesIteminfo($itemnumber); - - # use sql request to find all date between date_due and today - my $dbh = C4::Context->dbh; - my $query = qq|SELECT DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') as date -FROM `special_holidays` -WHERE DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') >= ? -AND DATE_FORMAT(concat(year,'-',month,'-',day),'%Y-%m-%d') <= ? -AND branchcode=? -|; - my @result = GetWdayFromItemnumber($itemnumber); - my @result_date; - my $wday; - my $dateinsec; - my $sth = $dbh->prepare($query); - $sth->execute( $date_dues, $today, $iteminfo->{'branchcode'} ) - ; # FIXME: just use NOW() in SQL instead of passing in $today - - while ( my $special_date = $sth->fetchrow_hashref ) { - push( @result_date, $special_date ); - } - - my $specialdaycount = scalar(@result_date); - - for ( my $i = 0 ; $i < scalar(@result_date) ; $i++ ) { - $dateinsec = UnixDate( $result_date[$i]->{'date'}, "%o" ); - ( undef, undef, undef, undef, undef, undef, $wday, undef, undef ) = localtime($dateinsec); - for ( my $j = 0 ; $j < scalar(@result) ; $j++ ) { - if ( $wday == ( $result[$j]->{'weekday'} ) ) { - $specialdaycount--; - } - } - } - - return $specialdaycount; -} - -=head2 GetRepeatableHolidays - - &GetRepeatableHolidays($date_dues, $itemnumber, $difference,); - -return number of day closed between date of the day and date due - -C<$date_dues> is the envisaged date of book return. - -C<$itemnumber> is item number. - -C<$difference> numbers of between day date of the day and date due - -=cut - -sub GetRepeatableHolidays { - my ( $date_dues, $itemnumber, $difference ) = @_; - my $dateinsec = UnixDate( $date_dues, "%o" ); - my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime($dateinsec); - my @result = GetWdayFromItemnumber($itemnumber); - my @dayclosedcount; - my $j; - - for ( my $i = 0 ; $i < scalar(@result) ; $i++ ) { - my $k = $wday; - - for ( $j = 0 ; $j < $difference ; $j++ ) { - if ( $result[$i]->{'weekday'} == $k ) { - push( @dayclosedcount, $k ); - } - $k++; - ( $k = 0 ) if ( $k eq 7 ); - } - } - return scalar(@dayclosedcount); -} - -=head2 GetWayFromItemnumber - - &Getwdayfromitemnumber($itemnumber); - -return the different week day from repeatable_holidays table - -C<$itemnumber> is item number. - -=cut - -=head2 GetWdayFromItemnumber - -Missing POD for GetWdayFromItemnumber. - -=cut - -sub GetWdayFromItemnumber { - my ($itemnumber) = @_; - my $iteminfo = GetIssuesIteminfo($itemnumber); - my @result; - my $query = qq|SELECT weekday - FROM repeatable_holidays - WHERE branchcode=? -|; - my $sth = C4::Context->dbh->prepare($query); - - $sth->execute( $iteminfo->{'branchcode'} ); - while ( my $weekday = $sth->fetchrow_hashref ) { - push( @result, $weekday ); - } - return @result; -} - =head2 GetIssuesIteminfo &GetIssuesIteminfo($itemnumber); diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index f7002d3923..27131b24a8 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -5770,7 +5770,7 @@ CREATE TABLE `record_sources` ( /*!40101 SET character_set_client = @saved_cs_client */; -- --- Table structure for table `repeatable_holidays` +-- Table structure for table `library_repeating_closures` -- DROP TABLE IF EXISTS `library_repeating_closures`; diff --git a/misc/cronjobs/cleanup_database.pl b/misc/cronjobs/cleanup_database.pl index 4a227d3b22..9312d3d209 100755 --- a/misc/cronjobs/cleanup_database.pl +++ b/misc/cronjobs/cleanup_database.pl @@ -48,6 +48,8 @@ use Koha::BackgroundJobs; use Koha::Database; use Koha::DateUtils qw( dt_from_string ); use Koha::Item::Transfers; +use Koha::Library::Calendar::Exceptions; +use Koha::Library::Calendar::SingleClosures; use Koha::Old::Biblioitems; use Koha::Old::Biblios; use Koha::Old::Checkouts; @@ -1015,13 +1017,13 @@ sub PurgeCreatorBatches { sub DeleteSpecialHolidays { my ($days) = @_; - my $sth = $dbh->prepare( - q{ - DELETE FROM special_holidays - WHERE DATE( CONCAT( year, '-', month, '-', day ) ) < DATE_SUB( CAST(NOW() AS DATE), INTERVAL ? DAY ); - } - ); - my $count = $sth->execute($days) + 0; + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + my $cutoff = $dtf->format_date( dt_from_string->subtract( days => $days ) ); + + my $count = 0; + $count += Koha::Library::Calendar::SingleClosures->search( { date => { '<' => $cutoff } } )->delete + 0; + $count += Koha::Library::Calendar::Exceptions->search( { date => { '<' => $cutoff } } )->delete + 0; + print "Removed $count unique holidays\n" if $verbose; }