@@ -, +, @@ --- misc/migration_tools/recalculate_due_dates.pl | 146 +++++++++++++++++++++++++ 1 files changed, 146 insertions(+), 0 deletions(-) create mode 100755 misc/migration_tools/recalculate_due_dates.pl --- a/misc/migration_tools/recalculate_due_dates.pl +++ a/misc/migration_tools/recalculate_due_dates.pl @@ -0,0 +1,146 @@ +#!/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, runs in test mode if not supplied + + -d --due-in integer, 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. + +For example, recalculate_due_dates.pl -c -d 1 will recalculate +all issues that are due tomorrow or later. + +=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__ + --