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 |
1; |
91 |
__END__ |
92 |
|
93 |
=head1 NAME |
94 |
|
95 |
recalculate_due_dates.pl - recalculate and update due dates for issued items based on current circ rules |
96 |
|
97 |
=head1 SYNOPSIS |
98 |
|
99 |
recalculate_due_dates.pl |
100 |
|
101 |
Options: |
102 |
-c --commit calculate and update due dates, runs in test mode if not supplied |
103 |
|
104 |
-d --due-in integer, select only issues that are due in this many dates or more |
105 |
default is all issues |
106 |
|
107 |
-h --help brief help message |
108 |
-m --man full documentation |
109 |
|
110 |
=head1 OPTIONS |
111 |
|
112 |
=over 8 |
113 |
|
114 |
=item B<-help> |
115 |
|
116 |
Print a brief help message and exits. |
117 |
|
118 |
=item B<-man> |
119 |
|
120 |
Prints the manual page and exits. |
121 |
|
122 |
=item B<-commit> |
123 |
|
124 |
Calculates the new due dates and writes them to the database. |
125 |
|
126 |
=item B<-due-in> |
127 |
|
128 |
Select only issues that are due in this many dates or more. |
129 |
If not supplied, the script will update all current issues. |
130 |
|
131 |
For example, recalculate_due_dates.pl -c -d 1 will recalculate |
132 |
all issues that are due tomorrow or later. |
133 |
|
134 |
=back |
135 |
|
136 |
=head1 DESCRIPTION |
137 |
|
138 |
This script can be used to retroactively change the due dates |
139 |
on currently issued items based on the current issuing rules. |
140 |
|
141 |
CAVEAT: This script will overwrite any manually set due dates |
142 |
with the due date that Koha would have automatically calculated |
143 |
manually. |
144 |
|
145 |
=head1 AUTHOR |
146 |
|
147 |
Kyle M Hall <kyle@bywatersolutions.com> |
148 |
|
149 |
=cut |
150 |
|
151 |
|