Bug 24151: Sync patron's attributes
[koha.git] / Koha / PseudonymizedTransaction.pm
blobc5b0e8836f9f02fdeab63cce55aa29a77b798b31
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 along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 use Modern::Perl;
20 use Carp;
21 use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64);
23 use Koha::Database;
24 use Koha::Exceptions::Config;
25 use Koha::Patrons;
27 use base qw(Koha::Object);
29 =head1 NAME
31 Koha::PseudonymizedTransaction - Koha Koha::PseudonymizedTransaction Object class
33 =head1 API
35 =head2 Class methods
37 =head3 new
39 =cut
41 sub new_from_statistic {
42 my ( $class, $statistic ) = @_;
44 my $values = {
45 hashed_borrowernumber => $class->get_hash($statistic->borrowernumber),
48 my @t_fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || '';
50 if ( grep { $_ eq 'transaction_branchcode' } @t_fields_to_copy ) {
51 $values->{transaction_branchcode} = $statistic->branch;
53 if ( grep { $_ eq 'holdingbranch' } @t_fields_to_copy ) {
54 $values->{holdingbranch} = $statistic->item->holdingbranch;
56 if ( grep { $_ eq 'transaction_type' } @t_fields_to_copy ) {
57 $values->{transaction_type} = $statistic->type;
59 if ( grep { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
60 $values->{itemcallnumber} = $statistic->item->itemcallnumber;
64 @t_fields_to_copy = grep {
65 $_ ne 'transaction_branchcode'
66 && $_ ne 'holdingbranch'
67 && $_ ne 'transaction_type'
68 && $_ ne 'itemcallnumber'
69 } @t_fields_to_copy;
71 $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
73 my $patron = Koha::Patrons->find($statistic->borrowernumber);
74 my @p_fields_to_copy = split ',', C4::Context->preference('PseudonymizationPatronFields') || '';
75 $values = { %$values, map { $_ => $patron->$_ } @p_fields_to_copy };
77 $values->{branchcode} = $patron->branchcode; # FIXME Must be removed from the pref options, or FK removed (?)
78 $values->{categorycode} = $patron->categorycode;
80 $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
82 my $self = $class->SUPER::new($values);
84 my $extended_attributes = $patron->extended_attributes->unblessed;
85 for my $attribute (@$extended_attributes) {
86 next unless Koha::Patron::Attribute::Types->find(
87 $attribute->{code} )->keep_for_pseudonymization;
89 delete $attribute->{id};
90 delete $attribute->{borrowernumber};
92 $self->_result->create_related('pseudonymized_borrower_attributes', $attribute);
95 return $self;
98 sub get_hash {
99 my ( $class, $s ) = @_;
100 my $key = C4::Context->config('key');
102 Koha::Exceptions::Config::MissingEntry->throw(
103 "Missing 'key' entry in config file") unless $key;
105 return bcrypt($s, $key);
108 =head2 Internal methods
110 =head3 _type
112 =cut
114 sub _type {
115 return 'PseudonymizedTransaction';