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

(-)a/Koha/Patron.pm (-10 / +18 lines)
Lines 48-53 use Koha::Patron::Modifications; Link Here
48
use Koha::Patron::Relationships;
48
use Koha::Patron::Relationships;
49
use Koha::Patrons;
49
use Koha::Patrons;
50
use Koha::Plugins;
50
use Koha::Plugins;
51
use Koha::Result::Boolean;
51
use Koha::Subscription::Routinglists;
52
use Koha::Subscription::Routinglists;
52
use Koha::Token;
53
use Koha::Token;
53
use Koha::Virtualshelves;
54
use Koha::Virtualshelves;
Lines 1940-1958 sub safe_to_delete { Link Here
1940
1941
1941
    my $anonymous_patron = C4::Context->preference('AnonymousPatron');
1942
    my $anonymous_patron = C4::Context->preference('AnonymousPatron');
1942
1943
1943
    return 'is_anonymous_patron'
1944
    my $error;
1944
        if $anonymous_patron && $self->id eq $anonymous_patron;
1945
1945
1946
    return 'has_checkouts'
1946
    if ( $anonymous_patron && $self->id eq $anonymous_patron ) {
1947
        if $self->checkouts->count;
1947
        $error = 'is_anonymous_patron';
1948
1948
    }
1949
    return 'has_debt'
1949
    elsif ( $self->checkouts->count ) {
1950
        if $self->account->outstanding_debits->total_outstanding > 0;
1950
        $error = 'has_checkouts';
1951
    }
1952
    elsif ( $self->account->outstanding_debits->total_outstanding > 0 ) {
1953
        $error = 'has_debt';
1954
    }
1955
    elsif ( $self->guarantee_relationships->count ) {
1956
        $error = 'has_guarantees';
1957
    }
1951
1958
1952
    return 'has_guarantees'
1959
    if ( $error ) {
1953
        if $self->guarantee_relationships->count;
1960
        return Koha::Result::Boolean->new(0)->add_message({ message => $error });
1961
    }
1954
1962
1955
    return 'ok';
1963
    return Koha::Result::Boolean->new(1);
1956
}
1964
}
1957
1965
1958
=head2 Internal methods
1966
=head2 Internal methods
(-)a/t/db_dependent/Koha/Patron.t (-7 / +21 lines)
Lines 849-855 subtest 'article_requests() tests' => sub { Link Here
849
849
850
subtest 'safe_to_delete() tests' => sub {
850
subtest 'safe_to_delete() tests' => sub {
851
851
852
    plan tests => 5;
852
    plan tests => 14;
853
853
854
    $schema->storage->txn_begin;
854
    $schema->storage->txn_begin;
855
855
Lines 858-864 subtest 'safe_to_delete() tests' => sub { Link Here
858
    ## Make it the anonymous
858
    ## Make it the anonymous
859
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $patron->id );
859
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $patron->id );
860
860
861
    is( $patron->safe_to_delete, 'is_anonymous_patron', 'Cannot delete, it is the anonymous patron' );
861
    ok( !$patron->safe_to_delete, 'Cannot delete, it is the anonymous patron' );
862
    my $message = $patron->safe_to_delete->messages->[0];
863
    is( $message->type, 'error', 'Type is error' );
864
    is( $message->message, 'is_anonymous_patron', 'Cannot delete, it is the anonymous patron' );
862
    # cleanup
865
    # cleanup
863
    t::lib::Mocks::mock_preference( 'AnonymousPatron', 0 );
866
    t::lib::Mocks::mock_preference( 'AnonymousPatron', 0 );
864
867
Lines 870-876 subtest 'safe_to_delete() tests' => sub { Link Here
870
        }
873
        }
871
    );
874
    );
872
875
873
    is( $patron->safe_to_delete, 'has_checkouts', 'Cannot delete, has checkouts' );
876
    ok( !$patron->safe_to_delete, 'Cannot delete, has checkouts' );
877
    $message = $patron->safe_to_delete->messages->[0];
878
    is( $message->type, 'error', 'Type is error' );
879
    is( $message->message, 'has_checkouts', 'Cannot delete, has checkouts' );
874
    # cleanup
880
    # cleanup
875
    $checkout->delete;
881
    $checkout->delete;
876
882
Lines 879-897 subtest 'safe_to_delete() tests' => sub { Link Here
879
    $builder->build_object({ class => 'Koha::Patrons' })
885
    $builder->build_object({ class => 'Koha::Patrons' })
880
            ->add_guarantor({ guarantor_id => $patron->id, relationship => 'parent' });
886
            ->add_guarantor({ guarantor_id => $patron->id, relationship => 'parent' });
881
887
882
    is( $patron->safe_to_delete, 'has_guarantees', 'Cannot delete, has guarantees' );
888
    ok( !$patron->safe_to_delete, 'Cannot delete, has guarantees' );
889
    $message = $patron->safe_to_delete->messages->[0];
890
    is( $message->type, 'error', 'Type is error' );
891
    is( $message->message, 'has_guarantees', 'Cannot delete, has guarantees' );
892
883
    # cleanup
893
    # cleanup
884
    $patron->guarantee_relationships->delete;
894
    $patron->guarantee_relationships->delete;
885
895
886
    ## Make it have debt
896
    ## Make it have debt
887
    my $debit = $patron->account->add_debit({ amount => 10, interface => 'intranet', type => 'MANUAL' });
897
    my $debit = $patron->account->add_debit({ amount => 10, interface => 'intranet', type => 'MANUAL' });
888
898
889
    is( $patron->safe_to_delete, 'has_debt', 'Cannot delete, has debt' );
899
    ok( !$patron->safe_to_delete, 'Cannot delete, has debt' );
900
    $message = $patron->safe_to_delete->messages->[0];
901
    is( $message->type, 'error', 'Type is error' );
902
    is( $message->message, 'has_debt', 'Cannot delete, has debt' );
890
    # cleanup
903
    # cleanup
891
    $patron->account->pay({ amount => 10, debits => [ $debit ] });
904
    $patron->account->pay({ amount => 10, debits => [ $debit ] });
892
905
893
    ## Happy case :-D
906
    ## Happy case :-D
894
    is( $patron->safe_to_delete, 'ok', 'Can delete, all conditions met' );
907
    ok( $patron->safe_to_delete, 'Can delete, all conditions met' );
908
    my $messages = $patron->safe_to_delete->messages;
909
    is_deeply( $messages, [], 'Patron can be deleted, no messages' );
895
910
896
    $schema->storage->txn_rollback;
911
    $schema->storage->txn_rollback;
897
};
912
};
898
- 

Return to bug 29765