Bug 24151: Copy info to the pseudonymized table when a transaction is done
[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 along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Carp;
21 use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64);
22
23 use Koha::Database;
24 use Koha::Exceptions::Config;
25 use Koha::Patrons;
26
27 use base qw(Koha::Object);
28
29 =head1 NAME
30
31 Koha::PseudonymizedTransaction - Koha Koha::PseudonymizedTransaction Object class
32
33 =head1 API
34
35 =head2 Class methods
36
37 =head3 new
38
39 =cut
40
41 sub new_from_statistic {
42     my ( $class, $statistic ) = @_;
43
44     my $values = {
45         hashed_borrowernumber => $class->get_hash($statistic->borrowernumber),
46     };
47
48     my @t_fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || '';
49
50     if ( grep { $_ eq 'transaction_branchcode' } @t_fields_to_copy ) {
51         $values->{transaction_branchcode} = $statistic->branch;
52     }
53     if ( grep { $_ eq 'holdingbranch' } @t_fields_to_copy ) {
54         $values->{holdingbranch} = $statistic->item->holdingbranch;
55     }
56     if ( grep { $_ eq 'transaction_type' } @t_fields_to_copy ) {
57         $values->{transaction_type} = $statistic->type;
58     }
59     if ( grep { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
60         $values->{itemcallnumber} = $statistic->item->itemcallnumber;
61     }
62
63
64     @t_fields_to_copy = grep {
65              $_ ne 'transaction_branchcode'
66           && $_ ne 'holdingbranch'
67           && $_ ne 'transaction_type'
68           && $_ ne 'itemcallnumber'
69     } @t_fields_to_copy;
70
71     $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
72
73     my $patron = Koha::Patrons->find($statistic->borrowernumber);
74     my @p_fields_to_copy = split ',', C4::Context->preference('PseudonymizationPatronFields') || '';
75     $values = { %$values, map { $_ => $patron->$_ } @p_fields_to_copy };
76
77     $values->{branchcode} = $patron->branchcode; # FIXME Must be removed from the pref options, or FK removed (?)
78     $values->{categorycode} = $patron->categorycode;
79
80     $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
81
82     return $class->SUPER::new($values);
83 }
84
85 sub get_hash {
86     my ( $class, $s ) = @_;
87     my $key = C4::Context->config('key');
88
89     Koha::Exceptions::Config::MissingEntry->throw(
90         "Missing 'key' entry in config file") unless $key;
91
92     return bcrypt($s, $key);
93 }
94
95 =head2 Internal methods
96
97 =head3 _type
98
99 =cut
100
101 sub _type {
102     return 'PseudonymizedTransaction';
103 }
104
105 1;