Bug 32482: (follow-up) Add markup comments
[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
21 use Koha::Database;
22 use Koha::Exceptions::Config;
23 use Koha::Patrons;
24
25 use base qw(Koha::Object);
26
27 =head1 NAME
28
29 Koha::PseudonymizedTransaction - Koha Koha::PseudonymizedTransaction Object class
30
31 =head1 API
32
33 =head2 Class methods
34
35 =head3 new_from_statistic
36
37     Creates new object from a passed Koha::Statistic object
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 =head3 get_hash
103
104     Generates a hashed value for $s (e.g. borrowernumber) with Bcrypt.
105     Needs config entry 'bcrypt_settings' in koha-conf.
106
107 =cut
108
109 sub get_hash {
110     my ( $class, $s ) = @_;
111     my $bcrypt_settings = C4::Context->config('bcrypt_settings');
112
113     Koha::Exceptions::Config::MissingEntry->throw(
114         "Missing 'bcrypt_settings' entry in config file") unless $bcrypt_settings;
115
116     return bcrypt($s, $bcrypt_settings);
117 }
118
119 =head2 Internal methods
120
121 =head3 _type
122
123 =cut
124
125 sub _type {
126     return 'PseudonymizedTransaction';
127 }
128
129 1;