Increment version for 3.12.6 release
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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::Dates qw/format_date format_date_in_iso/;
33 use C4::Debug;
34 use C4::Serials;
35
36 use Date::Calc qw/Date_to_Days check_date/;
37 use Getopt::Long;
38 use Pod::Usage;
39
40 my $dbh = C4::Context->dbh;
41
42 my $man     = 0;
43 my $help    = 0;
44 my $confirm = 0;
45
46 GetOptions(
47     'help|?' => \$help,
48     'c'      => \$confirm,
49 ) or pod2usage(2);
50
51 pod2usage(1) if $help;
52 pod2usage( -verbose => 2 ) if $man;
53
54 # select all serials with not "irregular" periodicity that are late
55 my $sth = $dbh->prepare("
56      SELECT *
57      FROM serial 
58      LEFT JOIN subscription 
59        ON (subscription.subscriptionid=serial.subscriptionid) 
60      WHERE serial.status = 1 
61        AND periodicity <> 32
62        AND DATE_ADD(planneddate, INTERVAL CAST(graceperiod AS SIGNED) DAY) < NOW()
63        AND subscription.closed = 0
64      ");
65 $sth->execute();
66
67 while ( my $issue = $sth->fetchrow_hashref ) {
68
69     my $subscription      = &GetSubscription( $issue->{subscriptionid} );
70     my $planneddate       = $issue->{planneddate};
71
72     if( $subscription && $planneddate && $planneddate ne "0000-00-00" ){
73         my $nextpublisheddate = GetNextDate( $planneddate, $subscription );
74         my $today             = format_date_in_iso( C4::Dates->new()->output() );
75
76         if ($nextpublisheddate && $today){
77             my ( $year,  $month,  $day )  = split( /-/, $nextpublisheddate );
78             my ( $tyear, $tmonth, $tday ) = split( /-/, $today );
79             if (   check_date( $year, $month, $day )
80                 && check_date( $tyear, $tmonth, $tday )
81                 && Date_to_Days( $year, $month, $day ) <
82                 Date_to_Days( $tyear, $tmonth, $tday ) )
83             {
84         
85             ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
86                 $issue->{planneddate}, $issue->{publisheddate},
87                 3, "Automatically set to late" );
88             print $issue->{serialid}." update\n";
89             }
90         }else{
91             print "Error with serial(".$issue->{serialid}.") has no existent 
92                    subscription(".$issue->{subscriptionid}.") attached
93                    or planneddate is ";
94         }
95     }
96 }