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