Bug 17600: Standardize our EXPORT_OK
[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 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 Koha::Script -cron;
32 use C4::Context;
33 use C4::Serials qw( GetSubscription GetNextDate ModSerialStatus );
34 use C4::Serials::Frequency;
35 use C4::Log qw( cronlogaction );
36 use Koha::DateUtils qw( dt_from_string output_pref );
37
38 use Date::Calc qw( check_date Date_to_Days );
39 use Getopt::Long qw( GetOptions );
40 use Pod::Usage qw( pod2usage );
41
42 =head1 NAME
43
44 serialsUpdate.pl - change status of serial issues that are late
45
46 =head1 SYNOPSIS
47
48 serialsUpdate.pl [ -h | -m ][ -v ] -c ][ --note "you are late" ][ --no-note ]
49
50  Options:
51    --h --help -?   Brief help message
52    --man           Full documentation
53    --verbose -v    Verbose mode
54    -c              Confirm: without this option, the script will report on affected serial
55                    issues without modifying database
56    --note          Note set on affected serial issues, by default "Automatically set to late"
57    --no-note       Do not set a note on affected serial issues
58
59 =cut
60
61 my $dbh = C4::Context->dbh;
62
63 my $man     = 0;
64 my $help    = 0;
65 my $confirm = 0;
66 my $verbose = 0;
67 my $note    = '';
68 my $nonote  = 0;
69
70 GetOptions(
71     'help|h|?'  => \$help,
72     'man'       => \$man,
73     'v|verbose' => \$verbose,
74     'c'         => \$confirm,
75     'note:s'    => \$note,
76     'no-note'   => \$nonote,
77 ) or pod2usage(2);
78
79 pod2usage(1) if $help;
80 pod2usage( -verbose => 2 ) if $man;
81
82 cronlogaction();
83
84 $verbose and !$confirm and print "### Database will not be modified ###\n";
85
86 if ( $note && $nonote ) {
87     $note = '';
88 }
89 if ( !$note && !$nonote ) {
90     $note = 'Automatically set to late';
91 }
92 $verbose and print $note ? "Note : $note\n" : "No note\n";
93
94 # select all serials with not "irregular" periodicity that are late
95 my $sth = $dbh->prepare(
96     q{
97      SELECT
98        serial.serialid,
99        serial.serialseq,
100        serial.planneddate,
101        serial.publisheddate,
102        subscription.subscriptionid
103      FROM serial 
104      LEFT JOIN subscription 
105        ON (subscription.subscriptionid=serial.subscriptionid) 
106      LEFT JOIN subscription_frequencies
107        ON (subscription.periodicity = subscription_frequencies.id)
108      WHERE serial.status = 1 
109        AND subscription_frequencies.unit IS NOT NULL
110        AND DATE_ADD(planneddate, INTERVAL CAST(graceperiod AS SIGNED) DAY) < NOW()
111        AND subscription.closed = 0
112     }
113 );
114 $sth->execute();
115
116 while ( my $issue = $sth->fetchrow_hashref ) {
117
118     my $subscription = &GetSubscription( $issue->{subscriptionid} );
119     my $publisheddate  = $issue->{publisheddate};
120
121     if ( $subscription && $publisheddate ) {
122         my $freqdata = GetSubscriptionFrequency($subscription->{'periodicity'});
123         my $nextpublisheddate = GetNextDate( $subscription, $publisheddate, $freqdata );
124         my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
125
126         if ( $nextpublisheddate && $today ) {
127             my ( $year,  $month,  $day )  = split( /-/, $nextpublisheddate );
128             my ( $tyear, $tmonth, $tday ) = split( /-/, $today );
129             if (   check_date( $year, $month, $day )
130                 && check_date( $tyear, $tmonth, $tday )
131                 && Date_to_Days( $year, $month, $day ) <
132                 Date_to_Days( $tyear, $tmonth, $tday ) )
133             {
134                 $confirm
135                   and ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
136                     $issue->{planneddate}, $issue->{publisheddate}, $issue->{publisheddatetext},
137                     3, $note );
138                 $verbose
139                   and print "Serial issue with id=" . $issue->{serialid} . " updated\n";
140             }
141         }
142         else {
143             $verbose
144               and print "Error with serial("
145               . $issue->{serialid}
146               . ") has no existent subscription("
147               . $issue->{subscriptionid}
148               . ") attached or planneddate is wrong\n";
149         }
150     }
151 }