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 output_pref );
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;
65 'v|verbose' => \$verbose,
68 'no-note' => \$nonote,
71 pod2usage(1) if $help;
72 pod2usage( -verbose => 2 ) if $man;
76 $verbose and !$confirm and print "### Database will not be modified ###\n";
78 if ( $note && $nonote ) {
81 if ( !$note && !$nonote ) {
82 $note = 'Automatically set to late';
84 $verbose and print $note ? "Note : $note\n" : "No note\n";
86 # select all serials with not "irregular" periodicity that are late
87 my $sth = $dbh->prepare(
94 subscription.subscriptionid
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
108 while ( my $issue = $sth->fetchrow_hashref ) {
110 my $subscription = &GetSubscription( $issue->{subscriptionid} );
111 my $publisheddate = $issue->{publisheddate};
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 });
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 ) )
127 and ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
128 $issue->{planneddate}, $issue->{publisheddate}, $issue->{publisheddatetext},
131 and print "Serial issue with id=" . $issue->{serialid} . " updated\n";
136 and print "Error with serial("
138 . ") has no existent subscription("
139 . $issue->{subscriptionid}
140 . ") attached or planneddate is wrong\n";