From 0118d55f5261884f7915b4c0b72deaad9a62db54 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 28 Nov 2023 13:29:19 +0000 Subject: [PATCH] Bug 23260: Add anonymize_last_borrowers method to Patrons.pm This patch adds a new routine to allow anonymizing the items_last_borrower table Signed-off-by: Martin Renvoize --- Koha/Patrons.pm | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index 92d3918363a..d3b4c20371f 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); @@ -158,6 +159,62 @@ sub search_patrons_to_anonymise { return Koha::Patrons->_new_from_dbic($rs); } +=head3 anonymize_last_borrowers + + Koha::Patrons->search->anonymize_last_borrowers(); + + Anonymize items last borrower for all items_last_borrower older + than AnonymizeLastBorrowerDays. + +=cut + +sub anonymize_last_borrowers { + my ( $self, $params ) = @_; + + return unless C4::Context->preference("AnonymizeLastBorrower"); + my $days = C4::Context->preference("AnonymizeLastBorrowerDays") || 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_anonymize = $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_anonymize->update( { 'items_last_borrower.borrowernumber' => $anonymous_patron } ); + } + + return $nb_rows; +} + =head3 delete Koha::Patrons->search({ some filters here })->delete({ move => 1 }); -- 2.43.0