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

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