From 992b3cbc29a1b215bae7a62d1c8c52da192a203c Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 28 Dec 2018 18:48:25 +0000 Subject: [PATCH] Bug 22052: Refactor DeleteExpiredOpacReigstrations This patch changes the sub to use Koha::Objects and updates the tests. Previously the sub would die on borrowers with checkouts and would delete borrowers if they had fines To test: 1 - prove -v t/db_dependent/ 2 - Set your selfreg preferences: PatronSelfRegistration: Allow PatronSelfRegistrationDefaultCategory: SELFREG (or of your choice) PatronSelfRegistrationExpireTemporaryAccountsDelay: 30 3 - Register a patron into SELFREG or how you set above 4 - Set their date enrolled to two months ago 5 - Checkout an item to the patron 6 - Issue a fine to that patron 7 - perl misc/cronjobs/cleanup_database.pl --del-exp-selfreg -v 8 - The job should die with an error 9 - Check in the item 10 - run the corn again - patron is deleted, oops 11 - Apply patch 12 - Create another patron in the same way 13 - Checkout and fine the patron 14 - run the cron 15 - they are not deleted, and no error 16 - checkin the item 17 - run the cron 18 - they are not deleted and no error 19 - clear the fine 20 - run the cron 21 - patron is deleted, huzzah Signed-off-by: Charles Farmer Signed-off-by: Alex Arnaud Signed-off-by: Nick Clemens (cherry picked from commit b32756a5d7819e13290c2a145f3f0009280785c4) Signed-off-by: Martin Renvoize (cherry picked from commit 8102f6dc70623786c3247e008a0601cfd234d1e6) Signed-off-by: Jesse Maseto (cherry picked from commit d95301763e8017d0db18cfd142e2227cbaafaf0c) Signed-off-by: Fridolin Somers --- C4/Members.pm | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index 31a35b419e..d2d67b22e8 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -109,7 +109,7 @@ use C4::Members; =head1 DESCRIPTION -This module contains routines for adding, modifying and deleting members/patrons/borrowers +This module contains routines for adding, modifying and deleting members/patrons/borrowers =head1 FUNCTIONS @@ -151,7 +151,7 @@ The following will be set where applicable: $flags->{WAITING}->{message} Message -- deprecated $flags->{WAITING}->{itemlist} ref-to-array: list of available items -=over +=over =item C<$flags-E{ODUES}-E{itemlist}> is a reference-to-array listing the overdue items. Its elements are references-to-hash, each describing an @@ -167,7 +167,7 @@ fields from the reserves table of the Koha database. =back -All the "message" fields that include language generated in this function are deprecated, +All the "message" fields that include language generated in this function are deprecated, because such strings belong properly in the display layer. The "message" field that comes from the DB is OK. @@ -713,14 +713,14 @@ sub GetAllIssues { my $dbh = C4::Context->dbh; my $query = 'SELECT *, issues.timestamp as issuestimestamp, issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp - FROM issues + FROM issues LEFT JOIN items on items.itemnumber=issues.itemnumber LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber - WHERE borrowernumber=? + WHERE borrowernumber=? UNION ALL - SELECT *, old_issues.timestamp as issuestimestamp, old_issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp - FROM old_issues + SELECT *, old_issues.timestamp as issuestimestamp, old_issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp + FROM old_issues LEFT JOIN items on items.itemnumber=old_issues.itemnumber LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber @@ -990,11 +990,11 @@ sub GetBorrowersToExpunge { my $filtercategory = $params->{'category_code'}; my $filterbranch = $params->{'branchcode'} || ((C4::Context->preference('IndependentBranches') - && C4::Context->userenv + && C4::Context->userenv && !C4::Context->IsSuperLibrarian() && C4::Context->userenv->{branch}) ? C4::Context->userenv->{branch} - : ""); + : ""); my $filterpatronlist = $params->{'patron_list_id'}; my $dbh = C4::Context->dbh; @@ -1047,13 +1047,13 @@ sub GetBorrowersToExpunge { warn $query if $debug; my $sth = $dbh->prepare($query); - if (scalar(@query_params)>0){ + if (scalar(@query_params)>0){ $sth->execute(@query_params); } else { $sth->execute; } - + my @results; while ( my $data = $sth->fetchrow_hashref ) { push @results, $data; @@ -1247,18 +1247,18 @@ sub DeleteExpiredOpacRegistrations { my $category_code = C4::Context->preference('PatronSelfRegistrationDefaultCategory'); return 0 if not $category_code or not defined $delay or $delay eq q||; + my $date_enrolled = dt_from_string(); + $date_enrolled->subtract( days => $delay ); - my $query = qq| -SELECT borrowernumber -FROM borrowers -WHERE categorycode = ? AND DATEDIFF( NOW(), dateenrolled ) > ? |; + my $registrations_to_del = Koha::Patrons->search({ + dateenrolled => {'<=' => $date_enrolled->ymd}, + categorycode => $category_code, + }); - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare($query); - $sth->execute( $category_code, $delay ); my $cnt=0; - while ( my ($borrowernumber) = $sth->fetchrow_array() ) { - Koha::Patrons->find($borrowernumber)->delete; + while ( my $registration = $registrations_to_del->next() ) { + next if $registration->checkouts->count || $registration->account->balance; + $registration->delete; $cnt++; } return $cnt; -- 2.39.5