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

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

Return to bug 23260