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

(-)a/C4/Members.pm (-24 lines)
Lines 112-118 BEGIN { Link Here
112
    push @EXPORT, qw(
112
    push @EXPORT, qw(
113
        &AddMember
113
        &AddMember
114
        &AddMember_Opac
114
        &AddMember_Opac
115
        &MoveMemberToDeleted
116
        &ExtendMemberSubscriptionTo
115
        &ExtendMemberSubscriptionTo
117
    );
116
    );
118
117
Lines 1520-1548 sub GetSortDetails { Link Here
1520
    return ($sortvalue) unless ($lib);
1519
    return ($sortvalue) unless ($lib);
1521
}
1520
}
1522
1521
1523
=head2 MoveMemberToDeleted
1524
1525
  $result = &MoveMemberToDeleted($borrowernumber);
1526
1527
Copy the record from borrowers to deletedborrowers table.
1528
The routine returns 1 for success, undef for failure.
1529
1530
=cut
1531
1532
sub MoveMemberToDeleted {
1533
    my ($member) = shift or return;
1534
1535
    my $schema       = Koha::Database->new()->schema();
1536
    my $borrowers_rs = $schema->resultset('Borrower');
1537
    $borrowers_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
1538
    my $borrower = $borrowers_rs->find($member);
1539
    return unless $borrower;
1540
1541
    my $deleted = $schema->resultset('Deletedborrower')->create($borrower);
1542
1543
    return $deleted ? 1 : undef;
1544
}
1545
1546
=head2 DelMember
1522
=head2 DelMember
1547
1523
1548
    DelMember($borrowernumber);
1524
    DelMember($borrowernumber);
(-)a/Koha/Patron.pm (+15 lines)
Lines 208-213 sub update_password { Link Here
208
    return 1;
208
    return 1;
209
}
209
}
210
210
211
=head2 move_to_deleted
212
213
my $is_moved = $patron->move_to_deleted;
214
215
Move a patron to the deletedborrowers table.
216
This can be done before deleting a patron, to make sure the data are not completely deleted.
217
218
=cut
219
220
sub move_to_deleted {
221
    my ($self) = @_;
222
    my $patron_infos = $self->unblessed;
223
    return Koha::Database->new->schema->resultset('Deletedborrower')->create($patron_infos);
224
}
225
211
=head3 type
226
=head3 type
212
227
213
=cut
228
=cut
(-)a/members/deletemem.pl (-1 / +3 lines)
Lines 31-36 use C4::Auth; Link Here
31
use C4::Members;
31
use C4::Members;
32
use C4::Branch; # GetBranches
32
use C4::Branch; # GetBranches
33
use Module::Load;
33
use Module::Load;
34
use Koha::Patrons;
34
use Koha::Patron::Images;
35
use Koha::Patron::Images;
35
if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
36
if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
36
    load Koha::NorwegianPatronDB, qw( NLMarkForDeletion NLSync );
37
    load Koha::NorwegianPatronDB, qw( NLMarkForDeletion NLSync );
Lines 146-152 if ($countissues > 0 or $flags->{'CHARGES'} or $data->{'borrowernumber'} or $de Link Here
146
output_html_with_http_headers $input, $cookie, $template->output;
147
output_html_with_http_headers $input, $cookie, $template->output;
147
148
148
} else {
149
} else {
149
    MoveMemberToDeleted($member);
150
    my $patron = Koha::Patrons->find( $member );
151
    $patron->move_to_deleted;
150
    C4::Members::HandleDelBorrower($member);
152
    C4::Members::HandleDelBorrower($member);
151
    DelMember($member);
153
    DelMember($member);
152
    print $input->redirect("/cgi-bin/koha/members/members-home.pl");
154
    print $input->redirect("/cgi-bin/koha/members/members-home.pl");
(-)a/misc/cronjobs/delete_patrons.pl (-17 / +19 lines)
Lines 7-12 use Getopt::Long; Link Here
7
7
8
use C4::Members;
8
use C4::Members;
9
use Koha::DateUtils;
9
use Koha::DateUtils;
10
use Koha::Patrons;
10
use C4::Log;
11
use C4::Log;
11
12
12
my ( $help, $verbose, $not_borrowed_since, $expired_before, $category_code,
13
my ( $help, $verbose, $not_borrowed_since, $expired_before, $category_code,
Lines 71-93 for my $member (@$members) { Link Here
71
        next;
72
        next;
72
    }
73
    }
73
74
74
    eval {
75
    if ( $confirm ) {
75
        C4::Members::MoveMemberToDeleted( $borrowernumber )
76
        my $deleted = eval {
76
          if $confirm;
77
            Koha::Patrons->find( $borrowernumber )->move_to_deleted;
77
    };
78
        };
78
    if ($@) {
79
        if ($@ or not $deleted) {
79
        say "Failed to delete patron $borrowernumber, cannot move it: ($@)";
80
            say "Failed to delete patron $borrowernumber, cannot move it" . ( $@ ? ": ($@)" : "" );
80
        $dbh->rollback;
81
            $dbh->rollback;
81
        next;
82
            next;
82
    }
83
        }
83
    eval {
84
84
        C4::Members::HandleDelBorrower( $borrowernumber )
85
        eval {
85
          if $confirm;
86
            C4::Members::HandleDelBorrower( $borrowernumber );
86
    };
87
        };
87
    if ($@) {
88
        if ($@) {
88
        say "Failed to delete patron $borrowernumber, error handling its lists: ($@)";
89
            say "Failed to delete patron $borrowernumber, error handling its lists: ($@)";
89
        $dbh->rollback;
90
            $dbh->rollback;
90
        next;
91
            next;
92
        }
91
    }
93
    }
92
    eval { C4::Members::DelMember( $borrowernumber ) if $confirm; };
94
    eval { C4::Members::DelMember( $borrowernumber ) if $confirm; };
93
    if ($@) {
95
    if ($@) {
(-)a/t/db_dependent/Koha/Patrons.t (-1 / +15 lines)
Lines 19-27 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 6;
22
use Test::More tests => 7;
23
use Test::Warn;
23
use Test::Warn;
24
24
25
use C4::Members;
25
use Koha::Patron;
26
use Koha::Patron;
26
use Koha::Patrons;
27
use Koha::Patrons;
27
use Koha::Database;
28
use Koha::Database;
Lines 127-132 subtest 'update_password' => sub { Link Here
127
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
128
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
128
};
129
};
129
130
131
subtest "move_to_deleted" => sub {
132
    plan tests => 2;
133
    my $patron = $builder->build( { source => 'Borrower' } );
134
    my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
135
    is( ref( $retrieved_patron->move_to_deleted ), 'Koha::Schema::Result::Deletedborrower', 'Koha::Patron->move_to_deleted should return the Deleted patron' )
136
      ;    # FIXME This should be Koha::Deleted::Patron
137
    my $deleted_patron = $schema->resultset('Deletedborrower')
138
        ->search( { borrowernumber => $patron->{borrowernumber} }, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' } )
139
        ->next;
140
    is_deeply( $deleted_patron, $patron, 'Koha::Patron->move_to_deleted should have correctly moved the patron to the deleted table' );
141
    C4::Members::DelMember( $patron->{borrowernumber} );    # Cleanup
142
};
143
130
$retrieved_patron_1->delete;
144
$retrieved_patron_1->delete;
131
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
145
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
132
146
(-)a/t/db_dependent/Members.t (-15 / +1 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 79;
20
use Test::More tests => 77;
21
use Test::MockModule;
21
use Test::MockModule;
22
use Data::Dumper;
22
use Data::Dumper;
23
use C4::Context;
23
use C4::Context;
Lines 95-114 testAgeAccessors(\%data); #Age accessor tests don't touch the db so it is safe t Link Here
95
my $addmem=AddMember(%data);
95
my $addmem=AddMember(%data);
96
ok($addmem, "AddMember()");
96
ok($addmem, "AddMember()");
97
97
98
# It's not really a Move, it's a Copy.
99
my $result = MoveMemberToDeleted($addmem);
100
ok($result,"MoveMemberToDeleted()");
101
102
my $sth = $dbh->prepare("SELECT * from borrowers WHERE borrowernumber=?");
103
$sth->execute($addmem);
104
my $MemberAdded = $sth->fetchrow_hashref;
105
106
$sth = $dbh->prepare("SELECT * from deletedborrowers WHERE borrowernumber=?");
107
$sth->execute($addmem);
108
my $MemberMoved = $sth->fetchrow_hashref;
109
110
is_deeply($MemberMoved,$MemberAdded,"Confirm MoveMemberToDeleted.");
111
112
my $member=GetMemberDetails("",$CARDNUMBER)
98
my $member=GetMemberDetails("",$CARDNUMBER)
113
  or BAIL_OUT("Cannot read member with card $CARDNUMBER");
99
  or BAIL_OUT("Cannot read member with card $CARDNUMBER");
114
100
(-)a/tools/cleanborrowers.pl (-2 / +2 lines)
Lines 40-45 use C4::Output; Link Here
40
use C4::Members;        # GetBorrowersWhoHavexxxBorrowed.
40
use C4::Members;        # GetBorrowersWhoHavexxxBorrowed.
41
use C4::Circulation;    # AnonymiseIssueHistory.
41
use C4::Circulation;    # AnonymiseIssueHistory.
42
use Koha::DateUtils qw( dt_from_string output_pref );
42
use Koha::DateUtils qw( dt_from_string output_pref );
43
use Koha::Patrons;
43
use Date::Calc qw/Today Add_Delta_YM/;
44
use Date::Calc qw/Today Add_Delta_YM/;
44
use Koha::List::Patron;
45
use Koha::List::Patron;
45
46
Lines 128-134 elsif ( $step == 3 ) { Link Here
128
        for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
129
        for ( my $i = 0 ; $i < $totalDel ; $i++ ) {
129
            $radio eq 'testrun' && last;
130
            $radio eq 'testrun' && last;
130
            my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
131
            my $borrowernumber = $patrons_to_delete->[$i]->{'borrowernumber'};
131
            $radio eq 'trash' && MoveMemberToDeleted($borrowernumber);
132
            $radio eq 'trash' && Koha::Patrons->find($borrowernumber)->move_to_deleted;
132
            C4::Members::HandleDelBorrower($borrowernumber);
133
            C4::Members::HandleDelBorrower($borrowernumber);
133
            DelMember($borrowernumber);
134
            DelMember($borrowernumber);
134
        }
135
        }
135
- 

Return to bug 16891