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