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
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>.
19 use Crypt::Eksblowfish::Bcrypt qw( bcrypt );
20 use List::MoreUtils qw(any);
23 use Koha::Exceptions::Config;
26 use base qw(Koha::Object);
30 Koha::PseudonymizedTransaction - Koha Koha::PseudonymizedTransaction Object class
36 =head3 new_from_statistic
38 Creates new object from a passed Koha::Statistic object
42 sub new_from_statistic {
43 my ( $class, $statistic ) = @_;
46 hashed_borrowernumber => $class->get_hash($statistic->borrowernumber),
49 my @t_fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || '';
51 my $statistic_item = $statistic->item;
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;
57 if ( any { $_ eq 'transaction_type' } @t_fields_to_copy ) {
58 $values->{transaction_type} = $statistic->type;
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;
66 if ( any { $_ eq 'holdingbranch' } @t_fields_to_copy ) {
67 $values->{holdingbranch} = $statistic_item->holdingbranch;
69 if ( any { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
70 $values->{itemcallnumber} = $statistic_item->itemcallnumber;
74 # Remove fields we have already handled from the list
75 @t_fields_to_copy = grep {
76 $_ ne 'transaction_branchcode'
77 && $_ ne 'holdingbranch'
79 && $_ ne 'transaction_type'
80 && $_ ne 'itemcallnumber'
83 # Populate the remaining columns
84 $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
86 my $patron = Koha::Patrons->find( $statistic->borrowernumber );
88 my @p_fields_to_copy = split ',', C4::Context->preference('PseudonymizationPatronFields') || '';
89 $values = { %$values, map { $_ => $patron->$_ } @p_fields_to_copy };
91 $values->{branchcode} = $patron->branchcode; # FIXME Must be removed from the pref options, or FK removed (?)
92 $values->{categorycode} = $patron->categorycode;
94 $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
97 my $self = $class->SUPER::new($values);
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;
104 delete $attribute->{id};
105 delete $attribute->{borrowernumber};
107 $self->_result->create_related( 'pseudonymized_borrower_attributes', $attribute );
116 Generates a hashed value for $s (e.g. borrowernumber) with Bcrypt.
117 Needs config entry 'bcrypt_settings' in koha-conf.
122 my ( $class, $s ) = @_;
123 my $bcrypt_settings = C4::Context->config('bcrypt_settings');
125 Koha::Exceptions::Config::MissingEntry->throw(
126 "Missing 'bcrypt_settings' entry in config file") unless $bcrypt_settings;
128 return bcrypt($s, $bcrypt_settings);
131 =head2 Internal methods
138 return 'PseudonymizedTransaction';