Bug 11401: QA followup
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 BEGIN {
24
25     # find Koha's Perl modules
26     # test carefully before changing this
27     use FindBin;
28     eval { require "$FindBin::Bin/../kohalib.pl" };
29 }
30
31 use C4::Context;
32 use C4::Dates qw/format_date format_date_in_iso/;
33 use C4::Debug;
34 use C4::Serials;
35
36 use Date::Calc qw/Date_to_Days check_date/;
37 use Getopt::Long;
38 use Pod::Usage;
39
40 =head1 NAME
41
42 serialsUpdate.pl - change status of serial issues that are late
43
44 =head1 SYNOPSIS
45
46 serialsUpdate.pl [ -h | -m ][ -v ] -c ][ --note "you are late" ][ --no-note ]
47
48  Options:
49    --h --help -?   Brief help message
50    --man           Full documentation
51    --verbose -v    Verbose mode
52    -c              Confirm: without this option, the script will report on affected serial
53                    issues without modifying database
54    --note          Note set on affected serial issues, by default "Automatically set to late"
55    --no-note       Do not set a note on affected serial issues
56
57 =cut
58
59 my $dbh = C4::Context->dbh;
60
61 my $man     = 0;
62 my $help    = 0;
63 my $confirm = 0;
64 my $verbose = 0;
65 my $note    = '';
66 my $nonote  = 0;
67
68 GetOptions(
69     'help|h|?'  => \$help,
70     'man'       => \$man,
71     'v|verbose' => \$verbose,
72     'c'         => \$confirm,
73     'note:s'    => \$note,
74     'no-note'   => \$nonote,
75 ) or pod2usage(2);
76
77 pod2usage(1) if $help;
78 pod2usage( -verbose => 2 ) if $man;
79
80 $verbose and !$confirm and print "### Database will not be modified ###\n";
81
82 if ( $note && $nonote ) {
83     $note = '';
84 }
85 if ( !$note && !$nonote ) {
86     $note = 'Automatically set to late';
87 }
88 $verbose and print $note ? "Note : $note\n" : "No note\n";
89
90 # select all serials with not "irregular" periodicity that are late
91 my $sth = $dbh->prepare(
92     q{
93      SELECT
94        serial.serialid,
95        serial.serialseq,
96        serial.planneddate,
97        serial.publisheddate,
98        subscription.subscriptionid
99      FROM serial 
100      LEFT JOIN subscription 
101        ON (subscription.subscriptionid=serial.subscriptionid) 
102      LEFT JOIN subscription_frequencies
103        ON (subscription.periodicity = subscription_frequencies.id)
104      WHERE serial.status = 1 
105        AND subscription_frequencies.unit IS NOT NULL
106        AND DATE_ADD(planneddate, INTERVAL CAST(graceperiod AS SIGNED) DAY) < NOW()
107        AND subscription.closed = 0
108     }
109 );
110 $sth->execute();
111
112 while ( my $issue = $sth->fetchrow_hashref ) {
113
114     my $subscription = &GetSubscription( $issue->{subscriptionid} );
115     my $publisheddate  = $issue->{publisheddate};
116
117     if ( $subscription && $publisheddate && $publisheddate ne "0000-00-00" ) {
118         my $nextpublisheddate = GetNextDate( $subscription, $publisheddate );
119         my $today = format_date_in_iso( C4::Dates->new()->output() );
120
121         if ( $nextpublisheddate && $today ) {
122             my ( $year,  $month,  $day )  = split( /-/, $nextpublisheddate );
123             my ( $tyear, $tmonth, $tday ) = split( /-/, $today );
124             if (   check_date( $year, $month, $day )
125                 && check_date( $tyear, $tmonth, $tday )
126                 && Date_to_Days( $year, $month, $day ) <
127                 Date_to_Days( $tyear, $tmonth, $tday ) )
128             {
129                 $confirm
130                   and ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
131                     $issue->{planneddate}, $issue->{publisheddate},
132                     3, $note );
133                 $verbose
134                   and print "Serial issue with id=" . $issue->{serialid} . " updated\n";
135             }
136         }
137         else {
138             $verbose
139               and print "Error with serial("
140               . $issue->{serialid}
141               . ") has no existent subscription("
142               . $issue->{subscriptionid}
143               . ") attached or planneddate is wrong\n";
144         }
145     }
146 }