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>.
25 # find Koha's Perl modules
26 # test carefully before changing this
28 eval { require "$FindBin::Bin/../kohalib.pl" };
31 use Koha::Script -cron;
33 use C4::Serials qw( GetSubscription GetNextDate ModSerialStatus );
34 use C4::Serials::Frequency;
35 use C4::Log qw( cronlogaction );
36 use Koha::DateUtils qw( dt_from_string output_pref );
38 use Date::Calc qw( check_date Date_to_Days );
39 use Getopt::Long qw( GetOptions );
40 use Pod::Usage qw( pod2usage );
44 serialsUpdate.pl - change status of serial issues that are late
48 serialsUpdate.pl [ -h | -m ][ -v ] -c ][ --note "you are late" ][ --no-note ]
51 --h --help -? Brief help message
52 --man Full documentation
53 --verbose -v Verbose mode
54 -c Confirm: without this option, the script will report on affected serial
55 issues without modifying database
56 --note Note set on affected serial issues, by default "Automatically set to late"
57 --no-note Do not set a note on affected serial issues
61 my $dbh = C4::Context->dbh;
73 'v|verbose' => \$verbose,
76 'no-note' => \$nonote,
79 pod2usage(1) if $help;
80 pod2usage( -verbose => 2 ) if $man;
84 $verbose and !$confirm and print "### Database will not be modified ###\n";
86 if ( $note && $nonote ) {
89 if ( !$note && !$nonote ) {
90 $note = 'Automatically set to late';
92 $verbose and print $note ? "Note : $note\n" : "No note\n";
94 # select all serials with not "irregular" periodicity that are late
95 my $sth = $dbh->prepare(
101 serial.publisheddate,
102 subscription.subscriptionid
104 LEFT JOIN subscription
105 ON (subscription.subscriptionid=serial.subscriptionid)
106 LEFT JOIN subscription_frequencies
107 ON (subscription.periodicity = subscription_frequencies.id)
108 WHERE serial.status = 1
109 AND subscription_frequencies.unit IS NOT NULL
110 AND DATE_ADD(planneddate, INTERVAL CAST(graceperiod AS SIGNED) DAY) < NOW()
111 AND subscription.closed = 0
116 while ( my $issue = $sth->fetchrow_hashref ) {
118 my $subscription = &GetSubscription( $issue->{subscriptionid} );
119 my $publisheddate = $issue->{publisheddate};
121 if ( $subscription && $publisheddate ) {
122 my $freqdata = GetSubscriptionFrequency($subscription->{'periodicity'});
123 my $nextpublisheddate = GetNextDate( $subscription, $publisheddate, $freqdata );
124 my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
126 if ( $nextpublisheddate && $today ) {
127 my ( $year, $month, $day ) = split( /-/, $nextpublisheddate );
128 my ( $tyear, $tmonth, $tday ) = split( /-/, $today );
129 if ( check_date( $year, $month, $day )
130 && check_date( $tyear, $tmonth, $tday )
131 && Date_to_Days( $year, $month, $day ) <
132 Date_to_Days( $tyear, $tmonth, $tday ) )
135 and ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
136 $issue->{planneddate}, $issue->{publisheddate}, $issue->{publisheddatetext},
139 and print "Serial issue with id=" . $issue->{serialid} . " updated\n";
144 and print "Error with serial("
146 . ") has no existent subscription("
147 . $issue->{subscriptionid}
148 . ") attached or planneddate is wrong\n";