|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2013 ByWater Solutions |
| 4 |
|
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 2 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along with |
| 17 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
| 18 |
# Suite 330, Boston, MA 02111-1307 USA |
| 19 |
# |
| 20 |
|
| 21 |
use Modern::Perl; |
| 22 |
|
| 23 |
use DBI; |
| 24 |
use Getopt::Long; |
| 25 |
use Pod::Usage; |
| 26 |
use DateTime::Format::MySQL; |
| 27 |
|
| 28 |
use C4::Context; |
| 29 |
use C4::Items qw( GetItem ); |
| 30 |
use C4::Members qw( GetMember ); |
| 31 |
use C4::Circulation; # qw( CalcDateDue ); |
| 32 |
|
| 33 |
my $verbose = 0; |
| 34 |
my $help = 0; |
| 35 |
my $man = 0; |
| 36 |
my $commit = 0; |
| 37 |
my $due_in = 0; |
| 38 |
|
| 39 |
GetOptions( |
| 40 |
'v|verbose' => \$verbose, |
| 41 |
'h|help' => \$help, |
| 42 |
'm|man' => \$man, |
| 43 |
'c|commit' => \$commit, |
| 44 |
'd|due-in=s' => \$due_in, |
| 45 |
) or pod2usage(2); |
| 46 |
|
| 47 |
pod2usage(1) if ($help); |
| 48 |
pod2usage( -verbose => 2 ) if $man; |
| 49 |
|
| 50 |
my $dbh = C4::Context->dbh; |
| 51 |
my $sql = 'SELECT * FROM issues WHERE DATE( date_due ) >= DATE_ADD( CURRENT_DATE(), INTERVAL ? DAY )'; |
| 52 |
my $sth = $dbh->prepare($sql); |
| 53 |
$sth->execute($due_in); |
| 54 |
|
| 55 |
my $count_total = 0; |
| 56 |
my $count_updated = 0; |
| 57 |
while ( my $issue = $sth->fetchrow_hashref() ) { |
| 58 |
|
| 59 |
my $item = GetItem( $issue->{'itemnumber'} ); |
| 60 |
my $borrower = GetMember( 'borrowernumber' => $issue->{'borrowernumber'} ); |
| 61 |
|
| 62 |
my $date_due = DateTime::Format::MySQL->format_datetime( C4::Circulation::CalcDateDue( $issue->{'issuedate'}, $item->{'itype'}, $issue->{'branchcode'}, $borrower ) ); |
| 63 |
|
| 64 |
unless ( $issue->{'date_due'} eq $date_due ) { |
| 65 |
print "\n"; |
| 66 |
print "DUE DATE ALTERED\n"; |
| 67 |
print "ITEM NUMBER: $item->{'itemnumber'}\n"; |
| 68 |
print "BORROWER: $borrower->{'borrowernumber'}\n"; |
| 69 |
print "ITEM TYPE: $item->{'itype'}\n"; |
| 70 |
print "BORROWER CATEGORY: $borrower->{'categorycode'}\n"; |
| 71 |
print "ISSUE DATE: $issue->{'issuedate'}\n"; |
| 72 |
print "OLD DATE DUE: $issue->{'date_due'}\n"; |
| 73 |
print "NEW DATE DUE: $date_due\n"; |
| 74 |
|
| 75 |
if ($commit) { |
| 76 |
$dbh->do( "UPDATE issues SET date_due = ? WHERE borrowernumber = ? AND itemnumber = ?", {}, ( $date_due, $borrower->{'borrowernumber'}, $item->{'itemnumber'} ) ); |
| 77 |
print "*** NEW DATE DUE COMMITED TO DATABASE ***\n"; |
| 78 |
} else { |
| 79 |
print "*** TEST MODE, DATE DUE NOT UPDATED IN DATABASE ***\n"; |
| 80 |
} |
| 81 |
|
| 82 |
$count_updated++; |
| 83 |
} |
| 84 |
|
| 85 |
$count_total++; |
| 86 |
} |
| 87 |
|
| 88 |
print "\nUPDATED $count_updated OF $count_total ISSUES\n"; |
| 89 |
|
| 90 |
=head1 NAME |
| 91 |
|
| 92 |
recalculate_due_dates.pl - recalculate and update due dates for issued items based on current circ rules |
| 93 |
|
| 94 |
=head1 SYNOPSIS |
| 95 |
|
| 96 |
recalculate_due_dates.pl |
| 97 |
|
| 98 |
Options: |
| 99 |
-c --commit calculate and update due dates, runs in test mode if not supplied |
| 100 |
|
| 101 |
-d --due-in integer, select only issues that are due in this many dates or more |
| 102 |
default is all issues |
| 103 |
|
| 104 |
-h --help brief help message |
| 105 |
-m --man full documentation |
| 106 |
|
| 107 |
=head1 OPTIONS |
| 108 |
|
| 109 |
=over 8 |
| 110 |
|
| 111 |
=item B<--help> |
| 112 |
|
| 113 |
Print a brief help message and exits. |
| 114 |
|
| 115 |
=item B<--man> |
| 116 |
|
| 117 |
Prints the manual page and exits. |
| 118 |
|
| 119 |
=item B<--commit> |
| 120 |
|
| 121 |
Calculates the new due dates and writes them to the database. |
| 122 |
|
| 123 |
=item B<--due-in> |
| 124 |
|
| 125 |
Select only issues that are due in this many dates or more. |
| 126 |
If not supplied, the script will update all current issues. |
| 127 |
|
| 128 |
For example, recalculate_due_dates.pl -c -d 1 will recalculate |
| 129 |
all issues that are due tomorrow or later. |
| 130 |
|
| 131 |
=back |
| 132 |
|
| 133 |
=head1 DESCRIPTION |
| 134 |
|
| 135 |
This script can be used to retroactively change the due dates |
| 136 |
on currently issued items based on the current issuing rules. |
| 137 |
|
| 138 |
CAVEAT: This script will overwrite any manually set due dates |
| 139 |
with the due date that Koha would have automatically calculated |
| 140 |
manually. |
| 141 |
|
| 142 |
=cut |
| 143 |
|
| 144 |
1; |
| 145 |
__END__ |
| 146 |
|