From a5c65d8a90cdfc4d4147a7f82010396b69a0a8ca Mon Sep 17 00:00:00 2001 From: Matthias Meusburger Date: Mon, 22 Jul 2024 14:22:33 +0200 Subject: [PATCH] Bug 42051: serialsMod.pl, a command-line script to change serials status Usage: serialsMod.pl Changes the status of serials Options: --h --help -? Help message --verbose -v Verbose mode --confirm -c Confirm: without this option, the script will report on affected serial issues without modifying database --where -w Adds "WHERE" SQL condition --from_status -f Serial status (repeatable). See list below. --to_status -t Change issue to given status. --biblionumber -b Consider the given biblio --subscriptionid -s Consider the given subscription Status list: EXPECTED => 1, ARRIVED => 2, LATE => 3, MISSING => 4, MISSING_NEVER_RECIEVED => 41, MISSING_SOLD_OUT => 42, MISSING_DAMAGED => 43, MISSING_LOST => 44, NOT_ISSUED => 5, DELETED => 6, CLAIMED => 7, STOPPED => 8, Examples: perl misc/cronjobs/serialsMod.pl -v --from_status 2 --from_status 1 --to_status=6 --confirm perl misc/cronjobs/serialsMod.pl -v --where="planneddate >= NOW()" --to_status=3 --confirm --- misc/cronjobs/serialsMod.pl | 152 ++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 misc/cronjobs/serialsMod.pl diff --git a/misc/cronjobs/serialsMod.pl b/misc/cronjobs/serialsMod.pl new file mode 100644 index 00000000000..89251733d63 --- /dev/null +++ b/misc/cronjobs/serialsMod.pl @@ -0,0 +1,152 @@ +#!/usr/bin/perl + +# Copyright 2024 BibLibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use strict; +use warnings; + +use Koha::Script -cron; +use C4::Context; +use C4::Serials qw( ModSerialStatus ); +use C4::Log qw( cronlogaction ); +use Getopt::Long qw( GetOptions ); +use Pod::Usage qw( pod2usage ); + +=head1 NAME + +serialsMod.pl - change serial status + +=head1 SYNOPSIS + +serialsMod.pl + +Changes the status of serials + + Options: + --h --help -? Help message + --verbose -v Verbose mode + --confirm -c Confirm: without this option, the script will report on affected serial + issues without modifying database + --where -w Adds "WHERE" SQL condition + --from_status -f Serial status (repeatable). See list below. + --to_status -t Change issue to given status. + --biblionumber -b Consider the given biblio + --subscriptionid -s Consider the given subscription + + Status list: + + EXPECTED => 1, + ARRIVED => 2, + LATE => 3, + MISSING => 4, + MISSING_NEVER_RECIEVED => 41, + MISSING_SOLD_OUT => 42, + MISSING_DAMAGED => 43, + MISSING_LOST => 44, + NOT_ISSUED => 5, + DELETED => 6, + CLAIMED => 7, + STOPPED => 8, + +Examples: + +perl misc/cronjobs/serialsMod.pl -v --from_status 2 --from_status 1 --to_status=6 --confirm + +perl misc/cronjobs/serialsMod.pl -v --where="planneddate >= NOW()" --to_status=3 --confirm + +=cut + +my $dbh = C4::Context->dbh; + +my $help = 0; +my $confirm = 0; +my $verbose = 0; +my $where = ''; +my $biblionumber; +my $subscriptionid; +my @from_status; +my $to_status; + +my $command_line_options = join( " ", @ARGV ); + +GetOptions( + 'help|h|?' => \$help, + 'v|verbose' => \$verbose, + 'c|confirm' => \$confirm, + 'b|biblionumber=i' => \$biblionumber, + 's|subscriptionid=i' => \$subscriptionid, + 'w|where=s' => \$where, + 't|to_status=i', => \$to_status, + 'f|from_status=i', => \@from_status, +) or pod2usage(2); + +pod2usage(1) if $help; + +cronlogaction( { info => $command_line_options } ); + +$verbose and !$confirm and print "### Database will not be modified ###\n"; + +my $query = q{ + SELECT + serial.serialid, + serial.serialseq, + serial.subscriptionid, + serial.planneddate, + serial.publisheddate, + serial.notes, + serial.status + FROM serial + WHERE 1=1 + }; + +$query .= " AND $where" if ($where); +$query .= " AND biblionumber=$biblionumber" if ($biblionumber); +$query .= " AND subscriptionid=$subscriptionid" if ($subscriptionid); +$query .= " AND status IN(" . join( ',', @from_status ) . ")" if (@from_status); + +my $sth = $dbh->prepare($query); +$sth->execute(); + +while ( my $issue = $sth->fetchrow_hashref ) { + + if ($confirm) { + if ( $issue->{status} == $to_status ) { + $verbose and print "Serial issue with id=" . $issue->{serialid} . " has already status $to_status\n"; + } else { + ModSerialStatus( + $issue->{serialid}, $issue->{serialseq}, + $issue->{planneddate}, $issue->{publisheddate}, $issue->{publisheddatetext}, $to_status, $issue->{notes} + ); + $verbose + and print "Serial issue status with id=" + . $issue->{serialid} + . " went from " + . $issue->{status} + . " to $to_status\n"; + } + } else { + $verbose + and print "Serial issue status with id=" + . $issue->{serialid} + . " would have gone from " + . $issue->{status} + . " to $to_status\n"; + } +} + +cronlogaction( { action => 'End', info => "COMPLETED" } ); -- 2.39.5