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

(-)a/C4/Circulation.pm (-11 / +34 lines)
Lines 47-52 use Algorithm::CheckDigits; Link Here
47
use Data::Dumper;
47
use Data::Dumper;
48
use Koha::DateUtils;
48
use Koha::DateUtils;
49
use Koha::Calendar;
49
use Koha::Calendar;
50
use Koha::Database;
50
use Koha::Borrower::Debarments;
51
use Koha::Borrower::Debarments;
51
use Carp;
52
use Carp;
52
use Date::Calc qw(
53
use Date::Calc qw(
Lines 2000-2016 sub MarkIssueReturned { Link Here
2000
                                  WHERE borrowernumber = ?
2001
                                  WHERE borrowernumber = ?
2001
                                  AND itemnumber = ?');
2002
                                  AND itemnumber = ?');
2002
    $sth_copy->execute($borrowernumber, $itemnumber);
2003
    $sth_copy->execute($borrowernumber, $itemnumber);
2003
    # anonymise patron checkout immediately if $privacy set to 2 and AnonymousPatron is set to a valid borrowernumber
2004
    AnonymizeItemIssueHistory({ itemnumber => $itemnumber });
2004
    if ( $privacy == 2) {
2005
        # The default of 0 does not work due to foreign key constraints
2006
        # The anonymisation will fail quietly if AnonymousPatron is not a valid entry
2007
        # FIXME the above is unacceptable - bug 9942 relates
2008
        my $anonymouspatron = (C4::Context->preference('AnonymousPatron')) ? C4::Context->preference('AnonymousPatron') : 0;
2009
        my $sth_ano = $dbh->prepare("UPDATE old_issues SET borrowernumber=?
2010
                                  WHERE borrowernumber = ?
2011
                                  AND itemnumber = ?");
2012
       $sth_ano->execute($anonymouspatron, $borrowernumber, $itemnumber);
2013
    }
2014
    my $sth_del  = $dbh->prepare("DELETE FROM issues
2005
    my $sth_del  = $dbh->prepare("DELETE FROM issues
2015
                                  WHERE borrowernumber = ?
2006
                                  WHERE borrowernumber = ?
2016
                                  AND itemnumber = ?");
2007
                                  AND itemnumber = ?");
Lines 3522-3527 sub IsItemIssued { Link Here
3522
    return $sth->fetchrow;
3513
    return $sth->fetchrow;
3523
}
3514
}
3524
3515
3516
=head AnonymizeItemIssueHistory
3517
3518
    AnonymizeItemIssueHistory({ itemnumber => $itemnumber });
3519
3520
=cut
3521
3522
sub AnonymizeItemIssueHistory {
3523
    my ( $params ) = @_;
3524
3525
    my $itemnumber     = $params->{itemnumber};
3526
    return unless $itemnumber;
3527
    
3528
    my $a = C4::Context->preference('AnonymousPatron');
3529
    return unless $a;
3530
3531
    my $schema = Koha::Database->new()->schema();
3532
3533
    my $old_issues_rs = $schema->resultset('OldIssue')->search(
3534
        {
3535
            'me.itemnumber'     => $itemnumber,
3536
            'me.borrowernumber' => { '!=' => $a },
3537
        },
3538
        { prefetch => 'borrower', order_by => { -desc => 'issue_id' } }
3539
    );
3540
3541
    my $oi = $old_issues_rs->next();
3542
    if ( $oi->borrower()->privacy() == 2 ) {
3543
        $oi->set_column( 'borrowernumber', $a );
3544
        $oi->update();
3545
    }
3546
}
3547
3525
1;
3548
1;
3526
3549
3527
__END__
3550
__END__
(-)a/Koha/Schema/Result/OldIssue.pm (+6 lines)
Lines 156-161 __PACKAGE__->belongs_to( Link Here
156
# Created by DBIx::Class::Schema::Loader v0.07000 @ 2013-11-07 08:15:21
156
# Created by DBIx::Class::Schema::Loader v0.07000 @ 2013-11-07 08:15:21
157
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:np3nmo0XYIYp92QbCY6/cw
157
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:np3nmo0XYIYp92QbCY6/cw
158
158
159
__PACKAGE__->belongs_to(
160
  "borrower",
161
  "Koha::Schema::Result::Borrower",
162
  { borrowernumber => "borrowernumber" },
163
  { join_type => "LEFT", on_delete => "CASCADE", on_update => "CASCADE" },
164
);
159
165
160
# You can replace this text with custom content, and it will be preserved on regeneration
166
# You can replace this text with custom content, and it will be preserved on regeneration
161
1;
167
1;
(-)a/t/db_dependent/Circulation_anonymization.t (-1 / +153 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use C4::Context;
6
use C4::Circulation;
7
use Koha::Database;
8
9
use Test::More tests => 4;
10
11
BEGIN {
12
    use_ok('C4::Circulation');
13
    use_ok('Koha::Database');
14
}
15
16
#Start transaction
17
my $dbh = C4::Context->dbh;
18
$dbh->{RaiseError} = 1;
19
$dbh->{AutoCommit} = 0;
20
21
$dbh->do(q|DELETE FROM issues|);
22
$dbh->do(q|DELETE FROM items|);
23
$dbh->do(q|DELETE FROM borrowers|);
24
$dbh->do(q|DELETE FROM branches|);
25
$dbh->do(q|DELETE FROM categories|);
26
$dbh->do(q|DELETE FROM accountlines|);
27
$dbh->do(q|DELETE FROM issuingrules|);
28
29
my $schema = Koha::Database->new()->schema();
30
31
#Add branch and category
32
my $samplebranch1 = $schema->resultset('Branch')->create(
33
    {
34
        branchcode     => 'CPL',
35
        branchname     => 'Sample Branch',
36
        branchaddress1 => 'sample adr1',
37
        branchaddress2 => 'sample adr2',
38
        branchaddress3 => 'sample adr3',
39
        branchzip      => 'sample zip',
40
        branchcity     => 'sample city',
41
        branchstate    => 'sample state',
42
        branchcountry  => 'sample country',
43
        branchphone    => 'sample phone',
44
        branchfax      => 'sample fax',
45
        branchemail    => 'sample email',
46
        branchurl      => 'sample url',
47
        branchip       => 'sample ip',
48
        branchprinter  => undef,
49
        opac_info      => 'sample opac',
50
    }
51
);
52
53
my $samplecat = $schema->resultset('Category')->create(
54
    {
55
        categorycode          => 'CAT1',
56
        description           => 'Description1',
57
        enrolmentperiod       => 'Null',
58
        enrolmentperioddate   => 'Null',
59
        dateofbirthrequired   => 'Null',
60
        finetype              => 'Null',
61
        bulk                  => 'Null',
62
        enrolmentfee          => 'Null',
63
        overduenoticerequired => 'Null',
64
        issuelimit            => 'Null',
65
        reservefee            => 'Null',
66
        hidelostitems         => 0,
67
        category_type         => 'Null'
68
    }
69
);
70
71
#Add biblio and item
72
my $record = MARC::Record->new();
73
$record->append_fields(
74
    MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
75
my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
76
77
my $itemnumber;
78
( $biblionumber, $biblioitemnumber, $itemnumber ) = C4::Items::AddItem(
79
    {
80
        barcode        => 'barcode_1',
81
        itemcallnumber => 'callnumber1',
82
        homebranch     => $samplebranch1->branchcode(),
83
        holdingbranch  => $samplebranch1->branchcode(),
84
        issue          => 1,
85
        reserve        => 1
86
    },
87
    $biblionumber
88
);
89
90
my $anonymous_patron = $schema->resultset('Borrower')->create(
91
    {
92
        firstname    => 'Anonymous',
93
        surname      => 'Patron',
94
        categorycode => $samplecat->categorycode(),
95
        branchcode   => $samplebranch1->branchcode(),
96
    }
97
);
98
C4::Context->set_preference( 'AnonymousPatron', $anonymous_patron->borrowernumber() );
99
100
my $borrower1 = $schema->resultset('Borrower')->create(
101
    {
102
        firstname    => 'firstname',
103
        surname      => 'surname',
104
        categorycode => $samplecat->categorycode(),
105
        branchcode   => $samplebranch1->branchcode(),
106
        privacy      => 2,
107
    }
108
);
109
my $borrower_1 = C4::Members::GetMember( borrowernumber => $borrower1->borrowernumber() );
110
111
my $borrower2 = $schema->resultset('Borrower')->create(
112
    {
113
        firstname    => 'firstname',
114
        surname      => 'surname',
115
        categorycode => $samplecat->categorycode(),
116
        branchcode   => $samplebranch1->branchcode(),
117
        privacy      => 1,
118
    }
119
);
120
my $borrower_2 = C4::Members::GetMember( borrowernumber => $borrower2->borrowernumber() );
121
122
# NEED TO BE FIXED !!!
123
# The first parameter for set_userenv is the class ref
124
#my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'username', 'CPL', 'CPL', 'email@example.org' );
125
my @USERENV = ( $borrower1->borrowernumber(), 'test', 'MASTERTEST', 'firstname', 'CPL', 'CPL', 'email@example.org' );
126
127
C4::Context->_new_userenv('DUMMY_SESSION_ID');
128
C4::Context->set_userenv(@USERENV);
129
130
my $userenv = C4::Context->userenv
131
  or BAIL_OUT("No userenv");
132
133
#Begin Tests
134
135
AddIssue( $borrower_1, 'barcode_1' );
136
AddReturn('barcode_1');
137
my $old_issue =
138
  $schema->resultset('OldIssue')->search( { itemnumber => $itemnumber },
139
    { order_by => { -desc => 'issue_id' } } )->next();
140
ok( $old_issue->borrower()->borrowernumber() == $anonymous_patron->borrowernumber(), "Patron with privacy of 2 anonymized" );
141
142
my $next_issue_id = $old_issue->issue_id() + 1;
143
144
145
AddIssue( $borrower_2, 'barcode_1' );
146
AddReturn('barcode_1');
147
$old_issue =
148
  $schema->resultset('OldIssue')->find( $next_issue_id );
149
ok( $old_issue->borrower()->borrowernumber() == $borrower2->borrowernumber(), "Patron with privacy of 1 not anonymized" );
150
151
152
#End transaction
153
$dbh->rollback;

Return to bug 9011