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

(-)a/Koha/Patrons.pm (+61 lines)
Lines 158-163 sub search_patrons_to_anonymise { Link Here
158
    return Koha::Patrons->_new_from_dbic($rs);
158
    return Koha::Patrons->_new_from_dbic($rs);
159
}
159
}
160
160
161
=head3 anonymise_issue_history
162
163
    Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
164
165
Anonymise issue history (old_issues and items_last_borrowers) for all issues older
166
than the given date (optional).
167
168
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.
170
171
=cut
172
173
sub anonymise_issue_history {
174
    my ( $self, $params ) = @_;
175
176
    my $older_than_date = $params->{before};
177
178
    $older_than_date = dt_from_string $older_than_date if $older_than_date;
179
180
    # The default of 0 does not work due to foreign key constraints
181
    # The anonymisation should not fail quietly if AnonymousPatron is not a valid entry
182
    # Set it to undef (NULL)
183
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
184
    my $nb_rows = 0;
185
    while ( my $patron = $self->next ) {
186
        my $old_issues_to_anonymise = $patron->old_checkouts->search(
187
        {
188
            (
189
                $older_than_date
190
                ? ( returndate =>
191
                      { '<' => $dtf->format_datetime($older_than_date) } )
192
                : ()
193
            )
194
        }
195
        );
196
197
        my $last_borrowers_to_anonymise =
198
          $patron->_result->items_last_borrowers->search(
199
            {
200
                (
201
                    $older_than_date
202
                    ? ( created_on =>
203
                          { '<' => $dtf->format_datetime($older_than_date) } )
204
                    : (),
205
                    "itemnumber.damaged" => 0,
206
                    "itemnumber.itemlost" => 0,
207
                    "itemnumber.withdrawn" => 0,
208
                )
209
            },
210
            {
211
                join => ['itemnumber']
212
            }
213
          );
214
215
        my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
216
        $nb_rows += $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
217
        $nb_rows += $last_borrowers_to_anonymise->update( { 'items_last_borrower.borrowernumber' => $anonymous_patron } );
218
    }
219
    return $nb_rows;
220
}
221
161
=head3 delete
222
=head3 delete
162
223
163
    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
224
    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
(-)a/t/db_dependent/Koha/Patrons.t (-1 / +116 lines)
Lines 1286-1291 subtest 'search_patrons_to_anonymise' => sub { Link Here
1286
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
1286
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
1287
};
1287
};
1288
1288
1289
subtest 'anonymise items_last_borrower' => sub {
1290
    plan tests => 1;
1291
1292
    # TODO create a subroutine in t::lib::Mocks
1293
    my $anonymous = $builder->build( { source => 'Borrower', }, );
1294
1295
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
1296
    t::lib::Mocks::mock_preference('StoreLastBorrower', 1);
1297
1298
    subtest 'withrawn, lost and damaged items' => sub {
1299
        plan tests => 5;
1300
1301
        my $patron = $builder->build(
1302
            {   source => 'Borrower',
1303
                value  => { privacy => 1, }
1304
            }
1305
        );
1306
        my $item_1 = $builder->build_object(
1307
            {   class => 'Koha::Items',
1308
                value  => {
1309
                    itemlost  => 0,
1310
                    withdrawn => 0,
1311
                    damaged => 0,
1312
                },
1313
            }
1314
        );
1315
        my $issue_1 = $builder->build(
1316
            {   source => 'Issue',
1317
                value  => {
1318
                    borrowernumber => $patron->{borrowernumber},
1319
                    itemnumber     => $item_1->itemnumber,
1320
                },
1321
            }
1322
        );
1323
        my $item_2 = $builder->build_object(
1324
            {   class => 'Koha::Items',
1325
                value  => {
1326
                    itemlost  => 0,
1327
                    withdrawn => 0,
1328
                    damaged => 0,
1329
                },
1330
            }
1331
        );
1332
        my $issue_2 = $builder->build(
1333
            {   source => 'Issue',
1334
                value  => {
1335
                    borrowernumber => $patron->{borrowernumber},
1336
                    itemnumber     => $item_2->itemnumber,
1337
                },
1338
            }
1339
        );
1340
        my $item_3 = $builder->build_object(
1341
            {   class => 'Koha::Items',
1342
                value  => {
1343
                    itemlost  => 0,
1344
                    withdrawn => 0,
1345
                    damaged => 0,
1346
                },
1347
            }
1348
        );
1349
        my $issue_3 = $builder->build(
1350
            {   source => 'Issue',
1351
                value  => {
1352
                    borrowernumber => $patron->{borrowernumber},
1353
                    itemnumber     => $item_3->itemnumber,
1354
                },
1355
            }
1356
        );
1357
        my $item_4 = $builder->build_object(
1358
            {   class => 'Koha::Items',
1359
                value  => {
1360
                    itemlost  => 0,
1361
                    withdrawn => 0,
1362
                    damaged => 0,
1363
                },
1364
            }
1365
        );
1366
        my $issue_4 = $builder->build(
1367
            {   source => 'Issue',
1368
                value  => {
1369
                    borrowernumber => $patron->{borrowernumber},
1370
                    itemnumber     => $item_4->itemnumber,
1371
                },
1372
            }
1373
        );
1374
1375
        my ( $returned_1, undef, undef ) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string('2010-10-11') );
1376
        my ( $returned_2, undef, undef ) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string('2010-10-11') );
1377
        my ( $returned_3, undef, undef ) = C4::Circulation::AddReturn( $item_3->barcode, undef, undef, dt_from_string('2010-10-11') );
1378
        my ( $returned_4, undef, undef ) = C4::Circulation::AddReturn( $item_4->barcode, undef, undef, dt_from_string('2010-10-11') );
1379
        is( $returned_1 && $returned_2 && $returned_3 && $returned_4, 1, 'The items should have been returned' );
1380
        $item_1->withdrawn(1)->store;
1381
        $item_2->itemlost(1)->store;
1382
        $item_3->damaged(1)->store;
1383
1384
        Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-12' } )->anonymise_issue_history();
1385
1386
        my $dbh = C4::Context->dbh;
1387
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1388
        $sth->execute($item_1->itemnumber);
1389
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1390
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'withrawn items should not be anonymised' );
1391
        $sth->execute($item_2->itemnumber);
1392
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1393
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'lost items should not be anonymised' );
1394
        $sth->execute($item_3->itemnumber);
1395
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1396
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'damaged items should not be anonymised' );
1397
        $sth->execute($item_4->itemnumber);
1398
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1399
        is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'not withdrawn, lost or damaged items are anonymised' );
1400
1401
    };
1402
1403
};
1404
1289
subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited' => sub {
1405
subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited' => sub {
1290
    plan tests => 3;
1406
    plan tests => 3;
1291
1407
1292
- 

Return to bug 23260