Bug 35397: Fix SIP2AddOpacMessagesToScreenMessage system preference description
[koha.git] / Koha / Statistic.pm
1 package Koha::Statistic;
2
3 # Copyright 2019, 2023 Koha development team
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use C4::Context;
23 use Koha::Database;
24 use Koha::DateUtils qw/dt_from_string/;
25 use Koha::Items;
26 use Koha::BackgroundJob::PseudonymizeStatistic;
27
28 use base qw(Koha::Object);
29
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 );
34
35 =head1 NAME
36
37 Koha::Statistic - Koha Statistic Object class
38
39 =head1 API
40
41 =head2 Class methods
42
43 =head3 new
44
45     my $record = Koha::Statistic->new( $params );
46
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
55     other              : sipmode
56     itemtype           : itemtype
57     ccode              : collection code
58     interface          : the context this action was taken in
59
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.
64
65     The method throws an exception when given bad data, otherwise returns a
66     Koha::Statistic object, which is not yet stored.
67
68 =cut
69
70 sub new {
71     my ( $class, $params ) = @_;
72
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};
75
76     if ( exists $params->{amount} ) {    # legacy amount parameter still supported
77         $params->{value} //= delete $params->{amount};
78     }
79
80     my $category;
81     if ( grep { $_ eq $params->{type} } @allowed_circulation_types ) {
82         $category = 'circulation';
83     } elsif ( grep { $_ eq $params->{type} } @allowed_accounts_types ) {
84         $category = 'accounts';
85     } else {
86         Koha::Exceptions::WrongParameter->throw( name => 'type', value => $params->{type} );
87     }
88
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;
92
93     my $datetime = Koha::Database->new->schema->storage->datetime_parser->format_datetime( dt_from_string() );
94     return $class->SUPER::new(
95         {
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,
108
109         }
110     );
111 }
112
113 =head3 store
114
115     $statistic->store;
116
117     This call includes pseudonymization if enabled.
118
119 =cut
120
121 sub store {
122     my ($self) = @_;
123     $self->SUPER::store;
124     Koha::BackgroundJob::PseudonymizeStatistic->new->enqueue( { statistic => $self->unblessed } )
125         if C4::Context->preference('Pseudonymization')
126         && $self->borrowernumber    # Not a real transaction if the patron does not exist
127                                     # For instance can be a transfer, or hold trigger
128         && grep { $_ eq $self->type } qw(renew issue return onsite_checkout);
129     return $self;
130 }
131
132 =head3 item
133
134     my $item = $statistic->item;
135
136     Return the item associated to this statistic.
137
138 =cut
139
140 sub item {
141     my ( $self ) = @_;
142     return Koha::Items->find( $self->itemnumber );
143 }
144
145 =head2 Internal methods
146
147 =head3 _type
148
149 =cut
150
151 sub _type {
152     return 'Statistic';
153 }
154
155 1;