Bugzilla – Attachment 60665 Details for
Bug 17168
Add a command line script for updating patron category based on status
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 17168 - Add a command line script for updating patron category based on status
Bug-17168---Add-a-command-line-script-for-updating.patch (text/plain), 11.37 KB, created by
Nick Clemens (kidclamp)
on 2017-02-24 22:21:10 UTC
(
hide
)
Description:
Bug 17168 - Add a command line script for updating patron category based on status
Filename:
MIME Type:
Creator:
Nick Clemens (kidclamp)
Created:
2017-02-24 22:21:10 UTC
Size:
11.37 KB
patch
obsolete
>From 43d318c6e0c344c245bf6cb530d8d6e08df7b68e Mon Sep 17 00:00:00 2001 >From: Nick Clemens <nick@bywatersolutions.com> >Date: Wed, 15 Feb 2017 16:34:04 +0000 >Subject: [PATCH] Bug 17168 - Add a command line script for updating patron > category based on status > >This patch adds a new script update_patrons_category.pl which allows for >updating patron categories in a batch upon meeting provided criteria. >This script additionally can replace j2a.pl. > >To test: >1 - perl update_patrons_category.pl -h >2 - Ensure help text makes sense and covers all options >3 - Test converting patrons supplying only fromcat and tocat > perl update_patrons_category.pl -f PT -t J -v --confirm > perl update_patrons_category.pl -f J -t PT -v --confirm >4 - All patrons should have been switched to and from Juveniles >5 - Try without --confirm switch > perl update_patrons_category.pl -f PT -t J -v >6 - Should list all patrons but not update >7 - Set the age for juvenile patrons to be outside the range provided in >categories (or set the upper age limit for juveniles to '2') >8 - Test with verbosity and with without --confirm > perl update_patrons_category.pl -f J -a -t PT -v > perl update_patrons_category.pl -f J -a -t PT -v --confirm >9 - Repeat above and verify linked/unlinked guarantors are removed in above scenario >10 - Test various fine and registration limits >11 - Test matching on specific fields i.e. --field surname=acosta >12 - Sign off > >Sponsored by: Round Rock Public Library >(https://www.roundrocktexas.gov/departments/library/) >--- > misc/cronjobs/update_patrons_category.pl | 273 +++++++++++++++++++++++++++++++ > 1 file changed, 273 insertions(+) > create mode 100644 misc/cronjobs/update_patrons_category.pl > >diff --git a/misc/cronjobs/update_patrons_category.pl b/misc/cronjobs/update_patrons_category.pl >new file mode 100644 >index 0000000..c9fef39 >--- /dev/null >+++ b/misc/cronjobs/update_patrons_category.pl >@@ -0,0 +1,273 @@ >+#!/usr/bin/perl >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+BEGIN { >+ # find Koha's Perl modules >+ # test carefully before changing this >+ use FindBin; >+ eval { require "$FindBin::Bin/../kohalib.pl" }; >+} >+ >+use C4::Context; >+use Getopt::Long; >+use Pod::Usage; >+use Koha::Logger; >+use Koha::Patrons; >+use Koha::Patron::Categories; >+use Koha::DateUtils; >+use Data::Dumper; >+ >+=head1 NAME >+ >+update_patrons_category.pl - Given a set of parameters update selected patrons from one catgeory to another >+ >+=head1 SYNOPSIS >+ >+update_patrons_category.pl -f=categorycode -t=categorycode >+ [-b=branchcode] [-n] [-a] [-fo=X] [-fu=X] >+ [-rb=date] [-ra=date] [-v] >+ [--field column=value ...] >+ >+update_patrons_category.pl --help | --man >+ >+Options: >+ --help brief help message >+ --man full documentation >+ -a update based on age restriction of current category >+ -fo=X update if fines over X amount >+ -fu=X update if fines under X amount >+ -rb=date update if registration date is before given date >+ -ra=date update if registration date is after a given date >+ --field name=value where <name> is a column in the borrowers table, patrons will be updated if the field is equal to given <value> >+ -v verbose mode >+ -confirm commit changes to db, no action will be taken unless this switch is included >+ -b <branchname> only deal with patrons from this library/branch >+ -f <categorycode> change patron category from this category >+ -t <categorycode> change patron category to this category >+ >+=head1 OPTIONS >+ >+=over 8 >+ >+=item B<--help> >+ >+Print a brief help message and exits. >+ >+=item B<--man> >+ >+Prints the manual page and exits. >+ >+=item B<-v> >+ >+Verbose. Without this flag set, only fatal errors are reported. >+ >+=item B<--confirm> >+ >+Commit changes. Unless this flag set is, the script will report changes but not actually execute them on the database. >+ >+=item B<-b> >+ >+changes patrons for one specific branch. Use the value in the >+branches.branchcode table. >+ >+=item B<-f> >+ >+*required* defines the category to update. Expects the code from categories.categorycode. >+ >+=item B<-t> >+ >+*required* defines the category patrons will be converted to. Expects the code from categories.categorycode. >+ >+=item B<-a> >+ >+Update patron only if they are outside the age range specified for the 'from' category. >+ >+=item B<-fo=X> >+ >+Supply a number and only account with fines over this number will be updated. >+ >+=item B<-fu=X> >+ >+Supply a number and only account with fines under this number will be updated. >+ >+=item B<-rb=date> >+ >+Enter a date in ISO format YYYY-MM-DD and only patrons registered before this date wil be updated. >+ >+=item B<-ra=date> >+ >+Enter a date in ISO format YYYY-MM-DD and only patrons registered after this date wil be updated. >+ >+=item B<--field column=value> >+ >+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) >+ >+e.g. >+--field dateexpiry=2016-01-01 >+will update all patrons who expired on that date, useful for schools etc. >+ >+=back >+ >+=head1 DESCRIPTION >+ >+This script is designed to update patrons from one category to another. >+ >+=head1 USAGE EXAMPLES >+ >+C<update_patron_categories.pl> - Suggests that you read this help. :) >+ >+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. >+ >+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. >+ >+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. >+ >+=cut >+ >+# These variables are set by command line options. >+# They are initially set to default values. >+ >+my $help = 0; >+my $man = 0; >+my $verbose = 0; >+my $doit = 0; >+my $age = 0; >+my $remove_guarantors = 0; >+my $fine_min; >+my $fine_max; >+my $fromcat; >+my $tocat; >+my $reg_bef; >+my $reg_aft; >+my $branch_lim; >+my %fields; >+ >+GetOptions( >+ 'help|?' => \$help, >+ 'man' => \$man, >+ 'v' => \$verbose, >+ 'confirm' => \$doit, >+ 'f=s' => \$fromcat, >+ 't=s' => \$tocat, >+ 'a' => \$age, >+ 'fo=s' => \$fine_min, >+ 'fu=s' => \$fine_max, >+ 'rb=s' => \$reg_bef, >+ 'ra=s' => \$reg_aft, >+ 'b=s' => \$branch_lim, >+ 'field=s' => \%fields >+) or pod2usage(2); >+pod2usage(1) if $help; >+pod2usage( -verbose => 2 ) if $man; >+ >+if ( not $fromcat && $tocat ) { #make sure we've specified the info we need. >+ print "Must supply category from and to (-f & -t) please specify -help for usage tips.\n"; >+ exit; >+} >+ >+($verbose && !$doit) and print "No actions will be taken (test mode)\n"; >+ >+$verbose and print "Will update patrons from $fromcat to $tocat with conditions below (if any)\n"; >+ >+my %params; >+my %query; >+ >+if ( $reg_bef || $reg_aft ){ >+ my $date_bef; >+ my $date_aft; >+ if (defined $reg_bef) {eval { $date_bef = dt_from_string( $reg_bef, 'iso' ); };} >+ die "$reg_bef is not a valid date before, aborting! Use a date in format YYYY-MM-DD.$@" >+ if $@; >+ if (defined $reg_aft) {eval { $date_aft = dt_from_string( $reg_aft, 'iso' ); };} >+ die "$reg_bef is not a valid date after, aborting! Use a date in format YYYY-MM-DD.$@" >+ if $@; >+ $params{dateenrolled}{'<='}=$reg_bef if defined $date_bef; >+ $params{dateenrolled}{'>='}=$reg_aft if defined $date_aft; >+} >+ >+if ($age && ($fine_min || $fine_max) ){ >+ %query = ( >+ join => ["accountlines" => "categorycode"], >+ select => ["borrowernumber", { sum => 'amountoutstanding', -as => 'total_fines'} ], >+ as => [qw/borrowernumber total_fines/], >+ where => \[' FLOOR(DATEDIFF (NOW(), dateofbirth)/365) NOT BETWEEN COALESCE(dateofbirthrequired,0) AND COALESCE( upperagelimit, 999 ) '], >+ group_by => ["borrowernumber"], >+ having => [{"total_fines" => { '>=', $fine_min, '<=', $fine_max }}], >+ ); >+ $query{having}{total_fines}{'<='}=$fine_max if defined $fine_max; >+ $query{having}{total_fines}{'>='}=$fine_min if defined $fine_min; >+} >+else{ >+ if ($age) { >+ if ( Koha::Patron::Categories->find($fromcat)->category_type eq 'C' && Koha::Patron::Categories->find($tocat)->category_type eq 'A' ) { $remove_guarantors = 1; } >+ $query{join} = ["categorycode"]; >+ $query{where} = \[' FLOOR(DATEDIFF (NOW(), dateofbirth)/365) NOT BETWEEN COALESCE(dateofbirthrequired,0) AND COALESCE( upperagelimit, 999 ) ']; >+ } >+ if ($fine_min || $fine_max) { >+ $query{join} = ["accountlines"]; >+ $query{select} = ["borrowernumber", { sum => 'amountoutstanding', -as => 'total_fines'} ]; >+ $query{as} = [qw/borrowernumber total_fines/]; >+ $query{group_by} = ["borrowernumber"]; >+ $query{having}{total_fines}{'<='}=$fine_max if defined $fine_max; >+ $query{having}{total_fines}{'>='}=$fine_min if defined $fine_min; >+ } >+} >+ >+$params{"me.categorycode"} = $fromcat if defined $fromcat; >+$params{"me.branchcode"} = $branch_lim if defined $branch_lim; >+ >+if ($verbose) { >+ if (defined $reg_bef) {print "Condition: Registered before $reg_bef\n";} >+ if (defined $reg_aft) {print "Condition: Registered before $reg_aft\n";} >+ if (defined $fine_min) {print "Condition: Total fines more than $fine_min\n";} >+ if (defined $fine_max) {print "Condition: Total fines less than $fine_max\n";} >+ if ($age) {print "Condition: Age is outside range defined for $fromcat\n";} >+ if (defined $branch_lim) {print "Condition: Branchcode of patron is $branch_lim\n";} >+} >+ >+while (my ($key,$value) = each %fields ) { >+ $verbose and print "Condition: borrower column $key is equal to $value\n"; >+ $params{"me.".$key} = $value; >+} >+ >+my $target_patrons = Koha::Patrons->search(\%params,\%query); >+ >+my $counter = 0; >+ >+my $testdisplay = $doit ? "" : "WOULD HAVE "; >+ >+while ( my $target_patron = $target_patrons->next() ){ >+ $counter++; >+ my $target = Koha::Patrons->find( $target_patron->borrowernumber ); >+ $verbose and print $testdisplay."Updated ".$target->firstname." ".$target->surname." from $fromcat to $tocat\n"; >+ if ( $remove_guarantors ){ >+ $verbose and print $testdisplay."Removing guarantor info\n"; >+ $target->guarantorid(0); >+ $target->contactname(''); >+ $target->contactfirstname(''); >+ $target->contacttitle(''); >+ $target->relationship(''); >+ } >+ if ($doit) { >+ $target->categorycode($tocat); >+ $target->store(); >+ } >+} >+ >+$verbose and print "$counter patrons $testdisplay updated\n"; >-- >2.1.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 17168
:
60279
|
60280
|
60665
|
70921
|
73012
|
74936
|
75698
|
76974
|
76975
|
76976
|
85870
|
85871
|
85872
|
85873
|
85878
|
88809
|
88810
|
88811
|
88812
|
88813
|
88814
|
91677
|
94665
|
94666
|
94667
|
94668
|
94669
|
94670
|
94671
|
94790
|
94793
|
94794