Bug 29741: Add Koha::Patron->safe_to_delete

This patchset adds a handy method for checking if a patron meets the
conditions to be deleted. This conditions are:

- Has no linked guarantees
- Has no pending debts
- Has no current checkouts
- Is not the system-configured anonymous user

To test:
1. Apply the unit tests patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Patron.t
=> FAIL: Of course heh
3. Apply this patch
4. Repeat 2
=> SUCCESS: Tests pass, conditions are validated and the right string is
returned on each case
5. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Tomás Cohen Arazi 2021-12-20 14:17:49 -03:00 committed by Fridolin Somers
parent b28f35efbf
commit b3f19f1e95

View file

@ -1910,6 +1910,51 @@ sub queue_notice {
return \%return;
}
=head3 safe_to_delete
my $result = $patron->safe_to_delete;
if ( $result eq 'has_guarantees' ) { ... }
elsif ( $result ) { ... }
else { # cannot delete }
This method tells if the Koha:Patron object can be deleted. Possible return values
=over 4
=item 'ok'
=item 'has_checkouts'
=item 'has_debt'
=item 'has_guarantees'
=item 'is_anonymous_patron'
=back
=cut
sub safe_to_delete {
my ($self) = @_;
my $anonymous_patron = C4::Context->preference('AnonymousPatron');
return 'is_anonymous_patron'
if $anonymous_patron && $self->id eq $anonymous_patron;
return 'has_checkouts'
if $self->checkouts->count;
return 'has_debt'
if $self->account->outstanding_debits->total_outstanding > 0;
return 'has_guarantees'
if $self->guarantee_relationships->count;
return 'ok';
}
=head2 Internal methods
=head3 _type