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

(-)a/Koha/Patrons.pm (-29 / +29 lines)
Lines 360-395 sub anonymize { Link Here
360
    if( $params->{verbose} ) {
360
    if( $params->{verbose} ) {
361
        warn "Anonymized $count patrons\n";
361
        warn "Anonymized $count patrons\n";
362
    }
362
    }
363
}
363
364
364
=head3 search_patrons_to_update
365
=head3 search_patrons_to_update_category
365
366
366
    my $patrons = Koha::Patrons->search_patrons_to_anonymise( {
367
    my $patrons = Koha::Patrons->search_patrons_to_update_category( {
367
                      from => $from_category,
368
                      from          => $from_category,
368
                      fine_max => $fine_max,
369
                      fine_max      => $fine_max,
369
                      fine_min  => $fin_min,
370
                      fine_min      => $fin_min,
370
                      au     => $au,
371
                      too_young     => $too_young,
371
                      ao    => $ao,
372
                      too_old      => $too_old,
372
                  });
373
                  });
373
374
374
This method returns all patron who should be updated form one category to another meeting criteria:
375
This method returns all patron who should be updated from one category to another meeting criteria:
375
376
376
from - original category
377
from          - borrower categorycode
377
fine_min - with fines totaling at least this amount
378
fine_min      - with fines totaling at least this amount
378
fine_max - with fines above this amount
379
fine_max      - with fines above this amount
379
au - under the age limit for 'from'
380
too_young     - if passed, select patrons who are under the age limit for the current category
380
ao - over the agelimit for 'from'
381
too_old       - if passed, select patrons who are over the age limit for the current category
381
382
382
=cut
383
=cut
383
384
384
sub search_patrons_to_update {
385
sub search_patrons_to_update_category {
385
    my ( $self, $params ) = @_;
386
    my ( $self, $params ) = @_;
386
    my %query;
387
    my %query;
387
    my $search_params = $params->{search_params};;
388
    my $search_params;
388
389
389
    my $cat_from = Koha::Patron::Categories->find($params->{from});
390
    my $cat_from = Koha::Patron::Categories->find($params->{from});
390
    $search_params->{categorycode}=$params->{from};
391
    $search_params->{categorycode}=$params->{from};
391
    if ($params->{ageover} || $params->{ageunder}){
392
    if ($params->{too_young} || $params->{too_old}){
392
        if( $cat_from->dateofbirthrequired && $params->{ageunder} ) {
393
        if( $cat_from->dateofbirthrequired && $params->{too_young} ) {
393
            my $date_after = output_pref({
394
            my $date_after = output_pref({
394
                dt         => dt_from_string()->subtract( years => $cat_from->dateofbirthrequired),
395
                dt         => dt_from_string()->subtract( years => $cat_from->dateofbirthrequired),
395
                dateonly   => 1,
396
                dateonly   => 1,
Lines 397-403 sub search_patrons_to_update { Link Here
397
            });
398
            });
398
            $search_params->{dateofbirth}{'>'} = $date_after;
399
            $search_params->{dateofbirth}{'>'} = $date_after;
399
        }
400
        }
400
        if( $cat_from->upperagelimit && $params->{ageover} ) {
401
        if( $cat_from->upperagelimit && $params->{too_old} ) {
401
            my $date_before = output_pref({
402
            my $date_before = output_pref({
402
                dt         => dt_from_string()->subtract( years => $cat_from->upperagelimit),
403
                dt         => dt_from_string()->subtract( years => $cat_from->upperagelimit),
403
                dateonly   => 1,
404
                dateonly   => 1,
Lines 408-437 sub search_patrons_to_update { Link Here
408
    }
409
    }
409
    if ($params->{fine_min} || $params->{fine_max}) {
410
    if ($params->{fine_min} || $params->{fine_max}) {
410
        $query{join} = ["accountlines"];
411
        $query{join} = ["accountlines"];
411
        $query{select} = ["borrowernumber", { sum => 'amountoutstanding', -as => 'total_fines'} ];
412
        $query{select} = ["borrowernumber", "accountlines.amountoutstanding" ];
412
        $query{as} = [qw/borrowernumber  total_fines/];
413
        $query{group_by} = ["borrowernumber"];
413
        $query{group_by} = ["borrowernumber"];
414
        $query{having}{total_fines}{'<='}=$params->{fine_max} if defined $params->{fine_max};
414
        $query{having} = \['sum(accountlines.amountoutstanding) <= ?',$params->{fine_max}] if defined $params->{fine_max};
415
        $query{having}{total_fines}{'>='}=$params->{fine_min} if defined $params->{fine_min};
415
        $query{having} = \['sum(accountlines.amountoutstanding) >= ?',$params->{fine_min}] if defined $params->{fine_min};
416
    }
416
    }
417
    return Koha::Patrons->search($search_params,\%query);
417
    return $self->search($search_params,\%query);
418
}
418
}
419
419
420
=head3 update_category
420
=head3 update_category_to
421
421
422
    Koha::Patrons->search->update_category( {
422
    Koha::Patrons->search->update_category_to( {
423
            to   => $to,
423
            category   => $to_category,
424
        });
424
        });
425
425
426
Update supplied patrons from one category to another and take care of guarantor info.
426
Update supplied patrons from current category to another and take care of guarantor info.
427
To make sure all the conditions are met, the caller has the responsibility to
427
To make sure all the conditions are met, the caller has the responsibility to
428
call search_patrons_to_update to filter the Koha::Patrons set
428
call search_patrons_to_update to filter the Koha::Patrons set
429
429
430
=cut
430
=cut
431
431
432
sub update_category {
432
sub update_category_to {
433
    my ( $self, $params ) = @_;
433
    my ( $self, $params ) = @_;
434
    my $to = $params->{to};
434
    my $to = $params->{category};
435
435
436
    my $to_cat = Koha::Patron::Categories->find($to);
436
    my $to_cat = Koha::Patron::Categories->find($to);
437
    return unless $to_cat;
437
    return unless $to_cat;
(-)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