From 6a240cf9a22c7d7083b81f6c17237db032d19d9b Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Fri, 30 Oct 2020 13:18:16 -0300 Subject: [PATCH] Bug 23260: (follow-up) Add anonymise_last_borrowers method to Patrons.pm This patch adds the method anonymise_last_borrowers in Patrons.pm. To test: 1) apply this patch and the previous ones 2) perl installer/data/mysql/updatedatabase.pl 3) set StoreLastBorrower preference to Allow. 4) set AnonymousPatron preference to a valid patron id 5) Create a Check out followed by a Check in. CHECK => a row should appear in items_last_borrower table with the borrower and the item number. 6) In mysql, update created_on of items_last_borrower and returndate of old_issues to two days earlier. 7) perl misc/cronjobs/anonymise_last_borrowers.pl CHECK => borrower number in items_last_borrower is not anonymised yet. 8) Repeat step 5 and 6 but instead of two days earlier, set the new entry in items_last_borrower to three days eralier. 9) set AnonymiseLastBorrower to 'Anonymise' and AnonymiseLastBorrowerDays to 2 days. 10) perl misc/cronjobs/anonymise_last_borrowers.pl SUCCESS => borrower number in items_last_borrower changed to AnonymousPatron id 11) repeat step 8 12) set AnonymousPatron preference to 0 13) perl misc/cronjobs/anonymise_last_borrowers.pl SUCCESS => borrower number in items_last_borrower changed to null 14) prove t/db_dependent/Koha/Patrons.t --- Koha/Patrons.pm | 114 +++++++++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 45 deletions(-) diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index 40defcff5d..d2fd2980fb 100644 --- a/Koha/Patrons.pm +++ b/Koha/Patrons.pm @@ -28,6 +28,7 @@ use Koha::ArticleRequests; use Koha::Patron; use Koha::Exceptions::Patron; use Koha::Patron::Categories; +use Date::Calc qw( Today Add_Delta_Days); use base qw(Koha::Objects); @@ -162,11 +163,9 @@ sub search_patrons_to_anonymise { Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } ); -Anonymise issue history (old_issues and items_last_borrowers) for all issues older -than the given date (optional). - +Anonymise issue history (old_issues) for all patrons older than the given date (optional). To make sure all the conditions are met, the caller has the responsibility to -call search_patrons_to_anonymise to filter the Koha::Patrons set. +call search_patrons_to_anonymise to filter the Koha::Patrons set =cut @@ -177,13 +176,6 @@ sub anonymise_issue_history { $older_than_date = dt_from_string $older_than_date if $older_than_date; - my $filter_damaged = - C4::Context->preference("KeepDamagedCheckouts") || 0; - my $filter_lost = - C4::Context->preference("KeepLostCheckouts") || 0; - my $filter_withdrawn = - C4::Context->preference("KeepWithdrawnCheckouts") || 0; - # The default of 0 does not work due to foreign key constraints # The anonymisation should not fail quietly if AnonymousPatron is not a valid entry # Set it to undef (NULL) @@ -191,47 +183,79 @@ sub anonymise_issue_history { my $nb_rows = 0; while ( my $patron = $self->next ) { my $old_issues_to_anonymise = $patron->old_checkouts->search( - { - ( - $older_than_date - ? ( returndate => - { '<' => $dtf->format_datetime($older_than_date) } ) - : () - ), - ( $filter_damaged ? ( "itemnumber.damaged" => 0 ) : () ), - ( $filter_lost ? ( "itemnumber.itemlost" => 0 ) : () ), - ( $filter_withdrawn ? ( "itemnumber.withdrawn" => 0 ) : () ) - }, - { - join => ['itemnumber'] - } + { + ( + $older_than_date + ? ( returndate => + { '<' => $dtf->format_datetime($older_than_date) } ) + : () + ) + } ); - - my $last_borrowers_to_anonymise = - $patron->_result->items_last_borrowers->search( - { - ( - $older_than_date - ? ( created_on => - { '<' => $dtf->format_datetime($older_than_date) } ) - : (), - ), - ( $filter_damaged ? ( "itemnumber.damaged" => 0 ) : () ), - ( $filter_lost ? ( "itemnumber.itemlost" => 0 ) : () ), - ( $filter_withdrawn ? ( "itemnumber.withdrawn" => 0 ) : () ) - }, - { - join => ['itemnumber'] - } - ); - my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef; $nb_rows += $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } ); - $last_borrowers_to_anonymise->update( { 'items_last_borrower.borrowernumber' => $anonymous_patron } ); } return $nb_rows; } + +=head3 anonymise_last_borrowers + + Koha::Patrons->search->anonymise_last_borrowers(); + + Anonymise items last borrower for all items_last_borrower older + than AnonymiseLastBorrowerDays. + +=cut + +sub anonymise_last_borrowers { + my ( $self, $params ) = @_; + + return unless C4::Context->preference("AnonymiseLastBorrower"); + my $days = C4::Context->preference("AnonymiseLastBorrowerDays") || 0; + my ($year,$month,$day) = Today(); + my ($newyear,$newmonth,$newday) = Add_Delta_Days ($year,$month,$day,(-1)*$days); + my $older_than_date = dt_from_string(sprintf "%4d-%02d-%02d",$newyear,$newmonth,$newday); + + my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef; + + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + my $rs = $self->_resultset->search( + { created_on => { '<' => $dtf->format_datetime($older_than_date), }, + 'items_last_borrowers.borrowernumber' => { 'not' => undef }, # Keep forever + ( $anonymous_patron ? ( 'items_last_borrowers.borrowernumber' => { '!=' => $anonymous_patron } ) : () ), + }, + { join => ["items_last_borrowers"], + distinct => 1, + } + ); + my $patrons = Koha::Patrons->_new_from_dbic($rs); + + my $nb_rows = 0; + while ( my $patron = $patrons->next ) { + my $last_borrowers_to_anonymise = + $patron->_result->items_last_borrowers->search( + { + ( + $older_than_date + ? ( created_on => + { '<' => $dtf->format_datetime($older_than_date) } ) + : (), + "itemnumber.damaged" => 0, + "itemnumber.itemlost" => 0, + "itemnumber.withdrawn" => 0, + ), + }, + { + join => ['itemnumber'] + } + ); + $nb_rows += $last_borrowers_to_anonymise->update( { 'items_last_borrower.borrowernumber' => $anonymous_patron } ); + } + + return $nb_rows; +} + =head3 delete Koha::Patrons->search({ some filters here })->delete({ move => 1 }); -- 2.30.2