View | Details | Raw Unified | Return to bug 9712
Collapse All | Expand All

(-)a/misc/migration_tools/recalculate_due_dates.pl (-1 / +170 lines)
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 List::MoreUtils qw( any );
27
use DateTime::Format::MySQL;
28
29
use C4::Context;
30
use C4::Items qw( GetItem );
31
use C4::Members qw( GetMember );
32
use C4::Circulation;    # qw( CalcDateDue );
33
34
my $verbose = 0;
35
my $help    = 0;
36
my $man     = 0;
37
my $commit  = 0;
38
my $due_in  = 0;
39
my @exclude_categories;
40
my @exclude_itemtypes;
41
42
GetOptions(
43
    'v|verbose'          => \$verbose,
44
    'h|help'             => \$help,
45
    'm|man'              => \$man,
46
    'c|commit'           => \$commit,
47
    'd|due-in=s'         => \$due_in,
48
    'exclude-category=s' => \@exclude_categories,
49
    'exclude-itemtype=s' => \@exclude_itemtypes,
50
) or pod2usage(2);
51
52
pod2usage(1) if ($help);
53
pod2usage( -verbose => 2 ) if $man;
54
55
my $dbh = C4::Context->dbh;
56
my $sql = 'SELECT * FROM issues WHERE DATE( date_due ) >= DATE_ADD( CURRENT_DATE(), INTERVAL ? DAY )';
57
my $sth = $dbh->prepare($sql);
58
$sth->execute($due_in);
59
60
my $count_total   = 0;
61
my $count_updated = 0;
62
while ( my $issue = $sth->fetchrow_hashref() ) {
63
64
    my $borrower = GetMember( 'borrowernumber' => $issue->{'borrowernumber'} );
65
    next if any { ( $_ eq $borrower->{'categorycode'} ) } @exclude_categories;
66
67
    my $item = GetItem( $issue->{'itemnumber'} );
68
    next if any { ( $_ eq $item->{'itype'} ) } @exclude_itemtypes;
69
70
    my $date_due = DateTime::Format::MySQL->format_datetime( C4::Circulation::CalcDateDue( $issue->{'issuedate'}, $item->{'itype'}, $issue->{'branchcode'}, $borrower ) );
71
72
    unless ( $issue->{'date_due'} eq $date_due ) {
73
        print "\n";
74
        print "DUE DATE ALTERED\n";
75
        print "ITEM NUMBER:       $item->{'itemnumber'}\n";
76
        print "BORROWER:          $borrower->{'borrowernumber'}\n";
77
        print "ITEM TYPE:         $item->{'itype'}\n";
78
        print "BORROWER CATEGORY: $borrower->{'categorycode'}\n";
79
        print "ISSUE DATE:        $issue->{'issuedate'}\n";
80
        print "OLD DATE DUE:      $issue->{'date_due'}\n";
81
        print "NEW DATE DUE:      $date_due\n";
82
83
        if ($commit) {
84
            $dbh->do( "UPDATE issues SET date_due = ? WHERE borrowernumber = ? AND itemnumber = ?", {}, ( $date_due, $borrower->{'borrowernumber'}, $item->{'itemnumber'} ) );
85
            print "*** NEW DATE DUE COMMITED TO DATABASE ***\n";
86
        } else {
87
            print "*** TEST MODE, DATE DUE NOT UPDATED IN DATABASE ***\n";
88
        }
89
90
        $count_updated++;
91
    }
92
93
    $count_total++;
94
}
95
96
print "\nUPDATED $count_updated OF $count_total ISSUES\n";
97
98
1;
99
__END__
100
101
=head1 NAME
102
103
recalculate_due_dates.pl - recalculate and update due dates for issued items based on current circ rules
104
105
=head1 SYNOPSIS
106
107
recalculate_due_dates.pl
108
109
 Options:
110
   -c --commit                        calculate and update due dates, runs in test mode if not supplied
111
112
   -d --due-in                        integer, select only issues that are due in this many dates or more
113
                                      default is all issues
114
115
   --exclude-category                 ignore all items issued to patrons of this category, repeatable
116
   --exclude-itemtype                 ignore all items issued with this itemtype, repeatable
117
118
   -h --help                          brief help message
119
   -m --man                           full documentation
120
121
=head1 OPTIONS
122
123
=over 8
124
125
=item B<-help>
126
127
Print a brief help message and exits.
128
129
=item B<-man>
130
131
Prints the manual page and exits.
132
133
=item B<-commit>
134
135
Calculates the new due dates and writes them to the database.
136
137
=item B<-exclude-category>
138
139
Skip issues to patrons of the category code. Repeatable.
140
141
=item B<-exclude-itemtype>
142
143
Skip issues of items having this item type. Repeatable.
144
145
=item B<-due-in>
146
147
Select only issues that are due in this many dates or more.
148
If not supplied, the script will update all current issues.
149
150
For example, recalculate_due_dates.pl -c -d 1 will recalculate
151
all issues that are due tomorrow or later.
152
153
=back
154
155
=head1 DESCRIPTION
156
157
This script can be used to retroactively change the due dates
158
on currently issued items based on the current issuing rules.
159
160
CAVEAT: This script will overwrite any manually set due dates
161
with the due date that Koha would have automatically calculated
162
manually.
163
164
=head1 AUTHOR
165
166
Kyle M Hall <kyle@bywatersolutions.com>
167
168
=cut
169
170

Return to bug 9712