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 2058-2074 sub MarkIssueReturned { Link Here
2058
                                  WHERE borrowernumber = ?
2059
                                  WHERE borrowernumber = ?
2059
                                  AND itemnumber = ?');
2060
                                  AND itemnumber = ?');
2060
    $sth_copy->execute($borrowernumber, $itemnumber);
2061
    $sth_copy->execute($borrowernumber, $itemnumber);
2061
    # anonymise patron checkout immediately if $privacy set to 2 and AnonymousPatron is set to a valid borrowernumber
2062
    AnonymizeItemIssueHistory({ itemnumber => $itemnumber });
2062
    if ( $privacy == 2) {
2063
        # The default of 0 does not work due to foreign key constraints
2064
        # The anonymisation will fail quietly if AnonymousPatron is not a valid entry
2065
        # FIXME the above is unacceptable - bug 9942 relates
2066
        my $anonymouspatron = (C4::Context->preference('AnonymousPatron')) ? C4::Context->preference('AnonymousPatron') : 0;
2067
        my $sth_ano = $dbh->prepare("UPDATE old_issues SET borrowernumber=?
2068
                                  WHERE borrowernumber = ?
2069
                                  AND itemnumber = ?");
2070
       $sth_ano->execute($anonymouspatron, $borrowernumber, $itemnumber);
2071
    }
2072
    my $sth_del  = $dbh->prepare("DELETE FROM issues
2063
    my $sth_del  = $dbh->prepare("DELETE FROM issues
2073
                                  WHERE borrowernumber = ?
2064
                                  WHERE borrowernumber = ?
2074
                                  AND itemnumber = ?");
2065
                                  AND itemnumber = ?");
Lines 3772-3777 sub GetAgeRestriction { Link Here
3772
    return $restriction_year;
3763
    return $restriction_year;
3773
}
3764
}
3774
3765
3766
=head AnonymizeItemIssueHistory
3767
3768
    AnonymizeItemIssueHistory({ itemnumber => $itemnumber });
3769
3770
=cut
3771
3772
sub AnonymizeItemIssueHistory {
3773
    my ( $params ) = @_;
3774
3775
    my $itemnumber     = $params->{itemnumber};
3776
    return unless $itemnumber;
3777
3778
    my $a = C4::Context->preference('AnonymousPatron');
3779
    return unless $a;
3780
3781
    my $schema = Koha::Database->new()->schema();
3782
3783
    my $old_issues_rs = $schema->resultset('OldIssue')->search(
3784
        {
3785
            'me.itemnumber'     => $itemnumber,
3786
            'me.borrowernumber' => { '!=' => $a },
3787
        },
3788
        { prefetch => 'borrower', order_by => { -desc => 'issue_id' } }
3789
    );
3790
3791
    my $oi = $old_issues_rs->next();
3792
    if ( $oi->borrower()->privacy() == 2 ) {
3793
        $oi->set_column( 'borrowernumber', $a );
3794
        $oi->update();
3795
    }
3796
}
3797
3775
1;
3798
1;
3776
3799
3777
__END__
3800
__END__
(-)a/Koha/Schema/Result/OldIssue.pm (+6 lines)
Lines 183-188 __PACKAGE__->belongs_to( Link Here
183
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-07-11 09:26:55
183
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-07-11 09:26:55
184
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:sgsDHihVYHrTycbaqoGbaQ
184
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:sgsDHihVYHrTycbaqoGbaQ
185
185
186
__PACKAGE__->belongs_to(
187
  "borrower",
188
  "Koha::Schema::Result::Borrower",
189
  { borrowernumber => "borrowernumber" },
190
  { join_type => "LEFT", on_delete => "CASCADE", on_update => "CASCADE" },
191
);
186
192
187
# You can replace this text with custom content, and it will be preserved on regeneration
193
# You can replace this text with custom content, and it will be preserved on regeneration
188
1;
194
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