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

(-)a/Koha/Patrons.pm (-31 / +29 lines)
Lines 238-272 sub delete { Link Here
238
    return $patrons_deleted;
238
    return $patrons_deleted;
239
}
239
}
240
240
241
=head3 search_patrons_to_update
241
=head3 search_patrons_to_update_category
242
242
243
    my $patrons = Koha::Patrons->search_patrons_to_anonymise( {
243
    my $patrons = Koha::Patrons->search_patrons_to_update_category( {
244
                      from => $from_category,
244
                      from          => $from_category,
245
                      fine_max => $fine_max,
245
                      fine_max      => $fine_max,
246
                      fine_min  => $fin_min,
246
                      fine_min      => $fin_min,
247
                      au     => $au,
247
                      too_young     => $too_young,
248
                      ao    => $ao,
248
                      too_old      => $too_old,
249
                  });
249
                  });
250
250
251
This method returns all patron who should be updated form one category to another meeting criteria:
251
This method returns all patron who should be updated from one category to another meeting criteria:
252
252
253
from - original category
253
from          - borrower categorycode
254
fine_min - with fines totaling at least this amount
254
fine_min      - with fines totaling at least this amount
255
fine_max - with fines above this amount
255
fine_max      - with fines above this amount
256
au - under the age limit for 'from'
256
too_young     - if passed, select patrons who are under the age limit for the current category
257
ao - over the agelimit for 'from'
257
too_old       - if passed, select patrons who are over the age limit for the current category
258
258
259
=cut
259
=cut
260
260
261
sub search_patrons_to_update {
261
sub search_patrons_to_update_category {
262
    my ( $self, $params ) = @_;
262
    my ( $self, $params ) = @_;
263
    my %query;
263
    my %query;
264
    my $search_params = $params->{search_params};;
264
    my $search_params;
265
265
266
    my $cat_from = Koha::Patron::Categories->find($params->{from});
266
    my $cat_from = Koha::Patron::Categories->find($params->{from});
267
    $search_params->{categorycode}=$params->{from};
267
    $search_params->{categorycode}=$params->{from};
268
    if ($params->{ageover} || $params->{ageunder}){
268
    if ($params->{too_young} || $params->{too_old}){
269
        if( $cat_from->dateofbirthrequired && $params->{ageunder} ) {
269
        if( $cat_from->dateofbirthrequired && $params->{too_young} ) {
270
            my $date_after = output_pref({
270
            my $date_after = output_pref({
271
                dt         => dt_from_string()->subtract( years => $cat_from->dateofbirthrequired),
271
                dt         => dt_from_string()->subtract( years => $cat_from->dateofbirthrequired),
272
                dateonly   => 1,
272
                dateonly   => 1,
Lines 274-280 sub search_patrons_to_update { Link Here
274
            });
274
            });
275
            $search_params->{dateofbirth}{'>'} = $date_after;
275
            $search_params->{dateofbirth}{'>'} = $date_after;
276
        }
276
        }
277
        if( $cat_from->upperagelimit && $params->{ageover} ) {
277
        if( $cat_from->upperagelimit && $params->{too_old} ) {
278
            my $date_before = output_pref({
278
            my $date_before = output_pref({
279
                dt         => dt_from_string()->subtract( years => $cat_from->upperagelimit),
279
                dt         => dt_from_string()->subtract( years => $cat_from->upperagelimit),
280
                dateonly   => 1,
280
                dateonly   => 1,
Lines 285-314 sub search_patrons_to_update { Link Here
285
    }
285
    }
286
    if ($params->{fine_min} || $params->{fine_max}) {
286
    if ($params->{fine_min} || $params->{fine_max}) {
287
        $query{join} = ["accountlines"];
287
        $query{join} = ["accountlines"];
288
        $query{select} = ["borrowernumber", { sum => 'amountoutstanding', -as => 'total_fines'} ];
288
        $query{select} = ["borrowernumber", "accountlines.amountoutstanding" ];
289
        $query{as} = [qw/borrowernumber  total_fines/];
290
        $query{group_by} = ["borrowernumber"];
289
        $query{group_by} = ["borrowernumber"];
291
        $query{having}{total_fines}{'<='}=$params->{fine_max} if defined $params->{fine_max};
290
        $query{having} = \['sum(accountlines.amountoutstanding) <= ?',$params->{fine_max}] if defined $params->{fine_max};
292
        $query{having}{total_fines}{'>='}=$params->{fine_min} if defined $params->{fine_min};
291
        $query{having} = \['sum(accountlines.amountoutstanding) >= ?',$params->{fine_min}] if defined $params->{fine_min};
293
    }
292
    }
294
    return Koha::Patrons->search($search_params,\%query);
293
    return $self->search($search_params,\%query);
295
}
294
}
296
295
297
=head3 update_category
296
=head3 update_category_to
298
297
299
    Koha::Patrons->search->update_category( {
298
    Koha::Patrons->search->update_category_to( {
300
            to   => $to,
299
            category   => $to_category,
301
        });
300
        });
302
301
303
Update supplied patrons from one category to another and take care of guarantor info.
302
Update supplied patrons from current category to another and take care of guarantor info.
304
To make sure all the conditions are met, the caller has the responsibility to
303
To make sure all the conditions are met, the caller has the responsibility to
305
call search_patrons_to_update to filter the Koha::Patrons set
304
call search_patrons_to_update to filter the Koha::Patrons set
306
305
307
=cut
306
=cut
308
307
309
sub update_category {
308
sub update_category_to {
310
    my ( $self, $params ) = @_;
309
    my ( $self, $params ) = @_;
311
    my $to = $params->{to};
310
    my $to = $params->{category};
312
311
313
    my $to_cat = Koha::Patron::Categories->find($to);
312
    my $to_cat = Koha::Patron::Categories->find($to);
314
    return unless $to_cat;
313
    return unless $to_cat;
Lines 328-334 sub update_category { Link Here
328
        $patron->store();
327
        $patron->store();
329
    }
328
    }
330
    return $counter;
329
    return $counter;
331
>>>>>>> Bug 17168: Add a command line script for updating patron category based on status
332
}
330
}
333
331
334
=head3 _type
332
=head3 _type
(-)a/misc/cronjobs/update_patrons_category.pl (-12 / +12 lines)
Lines 40-46 update_patrons_category.pl - Given a set of parameters update selected patrons f Link Here
40
=head1 SYNOPSIS
40
=head1 SYNOPSIS
41
41
42
update_patrons_category.pl -f=categorycode -t=categorycode
42
update_patrons_category.pl -f=categorycode -t=categorycode
43
                          [-b=branchcode] [-au] [-ao] [-fo=X] [-fu=X]
43
                          [-b=branchcode] [--too_old] [--too_young] [-fo=X] [-fu=X]
44
                          [-rb=date] [-ra=date] [-v]
44
                          [-rb=date] [-ra=date] [-v]
45
                          [--field column=value ...]
45
                          [--field column=value ...]
46
46
Lines 49-61 update_patrons_category.pl --help | --man Link Here
49
Options:
49
Options:
50
   --help                   brief help message
50
   --help                   brief help message
51
   --man                    full documentation
51
   --man                    full documentation
52
   -ao --ageover            update if over  maximum age for current category
52
   -too_old                 update if over  maximum age for current category
53
   -au --ageunder           update if under minimuum age  current category
53
   -too_young               update if under minimuum age  current category
54
   -fo=X --fineover=X       update if fines over  X amount
54
   -fo=X --fineover=X       update if fines over  X amount
55
   -fu=X --fineunder=X      update if fines under 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
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
57
   -ra=date --regafter      update if registration date is after a given date
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>
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              verbose mode
59
   -v -verbose              verbose mode
60
   -c --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 --branch <branchname> only deal with patrons from this library/branch
61
   -b --branch <branchname> only deal with patrons from this library/branch
Lines 95-105 branches.branchcode table. Link Here
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<--ageover | -ao>
98
=item B<--too_old>
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<--ageunde | -au>
102
=item B<--too_young>
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
Lines 139-145 C<update_patron_categories.pl> - Suggests that you read this help. :) Link Here
139
139
140
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.
140
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.
141
141
142
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.
142
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.
143
143
144
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.
144
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.
145
145
Lines 171-178 GetOptions( Link Here
171
    'c|confirm'       => \$doit,
171
    'c|confirm'       => \$doit,
172
    'f|from=s'        => \$fromcat,
172
    'f|from=s'        => \$fromcat,
173
    't|to=s'          => \$tocat,
173
    't|to=s'          => \$tocat,
174
    'ao|ageover'      => \$ageover,
174
    'too_old'         => \$ageover,
175
    'au|ageunder'     => \$ageunder,
175
    'too_young'       => \$ageunder,
176
    'fo|finesover=s'  => \$fine_min,
176
    'fo|finesover=s'  => \$fine_min,
177
    'fu|finesunder=s' => \$fine_max,
177
    'fu|finesunder=s' => \$fine_max,
178
    'rb|regbefore=s'  => \$reg_bef,
178
    'rb|regbefore=s'  => \$reg_bef,
Lines 244-255 while ( my ( $key, $value ) = each %fields ) { Link Here
244
    $params{ "me." . $key } = $value;
244
    $params{ "me." . $key } = $value;
245
}
245
}
246
246
247
my $target_patrons = Koha::Patrons->search_patrons_to_update(
247
my $target_patrons = Koha::Patrons->search({ search_params => \%params })->search_patrons_to_update(
248
    {
248
    {
249
        from          => $fromcat,
249
        from          => $fromcat,
250
        search_params => \%params,
250
        search_params => \%params,
251
        ageunder      => $ageunder,
251
        too_young     => $ageunder,
252
        ageover       => $ageover,
252
        too_old       => $ageover,
253
        fine_min      => $fine_min,
253
        fine_min      => $fine_min,
254
        fine_max      => $fine_max,
254
        fine_max      => $fine_max,
255
    }
255
    }
(-)a/t/db_dependent/Patrons.t (-25 / +14 lines)
Lines 105-114 foreach my $b ( $patrons->as_list() ) { Link Here
105
}
105
}
106
106
107
subtest "Update patron categories" => sub {
107
subtest "Update patron categories" => sub {
108
    plan tests => 23;
108
    plan tests => 17;
109
    $builder->schema->resultset( 'Issue' )->delete_all;
110
    $builder->schema->resultset( 'Borrower' )->delete_all;
111
    $builder->schema->resultset( 'Category' )->delete_all;
112
    my $c_categorycode = $builder->build({ source => 'Category', value => {
109
    my $c_categorycode = $builder->build({ source => 'Category', value => {
113
            category_type=>'C',
110
            category_type=>'C',
114
            upperagelimit=>17,
111
            upperagelimit=>17,
Lines 167-197 subtest "Update patron categories" => sub { Link Here
167
    $builder->build({source=>'Accountline',value => {amountoutstanding=>4.99,borrowernumber=>$adult1->{borrowernumber}}});
164
    $builder->build({source=>'Accountline',value => {amountoutstanding=>4.99,borrowernumber=>$adult1->{borrowernumber}}});
168
    $builder->build({source=>'Accountline',value => {amountoutstanding=>5.01,borrowernumber=>$adult2->{borrowernumber}}});
165
    $builder->build({source=>'Accountline',value => {amountoutstanding=>5.01,borrowernumber=>$adult2->{borrowernumber}}});
169
166
170
    is( Koha::Patrons->search_patrons_to_update({from=>$c_categorycode})->count,3,'Three patrons in child category');
167
    is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode})->count,3,'Three patrons 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');
168
    is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_young=>1})->count,1,'One under age patron in child category');
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');
169
    is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_young=>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,ageover=>1})->count,1,'One over age patron in child category');
170
    is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_old=>1})->count,1,'One over age patron in child category');
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');
171
    is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_old=>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');
172
    is( Koha::Patrons->search({branchcode=>$branchcode2})->search_patrons_to_update_category({from=>$a_categorycode})->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');
173
    is( Koha::Patrons->search({branchcode=>$branchcode2})->search_patrons_to_update_category({from=>$a_categorycode})->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');
174
    is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_min=>5})->count,1,'One patron with fines over $5');
178
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,fine_min=>5})->next->borrowernumber,$adult2->{borrowernumber},'One patron with fines over $5 is expected one');
175
    is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_min=>5})->next->borrowernumber,$adult2->{borrowernumber},'One patron with fines over $5 is expected one');
179
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,fine_max=>5})->count,1,'One patron with fines under $5');
176
    is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_max=>5})->count,1,'One patron with fines under $5');
180
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,fine_max=>5})->next->borrowernumber,$adult1->{borrowernumber},'One patron with fines under $5 is expected one');
177
    is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_max=>5})->next->borrowernumber,$adult1->{borrowernumber},'One patron with fines under $5 is expected one');
181
182
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{dateenrolled=>{'<'=>'2018-01-01'}}})->count,1,'One adult patron enrolled before date');
183
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{dateenrolled=>{'<'=>'2018-01-01'}}})->next->borrowernumber,$adult2->{borrowernumber},'One adult patron enrolled before date is expected one');
184
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{dateenrolled=>{'>'=>'2017-01-01'}}})->count,1,'One adult patron enrolled after date');
185
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{dateenrolled=>{'>'=>'2017-01-01'}}})->next->borrowernumber,$adult1->{borrowernumber},'One adult patron enrolled after date is expected one');
186
    is( Koha::Patrons->search_patrons_to_update({from=>$a_categorycode,search_params=>{'sort1'=>'quack'}})->count,1,'One adult patron has a quack');
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
178
189
    is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,3,'Guarantor has 3 guarantees');
179
    is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,3,'Guarantor has 3 guarantees');
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');
180
    is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_young=>1})->update_category_to({category=>$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');
181
    is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,2,'Guarantee was removed when made adult');
192
182
193
    is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,1,'Guarantor has 1 guarantees');
183
    is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,1,'Guarantor has 1 guarantees');
194
    is( Koha::Patrons->search_patrons_to_update({from=>$p_categorycode})->update_category({to=>$a_categorycode}),1,'One professional patron updated to adult category');
184
    is( Koha::Patrons->search_patrons_to_update_category({from=>$p_categorycode})->update_category_to({category=>$a_categorycode}),1,'One professional patron updated to adult category');
195
    is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,0,'Guarantee was removed when made adult');
185
    is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,0,'Guarantee was removed when made adult');
196
186
197
};
187
};
198
- 

Return to bug 17168