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>.
23 use Koha::Script -cron;
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 );
30 use Date::Calc qw( check_date Date_to_Days );
31 use Getopt::Long qw( GetOptions );
32 use Pod::Usage qw( pod2usage );
36 serialsUpdate.pl - change status of serial issues that are late
40 serialsUpdate.pl [ -h | -m ][ -v ] -c ][ --note "you are late" ][ --no-note ]
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
53 my $dbh = C4::Context->dbh;
62 my $command_line_options = join(" ",@ARGV);
67 'v|verbose' => \$verbose,
70 'no-note' => \$nonote,
73 pod2usage(1) if $help;
74 pod2usage( -verbose => 2 ) if $man;
76 cronlogaction({ info => $command_line_options });
78 $verbose and !$confirm and print "### Database will not be modified ###\n";
80 if ( $note && $nonote ) {
83 if ( !$note && !$nonote ) {
84 $note = 'Automatically set to late';
86 $verbose and print $note ? "Note : $note\n" : "No note\n";
88 # select all serials with not "irregular" periodicity that are late
89 my $sth = $dbh->prepare(
96 subscription.subscriptionid
98 LEFT JOIN subscription
99 ON (subscription.subscriptionid=serial.subscriptionid)
100 LEFT JOIN subscription_frequencies
101 ON (subscription.periodicity = subscription_frequencies.id)
102 WHERE serial.status = 1
103 AND subscription_frequencies.unit IS NOT NULL
104 AND DATE_ADD(planneddate, INTERVAL CAST(graceperiod AS SIGNED) DAY) < NOW()
105 AND subscription.closed = 0
110 while ( my $issue = $sth->fetchrow_hashref ) {
112 my $subscription = &GetSubscription( $issue->{subscriptionid} );
113 my $publisheddate = $issue->{publisheddate};
115 if ( $subscription && $publisheddate ) {
116 my $freqdata = GetSubscriptionFrequency($subscription->{'periodicity'});
117 my $nextpublisheddate = GetNextDate( $subscription, $publisheddate, $freqdata );
118 my $today = dt_from_string->ymd;
120 if ( $nextpublisheddate && $today ) {
121 my ( $year, $month, $day ) = split( /-/, $nextpublisheddate );
122 my ( $tyear, $tmonth, $tday ) = split( /-/, $today );
123 if ( check_date( $year, $month, $day )
124 && check_date( $tyear, $tmonth, $tday )
125 && Date_to_Days( $year, $month, $day ) <
126 Date_to_Days( $tyear, $tmonth, $tday ) )
129 and ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
130 $issue->{planneddate}, $issue->{publisheddate}, $issue->{publisheddatetext},
133 and print "Serial issue with id=" . $issue->{serialid} . " updated\n";
138 and print "Error with serial("
140 . ") has no existent subscription("
141 . $issue->{subscriptionid}
142 . ") attached or planneddate is wrong\n";
147 cronlogaction({ action => 'End', info => "COMPLETED" });