Bug 31203: Alter other cronjobs that currenlty use cronlogaction
[koha.git] / misc / cronjobs / serialsUpdate.pl
1 #!/usr/bin/perl
2
3 # Copyright 2008 SARL Biblibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22
23 use Koha::Script -cron;
24 use C4::Context;
25 use C4::Serials qw( GetSubscription GetNextDate ModSerialStatus );
26 use C4::Serials::Frequency;
27 use C4::Log qw( cronlogaction );
28 use Koha::DateUtils qw( dt_from_string );
29
30 use Date::Calc qw( check_date Date_to_Days );
31 use Getopt::Long qw( GetOptions );
32 use Pod::Usage qw( pod2usage );
33
34 =head1 NAME
35
36 serialsUpdate.pl - change status of serial issues that are late
37
38 =head1 SYNOPSIS
39
40 serialsUpdate.pl [ -h | -m ][ -v ] -c ][ --note "you are late" ][ --no-note ]
41
42  Options:
43    --h --help -?   Brief help message
44    --man           Full documentation
45    --verbose -v    Verbose mode
46    -c              Confirm: without this option, the script will report on affected serial
47                    issues without modifying database
48    --note          Note set on affected serial issues, by default "Automatically set to late"
49    --no-note       Do not set a note on affected serial issues
50
51 =cut
52
53 my $dbh = C4::Context->dbh;
54
55 my $man     = 0;
56 my $help    = 0;
57 my $confirm = 0;
58 my $verbose = 0;
59 my $note    = '';
60 my $nonote  = 0;
61
62 my $command_line_options = join(" ",@ARGV);
63
64 GetOptions(
65     'help|h|?'  => \$help,
66     'man'       => \$man,
67     'v|verbose' => \$verbose,
68     'c'         => \$confirm,
69     'note:s'    => \$note,
70     'no-note'   => \$nonote,
71 ) or pod2usage(2);
72
73 pod2usage(1) if $help;
74 pod2usage( -verbose => 2 ) if $man;
75
76 cronlogaction({ info => $command_line_options });
77
78 $verbose and !$confirm and print "### Database will not be modified ###\n";
79
80 if ( $note && $nonote ) {
81     $note = '';
82 }
83 if ( !$note && !$nonote ) {
84     $note = 'Automatically set to late';
85 }
86 $verbose and print $note ? "Note : $note\n" : "No note\n";
87
88 # select all serials with not "irregular" periodicity that are late
89 my $sth = $dbh->prepare(
90     q{
91      SELECT
92        serial.serialid,
93        serial.serialseq,
94        serial.planneddate,
95        serial.publisheddate,
96        subscription.subscriptionid
97      FROM serial 
98      LEFT JOIN subscription 
99        ON (subscription.subscriptionid=serial.subscriptionid) 
100      LEFT JOIN subscription_frequencies
101        ON (subscription.periodicity = subscription_frequencies.id)
102      WHERE serial.status = 1 
103        AND subscription_frequencies.unit IS NOT NULL
104        AND DATE_ADD(planneddate, INTERVAL CAST(graceperiod AS SIGNED) DAY) < NOW()
105        AND subscription.closed = 0
106     }
107 );
108 $sth->execute();
109
110 while ( my $issue = $sth->fetchrow_hashref ) {
111
112     my $subscription = &GetSubscription( $issue->{subscriptionid} );
113     my $publisheddate  = $issue->{publisheddate};
114
115     if ( $subscription && $publisheddate ) {
116         my $freqdata = GetSubscriptionFrequency($subscription->{'periodicity'});
117         my $nextpublisheddate = GetNextDate( $subscription, $publisheddate, $freqdata );
118         my $today = dt_from_string->ymd;
119
120         if ( $nextpublisheddate && $today ) {
121             my ( $year,  $month,  $day )  = split( /-/, $nextpublisheddate );
122             my ( $tyear, $tmonth, $tday ) = split( /-/, $today );
123             if (   check_date( $year, $month, $day )
124                 && check_date( $tyear, $tmonth, $tday )
125                 && Date_to_Days( $year, $month, $day ) <
126                 Date_to_Days( $tyear, $tmonth, $tday ) )
127             {
128                 $confirm
129                   and ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
130                     $issue->{planneddate}, $issue->{publisheddate}, $issue->{publisheddatetext},
131                     3, $note );
132                 $verbose
133                   and print "Serial issue with id=" . $issue->{serialid} . " updated\n";
134             }
135         }
136         else {
137             $verbose
138               and print "Error with serial("
139               . $issue->{serialid}
140               . ") has no existent subscription("
141               . $issue->{subscriptionid}
142               . ") attached or planneddate is wrong\n";
143         }
144     }
145 }
146
147 cronlogaction({ action => 'End', info => "COMPLETED" });