From 2fba65eb4d8fcb61c73c70855a96d915a65e82b0 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 25 Oct 2019 17:02:59 +0100 Subject: [PATCH] Bug 17168: (RM follow-up) Update for bug 14570 Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize --- Koha/Patron.pm | 13 +++++++++--- Koha/Patrons.pm | 16 +------------- t/db_dependent/Patrons.t | 46 +++++++++++++++++++++------------------- 3 files changed, 35 insertions(+), 40 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 67ab9bf7c9..e3a79bb3ae 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -254,11 +254,18 @@ sub store { # Password must be updated using $self->set_password $self->password($self_from_storage->password); - if ( C4::Context->preference('FeeOnChangePatronCategory') - and $self->category->categorycode ne + if ( $self->category->categorycode ne $self_from_storage->category->categorycode ) { - $self->add_enrolment_fee_if_needed(1); + # Add enrolement fee on category change if required + $self->add_enrolment_fee_if_needed(1) + if C4::Context->preference('FeeOnChangePatronCategory'); + + # Clean up guarantors on category change if required + $self->guarantor_relationships->delete + if ( $self->category->category_type ne 'C' + || $self->category->category_type ne 'P' ); + } # Actionlogs diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index 90e134a490..fbe696ec7a 100644 --- a/Koha/Patrons.pm +++ b/Koha/Patrons.pm @@ -424,24 +424,10 @@ call search_patrons_to_update to filter the Koha::Patrons set sub update_category_to { my ( $self, $params ) = @_; - my $to = $params->{category}; - - my $to_cat = Koha::Patron::Categories->find($to); - return unless $to_cat; - my $counter = 0; - my $remove_guarantor = ( $to_cat->category_type ne 'C' || $to_cat->category_type ne 'P' ) ? 1 : 0; while( my $patron = $self->next ) { $counter++; - if ( $remove_guarantor && ($patron->category->category_type eq 'C' || $patron->category->category_type eq 'P') ) { - $patron->guarantorid(0); - $patron->contactname(''); - $patron->contactfirstname(''); - $patron->contacttitle(''); - $patron->relationship(''); - } - $patron->categorycode($to); - $patron->store(); + $patron->categorycode($params->{category})->store(); } return $counter; } diff --git a/t/db_dependent/Patrons.t b/t/db_dependent/Patrons.t index 549a83bbe8..25739c9cbe 100755 --- a/t/db_dependent/Patrons.t +++ b/t/db_dependent/Patrons.t @@ -26,6 +26,7 @@ use Koha::DateUtils; use t::lib::Dates; use t::lib::TestBuilder; +use t::lib::Mocks; BEGIN { use_ok('Koha::Objects'); @@ -106,6 +107,7 @@ foreach my $b ( $patrons->as_list() ) { subtest "Update patron categories" => sub { plan tests => 17; + t::lib::Mocks::mock_preference( 'borrowerRelationship', 'test' ); my $c_categorycode = $builder->build({ source => 'Category', value => { category_type=>'C', upperagelimit=>17, @@ -116,73 +118,73 @@ subtest "Update patron categories" => sub { my $i_categorycode = $builder->build({ source => 'Category', value => {category_type=>'I'} })->{categorycode}; my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode}; my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode}; - my $adult1 = $builder->build({source => 'Borrower', value => { + my $adult1 = $builder->build_object({class => 'Koha::Patrons', value => { categorycode=>$a_categorycode, branchcode=>$branchcode1, dateenrolled=>'2018-01-01', sort1 =>'quack', } }); - my $adult2 = $builder->build({source => 'Borrower', value => { + my $adult2 = $builder->build_object({class => 'Koha::Patrons', value => { categorycode=>$a_categorycode, branchcode=>$branchcode2, dateenrolled=>'2017-01-01', } }); - my $inst = $builder->build({source => 'Borrower', value => { + my $inst = $builder->build_object({class => 'Koha::Patrons', value => { categorycode=>$i_categorycode, branchcode=>$branchcode2, } }); - my $prof = $builder->build({source => 'Borrower', value => { + my $prof = $builder->build_object({class => 'Koha::Patrons', value => { categorycode=>$p_categorycode, branchcode=>$branchcode2, - guarantorid=>$inst->{borrowernumber}, } }); - my $child1 = $builder->build({source => 'Borrower', value => { + $prof->add_guarantor({guarantor_id => $inst->borrowernumber, relationship => 'test'}); + my $child1 = $builder->build_object({class => 'Koha::Patrons', value => { dateofbirth => dt_from_string->add(years=>-4), categorycode=>$c_categorycode, - guarantorid=>$adult1->{borrowernumber}, branchcode=>$branchcode1, } }); - my $child2 = $builder->build({source => 'Borrower', value => { + $child1->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'}); + my $child2 = $builder->build_object({class => 'Koha::Patrons', value => { dateofbirth => dt_from_string->add(years=>-8), categorycode=>$c_categorycode, - guarantorid=>$adult1->{borrowernumber}, branchcode=>$branchcode1, } }); - my $child3 = $builder->build({source => 'Borrower', value => { + $child2->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'}); + my $child3 = $builder->build_object({class => 'Koha::Patrons', value => { dateofbirth => dt_from_string->add(years=>-18), categorycode=>$c_categorycode, - guarantorid=>$adult1->{borrowernumber}, branchcode=>$branchcode1, } }); - $builder->build({source=>'Accountline',value => {amountoutstanding=>4.99,borrowernumber=>$adult1->{borrowernumber}}}); - $builder->build({source=>'Accountline',value => {amountoutstanding=>5.01,borrowernumber=>$adult2->{borrowernumber}}}); + $child3->add_guarantor({guarantor_id => $adult1->borrowernumber, relationship => 'test'}); + $builder->build({source=>'Accountline',value => {amountoutstanding=>4.99,borrowernumber=>$adult1->borrowernumber}}); + $builder->build({source=>'Accountline',value => {amountoutstanding=>5.01,borrowernumber=>$adult2->borrowernumber}}); is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode})->count,3,'Three patrons in child category'); is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_young=>1})->count,1,'One under age patron in child category'); - 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'); + 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'); is( Koha::Patrons->search_patrons_to_update_category({from=>$c_categorycode,too_old=>1})->count,1,'One over age patron in child category'); - 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'); + 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'); is( Koha::Patrons->search({branchcode=>$branchcode2})->search_patrons_to_update_category({from=>$a_categorycode})->count,1,'One patron in branch 2'); - 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'); + 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'); is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_min=>5})->count,1,'One patron with fines over $5'); - 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'); + 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'); is( Koha::Patrons->search_patrons_to_update_category({from=>$a_categorycode,fine_max=>5})->count,1,'One patron with fines under $5'); - 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'); + 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'); - is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,3,'Guarantor has 3 guarantees'); + is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,3,'Guarantor has 3 guarantees'); 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'); - is( Koha::Patrons->find($adult1->{borrowernumber})->guarantees->count,2,'Guarantee was removed when made adult'); + is( Koha::Patrons->find($adult1->borrowernumber)->guarantee_relationships->guarantees->count,2,'Guarantee was removed when made adult'); - is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,1,'Guarantor has 1 guarantees'); + is( Koha::Patrons->find($inst->borrowernumber)->guarantee_relationships->guarantees->count,1,'Guarantor has 1 guarantees'); 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'); - is( Koha::Patrons->find($inst->{borrowernumber})->guarantees->count,0,'Guarantee was removed when made adult'); + is( Koha::Patrons->find($inst->borrowernumber)->guarantee_relationships->guarantees->count,0,'Guarantee was removed when made adult'); }; -- 2.39.5