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

(-)a/Koha/Patrons.pm (-47 / +69 lines)
Lines 30-36 use Koha::ArticleRequest::Status; Link Here
30
use Koha::Patron;
30
use Koha::Patron;
31
use Koha::Exceptions::Patron;
31
use Koha::Exceptions::Patron;
32
use Koha::Patron::Categories;
32
use Koha::Patron::Categories;
33
use Date::Calc qw( Today Add_Delta_YMD );
33
use Date::Calc qw( Today Add_Delta_YMD Add_Delta_Days);
34
34
35
use base qw(Koha::Objects);
35
use base qw(Koha::Objects);
36
36
Lines 165-175 sub search_patrons_to_anonymise { Link Here
165
165
166
    Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
166
    Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
167
167
168
Anonymise issue history (old_issues and items_last_borrowers) for all issues older
168
Anonymise issue history (old_issues) for all patrons older than the given date (optional).
169
than the given date (optional).
170
171
To make sure all the conditions are met, the caller has the responsibility to
169
To make sure all the conditions are met, the caller has the responsibility to
172
call search_patrons_to_anonymise to filter the Koha::Patrons set.
170
call search_patrons_to_anonymise to filter the Koha::Patrons set
173
171
174
=cut
172
=cut
175
173
Lines 180-192 sub anonymise_issue_history { Link Here
180
178
181
    $older_than_date = dt_from_string $older_than_date if $older_than_date;
179
    $older_than_date = dt_from_string $older_than_date if $older_than_date;
182
180
183
    my $filter_damaged =
184
      C4::Context->preference("KeepDamagedCheckouts") || 0;
185
    my $filter_lost =
186
      C4::Context->preference("KeepLostCheckouts") || 0;
187
    my $filter_withdrawn =
188
      C4::Context->preference("KeepWithdrawnCheckouts") || 0;
189
190
    # The default of 0 does not work due to foreign key constraints
181
    # The default of 0 does not work due to foreign key constraints
191
    # The anonymisation should not fail quietly if AnonymousPatron is not a valid entry
182
    # The anonymisation should not fail quietly if AnonymousPatron is not a valid entry
192
    # Set it to undef (NULL)
183
    # Set it to undef (NULL)
Lines 194-240 sub anonymise_issue_history { Link Here
194
    my $nb_rows = 0;
185
    my $nb_rows = 0;
195
    while ( my $patron = $self->next ) {
186
    while ( my $patron = $self->next ) {
196
        my $old_issues_to_anonymise = $patron->old_checkouts->search(
187
        my $old_issues_to_anonymise = $patron->old_checkouts->search(
197
            {
188
        {
198
                (
189
            (
199
                    $older_than_date
190
                $older_than_date
200
                    ? ( returndate =>
191
                ? ( returndate =>
201
                          { '<' => $dtf->format_datetime($older_than_date) } )
192
                      { '<' => $dtf->format_datetime($older_than_date) } )
202
                    : ()
193
                : ()
203
                ),
194
            )
204
                ( $filter_damaged   ? ( "itemnumber.damaged"   => 0 ) : () ),
195
        }
205
                ( $filter_lost      ? ( "itemnumber.itemlost"  => 0 ) : () ),
206
                ( $filter_withdrawn ? ( "itemnumber.withdrawn" => 0 ) : () )
207
            },
208
            {
209
                join => ['itemnumber']
210
            }
211
        );
196
        );
212
213
        my $last_borrowers_to_anonymise =
214
          $patron->_result->items_last_borrowers->search(
215
            {
216
                (
217
                    $older_than_date
218
                    ? ( created_on =>
219
                          { '<' => $dtf->format_datetime($older_than_date) } )
220
                    : (),
221
                ),
222
                ( $filter_damaged   ? ( "itemnumber.damaged"   => 0 ) : () ),
223
                ( $filter_lost      ? ( "itemnumber.itemlost"  => 0 ) : () ),
224
                ( $filter_withdrawn ? ( "itemnumber.withdrawn" => 0 ) : () )
225
            },
226
            {
227
                join => ['itemnumber']
228
            }
229
          );
230
231
        my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
197
        my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
232
        $nb_rows += $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
198
        $nb_rows += $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
233
        $last_borrowers_to_anonymise->update( { 'items_last_borrower.borrowernumber' => $anonymous_patron } );
234
    }
199
    }
235
    return $nb_rows;
200
    return $nb_rows;
236
}
201
}
237
202
203
204
=head3 anonymise_last_borrowers
205
206
    Koha::Patrons->search->anonymise_last_borrowers();
207
208
    Anonymise items last borrower for all items_last_borrower older
209
    than AnonymiseLastBorrowerDays.
210
211
=cut
212
213
sub anonymise_last_borrowers {
214
    my ( $self, $params ) = @_;
215
216
    return unless C4::Context->preference("AnonymiseLastBorrower");
217
    my $days = C4::Context->preference("AnonymiseLastBorrowerDays") || 0;
218
    my ($year,$month,$day) = Today();
219
    my ($newyear,$newmonth,$newday) = Add_Delta_Days ($year,$month,$day,(-1)*$days);
220
    my $older_than_date = dt_from_string(sprintf "%4d-%02d-%02d",$newyear,$newmonth,$newday);
221
222
    my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
223
224
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
225
    my $rs = $self->_resultset->search(
226
        {   created_on                  => { '<'   =>  $dtf->format_datetime($older_than_date), },
227
            'items_last_borrowers.borrowernumber' => { 'not' => undef },                 # Keep forever
228
            ( $anonymous_patron ? ( 'items_last_borrowers.borrowernumber' => { '!=' => $anonymous_patron } ) : () ),
229
        },
230
        {   join     => ["items_last_borrowers"],
231
            distinct => 1,
232
        }
233
    );
234
    my $patrons = Koha::Patrons->_new_from_dbic($rs);
235
236
    my $nb_rows = 0;
237
    while ( my $patron = $patrons->next ) {
238
        my $last_borrowers_to_anonymise =
239
        $patron->_result->items_last_borrowers->search(
240
        {
241
            (
242
                $older_than_date
243
                ? ( created_on =>
244
                        { '<' => $dtf->format_datetime($older_than_date) } )
245
                : (),
246
                "itemnumber.damaged"   => 0,
247
                "itemnumber.itemlost"  => 0,
248
                "itemnumber.withdrawn" => 0,
249
            ),
250
        },
251
        {
252
            join => ['itemnumber']
253
        }
254
        );
255
        $nb_rows += $last_borrowers_to_anonymise->update( { 'items_last_borrower.borrowernumber' => $anonymous_patron } );
256
    }
257
258
    return $nb_rows;
259
}
260
238
=head3 delete
261
=head3 delete
239
262
240
    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
263
    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
241
- 

Return to bug 23260