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

(-)a/Koha/Patrons.pm (-1 / +57 lines)
Lines 28-33 use Koha::ArticleRequests; Link Here
28
use Koha::Patron;
28
use Koha::Patron;
29
use Koha::Exceptions::Patron;
29
use Koha::Exceptions::Patron;
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);
33
use base qw(Koha::Objects);
33
34
Lines 158-163 sub search_patrons_to_anonymise { Link Here
158
    return Koha::Patrons->_new_from_dbic($rs);
159
    return Koha::Patrons->_new_from_dbic($rs);
159
}
160
}
160
161
162
=head3 anonymize_last_borrowers
163
164
    Koha::Patrons->search->anonymize_last_borrowers();
165
166
    Anonymize items last borrower for all items_last_borrower older
167
    than AnonymizeLastBorrowerDays.
168
169
=cut
170
171
sub anonymize_last_borrowers {
172
    my ( $self, $params ) = @_;
173
174
    return unless C4::Context->preference("AnonymizeLastBorrower");
175
    my $days = C4::Context->preference("AnonymizeLastBorrowerDays") || 0;
176
    my ( $year,    $month,    $day )    = Today();
177
    my ( $newyear, $newmonth, $newday ) = Add_Delta_Days( $year, $month, $day, (-1) * $days );
178
    my $older_than_date = dt_from_string( sprintf "%4d-%02d-%02d", $newyear, $newmonth, $newday );
179
180
    my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
181
182
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
183
    my $rs  = $self->_resultset->search(
184
        {
185
            created_on => { '<' => $dtf->format_datetime($older_than_date), },
186
            'items_last_borrowers.borrowernumber' => { 'not' => undef },    # Keep forever
187
            ( $anonymous_patron ? ( 'items_last_borrowers.borrowernumber' => { '!=' => $anonymous_patron } ) : () ),
188
        },
189
        {
190
            join     => ["items_last_borrowers"],
191
            distinct => 1,
192
        }
193
    );
194
    my $patrons = Koha::Patrons->_new_from_dbic($rs);
195
196
    my $nb_rows = 0;
197
    while ( my $patron = $patrons->next ) {
198
        my $last_borrowers_to_anonymize = $patron->_result->items_last_borrowers->search(
199
            {
200
                (
201
                    $older_than_date
202
                    ? ( created_on => { '<' => $dtf->format_datetime($older_than_date) } )
203
                    : (),
204
                    "itemnumber.damaged"   => 0,
205
                    "itemnumber.itemlost"  => 0,
206
                    "itemnumber.withdrawn" => 0,
207
                ),
208
            },
209
            { join => ['itemnumber'] }
210
        );
211
        $nb_rows +=
212
            $last_borrowers_to_anonymize->update( { 'items_last_borrower.borrowernumber' => $anonymous_patron } );
213
    }
214
215
    return $nb_rows;
216
}
217
161
=head3 delete
218
=head3 delete
162
219
163
    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
220
    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
164
- 

Return to bug 23260