Bug 26922: Regression tests
[koha.git] / Koha / PseudonymizedTransaction.pm
blob78a1190f7c236b2e3723c7d7f0d69afe1baaa896
1 package Koha::PseudonymizedTransaction;
3 # This file is part of Koha.
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.
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.
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>.
17 use Modern::Perl;
19 use Carp;
20 use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64);
22 use Koha::Database;
23 use Koha::Exceptions::Config;
24 use Koha::Patrons;
26 use base qw(Koha::Object);
28 =head1 NAME
30 Koha::PseudonymizedTransaction - Koha Koha::PseudonymizedTransaction Object class
32 =head1 API
34 =head2 Class methods
36 =head3 new_from_statistic
38 Creates new object from a passed Koha::Statistic object
40 =cut
42 sub new_from_statistic {
43 my ( $class, $statistic ) = @_;
45 my $values = {
46 hashed_borrowernumber => $class->get_hash($statistic->borrowernumber),
49 my @t_fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || '';
51 if ( grep { $_ eq 'transaction_branchcode' } @t_fields_to_copy ) {
52 $values->{transaction_branchcode} = $statistic->branch;
54 if ( grep { $_ eq 'holdingbranch' } @t_fields_to_copy ) {
55 $values->{holdingbranch} = $statistic->item->holdingbranch;
57 if ( grep { $_ eq 'homebranch' } @t_fields_to_copy ) {
58 $values->{homebranch} = $statistic->item->homebranch;
60 if ( grep { $_ eq 'transaction_type' } @t_fields_to_copy ) {
61 $values->{transaction_type} = $statistic->type;
63 if ( grep { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
64 $values->{itemcallnumber} = $statistic->item->itemcallnumber;
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;
76 $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
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 };
82 $values->{branchcode} = $patron->branchcode; # FIXME Must be removed from the pref options, or FK removed (?)
83 $values->{categorycode} = $patron->categorycode;
85 $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
87 my $self = $class->SUPER::new($values);
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;
94 delete $attribute->{id};
95 delete $attribute->{borrowernumber};
97 $self->_result->create_related('pseudonymized_borrower_attributes', $attribute);
100 return $self;
103 =head3 get_hash
105 Generates a hashed value for $s (e.g. borrowernumber) with Bcrypt.
106 Needs config entry 'bcrypt_settings' in koha-conf.
108 =cut
110 sub get_hash {
111 my ( $class, $s ) = @_;
112 my $bcrypt_settings = C4::Context->config('bcrypt_settings');
114 Koha::Exceptions::Config::MissingEntry->throw(
115 "Missing 'bcrypt_settings' entry in config file") unless $bcrypt_settings;
117 return bcrypt($s, $bcrypt_settings);
120 =head2 Internal methods
122 =head3 _type
124 =cut
126 sub _type {
127 return 'PseudonymizedTransaction';