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 1284-1289 subtest 'search_patrons_to_anonymise' => sub { Link Here
1284
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
1284
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
1285
};
1285
};
1286
1286
1287
subtest 'anonymise items_last_borrower' => sub {
1288
    plan tests => 1;
1289
1290
    # TODO create a subroutine in t::lib::Mocks
1291
    my $anonymous = $builder->build( { source => 'Borrower', }, );
1292
1293
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
1294
    t::lib::Mocks::mock_preference('StoreLastBorrower', 1);
1295
1296
    subtest 'withrawn, lost and damaged items' => sub {
1297
        plan tests => 5;
1298
1299
        my $patron = $builder->build(
1300
            {   source => 'Borrower',
1301
                value  => { privacy => 1, }
1302
            }
1303
        );
1304
        my $item_1 = $builder->build_object(
1305
            {   class => 'Koha::Items',
1306
                value  => {
1307
                    itemlost  => 0,
1308
                    withdrawn => 0,
1309
                    damaged => 0,
1310
                },
1311
            }
1312
        );
1313
        my $issue_1 = $builder->build(
1314
            {   source => 'Issue',
1315
                value  => {
1316
                    borrowernumber => $patron->{borrowernumber},
1317
                    itemnumber     => $item_1->itemnumber,
1318
                },
1319
            }
1320
        );
1321
        my $item_2 = $builder->build_object(
1322
            {   class => 'Koha::Items',
1323
                value  => {
1324
                    itemlost  => 0,
1325
                    withdrawn => 0,
1326
                    damaged => 0,
1327
                },
1328
            }
1329
        );
1330
        my $issue_2 = $builder->build(
1331
            {   source => 'Issue',
1332
                value  => {
1333
                    borrowernumber => $patron->{borrowernumber},
1334
                    itemnumber     => $item_2->itemnumber,
1335
                },
1336
            }
1337
        );
1338
        my $item_3 = $builder->build_object(
1339
            {   class => 'Koha::Items',
1340
                value  => {
1341
                    itemlost  => 0,
1342
                    withdrawn => 0,
1343
                    damaged => 0,
1344
                },
1345
            }
1346
        );
1347
        my $issue_3 = $builder->build(
1348
            {   source => 'Issue',
1349
                value  => {
1350
                    borrowernumber => $patron->{borrowernumber},
1351
                    itemnumber     => $item_3->itemnumber,
1352
                },
1353
            }
1354
        );
1355
        my $item_4 = $builder->build_object(
1356
            {   class => 'Koha::Items',
1357
                value  => {
1358
                    itemlost  => 0,
1359
                    withdrawn => 0,
1360
                    damaged => 0,
1361
                },
1362
            }
1363
        );
1364
        my $issue_4 = $builder->build(
1365
            {   source => 'Issue',
1366
                value  => {
1367
                    borrowernumber => $patron->{borrowernumber},
1368
                    itemnumber     => $item_4->itemnumber,
1369
                },
1370
            }
1371
        );
1372
1373
        my ( $returned_1, undef, undef ) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string('2010-10-11') );
1374
        my ( $returned_2, undef, undef ) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string('2010-10-11') );
1375
        my ( $returned_3, undef, undef ) = C4::Circulation::AddReturn( $item_3->barcode, undef, undef, dt_from_string('2010-10-11') );
1376
        my ( $returned_4, undef, undef ) = C4::Circulation::AddReturn( $item_4->barcode, undef, undef, dt_from_string('2010-10-11') );
1377
        is( $returned_1 && $returned_2 && $returned_3 && $returned_4, 1, 'The items should have been returned' );
1378
        $item_1->withdrawn(1)->store;
1379
        $item_2->itemlost(1)->store;
1380
        $item_3->damaged(1)->store;
1381
1382
        Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-12' } )->anonymise_issue_history();
1383
1384
        my $dbh = C4::Context->dbh;
1385
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1386
        $sth->execute($item_1->itemnumber);
1387
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1388
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'withrawn items should not be anonymised' );
1389
        $sth->execute($item_2->itemnumber);
1390
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1391
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'lost items should not be anonymised' );
1392
        $sth->execute($item_3->itemnumber);
1393
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1394
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'damaged items should not be anonymised' );
1395
        $sth->execute($item_4->itemnumber);
1396
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1397
        is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'not withdrawn, lost or damaged items are anonymised' );
1398
1399
    };
1400
1401
};
1402
1287
subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited' => sub {
1403
subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited' => sub {
1288
    plan tests => 3;
1404
    plan tests => 3;
1289
1405
1290
- 

Return to bug 23260