3 # This file is part of Koha.
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.
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.
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>.
21 # find Koha's Perl modules
22 # test carefully before changing this
24 eval { require "$FindBin::Bin/../kohalib.pl" };
32 use Koha
::Patron
::Categories
;
34 use Koha
::Script
-cron
;
38 update_patrons_category.pl - Given a set of parameters update selected patrons from one catgeory to another. Options are cumulative.
42 update_patrons_category.pl -f=categorycode -t=categorycode
43 [-b=branchcode] [--too_old] [--too_young] [-fo=X] [-fu=X]
44 [-rb=date] [-ra=date] [-v]
45 [--field column=value ...]
47 update_patrons_category.pl --help | --man
50 --help brief help message
51 --man full documentation
52 -too_old update if over maximum age for current category
53 -too_young 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 --field name=value where <name> is a column in the borrowers table, patrons will be updated if the field is equal to given <value>
59 --where <conditions> where clause to add to the query
60 -v -verbose verbose mode
61 -c --confirm commit changes to db, no action will be taken unless this switch is included
62 -b --branch <branchname> only deal with patrons from this library/branch
63 -f --from <categorycode> change patron category from this category
64 -t --to <categorycode> change patron category to this category
72 Print a brief help message and exits.
76 Prints the manual page and exits.
78 =item B<--verbose | -v>
80 Verbose. Without this flag set, only fatal errors are reported.
82 =item B<--confirm | -c>
84 Commit changes. Unless this flag set is, the script will report changes but not actually execute them on the database.
86 =item B<--branch | -b>
88 changes patrons for one specific branch. Use the value in the
89 branches.branchcode table.
93 *required* defines the category to update. Expects the code from categories.categorycode.
97 *required* defines the category patrons will be converted to. Expects the code from categories.categorycode.
101 Update patron only if they are above the maximum age range specified for the 'from' category.
105 Update patron only if they are below the minimum age range specified for the 'from' category.
107 =item B<--fineover=X | -fo=X>
109 Supply a number and only account with fines over this number will be updated.
111 =item B<--fineunder=X | -fu=X>
113 Supply a number and only account with fines under this number will be updated.
115 =item B<--regbefore=date | -rb=date>
117 Enter a date in ISO format YYYY-MM-DD and only patrons registered before this date wil be updated.
119 =item B<--regafter=date | -ra=date>
121 Enter a date in ISO format YYYY-MM-DD and only patrons registered after this date wil be updated.
123 =item B<--field column=value | -d column=value>
125 Use this flag to specify a column in the borrowers table and update only patrons whose value in that column equals the value supplied (repeatable)
126 A value of null will check for a field that is not set.
129 --field dateexpiry=2016-01-01
130 will update all patrons who expired on that date, useful for schools etc.
132 =item B<--where $conditions>
134 Use this option to specify a condition built with columns from the borrowers table
137 --where 'email IS NULL'
138 will update all patrons with no value for email
140 --where 'categorycode LIKE "%CHILD"'
141 will update all patrons with a category ending in CHILD.
143 --where 'categorycode LIKE RESIDENT%'
144 will update all patrons whose category does not begin with RESIDENT.
150 This script is designed to update patrons from one category to another.
152 =head1 USAGE EXAMPLES
154 C<update_patron_categories.pl> - Suggests that you read this help. :)
156 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.
158 C<update_patron_categories.pl> -b=<branchcode> -f=<categorycode> -t=<categorycode> --too_old --confirm - Processes a single branch, and updates the patron categories from fromcat to tocat for patrons over the age range of fromcat.
160 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.
164 # These variables are set by command line options.
165 # They are initially set to default values.
173 my $remove_guarantors = 0;
187 'v|verbose' => \
$verbose,
188 'c|confirm' => \
$doit,
189 'f|from=s' => \
$fromcat,
191 'too_old' => \
$ageover,
192 'too_young' => \
$ageunder,
193 'fo|finesover=s' => \
$fine_min,
194 'fu|finesunder=s' => \
$fine_max,
195 'rb|regbefore=s' => \
$reg_bef,
196 'ra|regafter=s' => \
$reg_aft,
197 'b|branch=s' => \
$branch_lim,
198 'd|field=s' => \
%fields,
199 'where=s' => \
@where,
202 pod2usage
(1) if $help;
204 pod2usage
( -verbose
=> 2 ) if $man;
206 if ( not $fromcat && $tocat ) { #make sure we've specified the info we need.
207 print "Must supply category from and to (-f & -t) please specify -help for usage tips.\n";
212 ( $verbose && !$doit ) and print "No actions will be taken (test mode)\n";
214 $verbose and print "Will update patrons from $fromcat to $tocat with conditions below (if any)\n";
218 if ( $reg_bef || $reg_aft ) {
221 if ( defined $reg_bef ) {
222 eval { $date_bef = dt_from_string
( $reg_bef, 'iso' ); };
224 die "$reg_bef is not a valid date before, aborting! Use a date in format YYYY-MM-DD.$@"
226 if ( defined $reg_aft ) {
227 eval { $date_aft = dt_from_string
( $reg_aft, 'iso' ); };
229 die "$reg_bef is not a valid date after, aborting! Use a date in format YYYY-MM-DD.$@"
231 $params{dateenrolled
}{'<='} = $reg_bef if defined $date_bef;
232 $params{dateenrolled
}{'>='} = $reg_aft if defined $date_aft;
235 my $cat_from = Koha
::Patron
::Categories
->find($fromcat);
236 my $cat_to = Koha
::Patron
::Categories
->find($tocat);
237 die "Categories not found" unless $cat_from && $cat_to;
239 $params{"me.categorycode"} = $fromcat;
240 $params{"me.branchcode"} = $branch_lim if $branch_lim;
243 print "Conditions:\n";
244 print " Registered before $reg_bef\n" if $reg_bef;
245 print " Registered after $reg_aft\n" if $reg_aft;
246 print " Total fines more than $fine_min\n" if $fine_min;
247 print " Total fines less than $fine_max\n" if $fine_max;
248 print " Age below minimum for " . $cat_from->description . "\n" if $ageunder;
249 print " Age above maximum for " . $cat_from->description . "\n" if $ageover;
250 if ( defined $branch_lim ) {
251 print " Branchcode of patron is $branch_lim\n";
255 while ( my ( $key, $value ) = each %fields ) {
256 $verbose and print " Borrower column $key is $value\n";
257 $value = undef if lc($value) eq 'null';
258 $params{ "me." . $key } = $value;
261 my $where_literal = join ' AND ', @where;
262 my $target_patrons = Koha
::Patrons
->search( \
%params );
263 $target_patrons = $target_patrons->search( \
$where_literal ) if @where;
264 $target_patrons = $target_patrons->search_patrons_to_update_category(
267 search_params
=> \
%params,
268 too_young
=> $ageunder,
270 fine_min
=> $fine_min,
271 fine_max
=> $fine_max,
275 my $patrons_found = $target_patrons->count;
276 my $actually_updated = 0;
277 my $testdisplay = $doit ?
"" : "WOULD HAVE ";
279 while ( my $target_patron = $target_patrons->next() ) {
280 $target_patron->discard_changes();
282 and print $testdisplay
284 . $target_patron->firstname() . " "
285 . $target_patron->surname()
286 . " from $fromcat to $tocat\n";
288 $target_patrons->reset;
291 $actually_updated = $target_patrons->update_category_to( { category
=> $tocat } );
294 $verbose and print "$patrons_found found, $actually_updated updated\n";