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

(-)a/Koha/Patrons.pm (-34 / +32 lines)
Lines 159-164 sub search_patrons_to_anonymise { Link Here
159
    return Koha::Patrons->_new_from_dbic($rs);
159
    return Koha::Patrons->_new_from_dbic($rs);
160
}
160
}
161
161
162
162
=head3 anonymise_issue_history
163
=head3 anonymise_issue_history
163
164
164
    Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
165
    Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
Lines 179-196 sub anonymise_issue_history { Link Here
179
    # 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
180
    # 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
181
    # Set it to undef (NULL)
182
    # Set it to undef (NULL)
182
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
183
    my $dtf     = Koha::Database->new->schema->storage->datetime_parser;
183
    my $nb_rows = 0;
184
    my $nb_rows = 0;
184
    while ( my $patron = $self->next ) {
185
    while ( my $patron = $self->next ) {
185
        my $old_issues_to_anonymise = $patron->old_checkouts->search(
186
        my $old_issues_to_anonymise = $patron->old_checkouts->search(
186
        {
187
            {
187
            (
188
                (
188
                $older_than_date
189
                    $older_than_date
189
                ? ( returndate =>
190
                    ? ( returndate => { '<' => $dtf->format_datetime($older_than_date) } )
190
                      { '<' => $dtf->format_datetime($older_than_date) } )
191
                    : ()
191
                : ()
192
                )
192
            )
193
            }
193
        }
194
        );
194
        );
195
        my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
195
        my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
196
        $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 } );
Lines 198-204 sub anonymise_issue_history { Link Here
198
    return $nb_rows;
198
    return $nb_rows;
199
}
199
}
200
200
201
202
=head3 anonymise_last_borrowers
201
=head3 anonymise_last_borrowers
203
202
204
    Koha::Patrons->search->anonymise_last_borrowers();
203
    Koha::Patrons->search->anonymise_last_borrowers();
Lines 213-231 sub anonymise_last_borrowers { Link Here
213
212
214
    return unless C4::Context->preference("AnonymiseLastBorrower");
213
    return unless C4::Context->preference("AnonymiseLastBorrower");
215
    my $days = C4::Context->preference("AnonymiseLastBorrowerDays") || 0;
214
    my $days = C4::Context->preference("AnonymiseLastBorrowerDays") || 0;
216
    my ($year,$month,$day) = Today();
215
    my ( $year,    $month,    $day )    = Today();
217
    my ($newyear,$newmonth,$newday) = Add_Delta_Days ($year,$month,$day,(-1)*$days);
216
    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);
217
    my $older_than_date = dt_from_string( sprintf "%4d-%02d-%02d", $newyear, $newmonth, $newday );
219
218
220
    my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
219
    my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
221
220
222
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
221
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
223
    my $rs = $self->_resultset->search(
222
    my $rs  = $self->_resultset->search(
224
        {   created_on                  => { '<'   =>  $dtf->format_datetime($older_than_date), },
223
        {
225
            'items_last_borrowers.borrowernumber' => { 'not' => undef },                 # Keep forever
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 } ) : () ),
226
            ( $anonymous_patron ? ( 'items_last_borrowers.borrowernumber' => { '!=' => $anonymous_patron } ) : () ),
227
        },
227
        },
228
        {   join     => ["items_last_borrowers"],
228
        {
229
            join     => ["items_last_borrowers"],
229
            distinct => 1,
230
            distinct => 1,
230
        }
231
        }
231
    );
232
    );
Lines 233-256 sub anonymise_last_borrowers { Link Here
233
234
234
    my $nb_rows = 0;
235
    my $nb_rows = 0;
235
    while ( my $patron = $patrons->next ) {
236
    while ( my $patron = $patrons->next ) {
236
        my $last_borrowers_to_anonymise =
237
        my $last_borrowers_to_anonymise = $patron->_result->items_last_borrowers->search(
237
        $patron->_result->items_last_borrowers->search(
238
            {
238
        {
239
                (
239
            (
240
                    $older_than_date
240
                $older_than_date
241
                    ? ( created_on => { '<' => $dtf->format_datetime($older_than_date) } )
241
                ? ( created_on =>
242
                    : (),
242
                        { '<' => $dtf->format_datetime($older_than_date) } )
243
                    "itemnumber.damaged"   => 0,
243
                : (),
244
                    "itemnumber.itemlost"  => 0,
244
                "itemnumber.damaged"   => 0,
245
                    "itemnumber.withdrawn" => 0,
245
                "itemnumber.itemlost"  => 0,
246
                ),
246
                "itemnumber.withdrawn" => 0,
247
            },
247
            ),
248
            { join => ['itemnumber'] }
248
        },
249
        {
250
            join => ['itemnumber']
251
        }
252
        );
249
        );
253
        $nb_rows += $last_borrowers_to_anonymise->update( { 'items_last_borrower.borrowernumber' => $anonymous_patron } );
250
        $nb_rows +=
251
            $last_borrowers_to_anonymise->update( { 'items_last_borrower.borrowernumber' => $anonymous_patron } );
254
    }
252
    }
255
253
256
    return $nb_rows;
254
    return $nb_rows;
(-)a/misc/cronjobs/anonymise_last_borrowers.pl (-6 / +3 lines)
Lines 1-4 Link Here
1
2
#!/usr/bin/perl
1
#!/usr/bin/perl
3
2
4
# Copyright 2011, ByWater Solutions.
3
# Copyright 2011, ByWater Solutions.
Lines 36-42 use Koha::Patrons; Link Here
36
use Getopt::Long;
35
use Getopt::Long;
37
use C4::Log;
36
use C4::Log;
38
37
39
40
sub usage {
38
sub usage {
41
    print STDERR <<USAGE;
39
    print STDERR <<USAGE;
42
Usage: $0  [-h|--help]
40
Usage: $0  [-h|--help]
Lines 51-58 USAGE Link Here
51
my ( $help, $verbose );
49
my ( $help, $verbose );
52
50
53
GetOptions(
51
GetOptions(
54
    'h|help'       => \$help,
52
    'h|help'    => \$help,
55
    'v|verbose'    => \$verbose,
53
    'v|verbose' => \$verbose,
56
) || usage(1);
54
) || usage(1);
57
55
58
if ($help) {
56
if ($help) {
Lines 61-68 if ($help) { Link Here
61
59
62
my $pref = C4::Context->preference("AnonymiseLastBorrower");
60
my $pref = C4::Context->preference("AnonymiseLastBorrower");
63
61
64
62
if ( !$pref ) {
65
if( !$pref ) {
66
    print "Preference 'AnonymiseLastBorrower' must be enabled to anonymise item's last borrower\n\n";
63
    print "Preference 'AnonymiseLastBorrower' must be enabled to anonymise item's last borrower\n\n";
67
    usage(1);
64
    usage(1);
68
}
65
}
(-)a/t/db_dependent/Koha/Patrons.t (-31 / +42 lines)
Lines 1289-1302 subtest 'anonymise items_last_borrower' => sub { Link Here
1289
1289
1290
    my $anonymous = $builder->build_object( { class => 'Koha::Patrons', }, );
1290
    my $anonymous = $builder->build_object( { class => 'Koha::Patrons', }, );
1291
1291
1292
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->borrowernumber );
1292
    t::lib::Mocks::mock_preference( 'AnonymousPatron',       $anonymous->borrowernumber );
1293
    t::lib::Mocks::mock_preference('StoreLastBorrower', 1);
1293
    t::lib::Mocks::mock_preference( 'StoreLastBorrower',     1 );
1294
    t::lib::Mocks::mock_preference('AnonymiseLastBorrower', 1);
1294
    t::lib::Mocks::mock_preference( 'AnonymiseLastBorrower', 1 );
1295
1295
1296
    subtest 'anonymise items_last_borrower by days' => sub {
1296
    subtest 'anonymise items_last_borrower by days' => sub {
1297
        plan tests => 4;
1297
        plan tests => 4;
1298
1298
1299
        t::lib::Mocks::mock_preference('AnonymiseLastBorrowerDays', 5);
1299
        t::lib::Mocks::mock_preference( 'AnonymiseLastBorrowerDays', 5 );
1300
1300
1301
        my $patron = $builder->build_object(
1301
        my $patron = $builder->build_object(
1302
            {
1302
            {
Lines 1304-1310 subtest 'anonymise items_last_borrower' => sub { Link Here
1304
                value => { privacy => 1, }
1304
                value => { privacy => 1, }
1305
            }
1305
            }
1306
        );
1306
        );
1307
        my $item_1 = $builder->build_sample_item;
1307
        my $item_1  = $builder->build_sample_item;
1308
        my $issue_1 = $builder->build_object(
1308
        my $issue_1 = $builder->build_object(
1309
            {
1309
            {
1310
                class => 'Koha::Checkouts',
1310
                class => 'Koha::Checkouts',
Lines 1314-1320 subtest 'anonymise items_last_borrower' => sub { Link Here
1314
                },
1314
                },
1315
            }
1315
            }
1316
        );
1316
        );
1317
        my $item_2 = $builder->build_sample_item;
1317
        my $item_2  = $builder->build_sample_item;
1318
        my $issue_2 = $builder->build_object(
1318
        my $issue_2 = $builder->build_object(
1319
            {
1319
            {
1320
                class => 'Koha::Checkouts',
1320
                class => 'Koha::Checkouts',
Lines 1326-1354 subtest 'anonymise items_last_borrower' => sub { Link Here
1326
        );
1326
        );
1327
1327
1328
        my $dbh = C4::Context->dbh;
1328
        my $dbh = C4::Context->dbh;
1329
        my ( $returned_1 ) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string );
1329
        my ($returned_1) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string );
1330
        my ( $returned_2 ) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string );
1330
        my ($returned_2) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string );
1331
        is( $returned_1 && $returned_2, 1, 'The items should have been returned' );
1331
        is( $returned_1 && $returned_2, 1, 'The items should have been returned' );
1332
        my $stu = $dbh->prepare(q|UPDATE items_last_borrower set created_on = ? where itemnumber = ?|);
1332
        my $stu = $dbh->prepare(q|UPDATE items_last_borrower set created_on = ? where itemnumber = ?|);
1333
        $stu->execute(DateTime->today(time_zone => C4::Context->tz())->add( days => -6 ), $item_2->itemnumber);
1333
        $stu->execute( DateTime->today( time_zone => C4::Context->tz() )->add( days => -6 ), $item_2->itemnumber );
1334
1334
1335
        my $nb_rows = Koha::Patrons->anonymise_last_borrowers();
1335
        my $nb_rows = Koha::Patrons->anonymise_last_borrowers();
1336
        is( $nb_rows, 1, 'anonymise_issue_history should have returned the number of last borrowers anonymised');
1336
        is( $nb_rows, 1, 'anonymise_issue_history should have returned the number of last borrowers anonymised' );
1337
1337
1338
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1338
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1339
        $sth->execute($item_1->itemnumber);
1339
        $sth->execute( $item_1->itemnumber );
1340
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1340
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1341
        is( $borrowernumber_used_to_anonymised, $patron->borrowernumber, 'Last borrower younger than 5 days are not anonymised' );
1341
        is(
1342
        $sth->execute($item_2->itemnumber);
1342
            $borrowernumber_used_to_anonymised, $patron->borrowernumber,
1343
            'Last borrower younger than 5 days are not anonymised'
1344
        );
1345
        $sth->execute( $item_2->itemnumber );
1343
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1346
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1344
        is( $borrowernumber_used_to_anonymised, $anonymous->borrowernumber, 'Last borrower older than 5 days are anonymised' );
1347
        is(
1348
            $borrowernumber_used_to_anonymised, $anonymous->borrowernumber,
1349
            'Last borrower older than 5 days are anonymised'
1350
        );
1345
1351
1346
    };
1352
    };
1347
1353
1348
    subtest 'withrawn, lost and damaged items' => sub {
1354
    subtest 'withrawn, lost and damaged items' => sub {
1349
        plan tests => 6;
1355
        plan tests => 6;
1350
1356
1351
        t::lib::Mocks::mock_preference('AnonymiseLastBorrowerDays', 0);
1357
        t::lib::Mocks::mock_preference( 'AnonymiseLastBorrowerDays', 0 );
1352
1358
1353
        my $patron = $builder->build_object(
1359
        my $patron = $builder->build_object(
1354
            {
1360
            {
Lines 1356-1362 subtest 'anonymise items_last_borrower' => sub { Link Here
1356
                value => { privacy => 1, }
1362
                value => { privacy => 1, }
1357
            }
1363
            }
1358
        );
1364
        );
1359
        my $item_1 = $builder->build_sample_item;
1365
        my $item_1  = $builder->build_sample_item;
1360
        my $issue_1 = $builder->build_object(
1366
        my $issue_1 = $builder->build_object(
1361
            {
1367
            {
1362
                class => 'Koha::Checkouts',
1368
                class => 'Koha::Checkouts',
Lines 1366-1372 subtest 'anonymise items_last_borrower' => sub { Link Here
1366
                },
1372
                },
1367
            }
1373
            }
1368
        );
1374
        );
1369
        my $item_2 = $builder->build_sample_item;
1375
        my $item_2  = $builder->build_sample_item;
1370
        my $issue_2 = $builder->build_object(
1376
        my $issue_2 = $builder->build_object(
1371
            {
1377
            {
1372
                class => 'Koha::Checkouts',
1378
                class => 'Koha::Checkouts',
Lines 1376-1382 subtest 'anonymise items_last_borrower' => sub { Link Here
1376
                },
1382
                },
1377
            }
1383
            }
1378
        );
1384
        );
1379
        my $item_3 = $builder->build_sample_item;
1385
        my $item_3  = $builder->build_sample_item;
1380
        my $issue_3 = $builder->build_object(
1386
        my $issue_3 = $builder->build_object(
1381
            {
1387
            {
1382
                class => 'Koha::Checkouts',
1388
                class => 'Koha::Checkouts',
Lines 1386-1392 subtest 'anonymise items_last_borrower' => sub { Link Here
1386
                },
1392
                },
1387
            }
1393
            }
1388
        );
1394
        );
1389
        my $item_4 = $builder->build_sample_item;
1395
        my $item_4  = $builder->build_sample_item;
1390
        my $issue_4 = $builder->build_object(
1396
        my $issue_4 = $builder->build_object(
1391
            {
1397
            {
1392
                class => 'Koha::Checkouts',
1398
                class => 'Koha::Checkouts',
Lines 1397-1406 subtest 'anonymise items_last_borrower' => sub { Link Here
1397
            }
1403
            }
1398
        );
1404
        );
1399
1405
1400
        my ( $returned_1 ) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string('2010-10-11') );
1406
        my ($returned_1) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string('2010-10-11') );
1401
        my ( $returned_2 ) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string('2010-10-11') );
1407
        my ($returned_2) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string('2010-10-11') );
1402
        my ( $returned_3 ) = C4::Circulation::AddReturn( $item_3->barcode, undef, undef, dt_from_string('2010-10-11') );
1408
        my ($returned_3) = C4::Circulation::AddReturn( $item_3->barcode, undef, undef, dt_from_string('2010-10-11') );
1403
        my ( $returned_4 ) = C4::Circulation::AddReturn( $item_4->barcode, undef, undef, dt_from_string('2010-10-11') );
1409
        my ($returned_4) = C4::Circulation::AddReturn( $item_4->barcode, undef, undef, dt_from_string('2010-10-11') );
1404
        is( $returned_1 && $returned_2 && $returned_3 && $returned_4, 1, 'The items should have been returned' );
1410
        is( $returned_1 && $returned_2 && $returned_3 && $returned_4, 1, 'The items should have been returned' );
1405
        $item_1->withdrawn(1)->store;
1411
        $item_1->withdrawn(1)->store;
1406
        $item_2->itemlost(1)->store;
1412
        $item_2->itemlost(1)->store;
Lines 1408-1431 subtest 'anonymise items_last_borrower' => sub { Link Here
1408
1414
1409
        my $dbh = C4::Context->dbh;
1415
        my $dbh = C4::Context->dbh;
1410
        my $stu = $dbh->prepare(q|UPDATE items_last_borrower set created_on = ? where itemnumber in (?, ?, ?, ?)|);
1416
        my $stu = $dbh->prepare(q|UPDATE items_last_borrower set created_on = ? where itemnumber in (?, ?, ?, ?)|);
1411
        $stu->execute(DateTime->today(time_zone => C4::Context->tz())->add( days => -1 ), $item_1->itemnumber,$item_2->itemnumber,$item_3->itemnumber,$item_4->itemnumber);
1417
        $stu->execute(
1418
            DateTime->today( time_zone => C4::Context->tz() )->add( days => -1 ), $item_1->itemnumber,
1419
            $item_2->itemnumber, $item_3->itemnumber, $item_4->itemnumber
1420
        );
1412
1421
1413
        my $nb_rows = Koha::Patrons->anonymise_last_borrowers();
1422
        my $nb_rows = Koha::Patrons->anonymise_last_borrowers();
1414
        is( $nb_rows, 1, 'anonymise_issue_history should have returned the number of last borrowers anonymised');
1423
        is( $nb_rows, 1, 'anonymise_issue_history should have returned the number of last borrowers anonymised' );
1415
1424
1416
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1425
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1417
        $sth->execute($item_1->itemnumber);
1426
        $sth->execute( $item_1->itemnumber );
1418
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1427
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1419
        is( $borrowernumber_used_to_anonymised, $patron->borrowernumber, 'withrawn items should not be anonymised' );
1428
        is( $borrowernumber_used_to_anonymised, $patron->borrowernumber, 'withrawn items should not be anonymised' );
1420
        $sth->execute($item_2->itemnumber);
1429
        $sth->execute( $item_2->itemnumber );
1421
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1430
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1422
        is( $borrowernumber_used_to_anonymised, $patron->borrowernumber, 'lost items should not be anonymised' );
1431
        is( $borrowernumber_used_to_anonymised, $patron->borrowernumber, 'lost items should not be anonymised' );
1423
        $sth->execute($item_3->itemnumber);
1432
        $sth->execute( $item_3->itemnumber );
1424
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1433
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1425
        is( $borrowernumber_used_to_anonymised, $patron->borrowernumber, 'damaged items should not be anonymised' );
1434
        is( $borrowernumber_used_to_anonymised, $patron->borrowernumber, 'damaged items should not be anonymised' );
1426
        $sth->execute($item_4->itemnumber);
1435
        $sth->execute( $item_4->itemnumber );
1427
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1436
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1428
        is( $borrowernumber_used_to_anonymised, $anonymous->borrowernumber, 'not withdrawn, lost or damaged items are anonymised' );
1437
        is(
1438
            $borrowernumber_used_to_anonymised, $anonymous->borrowernumber,
1439
            'not withdrawn, lost or damaged items are anonymised'
1440
        );
1429
1441
1430
    };
1442
    };
1431
1443
1432
- 

Return to bug 23260