Bug 20581: (follow-up) Fix dependency requirement
[koha.git] / misc / cronjobs / serialsUpdate.pl
bloba478476981cd48c680117ff2852f78c882805ba4
1 #!/usr/bin/perl
3 # Copyright 2008 SARL Biblibre
5 # This file is part of Koha.
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.
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.
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>.
20 use strict;
21 use warnings;
23 BEGIN {
25 # find Koha's Perl modules
26 # test carefully before changing this
27 use FindBin;
28 eval { require "$FindBin::Bin/../kohalib.pl" };
31 use C4::Context;
32 use C4::Debug;
33 use C4::Serials;
34 use C4::Log;
35 use Koha::DateUtils;
36 use C4::Serials::Frequency;
38 use Date::Calc qw/Date_to_Days check_date/;
39 use Getopt::Long;
40 use Pod::Usage;
42 =head1 NAME
44 serialsUpdate.pl - change status of serial issues that are late
46 =head1 SYNOPSIS
48 serialsUpdate.pl [ -h | -m ][ -v ] -c ][ --note "you are late" ][ --no-note ]
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
59 =cut
61 my $dbh = C4::Context->dbh;
63 my $man = 0;
64 my $help = 0;
65 my $confirm = 0;
66 my $verbose = 0;
67 my $note = '';
68 my $nonote = 0;
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);
79 pod2usage(1) if $help;
80 pod2usage( -verbose => 2 ) if $man;
82 cronlogaction();
84 $verbose and !$confirm and print "### Database will not be modified ###\n";
86 if ( $note && $nonote ) {
87 $note = '';
89 if ( !$note && !$nonote ) {
90 $note = 'Automatically set to late';
92 $verbose and print $note ? "Note : $note\n" : "No note\n";
94 # select all serials with not "irregular" periodicity that are late
95 my $sth = $dbh->prepare(
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
114 $sth->execute();
116 while ( my $issue = $sth->fetchrow_hashref ) {
118 my $subscription = &GetSubscription( $issue->{subscriptionid} );
119 my $publisheddate = $issue->{publisheddate};
121 if ( $subscription && $publisheddate && $publisheddate ne "0000-00-00" ) {
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 });
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 ) )
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";
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";