Bug 28617: Remove kohalib.pl and rely on PERL5LIB
[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 output_pref );
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 GetOptions(
63     'help|h|?'  => \$help,
64     'man'       => \$man,
65     'v|verbose' => \$verbose,
66     'c'         => \$confirm,
67     'note:s'    => \$note,
68     'no-note'   => \$nonote,
69 ) or pod2usage(2);
70
71 pod2usage(1) if $help;
72 pod2usage( -verbose => 2 ) if $man;
73
74 cronlogaction();
75
76 $verbose and !$confirm and print "### Database will not be modified ###\n";
77
78 if ( $note && $nonote ) {
79     $note = '';
80 }
81 if ( !$note && !$nonote ) {
82     $note = 'Automatically set to late';
83 }
84 $verbose and print $note ? "Note : $note\n" : "No note\n";
85
86 # select all serials with not "irregular" periodicity that are late
87 my $sth = $dbh->prepare(
88     q{
89      SELECT
90        serial.serialid,
91        serial.serialseq,
92        serial.planneddate,
93        serial.publisheddate,
94        subscription.subscriptionid
95      FROM serial 
96      LEFT JOIN subscription 
97        ON (subscription.subscriptionid=serial.subscriptionid) 
98      LEFT JOIN subscription_frequencies
99        ON (subscription.periodicity = subscription_frequencies.id)
100      WHERE serial.status = 1 
101        AND subscription_frequencies.unit IS NOT NULL
102        AND DATE_ADD(planneddate, INTERVAL CAST(graceperiod AS SIGNED) DAY) < NOW()
103        AND subscription.closed = 0
104     }
105 );
106 $sth->execute();
107
108 while ( my $issue = $sth->fetchrow_hashref ) {
109
110     my $subscription = &GetSubscription( $issue->{subscriptionid} );
111     my $publisheddate  = $issue->{publisheddate};
112
113     if ( $subscription && $publisheddate ) {
114         my $freqdata = GetSubscriptionFrequency($subscription->{'periodicity'});
115         my $nextpublisheddate = GetNextDate( $subscription, $publisheddate, $freqdata );
116         my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
117
118         if ( $nextpublisheddate && $today ) {
119             my ( $year,  $month,  $day )  = split( /-/, $nextpublisheddate );
120             my ( $tyear, $tmonth, $tday ) = split( /-/, $today );
121             if (   check_date( $year, $month, $day )
122                 && check_date( $tyear, $tmonth, $tday )
123                 && Date_to_Days( $year, $month, $day ) <
124                 Date_to_Days( $tyear, $tmonth, $tday ) )
125             {
126                 $confirm
127                   and ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
128                     $issue->{planneddate}, $issue->{publisheddate}, $issue->{publisheddatetext},
129                     3, $note );
130                 $verbose
131                   and print "Serial issue with id=" . $issue->{serialid} . " updated\n";
132             }
133         }
134         else {
135             $verbose
136               and print "Error with serial("
137               . $issue->{serialid}
138               . ") has no existent subscription("
139               . $issue->{subscriptionid}
140               . ") attached or planneddate is wrong\n";
141         }
142     }
143 }