1 package Koha::Statistic;
3 # Copyright 2019, 2023 Koha development team
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use Koha::DateUtils qw/dt_from_string/;
26 use Koha::BackgroundJob::PseudonymizeStatistic;
28 use base qw(Koha::Object);
30 our @allowed_accounts_types = qw( writeoff payment );
31 our @allowed_circulation_types = qw( renew issue localuse return onsite_checkout recall item_found item_lost );
32 our @mandatory_accounts_keys = qw( type branch borrowernumber value ); # note that amount is mapped to value
33 our @mandatory_circulation_keys = qw( type branch borrowernumber itemnumber ccode itemtype );
37 Koha::Statistic - Koha Statistic Object class
45 my $record = Koha::Statistic->new( $params );
47 C<$params> is an hashref whose expected keys are:
48 branch : transaction branch
49 type : transaction type
50 itemnumber : itemnumber
51 borrowernumber : borrowernumber
52 categorycode : patron category
53 amount : transaction amount (legacy parameter name)
54 value : transaction amount
57 ccode : collection code
58 interface : the context this action was taken in
60 The type key is mandatory, see @Koha::Statistic::allowed_account_types or
61 @Koha::Statistic::allowed_circulation_types.
62 Some keys are mandatory, depending on type. See @mandatory_accounts_keys
63 or @mandatory_circulation_keys.
65 The method throws an exception when given bad data, otherwise returns a
66 Koha::Statistic object, which is not yet stored.
71 my ( $class, $params ) = @_;
73 Koha::Exceptions::BadParameter->throw( parameter => $params ) if !$params || ref($params) ne 'HASH';
74 Koha::Exceptions::WrongParameter->throw( name => 'type', value => $params->{type} ) if !$params->{type};
76 if ( exists $params->{amount} ) { # legacy amount parameter still supported
77 $params->{value} //= delete $params->{amount};
81 if ( grep { $_ eq $params->{type} } @allowed_circulation_types ) {
82 $category = 'circulation';
83 } elsif ( grep { $_ eq $params->{type} } @allowed_accounts_types ) {
84 $category = 'accounts';
86 Koha::Exceptions::WrongParameter->throw( name => 'type', value => $params->{type} );
89 my @mandatory_keys = $category eq 'circulation' ? @mandatory_circulation_keys : @mandatory_accounts_keys;
90 my @missing = map { exists $params->{$_} ? () : $_ } @mandatory_keys;
91 Koha::Exceptions::MissingParameter->throw( parameter => join( ',', @missing ) ) if @missing;
93 my $datetime = $params->{datetime} ? $params->{datetime} : dt_from_string();
94 return $class->SUPER::new(
96 borrowernumber => $params->{borrowernumber}, # no longer sending empty string (changed 2023)
97 branch => $params->{branch},
98 categorycode => $params->{categorycode},
99 ccode => exists $params->{ccode} ? $params->{ccode} : q{},
100 datetime => $datetime,
101 interface => $params->{interface} // C4::Context->interface,
102 itemnumber => $params->{itemnumber},
103 itemtype => exists $params->{itemtype} ? $params->{itemtype} : q{},
104 location => $params->{location},
105 other => exists $params->{other} ? $params->{other} : q{},
106 type => $params->{type},
107 value => exists $params->{value} ? $params->{value} : 0,
117 This call includes pseudonymization if enabled.
124 $self->pseudonymize() if C4::Context->preference('Pseudonymization');
130 my $item = $statistic->item;
132 Return the item associated to this statistic.
138 return Koha::Items->find( $self->itemnumber );
143 my $pseudonymized_stat = $statistic->pseudonymize;
145 Generate a pesudonymized version of the statistic.
152 return unless ( $self->borrowernumber && grep { $_ eq $self->type } qw(renew issue return onsite_checkout) );
153 Koha::BackgroundJob::PseudonymizeStatistic->new->enqueue( { statistic => $self->unblessed } );
157 =head2 Internal methods