Bug 24151: Add items.homebranch to the list
[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 'homebranch' } @t_fields_to_copy ) {
57         $values->{homebranch} = $statistic->item->homebranch;
58     }
59     if ( grep { $_ eq 'transaction_type' } @t_fields_to_copy ) {
60         $values->{transaction_type} = $statistic->type;
61     }
62     if ( grep { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
63         $values->{itemcallnumber} = $statistic->item->itemcallnumber;
64     }
65
66
67     @t_fields_to_copy = grep {
68              $_ ne 'transaction_branchcode'
69           && $_ ne 'holdingbranch'
70           && $_ ne 'homebranch'
71           && $_ ne 'transaction_type'
72           && $_ ne 'itemcallnumber'
73     } @t_fields_to_copy;
74
75     $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
76
77     my $patron = Koha::Patrons->find($statistic->borrowernumber);
78     my @p_fields_to_copy = split ',', C4::Context->preference('PseudonymizationPatronFields') || '';
79     $values = { %$values, map { $_ => $patron->$_ } @p_fields_to_copy };
80
81     $values->{branchcode} = $patron->branchcode; # FIXME Must be removed from the pref options, or FK removed (?)
82     $values->{categorycode} = $patron->categorycode;
83
84     $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
85
86     my $self = $class->SUPER::new($values);
87
88     my $extended_attributes = $patron->extended_attributes->unblessed;
89     for my $attribute (@$extended_attributes) {
90         next unless Koha::Patron::Attribute::Types->find(
91             $attribute->{code} )->keep_for_pseudonymization;
92
93         delete $attribute->{id};
94         delete $attribute->{borrowernumber};
95
96         $self->_result->create_related('pseudonymized_borrower_attributes', $attribute);
97     }
98
99     return $self;
100 }
101
102 sub get_hash {
103     my ( $class, $s ) = @_;
104     my $key = C4::Context->config('key');
105
106     Koha::Exceptions::Config::MissingEntry->throw(
107         "Missing 'key' entry in config file") unless $key;
108
109     return bcrypt($s, $key);
110 }
111
112 =head2 Internal methods
113
114 =head3 _type
115
116 =cut
117
118 sub _type {
119     return 'PseudonymizedTransaction';
120 }
121
122 1;