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

(-)a/C4/Members.pm (-34 lines)
Lines 1618-1657 sub GetSortDetails { Link Here
1618
    return ($sortvalue) unless ($lib);
1618
    return ($sortvalue) unless ($lib);
1619
}
1619
}
1620
1620
1621
=head2 HandleDelBorrower
1622
1623
     HandleDelBorrower($borrower);
1624
1625
When a member is deleted, you should call me first.
1626
This routine deletes/moves lists and entries for the deleted member/borrower.
1627
Lists owned by the borrower are deleted, but entries from the borrower to
1628
other lists are kept.
1629
1630
=cut
1631
1632
sub HandleDelBorrower {
1633
    my ($borrower)= @_;
1634
    my $query;
1635
    my $dbh = C4::Context->dbh;
1636
1637
    #Delete all lists and all shares of this borrower
1638
    #Consistent with the approach Koha uses on deleting individual lists
1639
    #Note that entries in virtualshelfcontents added by this borrower to
1640
    #lists of others will be handled by a table constraint: the borrower
1641
    #is set to NULL in those entries.
1642
    $query="DELETE FROM virtualshelves WHERE owner=?";
1643
    $dbh->do($query,undef,($borrower));
1644
1645
    #NOTE:
1646
    #We could handle the above deletes via a constraint too.
1647
    #But a new BZ report 11889 has been opened to discuss another approach.
1648
    #Instead of deleting we could also disown lists (based on a pref).
1649
    #In that way we could save shared and public lists.
1650
    #The current table constraints support that idea now.
1651
    #This pref should then govern the results of other routines/methods such as
1652
    #Koha::Virtualshelf->new->delete too.
1653
}
1654
1655
=head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1621
=head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
1656
1622
1657
    $date = ExtendMemberSubscriptionTo($borrowerid, $date);
1623
    $date = ExtendMemberSubscriptionTo($borrowerid, $date);
(-)a/Koha/Patron.pm (-1 / +21 lines)
Lines 31-36 use Koha::OldIssues; Link Here
31
use Koha::Patron::Categories;
31
use Koha::Patron::Categories;
32
use Koha::Patron::Images;
32
use Koha::Patron::Images;
33
use Koha::Patrons;
33
use Koha::Patrons;
34
use Koha::Virtualshelves;
34
35
35
use base qw(Koha::Object);
36
use base qw(Koha::Object);
36
37
Lines 48-54 Koha::Patron - Koha Patron Object class Link Here
48
49
49
$patron->delete
50
$patron->delete
50
51
51
Delete a patron.
52
Delete patron's holds, lists and finally the patron.
53
54
Lists owned by the borrower are deleted, but entries from the borrower to
55
other lists are kept.
52
56
53
=cut
57
=cut
54
58
Lines 62-67 sub delete { Link Here
62
            # FIXME Should be $patron->get_holds
66
            # FIXME Should be $patron->get_holds
63
            $_->delete for Koha::Holds->search( { borrowernumber => $self->borrowernumber } );
67
            $_->delete for Koha::Holds->search( { borrowernumber => $self->borrowernumber } );
64
68
69
            # Delete all lists and all shares of this borrower
70
            # Consistent with the approach Koha uses on deleting individual lists
71
            # Note that entries in virtualshelfcontents added by this borrower to
72
            # lists of others will be handled by a table constraint: the borrower
73
            # is set to NULL in those entries.
74
            # NOTE:
75
            # We could handle the above deletes via a constraint too.
76
            # But a new BZ report 11889 has been opened to discuss another approach.
77
            # Instead of deleting we could also disown lists (based on a pref).
78
            # In that way we could save shared and public lists.
79
            # The current table constraints support that idea now.
80
            # This pref should then govern the results of other routines/methods such as
81
            # Koha::Virtualshelf->new->delete too.
82
            # FIXME Could be $patron->get_lists
83
            $_->delete for Koha::Virtualshelves->search( { owner => $self->borrowernumber } );
84
65
            logaction( "MEMBERS", "DELETE", $self->borrowernumber, "" ) if C4::Context->preference("BorrowersLog");
85
            logaction( "MEMBERS", "DELETE", $self->borrowernumber, "" ) if C4::Context->preference("BorrowersLog");
66
            $deleted = $self->SUPER::delete;
86
            $deleted = $self->SUPER::delete;
67
        }
87
        }
(-)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 25-30 use Koha::Holds; Link Here
25
use Koha::Patron;
25
use Koha::Patron;
26
use Koha::Patrons;
26
use Koha::Patrons;
27
use Koha::Database;
27
use Koha::Database;
28
use Koha::Virtualshelves;
28
29
29
use t::lib::TestBuilder;
30
use t::lib::TestBuilder;
30
use t::lib::Mocks;
31
use t::lib::Mocks;
Lines 114-120 subtest "move_to_deleted" => sub { Link Here
114
};
115
};
115
116
116
subtest "delete" => sub {
117
subtest "delete" => sub {
117
    plan tests => 4;
118
    plan tests => 5;
118
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
119
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
119
    my $patron           = $builder->build( { source => 'Borrower' } );
120
    my $patron           = $builder->build( { source => 'Borrower' } );
120
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
121
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
Lines 123-134 subtest "delete" => sub { Link Here
123
            value  => { borrowernumber => $patron->{borrowernumber} }
124
            value  => { borrowernumber => $patron->{borrowernumber} }
124
        }
125
        }
125
    );
126
    );
127
    my $list = $builder->build(
128
        {   source => 'Virtualshelve',
129
            value  => { owner => $patron->{borrowernumber} }
130
        }
131
    );
132
126
    my $deleted = $retrieved_patron->delete;
133
    my $deleted = $retrieved_patron->delete;
127
    is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
134
    is( $deleted, 1, 'Koha::Patron->delete should return 1 if the patron has been correctly deleted' );
128
    is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron');
135
136
    is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
129
137
130
    is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
138
    is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's holds| );
131
139
140
    is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
141
132
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
142
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
133
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
143
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
134
};
144
};
(-)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