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

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

Return to bug 23260