View | Details | Raw Unified | Return to bug 17168
Collapse All | Expand All

(-)a/Koha/Patrons.pm (-8 / +15 lines)
Lines 398-412 sub search_patrons_to_update { Link Here
398
398
399
    my $cat_from = Koha::Patron::Categories->find($params->{from});
399
    my $cat_from = Koha::Patron::Categories->find($params->{from});
400
    $search_params->{categorycode}=$params->{from};
400
    $search_params->{categorycode}=$params->{from};
401
    if ($params->{ao} || $params->{au}){
401
    if ($params->{ageover} || $params->{ageunder}){
402
        my ($y,$m,$d) = Today();
402
        if( $cat_from->dateofbirthrequired && $params->{ageunder} ) {
403
        if( $cat_from->dateofbirthrequired && $params->{au} ) {
403
            my $date_after = output_pref({
404
            my ($dy,$dm,$dd) =Add_Delta_YMD($y,$m,$d,-$cat_from->dateofbirthrequired,0,0);
404
                dt         => dt_from_string()->subtract( years => $cat_from->dateofbirthrequired),
405
            $search_params->{dateofbirth}{'>'} = $dy."-".sprintf("%02d",$dm)."-".sprintf("%02d",$dd);
405
                dateonly   => 1,
406
                dateformat => 'sql'
407
            });
408
            $search_params->{dateofbirth}{'>'} = $date_after;
406
        }
409
        }
407
        if( $cat_from->upperagelimit && $params->{ao} ) {
410
        if( $cat_from->upperagelimit && $params->{ageover} ) {
408
            my ($dy,$dm,$dd) =Add_Delta_YMD($y,$m,$d,-$cat_from->upperagelimit,0,0);
411
            my $date_before = output_pref({
409
            $search_params->{dateofbirth}{'<'} = $dy."-".sprintf("%02d",$dm)."-".sprintf("%02d",$dd);
412
                dt         => dt_from_string()->subtract( years => $cat_from->upperagelimit),
413
                dateonly   => 1,
414
                dateformat => 'sql'
415
            });
416
            $search_params->{dateofbirth}{'<'} = $date_before;
410
        }
417
        }
411
    }
418
    }
412
    if ($params->{fine_min} || $params->{fine_max}) {
419
    if ($params->{fine_min} || $params->{fine_max}) {
(-)a/misc/cronjobs/update_patrons_category.pl (-78 / +98 lines)
Lines 47-66 update_patrons_category.pl -f=categorycode -t=categorycode Link Here
47
update_patrons_category.pl --help | --man
47
update_patrons_category.pl --help | --man
48
48
49
Options:
49
Options:
50
   --help       brief help message
50
   --help                   brief help message
51
   --man        full documentation
51
   --man                    full documentation
52
   -ao          update if over  maximum age for current category
52
   -ao --ageover            update if over  maximum age for current category
53
   -au          update if under minimuum age  current category
53
   -au --ageunder           update if under minimuum age  current category
54
   -fo=X        update if fines over  X amount
54
   -fo=X --fineover=X       update if fines over  X amount
55
   -fu=X        update if fines under X amount
55
   -fu=X --fineunder=X      update if fines under X amount
56
   -rb=date     update if registration date is before given date
56
   -rb=date --regbefore     update if registration date is before given date
57
   -ra=date     update if registration date is after a given date
57
   -ra=date --regafter      update if registration date is after a given date
58
   --field name=value  where <name> is a column in the borrowers table, patrons will be updated if the field is equal to given <value>
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 mode
59
   -v -verbose              verbose mode
60
   -confirm     commit changes to db, no action will be taken unless this switch is included
60
   -c --confirm             commit changes to db, no action will be taken unless this switch is included
61
   -b           <branchname>       only deal with patrons from this library/branch
61
   -b --branch <branchname> only deal with patrons from this library/branch
62
   -f           <categorycode>     change patron category from this category
62
   -f --from <categorycode> change patron category from this category
63
   -t           <categorycode>     change patron category to this category
63
   -t --to   <categorycode> change patron category to this category
64
64
65
=head1 OPTIONS
65
=head1 OPTIONS
66
66
Lines 74-125 Print a brief help message and exits. Link Here
74
74
75
Prints the manual page and exits.
75
Prints the manual page and exits.
76
76
77
=item B<-v>
77
=item B<--verbosse | -v>
78
78
79
Verbose. Without this flag set, only fatal errors are reported.
79
Verbose. Without this flag set, only fatal errors are reported.
80
80
81
=item B<--confirm>
81
=item B<--confirm | -c>
82
82
83
Commit changes. Unless this flag set is, the script will report changes but not actually execute them on the database.
83
Commit changes. Unless this flag set is, the script will report changes but not actually execute them on the database.
84
84
85
=item B<-b>
85
=item B<--branch | -b>
86
86
87
changes patrons for one specific branch. Use the value in the
87
changes patrons for one specific branch. Use the value in the
88
branches.branchcode table.
88
branches.branchcode table.
89
89
90
=item B<-f>
90
=item B<--from | -f>
91
91
92
*required* defines the category to update. Expects the code from categories.categorycode.
92
*required* defines the category to update. Expects the code from categories.categorycode.
93
93
94
=item B<-t>
94
=item B<--to | -t>
95
95
96
*required* defines the category patrons will be converted to. Expects the code from categories.categorycode.
96
*required* defines the category patrons will be converted to. Expects the code from categories.categorycode.
97
97
98
=item B<-ao>
98
=item B<--ageover | -ao>
99
99
100
Update patron only if they are above the maximum age range specified for the 'from' category.
100
Update patron only if they are above the maximum age range specified for the 'from' category.
101
101
102
=item B<-au>
102
=item B<--ageunde | -au>
103
103
104
Update patron only if they are below the minimum age range specified for the 'from' category.
104
Update patron only if they are below the minimum age range specified for the 'from' category.
105
105
106
=item B<-fo=X>
106
=item B<--fineover=X | -fo=X>
107
107
108
Supply a number and only account with fines over this number will be updated.
108
Supply a number and only account with fines over this number will be updated.
109
109
110
=item B<-fu=X>
110
=item B<==fineunder=X | -fu=X>
111
111
112
Supply a number and only account with fines under this number will be updated.
112
Supply a number and only account with fines under this number will be updated.
113
113
114
=item B<-rb=date>
114
=item B<--regbefore=date | -rb=date>
115
115
116
Enter a date in ISO format YYYY-MM-DD and only patrons registered before this date wil be updated.
116
Enter a date in ISO format YYYY-MM-DD and only patrons registered before this date wil be updated.
117
117
118
=item B<-ra=date>
118
=item B<--regafter=date | -ra=date>
119
119
120
Enter a date in ISO format YYYY-MM-DD and only patrons registered after this date wil be updated.
120
Enter a date in ISO format YYYY-MM-DD and only patrons registered after this date wil be updated.
121
121
122
=item B<--field column=value>
122
=item B<--field column=value | -d column=value>
123
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)
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
125
Lines 148-157 C<update_patron_categories.pl> -f=<categorycode> -t=<categorycode> -v - Process Link Here
148
# These variables are set by command line options.
148
# These variables are set by command line options.
149
# They are initially set to default values.
149
# They are initially set to default values.
150
150
151
my $help     = 0;
151
my $help    = 0;
152
my $man      = 0;
152
my $man     = 0;
153
my $verbose  = 0;
153
my $verbose = 0;
154
my $doit = 0;
154
my $doit    = 0;
155
my $au;
155
my $au;
156
my $ao;
156
my $ao;
157
my $min_dob;
157
my $min_dob;
Lines 167-212 my $branch_lim; Link Here
167
my %fields;
167
my %fields;
168
168
169
GetOptions(
169
GetOptions(
170
    'help|?'  => \$help,
170
    'help|?'          => \$help,
171
    'man'     => \$man,
171
    'man'             => \$man,
172
    'v'       => \$verbose,
172
    'v|verbose'       => \$verbose,
173
    'confirm' => \$doit,
173
    'c|confirm'       => \$doit,
174
    'f=s'     => \$fromcat,
174
    'f|from=s'        => \$fromcat,
175
    't=s'     => \$tocat,
175
    't|to=s'          => \$tocat,
176
    'ao'       => \$ao,
176
    'ao|ageover'      => \$ageover,
177
    'au'       => \$au,
177
    'au|ageunder'     => \$ageunder,
178
    'fo=s'    => \$fine_min,
178
    'fo|finesover=s'  => \$fine_min,
179
    'fu=s'    => \$fine_max,
179
    'fu|finesunder=s' => \$fine_max,
180
    'rb=s'    => \$reg_bef,
180
    'rb|regbefore=s'  => \$reg_bef,
181
    'ra=s'    => \$reg_aft,
181
    'ra|regafter=s'   => \$reg_aft,
182
    'b=s'     => \$branch_lim,
182
    'b|branch=s'      => \$branch_lim,
183
    'field=s' => \%fields
183
    'd|field=s'       => \%fields
184
) or pod2usage(2);
184
) or pod2usage(2);
185
pod2usage(1) if $help;
185
pod2usage(1) if $help;
186
pod2usage( -verbose => 2 ) if $man;
186
pod2usage( -verbose => 2 ) if $man;
187
187
188
warn "v $verbose c $doit f $fromcat t $tocat";
189
warn Data::Dumper::Dumper(%fields);
190
exit;
191
188
if ( not $fromcat && $tocat ) {    #make sure we've specified the info we need.
192
if ( not $fromcat && $tocat ) {    #make sure we've specified the info we need.
189
    print "Must supply category from and to (-f & -t) please specify -help for usage tips.\n";
193
    print
194
"Must supply category from and to (-f & -t) please specify -help for usage tips.\n";
190
    exit;
195
    exit;
191
}
196
}
192
197
193
($verbose && !$doit) and print "No actions will be taken (test mode)\n";
198
( $verbose && !$doit ) and print "No actions will be taken (test mode)\n";
194
199
195
$verbose and print "Will update patrons from $fromcat to $tocat with conditions below (if any)\n";
200
$verbose
201
  and print
202
"Will update patrons from $fromcat to $tocat with conditions below (if any)\n";
196
203
197
my %params;
204
my %params;
198
205
199
if ( $reg_bef || $reg_aft ){
206
if ( $reg_bef || $reg_aft ) {
200
    my $date_bef;
207
    my $date_bef;
201
    my $date_aft;
208
    my $date_aft;
202
    if (defined $reg_bef) {eval { $date_bef = dt_from_string( $reg_bef, 'iso' ); };}
209
    if ( defined $reg_bef ) {
203
    die "$reg_bef is not a valid date before, aborting! Use a date in format YYYY-MM-DD.$@"
210
        eval { $date_bef = dt_from_string( $reg_bef, 'iso' ); };
204
    if $@;
211
    }
205
    if (defined $reg_aft) {eval { $date_aft = dt_from_string( $reg_aft, 'iso' ); };}
212
    die
206
    die "$reg_bef is not a valid date after, aborting! Use a date in format YYYY-MM-DD.$@"
213
"$reg_bef is not a valid date before, aborting! Use a date in format YYYY-MM-DD.$@"
207
    if $@;
214
      if $@;
208
    $params{dateenrolled}{'<='}=$reg_bef if defined $date_bef;
215
    if ( defined $reg_aft ) {
209
    $params{dateenrolled}{'>='}=$reg_aft if defined $date_aft;
216
        eval { $date_aft = dt_from_string( $reg_aft, 'iso' ); };
217
    }
218
    die
219
"$reg_bef is not a valid date after, aborting! Use a date in format YYYY-MM-DD.$@"
220
      if $@;
221
    $params{dateenrolled}{'<='} = $reg_bef if defined $date_bef;
222
    $params{dateenrolled}{'>='} = $reg_aft if defined $date_aft;
210
}
223
}
211
224
212
my $cat_from = Koha::Patron::Categories->find($fromcat);
225
my $cat_from = Koha::Patron::Categories->find($fromcat);
Lines 218-259 $params{"me.branchcode"} = $branch_lim if $branch_lim; Link Here
218
231
219
if ($verbose) {
232
if ($verbose) {
220
    print "Conditions:\n";
233
    print "Conditions:\n";
221
    print "    Registered before $reg_bef\n" if $reg_bef;
234
    print "    Registered before $reg_bef\n"      if $reg_bef;
222
    print "    Registered after  $reg_aft\n" if $reg_aft;
235
    print "    Registered after  $reg_aft\n"      if $reg_aft;
223
    print "    Total fines more than $fine_min\n" if $fine_min;
236
    print "    Total fines more than $fine_min\n" if $fine_min;
224
    print "    Total fines less than $fine_max\n" if $fine_max;
237
    print "    Total fines less than $fine_max\n" if $fine_max;
225
    print "    Age below minimum for ".$cat_from->description."\n" if $au;
238
    print "    Age below minimum for " . $cat_from->description . "\n" if $ageunder;
226
    print "    Age above maximum for ".$cat_from->description."\n" if $ao;
239
    print "    Age above maximum for " . $cat_from->description . "\n" if $ageover;
227
    if (defined $branch_lim) {
240
    if ( defined $branch_lim ) {
228
        print "    Branchcode of patron is $branch_lim\n";
241
        print "    Branchcode of patron is $branch_lim\n";
229
    }
242
    }
230
}
243
}
231
244
232
while (my ($key,$value) = each %fields ) {
245
while ( my ( $key, $value ) = each %fields ) {
233
    $verbose and print "    Borrower column $key is equal to $value\n";
246
    $verbose and print "    Borrower column $key is equal to $value\n";
234
    $params{"me.".$key} = $value;
247
    $params{ "me." . $key } = $value;
235
}
248
}
236
249
237
my $target_patrons = Koha::Patrons->search_patrons_to_update({
250
my $target_patrons = Koha::Patrons->search_patrons_to_update(
238
        from => $fromcat,
251
    {
252
        from          => $fromcat,
239
        search_params => \%params,
253
        search_params => \%params,
240
        au => $au,
254
        ageunder      => $ageunder,
241
        ao => $ao,
255
        ageover       => $ageover,
242
        fine_min => $fine_min,
256
        fine_min      => $fine_min,
243
        fine_max => $fine_max,
257
        fine_max      => $fine_max,
244
    });
258
    }
245
my $patrons_found = $target_patrons->count;
259
);
260
my $patrons_found    = $target_patrons->count;
246
my $actually_updated = 0;
261
my $actually_updated = 0;
247
my $testdisplay  = $doit ? "" : "WOULD HAVE ";
262
my $testdisplay      = $doit ? "" : "WOULD HAVE ";
248
if ( $verbose ) {
263
if ($verbose) {
249
    while ( my $target_patron = $target_patrons->next() ){
264
    while ( my $target_patron = $target_patrons->next() ) {
250
    my $target = Koha::Patrons->find( $target_patron->borrowernumber );
265
        my $target = Koha::Patrons->find( $target_patron->borrowernumber );
251
    $verbose and print $testdisplay."Updated ".$target->firstname." ".$target->surname." from $fromcat to $tocat\n";
266
        $verbose
267
          and print $testdisplay
268
          . "Updated "
269
          . $target->firstname . " "
270
          . $target->surname
271
          . " from $fromcat to $tocat\n";
252
    }
272
    }
253
    $target_patrons->reset;
273
    $target_patrons->reset;
254
}
274
}
255
if ( $doit ) {
275
if ($doit) {
256
    $actually_updated = $target_patrons->update_category({to=>$tocat});
276
    $actually_updated = $target_patrons->update_category( { to => $tocat } );
257
}
277
}
258
278
259
$verbose and print "$patrons_found found, $actually_updated updated\n";
279
$verbose and print "$patrons_found found, $actually_updated updated\n";
(-)a/t/db_dependent/Patrons.t (-6 / +5 lines)
Lines 168-177 subtest "Update patron categories" => sub { Link Here
168
    $builder->build({source=>'Accountline',value => {amountoutstanding=>5.01,borrowernumber=>$adult2->{borrowernumber}}});
168
    $builder->build({source=>'Accountline',value => {amountoutstanding=>5.01,borrowernumber=>$adult2->{borrowernumber}}});
169
169
170
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode})->count,3,'Three patrons in child category');
170
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode})->count,3,'Three patrons in child category');
171
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,au=>1})->count,1,'One under age patron in child category');
171
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,ageunder=>1})->count,1,'One under age patron in child category');
172
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,au=>1})->next->borrowernumber,$child1->{borrowernumber},'Under age patron in child category is expected one');
172
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,ageunder=>1})->next->borrowernumber,$child1->{borrowernumber},'Under age patron in child category is expected one');
173
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,ao=>1})->count,1,'One over age patron in child category');
173
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,ageover=>1})->count,1,'One over age patron in child category');
174
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,ao=>1})->next->borrowernumber,$child3->{borrowernumber},'Over age patron in child category is expected one');
174
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,ageover=>1})->next->borrowernumber,$child3->{borrowernumber},'Over age patron in child category is expected one');
175
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{branchcode=>$branchcode2}})->count,1,'One patron in branch 2');
175
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{branchcode=>$branchcode2}})->count,1,'One patron in branch 2');
176
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{branchcode=>$branchcode2}})->next->borrowernumber,$adult2->{borrowernumber},'Adult patron in branch 2 is expected one');
176
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{branchcode=>$branchcode2}})->next->borrowernumber,$adult2->{borrowernumber},'Adult patron in branch 2 is expected one');
177
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,fine_min=>5})->count,1,'One patron with fines over $5');
177
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,fine_min=>5})->count,1,'One patron with fines over $5');
Lines 187-193 subtest "Update patron categories" => sub { Link Here
187
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{'sort1'=>'quack'}})->next->borrowernumber,$adult1->{borrowernumber},'One adult patron with a quack is expected one');
187
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{'sort1'=>'quack'}})->next->borrowernumber,$adult1->{borrowernumber},'One adult patron with a quack is expected one');
188
188
189
    is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,3,'Guarantor has 3 guarantees');
189
    is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,3,'Guarantor has 3 guarantees');
190
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,au=>1})->update_category({to=>$a_categorycode}),1,'One child patron updated to adult category');
190
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode,ageunder=>1})->update_category({to=>$a_categorycode}),1,'One child patron updated to adult category');
191
    is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,2,'Guarantee was removed when made adult');
191
    is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,2,'Guarantee was removed when made adult');
192
192
193
    is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,1,'Guarantor has 1 guarantees');
193
    is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,1,'Guarantor has 1 guarantees');
194
- 

Return to bug 17168