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