From 0bb9781d9225d96f4b3e44adeed9d1d98f33734e Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 26 Feb 2013 11:48:57 -0500 Subject: [PATCH] Bug 9712 - Add command line script to recalculate existing due dates based on current issuing rules and retroactively update the issues This script can take existing issues and check to see of the due dates match up with the current issuing rules. If can optionally update the due dates based on the current issuing rules. Test Plan: 1) Set an issuing rule 2) Check out an item to a patron where that issuing rules applies 3) Modify the issuing rule's issue length 4) Run migration_tools/recalculate_due_dates.pl -t 5) You should see it list your issue has having a changed due date 6) run migration_tools/recalculate_due_dates.pl -c 7) You should see the due date updated in the database for this issue --- misc/migration_tools/recalculate_due_dates.pl | 147 +++++++++++++++++++++++++ 1 files changed, 147 insertions(+), 0 deletions(-) create mode 100755 misc/migration_tools/recalculate_due_dates.pl diff --git a/misc/migration_tools/recalculate_due_dates.pl b/misc/migration_tools/recalculate_due_dates.pl new file mode 100755 index 0000000..6a48208 --- /dev/null +++ b/misc/migration_tools/recalculate_due_dates.pl @@ -0,0 +1,147 @@ +#!/usr/bin/perl + +# Copyright 2013 ByWater Solutions + +# 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 2 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, write to the Free Software Foundation, Inc., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA +# + +use Modern::Perl; + +use DBI; +use Getopt::Long; +use Pod::Usage; +use DateTime::Format::MySQL; + +use C4::Context; +use C4::Items qw( GetItem ); +use C4::Members qw( GetMember ); +use C4::Circulation; # qw( CalcDateDue ); + +my $verbose = 0; +my $help = 0; +my $man = 0; +my $commit = 0; +my $due_in = 0; + +GetOptions( + 'v|verbose' => \$verbose, + 'h|help' => \$help, + 'm|man' => \$man, + 'c|commit' => \$commit, + 'd|due-in=s' => \$due_in, +) or pod2usage(2); + +pod2usage(1) if ($help); +pod2usage( -verbose => 2 ) if $man; + +my $dbh = C4::Context->dbh; +my $sql = 'SELECT * FROM issues WHERE DATE( date_due ) >= DATE_ADD( CURRENT_DATE(), INTERVAL ? DAY )'; +my $sth = $dbh->prepare($sql); +$sth->execute($due_in); + +my $count_total = 0; +my $count_updated = 0; +while ( my $issue = $sth->fetchrow_hashref() ) { + + my $item = GetItem( $issue->{'itemnumber'} ); + my $borrower = GetMember( 'borrowernumber' => $issue->{'borrowernumber'} ); + + my $date_due = DateTime::Format::MySQL->format_datetime( C4::Circulation::CalcDateDue( $issue->{'issuedate'}, $item->{'itype'}, $issue->{'branchcode'}, $borrower ) ); + + unless ( $issue->{'date_due'} eq $date_due ) { + print "\n"; + print "DUE DATE ALTERED\n"; + print "ITEM NUMBER: $item->{'itemnumber'}\n"; + print "BORROWER: $borrower->{'borrowernumber'}\n"; + print "ITEM TYPE: $item->{'itype'}\n"; + print "BORROWER CATEGORY: $borrower->{'categorycode'}\n"; + print "ISSUE DATE: $issue->{'issuedate'}\n"; + print "OLD DATE DUE: $issue->{'date_due'}\n"; + print "NEW DATE DUE: $date_due\n"; + + if ($commit) { + $dbh->do( "UPDATE issues SET date_due = ? WHERE borrowernumber = ? AND itemnumber = ?", {}, ( $date_due, $borrower->{'borrowernumber'}, $item->{'itemnumber'} ) ); + print "*** NEW DATE DUE COMMITED TO DATABASE ***\n"; + } else { + print "*** TEST MODE, DATE DUE NOT UPDATED IN DATABASE ***\n"; + } + + $count_updated++; + } + + $count_total++; +} + +print "\nUPDATED $count_updated OF $count_total ISSUES\n"; + +=head1 NAME + +recalculate_due_dates.pl - recalculate and update due dates for issued items based on current circ rules + +=head1 SYNOPSIS + +recalculate_due_dates.pl + + Options: + -c --commit calculate and update due dates + + -d --due-in select only issues that are due in this many dates or more + default is all issues + + -h --help brief help message + -m --man full documentation + +=head1 OPTIONS + +=over 8 + +=item B<--help> + +Print a brief help message and exits. + +=item B<--man> + +Prints the manual page and exits. + +=item B<--commit> + +Calculates the new due dates and writes them to the database. + +=item B<--due-in> + +Select only issues that are due in this many dates or more. +If not supplied, the script will update all current issues. + +=back + +=head1 DESCRIPTION + +This script can be used to retroactively change the due dates +on currently issued items based on the current issuing rules. + +CAVEAT: This script will overwrite any manually set due dates +with the due date that Koha would have automatically calculated +manually. + +=cut + +1; +__END__ + +## Please see file perltidy.ERR +## Please see file perltidy.ERR +## Please see file perltidy.ERR +## Please see file perltidy.ERR -- 1.7.2.5