Bug 17168: (follow-up) Clean up QA errors
[koha-ffzg.git] / misc / cronjobs / update_patrons_category.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 BEGIN {
21     # find Koha's Perl modules
22     # test carefully before changing this
23     use FindBin;
24     eval { require "$FindBin::Bin/../kohalib.pl" };
25 }
26
27 use C4::Context;
28 use Getopt::Long;
29 use Pod::Usage;
30 use Koha::Logger;
31 use Koha::Patrons;
32 use Koha::Patron::Categories;
33 use Koha::DateUtils;
34 use Data::Dumper;
35
36 =head1 NAME
37
38 update_patrons_category.pl - Given a set of parameters update selected patrons from one catgeory to another. Options are cumulative.
39
40 =head1 SYNOPSIS
41
42 update_patrons_category.pl -f=categorycode -t=categorycode
43                           [-b=branchcode] [-au] [-ao] [-fo=X] [-fu=X]
44                           [-rb=date] [-ra=date] [-v]
45                           [--field column=value ...]
46
47 update_patrons_category.pl --help | --man
48
49 Options:
50    --help                   brief help message
51    --man                    full documentation
52    -ao --ageover            update if over  maximum age for current category
53    -au --ageunder           update if under minimuum age  current category
54    -fo=X --fineover=X       update if fines over  X amount
55    -fu=X --fineunder=X      update if fines under X amount
56    -rb=date --regbefore     update if registration date is before given date
57    -ra=date --regafter      update if registration date is after a given date
58    -d --dbfield name=value    where <name> is a column in the borrowers table, patrons will be updated if the field is equal to given <value>
59    -v -verbose              verbose mode
60    -c --confirm             commit changes to db, no action will be taken unless this switch is included
61    -b --branch <branchname> only deal with patrons from this library/branch
62    -f --from <categorycode> change patron category from this category
63    -t --to   <categorycode> change patron category to this category
64
65 =head1 OPTIONS
66
67 =over 8
68
69 =item B<--help>
70
71 Print a brief help message and exits.
72
73 =item B<--man>
74
75 Prints the manual page and exits.
76
77 =item B<--verbosse | -v>
78
79 Verbose. Without this flag set, only fatal errors are reported.
80
81 =item B<--confirm | -c>
82
83 Commit changes. Unless this flag set is, the script will report changes but not actually execute them on the database.
84
85 =item B<--branch | -b>
86
87 changes patrons for one specific branch. Use the value in the
88 branches.branchcode table.
89
90 =item B<--from | -f>
91
92 *required* defines the category to update. Expects the code from categories.categorycode.
93
94 =item B<--to | -t>
95
96 *required* defines the category patrons will be converted to. Expects the code from categories.categorycode.
97
98 =item B<--ageover | -ao>
99
100 Update patron only if they are above the maximum age range specified for the 'from' category.
101
102 =item B<--ageunde | -au>
103
104 Update patron only if they are below the minimum age range specified for the 'from' category.
105
106 =item B<--fineover=X | -fo=X>
107
108 Supply a number and only account with fines over this number will be updated.
109
110 =item B<==fineunder=X | -fu=X>
111
112 Supply a number and only account with fines under this number will be updated.
113
114 =item B<--regbefore=date | -rb=date>
115
116 Enter a date in ISO format YYYY-MM-DD and only patrons registered before this date wil be updated.
117
118 =item B<--regafter=date | -ra=date>
119
120 Enter a date in ISO format YYYY-MM-DD and only patrons registered after this date wil be updated.
121
122 =item B<--field column=value | -d column=value>
123
124 Use this flag to specify a column in the borrowers table and update only patrons whose value in that column matches the value supplied (repeatable)
125
126 e.g.
127 --field dateexpiry=2016-01-01
128 will update all patrons who expired on that date, useful for schools etc.
129
130 =back
131
132 =head1 DESCRIPTION
133
134 This script is designed to update patrons from one category to another.
135
136 =head1 USAGE EXAMPLES
137
138 C<update_patron_categories.pl> - Suggests that you read this help. :)
139
140 C<update_patron_categories.pl> -b=<branchcode> -f=<categorycode> -t=<categorycode> --confirm  - Processes a single branch, and updates the patron categories from fromcat to tocat.
141
142 C<update_patron_categories.pl> -b=<branchcode> -f=<categorycode> -t=<categorycode>  -a --confirm  - Processes a single branch, and updates the patron categories from fromcat to tocat for patrons outside the age range of fromcat.
143
144 C<update_patron_categories.pl> -f=<categorycode> -t=<categorycode> -v  - Processes all branches, shows all messages, and reports the patrons who would be affected. Takes no action on the database.
145
146 =cut
147
148 # These variables are set by command line options.
149 # They are initially set to default values.
150
151 my $help    = 0;
152 my $man     = 0;
153 my $verbose = 0;
154 my $doit    = 0;
155 my $ageunder;
156 my $ageover;
157 my $remove_guarantors = 0;
158 my $fine_min;
159 my $fine_max;
160 my $fromcat;
161 my $tocat;
162 my $reg_bef;
163 my $reg_aft;
164 my $branch_lim;
165 my %fields;
166
167 GetOptions(
168     'help|?'          => \$help,
169     'man'             => \$man,
170     'v|verbose'       => \$verbose,
171     'c|confirm'       => \$doit,
172     'f|from=s'        => \$fromcat,
173     't|to=s'          => \$tocat,
174     'ao|ageover'      => \$ageover,
175     'au|ageunder'     => \$ageunder,
176     'fo|finesover=s'  => \$fine_min,
177     'fu|finesunder=s' => \$fine_max,
178     'rb|regbefore=s'  => \$reg_bef,
179     'ra|regafter=s'   => \$reg_aft,
180     'b|branch=s'      => \$branch_lim,
181     'd|field=s'       => \%fields
182 ) or pod2usage(2);
183 pod2usage(1) if $help;
184 pod2usage( -verbose => 2 ) if $man;
185
186 warn "v $verbose c $doit f $fromcat t $tocat";
187 exit;
188
189 if ( not $fromcat && $tocat ) {    #make sure we've specified the info we need.
190     print
191 "Must supply category from and to (-f & -t) please specify -help for usage tips.\n";
192     exit;
193 }
194
195 ( $verbose && !$doit ) and print "No actions will be taken (test mode)\n";
196
197 $verbose
198   and print
199 "Will update patrons from $fromcat to $tocat with conditions below (if any)\n";
200
201 my %params;
202
203 if ( $reg_bef || $reg_aft ) {
204     my $date_bef;
205     my $date_aft;
206     if ( defined $reg_bef ) {
207         eval { $date_bef = dt_from_string( $reg_bef, 'iso' ); };
208     }
209     die
210 "$reg_bef is not a valid date before, aborting! Use a date in format YYYY-MM-DD.$@"
211       if $@;
212     if ( defined $reg_aft ) {
213         eval { $date_aft = dt_from_string( $reg_aft, 'iso' ); };
214     }
215     die
216 "$reg_bef is not a valid date after, aborting! Use a date in format YYYY-MM-DD.$@"
217       if $@;
218     $params{dateenrolled}{'<='} = $reg_bef if defined $date_bef;
219     $params{dateenrolled}{'>='} = $reg_aft if defined $date_aft;
220 }
221
222 my $cat_from = Koha::Patron::Categories->find($fromcat);
223 my $cat_to   = Koha::Patron::Categories->find($tocat);
224 die "Categories not found" unless $cat_from && $cat_to;
225
226 $params{"me.categorycode"} = $fromcat;
227 $params{"me.branchcode"} = $branch_lim if $branch_lim;
228
229 if ($verbose) {
230     print "Conditions:\n";
231     print "    Registered before $reg_bef\n"      if $reg_bef;
232     print "    Registered after  $reg_aft\n"      if $reg_aft;
233     print "    Total fines more than $fine_min\n" if $fine_min;
234     print "    Total fines less than $fine_max\n" if $fine_max;
235     print "    Age below minimum for " . $cat_from->description . "\n" if $ageunder;
236     print "    Age above maximum for " . $cat_from->description . "\n" if $ageover;
237     if ( defined $branch_lim ) {
238         print "    Branchcode of patron is $branch_lim\n";
239     }
240 }
241
242 while ( my ( $key, $value ) = each %fields ) {
243     $verbose and print "    Borrower column $key is equal to $value\n";
244     $params{ "me." . $key } = $value;
245 }
246
247 my $target_patrons = Koha::Patrons->search_patrons_to_update(
248     {
249         from          => $fromcat,
250         search_params => \%params,
251         ageunder      => $ageunder,
252         ageover       => $ageover,
253         fine_min      => $fine_min,
254         fine_max      => $fine_max,
255     }
256 );
257 my $patrons_found    = $target_patrons->count;
258 my $actually_updated = 0;
259 my $testdisplay      = $doit ? "" : "WOULD HAVE ";
260 if ($verbose) {
261     while ( my $target_patron = $target_patrons->next() ) {
262         my $target = Koha::Patrons->find( $target_patron->borrowernumber );
263         $verbose
264           and print $testdisplay
265           . "Updated "
266           . $target->firstname . " "
267           . $target->surname
268           . " from $fromcat to $tocat\n";
269     }
270     $target_patrons->reset;
271 }
272 if ($doit) {
273     $actually_updated = $target_patrons->update_category( { to => $tocat } );
274 }
275
276 $verbose and print "$patrons_found found, $actually_updated updated\n";