ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / misc / cronjobs / serialsUpdate.pl
index d82a0bb..a478476 100755 (executable)
@@ -4,18 +4,18 @@
 #
 # This file is part of Koha.
 #
-# Koha 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.
+# Koha 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 3 of the License, or
+# (at your option) any later version.
 #
-# Koha 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.
+# Koha 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 Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
@@ -29,9 +29,11 @@ BEGIN {
 }
 
 use C4::Context;
-use C4::Dates qw/format_date format_date_in_iso/;
 use C4::Debug;
 use C4::Serials;
+use C4::Log;
+use Koha::DateUtils;
+use C4::Serials::Frequency;
 
 use Date::Calc qw/Date_to_Days check_date/;
 use Getopt::Long;
@@ -39,7 +41,7 @@ use Pod::Usage;
 
 =head1 NAME
 
-serialsUpdate.pl - change status of serials that are late
+serialsUpdate.pl - change status of serial issues that are late
 
 =head1 SYNOPSIS
 
@@ -49,10 +51,10 @@ serialsUpdate.pl [ -h | -m ][ -v ] -c ][ --note "you are late" ][ --no-note ]
    --h --help -?   Brief help message
    --man           Full documentation
    --verbose -v    Verbose mode
-   -c              Confirm : without this option, the script will report the concerned serials
-                   without modifying database
-   --note          Note set to concerned serials, by default "Automatically set to late"
-   --no-note       Do not set a note one concerned serials
+   -c              Confirm: without this option, the script will report on affected serial
+                   issues without modifying database
+   --note          Note set on affected serial issues, by default "Automatically set to late"
+   --no-note       Do not set a note on affected serial issues
 
 =cut
 
@@ -77,6 +79,8 @@ GetOptions(
 pod2usage(1) if $help;
 pod2usage( -verbose => 2 ) if $man;
 
+cronlogaction();
+
 $verbose and !$confirm and print "### Database will not be modified ###\n";
 
 if ( $note && $nonote ) {
@@ -94,13 +98,15 @@ my $sth = $dbh->prepare(
        serial.serialid,
        serial.serialseq,
        serial.planneddate,
-       serial.publisheddate
+       serial.publisheddate,
        subscription.subscriptionid
      FROM serial 
      LEFT JOIN subscription 
        ON (subscription.subscriptionid=serial.subscriptionid) 
+     LEFT JOIN subscription_frequencies
+       ON (subscription.periodicity = subscription_frequencies.id)
      WHERE serial.status = 1 
-       AND periodicity <> 32
+       AND subscription_frequencies.unit IS NOT NULL
        AND DATE_ADD(planneddate, INTERVAL CAST(graceperiod AS SIGNED) DAY) < NOW()
        AND subscription.closed = 0
     }
@@ -110,11 +116,12 @@ $sth->execute();
 while ( my $issue = $sth->fetchrow_hashref ) {
 
     my $subscription = &GetSubscription( $issue->{subscriptionid} );
-    my $planneddate  = $issue->{planneddate};
+    my $publisheddate  = $issue->{publisheddate};
 
-    if ( $subscription && $planneddate && $planneddate ne "0000-00-00" ) {
-        my $nextpublisheddate = GetNextDate( $planneddate, $subscription );
-        my $today = format_date_in_iso( C4::Dates->new()->output() );
+    if ( $subscription && $publisheddate && $publisheddate ne "0000-00-00" ) {
+        my $freqdata = GetSubscriptionFrequency($subscription->{'periodicity'});
+        my $nextpublisheddate = GetNextDate( $subscription, $publisheddate, $freqdata );
+        my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
 
         if ( $nextpublisheddate && $today ) {
             my ( $year,  $month,  $day )  = split( /-/, $nextpublisheddate );
@@ -126,10 +133,10 @@ while ( my $issue = $sth->fetchrow_hashref ) {
             {
                 $confirm
                   and ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
-                    $issue->{planneddate}, $issue->{publisheddate},
+                    $issue->{planneddate}, $issue->{publisheddate}, $issue->{publisheddatetext},
                     3, $note );
                 $verbose
-                  and print "Serial id=" . $issue->{serialid} . " update\n";
+                  and print "Serial issue with id=" . $issue->{serialid} . " updated\n";
             }
         }
         else {