3 # 2011 Liz Rea - Northeast Kansas Library System <lrea@nekls.org>
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 # find Koha's Perl modules
25 # test carefully before changing this
27 eval { require "$FindBin::Bin/../kohalib.pl" };
39 juv2adult.pl - convert juvenile/child patrons from juvenile patron category and category code to corresponding adult patron category and category code when they reach the upper age limit defined in the Patron Categories.
43 juv2adult.pl [ -b=<branchcode> -f=<categorycode> -t=<categorycode> ]
46 --help brief help message
47 --man full documentation
49 -n take no action, display only
50 -b <branchname> only deal with patrons from this library/branch
51 -f <categorycode> change patron category from this category
52 -t <categorycode> change patron category to this category
59 Print a brief help message and exits.
63 Prints the manual page and exits.
67 Verbose. Without this flag set, only fatal errors are reported.
71 No Action. With this flag set, script will report changes but not actually execute them on the database.
75 changes patrons for one specific branch. Use the value in the
76 branches.branchcode table.
80 *required* defines the juvenile category to update. Expects the code from categories.categorycode.
84 *required* defines the category juvenile patrons will be converted to. Expects the code from categories.categorycode.
90 This script is designed to update patrons from juvenile to adult patron types, remove the guarantor, and update their category codes appropriately when they reach the upper age limit defined in the Patron Categories.
94 C<juv2adult.pl> - Suggests that you read this help. :)
96 C<juv2adult.pl> -b=<branchcode> -f=<categorycode> -t=<categorycode> - Processes a single branch, and updates the patron categories from fromcat to tocat.
98 C<juv2adult.pl> -f=<categorycode> -t=<categorycode> -v -n - Processes all branches, shows all messages, and reports the patrons who would be affected. Takes no action on the database.
101 # These variables are set by command line options.
102 # They are initially set to default values.
122 pod2usage(1) if $help;
123 pod2usage( -verbose => 2 ) if $man;
125 if(not $fromcat && $tocat) { #make sure we've specified the info we need.
126 print "please specify -help for usage tips.\n";
132 my $dbh=C4::Context->dbh;
133 #get today's date, format it and subtract upperagelimit
134 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
136 $mon +=1; if ($mon < 10) {$mon = "0".$mon;}
137 if ($mday < 10) {$mday = "0".$mday;}
138 # get the upperagelimit from the category to be transitioned from
139 my $query=qq|SELECT upperagelimit from categories where categorycode =?|;
140 my $sth=$dbh->prepare( $query );
141 $sth->execute( $fromcat )
142 or die "Couldn't execute statement: " . $sth->errstr;
143 my $agelimit = $sth->fetchrow_array();
144 if ( not $agelimit ) {
146 die "No patron category $fromcat. Please try again. \n";
148 $query=qq|SELECT categorycode from categories where categorycode=?|;
149 $sth=$dbh->prepare( $query );
150 $sth->execute( $tocat )
151 or die "Couldn't execute statement: " . $sth->errstr;
152 my $tocatage = $sth->fetchrow_array();
153 if ( not $tocatage ){
154 die "No patron category $tocat. Please try again. \n";
159 $verbose and print "The age limit for category $fromcat is $agelimit\n";
161 my $itsyourbirthday = "$year-$mon-$mday";
164 if ( not $noaction) {
165 if ( $mybranch ) { #yep, we received a specific branch to work on.
166 $verbose and print "Looking for patrons of $mybranch to update from $fromcat to $tocat that were born before $itsyourbirthday\n";
167 my $query=qq|UPDATE borrowers
168 SET guarantorid ='0',
171 AND dateofbirth!='0000-00-00'
173 AND categorycode IN (select categorycode from categories where category_type='C' and categorycode=?)|;
174 my $sth=$dbh->prepare($query);
175 my $res = $sth->execute( $tocat, $itsyourbirthday, $mybranch, $fromcat ) or die "can't execute";
176 if ($res eq '0E0') { print "No patrons updated\n";
177 } else { print "Updated $res patrons\n"; }
178 } else { # branch was not supplied, processing all branches
179 $verbose and print "Looking in all branches for patrons to update from $fromcat to $tocat that were born before $itsyourbirthday\n";
180 my $query=qq|UPDATE borrowers
181 SET guarantorid ='0',
184 AND dateofbirth!='0000-00-00'
185 AND categorycode IN (select categorycode from categories where category_type='C' and categorycode=?)|;
186 my $sth=$dbh->prepare($query);
187 my $res = $sth->execute( $tocat, $itsyourbirthday, $fromcat ) or die "can't execute";
188 if ($res eq '0E0') { print "No patrons updated\n";
189 } else { print "Updated $res patrons\n"; }
194 $verbose and print "Displaying patrons that would be updated from $fromcat to $tocat from $mybranch\n";
195 my $query=qq|SELECT firstname,
201 AND dateofbirth!='0000-00-00'
203 AND categorycode IN (select categorycode from categories where category_type='C' and categorycode=?)|;
204 my $sth=$dbh->prepare( $query );
205 $sth->execute( $itsyourbirthday, $mybranch, $fromcat )
206 or die "Couldn't execute statement: " . $sth->errstr;
207 while ( my @res = $sth->fetchrow_array()) {
208 my $firstname = $res[0];
209 my $surname = $res[1];
210 my $barcode = $res[2];
212 print "$firstname $surname $barcode $birthday\n";
215 $verbose and print "Displaying patrons that would be updated from $fromcat to $tocat.\n";
216 my $query=qq|SELECT firstname,
222 AND dateofbirth!='0000-00-00'
223 AND categorycode IN (select categorycode from categories where category_type='C' and categorycode=?)|;
224 my $sth=$dbh->prepare( $query );
225 $sth->execute( $itsyourbirthday, $fromcat )
226 or die "Couldn't execute statement: " . $sth->errstr;
227 while ( my @res = $sth->fetchrow_array()) {
228 my $firstname = $res[0];
229 my $surname = $res[1];
230 my $barcode = $res[2];
232 print "$firstname $surname $barcode $birthday\n";