# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
-use strict;
-use warnings;
+use Modern::Perl;
require Exporter;
use Carp;
use C4::Context;
use C4::Debug;
+
+use Koha::DateUtils qw( dt_from_string );
+use Koha::Statistics;
+use Koha::PseudonymizedTransactions;
+
use vars qw(@ISA @EXPORT);
our $debug;
my $location = exists $params->{location} ? $params->{location} : undef;
my $ccode = exists $params->{ccode} ? $params->{ccode} : '';
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare(
- "INSERT INTO statistics
- (datetime,
- branch, type, value,
- other, itemnumber, itemtype, location,
- borrowernumber, ccode)
- VALUES (now(),?,?,?,?,?,?,?,?,?)"
- );
- $sth->execute(
- $branch, $type, $amount, $other,
- $itemnumber, $itemtype, $location, $borrowernumber,
- $ccode
- );
+ my $dtf = Koha::Database->new->schema->storage->datetime_parser;
+ my $statistic = Koha::Statistic->new(
+ {
+ datetime => $dtf->format_datetime( dt_from_string ),
+ branch => $branch,
+ type => $type,
+ value => $amount,
+ other => $other,
+ itemnumber => $itemnumber,
+ itemtype => $itemtype,
+ location => $location,
+ borrowernumber => $borrowernumber,
+ ccode => $ccode,
+ }
+ )->store;
+
+ Koha::PseudonymizedTransaction->new_from_statistic($statistic)->store
+ if C4::Context->preference('Pseudonymization')
+ && $borrowernumber # Not a real transaction if the patron does not exist
+ # For instance can be a transfer, or hold trigger
+ && grep { $_ eq $params->{type} } qw(renew issue return onsite_checkout);
}
1;
--- /dev/null
+package Koha::PseudonymizedTransaction;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Carp;
+use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64);
+
+use Koha::Database;
+use Koha::Exceptions::Config;
+use Koha::Patrons;
+
+use base qw(Koha::Object);
+
+=head1 NAME
+
+Koha::PseudonymizedTransaction - Koha Koha::PseudonymizedTransaction Object class
+
+=head1 API
+
+=head2 Class methods
+
+=head3 new
+
+=cut
+
+sub new_from_statistic {
+ my ( $class, $statistic ) = @_;
+
+ my $values = {
+ hashed_borrowernumber => $class->get_hash($statistic->borrowernumber),
+ };
+
+ my @t_fields_to_copy = split ',', C4::Context->preference('PseudonymizationTransactionFields') || '';
+
+ if ( grep { $_ eq 'transaction_branchcode' } @t_fields_to_copy ) {
+ $values->{transaction_branchcode} = $statistic->branch;
+ }
+ if ( grep { $_ eq 'holdingbranch' } @t_fields_to_copy ) {
+ $values->{holdingbranch} = $statistic->item->holdingbranch;
+ }
+ if ( grep { $_ eq 'transaction_type' } @t_fields_to_copy ) {
+ $values->{transaction_type} = $statistic->type;
+ }
+ if ( grep { $_ eq 'itemcallnumber' } @t_fields_to_copy ) {
+ $values->{itemcallnumber} = $statistic->item->itemcallnumber;
+ }
+
+
+ @t_fields_to_copy = grep {
+ $_ ne 'transaction_branchcode'
+ && $_ ne 'holdingbranch'
+ && $_ ne 'transaction_type'
+ && $_ ne 'itemcallnumber'
+ } @t_fields_to_copy;
+
+ $values = { %$values, map { $_ => $statistic->$_ } @t_fields_to_copy };
+
+ my $patron = Koha::Patrons->find($statistic->borrowernumber);
+ my @p_fields_to_copy = split ',', C4::Context->preference('PseudonymizationPatronFields') || '';
+ $values = { %$values, map { $_ => $patron->$_ } @p_fields_to_copy };
+
+ $values->{branchcode} = $patron->branchcode; # FIXME Must be removed from the pref options, or FK removed (?)
+ $values->{categorycode} = $patron->categorycode;
+
+ $values->{has_cardnumber} = $patron->cardnumber ? 1 : 0;
+
+ return $class->SUPER::new($values);
+}
+
+sub get_hash {
+ my ( $class, $s ) = @_;
+ my $key = C4::Context->config('key');
+
+ Koha::Exceptions::Config::MissingEntry->throw(
+ "Missing 'key' entry in config file") unless $key;
+
+ return bcrypt($s, $key);
+}
+
+=head2 Internal methods
+
+=head3 _type
+
+=cut
+
+sub _type {
+ return 'PseudonymizedTransaction';
+}
+
+1;
--- /dev/null
+package Koha::PseudonymizedTransactions;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Carp;
+
+use Koha::Database;
+use Koha::PseudonymizedTransaction;
+
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::PseudonymizedTransactions - Koha PseudonymizedTransaction Object set class
+
+=head1 API
+
+=cut
+
+=head2 Class Methods
+
+=cut
+
+=head3 type
+
+=cut
+
+sub _type {
+ return 'PseudonymizedTransaction';
+}
+
+sub object_class {
+ return 'Koha::PseudonymizedTransaction';
+}
+
+1;