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

(-)a/C4/Members.pm (-34 lines)
Lines 1514-1553 sub GetSortDetails { Link Here
1514
    return ($sortvalue) unless ($lib);
1514
    return ($sortvalue) unless ($lib);
1515
}
1515
}
1516
1516
1517
=head2 HandleDelBorrower
1518
1519
     HandleDelBorrower($borrower);
1520
1521
When a member is deleted, you should call me first.
1522
This routine deletes/moves lists and entries for the deleted member/borrower.
1523
Lists owned by the borrower are deleted, but entries from the borrower to
1524
other lists are kept.
1525
1526
=cut
1527
1528
sub HandleDelBorrower {
1529
    my ($borrower)= @_;
1530
    my $query;
1531
    my $dbh = C4::Context->dbh;
1532
1533
    #Delete all lists and all shares of this borrower
1534
    #Consistent with the approach Koha uses on deleting individual lists
1535
    #Note that entries in virtualshelfcontents added by this borrower to
1536
    #lists of others will be handled by a table constraint: the borrower
1537
    #is set to NULL in those entries.
1538
    $query="DELETE FROM virtualshelves WHERE owner=?";
1539
    $dbh->do($query,undef,($borrower));
1540
1541
    #NOTE:
1542
    #We could handle the above deletes via a constraint too.
1543
    #But a new BZ report 11889 has been opened to discuss another approach.
1544
    #Instead of deleting we could also disown lists (based on a pref).
1545
    #In that way we could save shared and public lists.
1546
    #The current table constraints support that idea now.
1547
    #This pref should then govern the results of other routines/methods such as
1548
    #Koha::Virtualshelf->new->delete too.
1549
}
1550
1551
=head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1517
=head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1552
1518
1553
    $date = ExtendMemberSubscriptionTo($borrowerid, $date);
1519
    $date = ExtendMemberSubscriptionTo($borrowerid, $date);
(-)a/Koha/Patron.pm (-1 / +21 lines)
Lines 32-37 use Koha::OldIssues; Link Here
32
use Koha::Patron::Categories;
32
use Koha::Patron::Categories;
33
use Koha::Patron::Images;
33
use Koha::Patron::Images;
34
use Koha::Patrons;
34
use Koha::Patrons;
35
use Koha::Virtualshelves;
35
36
36
use base qw(Koha::Object);
37
use base qw(Koha::Object);
37
38
Lines 49-55 Koha::Patron - Koha Patron Object class Link Here
49
50
50
$patron->delete
51
$patron->delete
51
52
52
Delete a patron.
53
Delete patron's holds, lists and finally the patron.
54
55
Lists owned by the borrower are deleted, but entries from the borrower to
56
other lists are kept.
53
57
54
=cut
58
=cut
55
59
Lines 63-68 sub delete { Link Here
63
            # FIXME Should be $patron->get_holds
67
            # FIXME Should be $patron->get_holds
64
            $_->delete for Koha::Holds->search( { borrowernumber => $self->borrowernumber } );
68
            $_->delete for Koha::Holds->search( { borrowernumber => $self->borrowernumber } );
65
69
70
            # Delete all lists and all shares of this borrower
71
            # Consistent with the approach Koha uses on deleting individual lists
72
            # Note that entries in virtualshelfcontents added by this borrower to
73
            # lists of others will be handled by a table constraint: the borrower
74
            # is set to NULL in those entries.
75
            # NOTE:
76
            # We could handle the above deletes via a constraint too.
77
            # But a new BZ report 11889 has been opened to discuss another approach.
78
            # Instead of deleting we could also disown lists (based on a pref).
79
            # In that way we could save shared and public lists.
80
            # The current table constraints support that idea now.
81
            # This pref should then govern the results of other routines/methods such as
82
            # Koha::Virtualshelf->new->delete too.
83
            # FIXME Could be $patron->get_lists
84
            $_->delete for Koha::Virtualshelves->search( { owner => $self->borrowernumber } );
85
66
            logaction( "MEMBERS", "DELETE", $self->borrowernumber, "" ) if C4::Context->preference("BorrowersLog");
86
            logaction( "MEMBERS", "DELETE", $self->borrowernumber, "" ) if C4::Context->preference("BorrowersLog");
67
            $deleted = $self->SUPER::delete;
87
            $deleted = $self->SUPER::delete;
68
        }
88
        }
(-)a/members/deletemem.pl (-1 lines)
Lines 149-155 output_html_with_http_headers $input, $cookie, $template->output; Link Here
149
} else {
149
} else {
150
    my $patron = Koha::Patrons->find( $member );
150
    my $patron = Koha::Patrons->find( $member );
151
    $patron->move_to_deleted;
151
    $patron->move_to_deleted;
152
    C4::Members::HandleDelBorrower($member);
153
    $patron->delete;
152
    $patron->delete;
154
    print $input->redirect("/cgi-bin/koha/members/members-home.pl");
153
    print $input->redirect("/cgi-bin/koha/members/members-home.pl");
155
}
154
}
(-)a/misc/cronjobs/delete_patrons.pl (-17 / +1 lines)
Lines 55-65 unless ($confirm) { Link Here
55
55
56
say scalar(@$members) . " patrons to delete";
56
say scalar(@$members) . " patrons to delete";
57
57
58
my $dbh = C4::Context->dbh;
59
$dbh->{RaiseError} = 1;
60
$dbh->{PrintError} = 0;
61
62
$dbh->{AutoCommit} = 0; # use transactions to avoid partial deletes
63
my $deleted = 0;
58
my $deleted = 0;
64
for my $member (@$members) {
59
for my $member (@$members) {
65
    print "Trying to delete patron $member->{borrowernumber}... "
60
    print "Trying to delete patron $member->{borrowernumber}... "
Lines 77-102 for my $member (@$members) { Link Here
77
        my $deleted = eval { $patron->move_to_deleted; };
72
        my $deleted = eval { $patron->move_to_deleted; };
78
        if ($@ or not $deleted) {
73
        if ($@ or not $deleted) {
79
            say "Failed to delete patron $borrowernumber, cannot move it" . ( $@ ? ": ($@)" : "" );
74
            say "Failed to delete patron $borrowernumber, cannot move it" . ( $@ ? ": ($@)" : "" );
80
            $dbh->rollback;
81
            next;
75
            next;
82
        }
76
        }
83
77
84
        eval {
78
        eval { $patron->delete };
85
            C4::Members::HandleDelBorrower( $borrowernumber );
86
        };
87
        if ($@) {
88
            say "Failed to delete patron $borrowernumber, error handling its lists: ($@)";
89
            $dbh->rollback;
90
            next;
91
        }
92
        eval { $patron->delete if $confirm; };
93
        if ($@) {
79
        if ($@) {
94
            say "Failed to delete patron $borrowernumber: $@)";
80
            say "Failed to delete patron $borrowernumber: $@)";
95
            $dbh->rollback;
96
            next;
81
            next;
97
        }
82
        }
98
    }
83
    }
99
    $dbh->commit;
100
    $deleted++;
84
    $deleted++;
101
    say "OK" if $verbose;
85
    say "OK" if $verbose;
102
}
86
}
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +12 lines)
Lines 26-31 use Koha::Holds; Link Here
26
use Koha::Patron;
26
use Koha::Patron;
27
use Koha::Patrons;
27
use Koha::Patrons;
28
use Koha::Database;
28
use Koha::Database;
29
use Koha::Virtualshelves;
29
30
30
use t::lib::TestBuilder;
31
use t::lib::TestBuilder;
31
use t::lib::Mocks;
32
use t::lib::Mocks;
Lines 142-148 subtest "move_to_deleted" => sub { Link Here
142
};
143
};
143
144
144
subtest "delete" => sub {
145
subtest "delete" => sub {
145
    plan tests => 4;
146
    plan tests => 5;
146
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
147
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
147
    my $patron           = $builder->build( { source => 'Borrower' } );
148
    my $patron           = $builder->build( { source => 'Borrower' } );
148
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
149
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
Lines 151-162 subtest "delete" => sub { Link Here
151
            value  => { borrowernumber => $patron->{borrowernumber} }
152
            value  => { borrowernumber => $patron->{borrowernumber} }
152
        }
153
        }
153
    );
154
    );
155
    my $list = $builder->build(
156
        {   source => 'Virtualshelve',
157
            value  => { owner => $patron->{borrowernumber} }
158
        }
159
    );
160
154
    my $deleted = $retrieved_patron->delete;
161
    my $deleted = $retrieved_patron->delete;
155
    is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
162
    is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
156
    is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron');
163
164
    is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
157
165
158
    is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
166
    is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
159
167
168
    is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
169
160
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
170
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
161
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
171
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
162
};
172
};
(-)a/tools/cleanborrowers.pl (-2 lines)
Lines 131-137 elsif ( $step == 3 ) { Link Here
131
            my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
131
            my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
132
            my $patron = Koha::Patrons->find($borrowernumber);
132
            my $patron = Koha::Patrons->find($borrowernumber);
133
            $radio eq 'trash' && $patron->move_to_deleted;
133
            $radio eq 'trash' && $patron->move_to_deleted;
134
            C4::Members::HandleDelBorrower($borrowernumber);
135
            $patron->delete;
134
            $patron->delete;
136
        }
135
        }
137
        $template->param(
136
        $template->param(
138
- 

Return to bug 16907