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

(-)a/Koha/Patron.pm (-3 / +10 lines)
Lines 254-264 sub store { Link Here
254
                # Password must be updated using $self->set_password
254
                # Password must be updated using $self->set_password
255
                $self->password($self_from_storage->password);
255
                $self->password($self_from_storage->password);
256
256
257
                if ( C4::Context->preference('FeeOnChangePatronCategory')
257
                if ( $self->category->categorycode ne
258
                    and $self->category->categorycode ne
259
                    $self_from_storage->category->categorycode )
258
                    $self_from_storage->category->categorycode )
260
                {
259
                {
261
                    $self->add_enrolment_fee_if_needed(1);
260
                    # Add enrolement fee on category change if required
261
                    $self->add_enrolment_fee_if_needed(1)
262
                      if C4::Context->preference('FeeOnChangePatronCategory');
263
264
                    # Clean up guarantors on category change if required
265
                    $self->guarantor_relationships->delete
266
                      if ( $self->category->category_type ne 'C'
267
                        || $self->category->category_type ne 'P' );
268
262
                }
269
                }
263
270
264
                # Actionlogs
271
                # Actionlogs
(-)a/Koha/Patrons.pm (-15 / +1 lines)
Lines 424-447 call search_patrons_to_update to filter the Koha::Patrons set Link Here
424
424
425
sub update_category_to {
425
sub update_category_to {
426
    my ( $self, $params ) = @_;
426
    my ( $self, $params ) = @_;
427
    my $to = $params->{category};
428
429
    my $to_cat = Koha::Patron::Categories->find($to);
430
    return unless $to_cat;
431
432
    my $counter = 0;
427
    my $counter = 0;
433
    my $remove_guarantor = ( $to_cat->category_type ne 'C' || $to_cat->category_type ne 'P' ) ? 1 : 0;
434
    while( my $patron = $self->next ) {
428
    while( my $patron = $self->next ) {
435
        $counter++;
429
        $counter++;
436
        if ( $remove_guarantor && ($patron->category->category_type eq 'C' || $patron->category->category_type eq 'P') ) {
430
        $patron->categorycode($params->{category})->store();
437
            $patron->guarantorid(0);
438
            $patron->contactname('');
439
            $patron->contactfirstname('');
440
            $patron->contacttitle('');
441
            $patron->relationship('');
442
        }
443
        $patron->categorycode($to);
444
        $patron->store();
445
    }
431
    }
446
    return $counter;
432
    return $counter;
447
}
433
}
(-)a/t/db_dependent/Patrons.t (-23 / +24 lines)
Lines 26-31 use Koha::DateUtils; Link Here
26
26
27
use t::lib::Dates;
27
use t::lib::Dates;
28
use t::lib::TestBuilder;
28
use t::lib::TestBuilder;
29
use t::lib::Mocks;
29
30
30
BEGIN {
31
BEGIN {
31
    use_ok('Koha::Objects');
32
    use_ok('Koha::Objects');
Lines 106-111 foreach my $b ( $patrons->as_list() ) { Link Here
106
107
107
subtest "Update patron categories" => sub {
108
subtest "Update patron categories" => sub {
108
    plan tests => 17;
109
    plan tests => 17;
110
    t::lib::Mocks::mock_preference( 'borrowerRelationship', 'test' );
109
    my $c_categorycode = $builder->build({ source => 'Category', value => {
111
    my $c_categorycode = $builder->build({ source => 'Category', value => {
110
            category_type=>'C',
112
            category_type=>'C',
111
            upperagelimit=>17,
113
            upperagelimit=>17,
Lines 116-188 subtest "Update patron categories" => sub { Link Here
116
    my $i_categorycode = $builder->build({ source => 'Category', value => {category_type=>'I'} })->{categorycode};
118
    my $i_categorycode = $builder->build({ source => 'Category', value => {category_type=>'I'} })->{categorycode};
117
    my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode};
119
    my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode};
118
    my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
120
    my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
119
    my $adult1 = $builder->build({source => 'Borrower', value => {
121
    my $adult1 = $builder->build_object({class => 'Koha::Patrons', value => {
120
            categorycode=>$a_categorycode,
122
            categorycode=>$a_categorycode,
121
            branchcode=>$branchcode1,
123
            branchcode=>$branchcode1,
122
            dateenrolled=>'2018-01-01',
124
            dateenrolled=>'2018-01-01',
123
            sort1 =>'quack',
125
            sort1 =>'quack',
124
        }
126
        }
125
    });
127
    });
126
    my $adult2 = $builder->build({source => 'Borrower', value => {
128
    my $adult2 = $builder->build_object({class => 'Koha::Patrons', value => {
127
            categorycode=>$a_categorycode,
129
            categorycode=>$a_categorycode,
128
            branchcode=>$branchcode2,
130
            branchcode=>$branchcode2,
129
            dateenrolled=>'2017-01-01',
131
            dateenrolled=>'2017-01-01',
130
        }
132
        }
131
    });
133
    });
132
    my $inst = $builder->build({source => 'Borrower', value => {
134
    my $inst = $builder->build_object({class => 'Koha::Patrons', value => {
133
            categorycode=>$i_categorycode,
135
            categorycode=>$i_categorycode,
134
            branchcode=>$branchcode2,
136
            branchcode=>$branchcode2,
135
        }
137
        }
136
    });
138
    });
137
    my $prof = $builder->build({source => 'Borrower', value => {
139
    my $prof = $builder->build_object({class => 'Koha::Patrons', value => {
138
            categorycode=>$p_categorycode,
140
            categorycode=>$p_categorycode,
139
            branchcode=>$branchcode2,
141
            branchcode=>$branchcode2,
140
            guarantorid=>$inst->{borrowernumber},
141
        }
142
        }
142
    });
143
    });
143
    my $child1 = $builder->build({source => 'Borrower', value => {
144
    $prof->add_guarantor({guarantor_id => $inst->borrowernumber, relationship => 'test'});
145
    my $child1 = $builder->build_object({class => 'Koha::Patrons', value => {
144
            dateofbirth => dt_from_string->add(years=>-4),
146
            dateofbirth => dt_from_string->add(years=>-4),
145
            categorycode=>$c_categorycode,
147
            categorycode=>$c_categorycode,
146
            guarantorid=>$adult1->{borrowernumber},
147
            branchcode=>$branchcode1,
148
            branchcode=>$branchcode1,
148
        }
149
        }
149
    });
150
    });
150
    my $child2 = $builder->build({source => 'Borrower', value => {
151
    $child1->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'});
152
    my $child2 = $builder->build_object({class => 'Koha::Patrons', value => {
151
            dateofbirth => dt_from_string->add(years=>-8),
153
            dateofbirth => dt_from_string->add(years=>-8),
152
            categorycode=>$c_categorycode,
154
            categorycode=>$c_categorycode,
153
            guarantorid=>$adult1->{borrowernumber},
154
            branchcode=>$branchcode1,
155
            branchcode=>$branchcode1,
155
        }
156
        }
156
    });
157
    });
157
    my $child3 = $builder->build({source => 'Borrower', value => {
158
    $child2->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'});
159
    my $child3 = $builder->build_object({class => 'Koha::Patrons', value => {
158
            dateofbirth => dt_from_string->add(years=>-18),
160
            dateofbirth => dt_from_string->add(years=>-18),
159
            categorycode=>$c_categorycode,
161
            categorycode=>$c_categorycode,
160
            guarantorid=>$adult1->{borrowernumber},
161
            branchcode=>$branchcode1,
162
            branchcode=>$branchcode1,
162
        }
163
        }
163
    });
164
    });
164
    $builder->build({source=>'Accountline',value => {amountoutstanding=>4.99,borrowernumber=>$adult1->{borrowernumber}}});
165
    $child3->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'});
165
    $builder->build({source=>'Accountline',value => {amountoutstanding=>5.01,borrowernumber=>$adult2->{borrowernumber}}});
166
    $builder->build({source=>'Accountline',value => {amountoutstanding=>4.99,borrowernumber=>$adult1->borrowernumber}});
167
    $builder->build({source=>'Accountline',value => {amountoutstanding=>5.01,borrowernumber=>$adult2->borrowernumber}});
166
168
167
    is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode})->count,3,'Three patrons in child category');
169
    is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode})->count,3,'Three patrons 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');
170
    is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_young=>1})->count,1,'One under age patron in child category');
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');
171
    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');
170
    is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_old=>1})->count,1,'One over age patron in child category');
172
    is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_old=>1})->count,1,'One over age patron in child category');
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');
173
    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');
172
    is( Koha::Patrons->search({branchcode=>$branchcode2})->search_patrons_to_update_category({from=>$a_categorycode})->count,1,'One patron in branch 2');
174
    is( Koha::Patrons->search({branchcode=>$branchcode2})->search_patrons_to_update_category({from=>$a_categorycode})->count,1,'One patron in branch 2');
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');
175
    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');
174
    is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_min=>5})->count,1,'One patron with fines over $5');
176
    is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_min=>5})->count,1,'One patron with fines over $5');
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');
177
    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');
176
    is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_max=>5})->count,1,'One patron with fines under $5');
178
    is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_max=>5})->count,1,'One patron with fines under $5');
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');
179
    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');
178
180
179
    is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,3,'Guarantor has 3 guarantees');
181
    is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,3,'Guarantor has 3 guarantees');
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');
182
    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');
181
    is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,2,'Guarantee was removed when made adult');
183
    is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,2,'Guarantee was removed when made adult');
182
184
183
    is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,1,'Guarantor has 1 guarantees');
185
    is( Koha::Patrons->find($inst->borrowernumber)->guarantee_relationships->guarantees->count,1,'Guarantor has 1 guarantees');
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');
186
    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');
185
    is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,0,'Guarantee was removed when made adult');
187
    is( Koha::Patrons->find($inst->borrowernumber)->guarantee_relationships->guarantees->count,0,'Guarantee was removed when made adult');
186
188
187
};
189
};
188
190
189
- 

Return to bug 17168