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>.
23 # find Koha's Perl modules
24 # test carefully before changing this
26 eval { require "$FindBin::Bin/../kohalib.pl" };
37 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.
41 juv2adult.pl [ -b=<branchcode> -f=<categorycode> -t=<categorycode> ]
44 --help brief help message
45 --man full documentation
47 -n take no action, display only
48 -b <branchname> only deal with patrons from this library/branch
49 -f <categorycode> change patron category from this category
50 -t <categorycode> change patron category to this category
58 Print a brief help message and exits.
62 Prints the manual page and exits.
66 Verbose. Without this flag set, only fatal errors are reported.
70 No Action. With this flag set, script will report changes but not actually execute them on the database.
74 changes patrons for one specific branch. Use the value in the
75 branches.branchcode table.
79 *required* defines the juvenile category to update. Expects the code from categories.categorycode.
83 *required* defines the category juvenile patrons will be converted to. Expects the code from categories.categorycode.
89 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.
93 C<juv2adult.pl> - Suggests that you read this help. :)
95 C<juv2adult.pl> -b=<branchcode> -f=<categorycode> -t=<categorycode> - Processes a single branch, and updates the patron categories from fromcat to tocat.
97 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.
100 # These variables are set by command line options.
101 # They are initially set to default values.
120 pod2usage
(1) if $help;
121 pod2usage
( -verbose
=> 2 ) if $man;
123 if ( not $fromcat && $tocat ) { #make sure we've specified the info we need.
124 print "please specify -help for usage tips.\n";
130 my $dbh = C4
::Context
->dbh;
132 #get today's date, format it and subtract upperagelimit
133 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
137 if ( $mon < 10 ) { $mon = "0" . $mon; }
138 if ( $mday < 10 ) { $mday = "0" . $mday; }
140 # get the upperagelimit from the category to be transitioned from
141 my $query = qq|SELECT upperagelimit from categories where categorycode
=?
|;
142 my $sth = $dbh->prepare($query);
143 $sth->execute($fromcat)
144 or die "Couldn't execute statement: " . $sth->errstr;
145 my $agelimit = $sth->fetchrow_array();
146 if ( not $agelimit ) {
147 die "No patron category $fromcat. Please try again. \n";
150 $query = qq|SELECT categorycode from categories where categorycode
=?
|;
151 $sth = $dbh->prepare($query);
152 $sth->execute($tocat)
153 or die "Couldn't execute statement: " . $sth->errstr;
154 my $tocatage = $sth->fetchrow_array();
155 if ( not $tocatage ) {
156 die "No patron category $tocat. Please try again. \n";
161 $verbose and print "The age limit for category $fromcat is $agelimit\n";
163 my $itsyourbirthday = "$year-$mon-$mday";
165 if ( not $noaction ) {
166 if ($mybranch) { #yep, we received a specific branch to work on.
167 $verbose and print "Looking for patrons of $mybranch to update from $fromcat to $tocat that were born before $itsyourbirthday\n";
170 SET guarantorid
='0',
172 WHERE dateofbirth
<= ?
173 AND dateofbirth
!= '0000-00-00'
175 AND categorycode IN
(
178 WHERE category_type
= 'C'
181 my $sth = $dbh->prepare($query);
182 my $res = $sth->execute( $tocat, $itsyourbirthday, $mybranch, $fromcat )
183 or die "can't execute";
185 if ( $res eq '0E0' ) {
186 print "No patrons updated\n";
189 print "Updated $res patrons\n";
192 else { # branch was not supplied, processing all branches
193 $verbose and print "Looking in all branches for patrons to update from $fromcat to $tocat that were born before $itsyourbirthday\n";
196 SET guarantorid
= '0',
198 WHERE dateofbirth
<= ?
199 AND dateofbirth
!='0000-00-00'
200 AND categorycode IN
(
203 WHERE category_type
= 'C'
206 my $sth = $dbh->prepare($query);
207 my $res = $sth->execute( $tocat, $itsyourbirthday, $fromcat )
208 or die "can't execute";
210 if ( $res eq '0E0' ) {
211 print "No patrons updated\n";
214 print "Updated $res patrons\n";
221 $verbose and print "Displaying patrons that would be updated from $fromcat to $tocat from $mybranch\n";
228 WHERE dateofbirth
<= ?
229 AND dateofbirth
!= '0000-00-00'
231 AND categorycode IN
(
234 WHERE category_type
= 'C'
238 my $sth = $dbh->prepare($query);
239 $sth->execute( $itsyourbirthday, $mybranch, $fromcat )
240 or die "Couldn't execute statement: " . $sth->errstr;
242 while ( my @res = $sth->fetchrow_array() ) {
243 my $firstname = $res[0];
244 my $surname = $res[1];
245 my $barcode = $res[2];
247 print "$firstname $surname $barcode $birthday\n";
251 $verbose and print "Displaying patrons that would be updated from $fromcat to $tocat.\n";
258 WHERE dateofbirth
<= ?
259 AND dateofbirth
!= '0000-00-00'
260 AND categorycode IN
(
263 WHERE category_type
= 'C'
267 my $sth = $dbh->prepare($query);
268 $sth->execute( $itsyourbirthday, $fromcat )
269 or die "Couldn't execute statement: " . $sth->errstr;
271 while ( my @res = $sth->fetchrow_array() ) {
272 my $firstname = $res[0];
273 my $surname = $res[1];
274 my $barcode = $res[2];
276 print "$firstname $surname $barcode $birthday\n";