Bug 35702: Fetch biblios at once and loop
[koha.git] / C4 / Stats.pm
1 package C4::Stats;
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use Carp qw( croak );
23 use C4::Context;
24
25 use Koha::DateUtils qw( dt_from_string );
26 use Koha::Statistics;
27 use Koha::PseudonymizedTransactions;
28
29 our (@ISA, @EXPORT_OK);
30 BEGIN {
31     require Exporter;
32     @ISA    = qw(Exporter);
33     @EXPORT_OK = qw(
34       UpdateStats
35     );
36 }
37
38
39 =head1 NAME
40
41 C4::Stats - Update Koha statistics (log)
42
43 =head1 SYNOPSIS
44
45   use C4::Stats;
46
47 =head1 DESCRIPTION
48
49 The functions of this module deals with statistics table of Koha database.
50
51 =head1 FUNCTIONS
52
53 =head2 UpdateStats
54
55   &UpdateStats($params);
56
57 Adds an entry to the statistics table in the Koha database, which acts as an activity log.
58
59 C<$params> is an hashref whose expected keys are:
60     branch             : the branch where the transaction occurred
61     type               : the type of transaction (renew, issue, localuse, return, writeoff, payment
62     itemnumber         : the itemnumber of the item
63     borrowernumber     : the borrowernumber of the patron
64     amount             : the amount of the transaction
65     other              : sipmode
66     itemtype           : the type of the item
67     ccode              : the collection code of the item
68     categorycode       : the categorycode of the patron
69     interface          : the context this action was taken in
70
71 type key is mandatory.
72 For types used in C4::Circulation (renew,issue,localuse,return), the following other keys are mandatory:
73 branch, borrowernumber, itemnumber, ccode, itemtype
74 For types used in C4::Accounts (writeoff, payment), the following other keys are mandatory:
75 branch, borrowernumber, itemnumber, ccode, itemtype
76 If an optional key is not provided, the value '' is used for this key.
77
78 Returns undef if no C<$param> is given
79
80 =cut
81
82 sub UpdateStats {
83     my ($params) = @_;
84 # make some controls
85     return () if ! defined $params;
86 # change these arrays if new types of transaction or new parameters are allowed
87     my @allowed_keys = qw (type branch amount other itemnumber itemtype borrowernumber ccode location categorycode interface);
88     my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout recall);
89     my @allowed_accounts_types = qw (writeoff payment);
90     my @circulation_mandatory_keys = qw (type branch borrowernumber itemnumber ccode itemtype);
91     my @accounts_mandatory_keys = qw (type branch borrowernumber amount);
92
93     my @mandatory_keys = ();
94     if (! exists $params->{type} or ! defined $params->{type}) {
95         croak ("UpdateStats did not received type param");
96     }
97     if (grep ($_ eq $params->{type}, @allowed_circulation_types  )) {
98         @mandatory_keys = @circulation_mandatory_keys;
99     } elsif (grep ($_ eq $params->{type}, @allowed_accounts_types )) {
100         @mandatory_keys = @accounts_mandatory_keys;
101     } else {
102         croak ("UpdateStats received forbidden type param: ".$params->{type});
103     }
104     my @missing_params = ();
105     for my $mykey (@mandatory_keys ) {
106         push @missing_params, $mykey if !grep (/^$mykey/, keys %$params);
107     }
108     if (scalar @missing_params > 0 ) {
109         croak ("UpdateStats did not received mandatory param(s): ".join (", ",@missing_params ));
110     }
111     my @invalid_params = ();
112     for my $myparam (keys %$params ) {
113         push @invalid_params, $myparam unless grep { $_ eq $myparam } @allowed_keys;
114     }
115     if (scalar @invalid_params > 0 ) {
116         croak ("UpdateStats received invalid param(s): ".join (", ",@invalid_params ));
117     }
118 # get the parameters
119     my $branch            = $params->{branch};
120     my $type              = $params->{type};
121     my $borrowernumber    = exists $params->{borrowernumber} ? $params->{borrowernumber} : '';
122     my $itemnumber        = exists $params->{itemnumber}     ? $params->{itemnumber}     : undef;
123     my $amount            = exists $params->{amount}         ? $params->{amount}         : 0;
124     my $other             = exists $params->{other}          ? $params->{other}          : '';
125     my $itemtype          = exists $params->{itemtype}       ? $params->{itemtype}       : '';
126     my $location          = exists $params->{location}       ? $params->{location}       : undef;
127     my $ccode             = exists $params->{ccode}          ? $params->{ccode}          : '';
128     my $categorycode      = exists $params->{categorycode}   ? $params->{categorycode}   : undef;
129     my $interface         = exists $params->{interface}      ? $params->{interface}      : undef;
130
131     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
132     my $statistic = Koha::Statistic->new(
133         {
134             datetime       => $dtf->format_datetime( dt_from_string ),
135             branch         => $branch,
136             type           => $type,
137             value          => $amount,
138             other          => $other,
139             itemnumber     => $itemnumber,
140             itemtype       => $itemtype,
141             location       => $location,
142             borrowernumber => $borrowernumber,
143             ccode          => $ccode,
144             categorycode   => $categorycode,
145             interface      => $interface,
146         }
147     )->store;
148
149     Koha::PseudonymizedTransaction->new_from_statistic($statistic)->store
150       if C4::Context->preference('Pseudonymization')
151         && $borrowernumber # Not a real transaction if the patron does not exist
152                            # For instance can be a transfer, or hold trigger
153         && grep { $_ eq $params->{type} } qw(renew issue return onsite_checkout);
154 }
155
156 1;
157 __END__
158
159 =head1 AUTHOR
160
161 Koha Development Team <http://koha-community.org/>
162
163 =cut
164