Bug 35536: Add RemovePlugins calls in plugin unit tests
[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 Crypt::Eksblowfish::Bcrypt qw( bcrypt );
20 use List::MoreUtils            qw(any);
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_from_statistic
37
38     Creates new object from a passed Koha::Statistic object
39
40 =cut
41
42 sub new_from_statistic {
43     my ( $class, $statistic ) = @_;
44
45     my $values = {
46         hashed_borrowernumber => $class->get_hash($statistic->borrowernumber),
47     };
48
49     my @t_fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || '';
50
51     my $statistic_item = $statistic->item;
52
53     # These fields are treated separately as the column names do not match
54     if ( any { $_ eq 'transaction_branchcode' } @t_fields_to_copy ) {
55         $values->{transaction_branchcode} = $statistic->branch;
56     }
57     if ( any { $_ eq 'transaction_type' } @t_fields_to_copy ) {
58         $values->{transaction_type} = $statistic->type;
59     }
60
61     # These fields are not captured in the statistic so must be pulled from the item
62     if ($statistic_item) {
63         if ( any { $_ eq 'homebranch' } @t_fields_to_copy ) {
64             $values->{homebranch} = $statistic_item->homebranch;
65         }
66         if ( any { $_ eq 'holdingbranch' } @t_fields_to_copy ) {
67             $values->{holdingbranch} = $statistic_item->holdingbranch;
68         }
69         if ( any { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
70             $values->{itemcallnumber} = $statistic_item->itemcallnumber;
71         }
72     }
73
74     # Remove fields we have already handled from the list
75     @t_fields_to_copy = grep {
76              $_ ne 'transaction_branchcode'
77           && $_ ne 'holdingbranch'
78           && $_ ne 'homebranch'
79           && $_ ne 'transaction_type'
80           && $_ ne 'itemcallnumber'
81     } @t_fields_to_copy;
82
83     # Populate the remaining columns
84     $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
85
86     my $patron = Koha::Patrons->find( $statistic->borrowernumber );
87     if ($patron) {
88         my @p_fields_to_copy = split ',', C4::Context->preference('PseudonymizationPatronFields') || '';
89         $values = { %$values, map { $_ => $patron->$_ } @p_fields_to_copy };
90
91         $values->{branchcode}   = $patron->branchcode;  # FIXME Must be removed from the pref options, or FK removed (?)
92         $values->{categorycode} = $patron->categorycode;
93
94         $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
95     }
96
97     my $self = $class->SUPER::new($values);
98
99     if ($patron) {
100         my $extended_attributes = $patron->extended_attributes->unblessed;
101         for my $attribute (@$extended_attributes) {
102             next unless Koha::Patron::Attribute::Types->find( $attribute->{code} )->keep_for_pseudonymization;
103
104             delete $attribute->{id};
105             delete $attribute->{borrowernumber};
106
107             $self->_result->create_related( 'pseudonymized_borrower_attributes', $attribute );
108         }
109     }
110
111     return $self;
112 }
113
114 =head3 get_hash
115
116     Generates a hashed value for $s (e.g. borrowernumber) with Bcrypt.
117     Needs config entry 'bcrypt_settings' in koha-conf.
118
119 =cut
120
121 sub get_hash {
122     my ( $class, $s ) = @_;
123     my $bcrypt_settings = C4::Context->config('bcrypt_settings');
124
125     Koha::Exceptions::Config::MissingEntry->throw(
126         "Missing 'bcrypt_settings' entry in config file") unless $bcrypt_settings;
127
128     return bcrypt($s, $bcrypt_settings);
129 }
130
131 =head2 Internal methods
132
133 =head3 _type
134
135 =cut
136
137 sub _type {
138     return 'PseudonymizedTransaction';
139 }
140
141 1;