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

(-)a/Koha/Patrons.pm (-1 / +57 lines)
Lines 28-33 use Koha::Patron; Link Here
28
use Koha::Exceptions::Patron;
28
use Koha::Exceptions::Patron;
29
use Koha::Exceptions::SysPref;
29
use Koha::Exceptions::SysPref;
30
use Koha::Patron::Categories;
30
use Koha::Patron::Categories;
31
use Date::Calc qw( Today Add_Delta_Days);
31
32
32
use base qw(Koha::Objects::Mixin::ExtendedAttributes Koha::Objects);
33
use base qw(Koha::Objects::Mixin::ExtendedAttributes Koha::Objects);
33
34
Lines 190-195 sub search_patrons_to_anonymise { Link Here
190
    return Koha::Patrons->_new_from_dbic($rs);
191
    return Koha::Patrons->_new_from_dbic($rs);
191
}
192
}
192
193
194
=head3 anonymize_last_borrowers
195
196
    Koha::Patrons->search->anonymize_last_borrowers();
197
198
    Anonymize items last borrower for all items_last_borrower older
199
    than AnonymizeLastBorrowerDays.
200
201
=cut
202
203
sub anonymize_last_borrowers {
204
    my ( $self, $params ) = @_;
205
206
    return unless C4::Context->preference("AnonymizeLastBorrower");
207
    my $days = C4::Context->preference("AnonymizeLastBorrowerDays") || 0;
208
    my ( $year, $month, $day )          = Today();
209
    my ( $newyear, $newmonth, $newday ) = Add_Delta_Days( $year, $month, $day, (-1) * $days );
210
    my $older_than_date = dt_from_string( sprintf "%4d-%02d-%02d", $newyear, $newmonth, $newday );
211
212
    my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
213
214
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
215
    my $rs  = $self->_resultset->search(
216
        {
217
            created_on                            => { '<'   => $dtf->format_datetime($older_than_date), },
218
            'items_last_borrowers.borrowernumber' => { 'not' => undef },    # Keep forever
219
            ( $anonymous_patron ? ( 'items_last_borrowers.borrowernumber' => { '!=' => $anonymous_patron } ) : () ),
220
        },
221
        {
222
            join     => ["items_last_borrowers"],
223
            distinct => 1,
224
        }
225
    );
226
    my $patrons = Koha::Patrons->_new_from_dbic($rs);
227
228
    my $nb_rows = 0;
229
    while ( my $patron = $patrons->next ) {
230
        my $last_borrowers_to_anonymize = $patron->_result->items_last_borrowers->search(
231
            {
232
                (
233
                    $older_than_date
234
                    ? ( created_on => { '<' => $dtf->format_datetime($older_than_date) } )
235
                    : (),
236
                    "itemnumber.damaged"   => 0,
237
                    "itemnumber.itemlost"  => 0,
238
                    "itemnumber.withdrawn" => 0,
239
                ),
240
            },
241
            { join => ['itemnumber'] }
242
        );
243
        $nb_rows +=
244
            $last_borrowers_to_anonymize->update( { 'items_last_borrower.borrowernumber' => $anonymous_patron } );
245
    }
246
247
    return $nb_rows;
248
}
249
193
=head3 delete
250
=head3 delete
194
251
195
    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
252
    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
196
- 

Return to bug 23260