diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 70cbe3ab7f..104c3a00e6 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -315,7 +315,7 @@ sub store { } foreach my $guarantor (@$guarantors) { - if ( $guarantor->is_child or $guarantor->category->can_be_guarantee ) { + if ( $guarantor->is_child ) { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 ); } } @@ -372,7 +372,7 @@ sub store { } foreach my $guarantor (@$guarantors) { - if ( $guarantor->is_child or $guarantor->category->can_be_guarantee ) { + if ( $guarantor->is_child ) { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( invalid_guarantor => 1 ); } } @@ -533,6 +533,18 @@ sub guarantor_relationships { return Koha::Patron::Relationships->search( { guarantee_id => $self->id } ); } +=head3 is_guarantee + +Returns true if the patron has a guarantor. + +=cut + +sub is_guarantee { + my ($self) = @_; + return $self->guarantor_relationships()->count(); +} + + =head3 guarantee_relationships Returns Koha::Patron::Relationships object for this patron's guarantors @@ -557,6 +569,18 @@ sub guarantee_relationships { ); } +=head3 is_guarantor + +Returns true if the patron is a guarantor. + +=cut + +sub is_guarantor { + my ($self) = @_; + return $self->guarantee_relationships()->count(); +} + + =head3 relationships_debt Returns the amount owed by the patron's guarantors *and* the other guarantees of those guarantors diff --git a/members/memberentry.pl b/members/memberentry.pl index f647234691..17c1f0305a 100755 --- a/members/memberentry.pl +++ b/members/memberentry.pl @@ -280,7 +280,7 @@ if ( ( $op eq 'cud-save' || $op eq 'cud-insert' ) } foreach my $guarantor (@guarantors) { - if ( ( $op eq 'cud-save' || $op eq 'cud-insert' ) && $guarantor->is_child || $guarantor->category->can_be_guarantee ) { + if ( ( $op eq 'cud-save' || $op eq 'cud-insert' ) && ($guarantor->is_child ) || $guarantor->is_guarantee || $patron->is_guarantor) { push @errors, 'ERROR_guarantor_is_guarantee'; } } diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index bad1cc1bfa..8df5658384 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 33; +use Test::More tests => 34; use Test::Exception; use Test::Warn; use Time::Fake; @@ -410,6 +410,51 @@ subtest 'add_guarantor() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'guarantor checks on patron creation / update tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + t::lib::Mocks::mock_preference( 'borrowerRelationship', 'guarantor' ); + my $category = $builder->build_object( + { class => 'Koha::Patron::Categories', value => { can_be_guarantee => 1, category_type => 'A' } } ); + + my $guarantor = + $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $category->categorycode } } ); + my $guarantee = + $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $category->categorycode } } ); + + subtest 'patron update tests' => sub { + plan tests => 4; + ok( + $guarantee->add_guarantor( { guarantor_id => $guarantor->borrowernumber, relationship => 'guarantor' } ), + "Relationship is added, no problem" + ); + is( $guarantor->guarantee_relationships->count, 1, 'Relationship added' ); + ok( $guarantor->surname("Duck")->store(), "Updating guarantor is okay" ); + ok( $guarantee->surname("Duck")->store(), "Updating guarantee is okay" ); + }; + + subtest 'patron creation tests' => sub { + plan tests => 1; + + my $new_guarantee = $builder->build_object( + { class => 'Koha::Patrons', value => { categorycode => $category->categorycode } } ); + my $new_guarantee_hash = $new_guarantee->unblessed; + $new_guarantee->delete(); + + delete $new_guarantee_hash->{borrowernumber}; + + ok( + Koha::Patron->new($new_guarantee_hash)->store( { guarantors => [$guarantor] } ), + "We can add the new patron and indicate guarantor" + ); + }; + + $schema->storage->txn_rollback; +}; + subtest 'relationships_debt() tests' => sub { plan tests => 168;