Bug 24151: (QA follow-up) Correct license in two modules
[koha.git] / Koha / PseudonymizedTransaction.pm
1 package Koha::PseudonymizedTransaction;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with Koha; if not, see <http://www.gnu.org/licenses>.
16
17 use Modern::Perl;
18
19 use Carp;
20 use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64);
21
22 use Koha::Database;
23 use Koha::Exceptions::Config;
24 use Koha::Patrons;
25
26 use base qw(Koha::Object);
27
28 =head1 NAME
29
30 Koha::PseudonymizedTransaction - Koha Koha::PseudonymizedTransaction Object class
31
32 =head1 API
33
34 =head2 Class methods
35
36 =head3 new
37
38 =cut
39
40 sub new_from_statistic {
41     my ( $class, $statistic ) = @_;
42
43     my $values = {
44         hashed_borrowernumber => $class->get_hash($statistic->borrowernumber),
45     };
46
47     my @t_fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || '';
48
49     if ( grep { $_ eq 'transaction_branchcode' } @t_fields_to_copy ) {
50         $values->{transaction_branchcode} = $statistic->branch;
51     }
52     if ( grep { $_ eq 'holdingbranch' } @t_fields_to_copy ) {
53         $values->{holdingbranch} = $statistic->item->holdingbranch;
54     }
55     if ( grep { $_ eq 'homebranch' } @t_fields_to_copy ) {
56         $values->{homebranch} = $statistic->item->homebranch;
57     }
58     if ( grep { $_ eq 'transaction_type' } @t_fields_to_copy ) {
59         $values->{transaction_type} = $statistic->type;
60     }
61     if ( grep { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
62         $values->{itemcallnumber} = $statistic->item->itemcallnumber;
63     }
64
65
66     @t_fields_to_copy = grep {
67              $_ ne 'transaction_branchcode'
68           && $_ ne 'holdingbranch'
69           && $_ ne 'homebranch'
70           && $_ ne 'transaction_type'
71           && $_ ne 'itemcallnumber'
72     } @t_fields_to_copy;
73
74     $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
75
76     my $patron = Koha::Patrons->find($statistic->borrowernumber);
77     my @p_fields_to_copy = split ',', C4::Context->preference('PseudonymizationPatronFields') || '';
78     $values = { %$values, map { $_ => $patron->$_ } @p_fields_to_copy };
79
80     $values->{branchcode} = $patron->branchcode; # FIXME Must be removed from the pref options, or FK removed (?)
81     $values->{categorycode} = $patron->categorycode;
82
83     $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
84
85     my $self = $class->SUPER::new($values);
86
87     my $extended_attributes = $patron->extended_attributes->unblessed;
88     for my $attribute (@$extended_attributes) {
89         next unless Koha::Patron::Attribute::Types->find(
90             $attribute->{code} )->keep_for_pseudonymization;
91
92         delete $attribute->{id};
93         delete $attribute->{borrowernumber};
94
95         $self->_result->create_related('pseudonymized_borrower_attributes', $attribute);
96     }
97
98     return $self;
99 }
100
101 sub get_hash {
102     my ( $class, $s ) = @_;
103     my $key = C4::Context->config('key');
104
105     Koha::Exceptions::Config::MissingEntry->throw(
106         "Missing 'key' entry in config file") unless $key;
107
108     return bcrypt($s, $key);
109 }
110
111 =head2 Internal methods
112
113 =head3 _type
114
115 =cut
116
117 sub _type {
118     return 'PseudonymizedTransaction';
119 }
120
121 1;