From b3f19f1e95fff652fdc889e01ff2f75b38055bae Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 20 Dec 2021 14:17:49 -0300 Subject: [PATCH] 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 Signed-off-by: David Nind Signed-off-by: Jonathan Druart Signed-off-by: Fridolin Somers --- Koha/Patron.pm | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 85d9c01bf1..7a70ecc2dd 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -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 -- 2.39.5