Bug 16011: $VERSION - Remove the $VERSION init
[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 strict;
22 use warnings;
23 require Exporter;
24 use Carp;
25 use C4::Context;
26 use C4::Debug;
27 use vars qw(@ISA @EXPORT);
28
29 our $debug;
30
31 BEGIN {
32         # set the version for version checking
33         @ISA    = qw(Exporter);
34         @EXPORT = qw(
35                 &UpdateStats
36                 &TotalPaid
37         );
38 }
39
40
41 =head1 NAME
42
43 C4::Stats - Update Koha statistics (log)
44
45 =head1 SYNOPSIS
46
47   use C4::Stats;
48
49 =head1 DESCRIPTION
50
51 The functions of this module deals with statistics table of Koha database.
52
53 =head1 FUNCTIONS
54
55 =head2 UpdateStats
56
57   &UpdateStats($params);
58
59 Adds an entry to the statistics table in the Koha database, which acts as an activity log.
60
61 C<$params> is an hashref whose expected keys are:
62     branch             : the branch where the transaction occurred
63     type               : the type of transaction (renew, issue, localuse, return, writeoff, payment
64     itemnumber         : the itemnumber of the item
65     borrowernumber     : the borrowernumber of the patron
66     amount             : the amount of the transaction
67     other              : sipmode
68     itemtype           : the type of the item
69     accountno          : the count
70     ccode              : the collection code of the item
71
72 type key is mandatory.
73 For types used in C4::Circulation (renew,issue,localuse,return), the following other keys are mandatory:
74 branch, borrowernumber, itemnumber, ccode, itemtype
75 For types used in C4::Accounts (writeoff, payment), the following other keys are mandatory:
76 branch, borrowernumber, itemnumber, ccode, itemtype
77 If an optional key is not provided, the value '' is used for this key.
78
79 Returns undef if no C<$param> is given
80
81 =cut
82
83 sub UpdateStats {
84     my ($params) = @_;
85 # make some controls
86     return () if ! defined $params;
87 # change these arrays if new types of transaction or new parameters are allowed
88     my @allowed_keys = qw (type branch amount other itemnumber itemtype borrowernumber accountno ccode);
89     my @allowed_circulation_types = qw (renew issue localuse return onsite_checkout);
90     my @allowed_accounts_types = qw (writeoff payment);
91     my @circulation_mandatory_keys = qw (type branch borrowernumber itemnumber ccode itemtype);
92     my @accounts_mandatory_keys = qw (type branch borrowernumber amount);
93
94     my @mandatory_keys = ();
95     if (! exists $params->{type} or ! defined $params->{type}) {
96         croak ("UpdateStats did not received type param");
97     }
98     if (grep ($_ eq $params->{type}, @allowed_circulation_types  )) {
99         @mandatory_keys = @circulation_mandatory_keys;
100     } elsif (grep ($_ eq $params->{type}, @allowed_accounts_types )) {
101         @mandatory_keys = @accounts_mandatory_keys;
102     } else {
103         croak ("UpdateStats received forbidden type param: ".$params->{type});
104     }
105     my @missing_params = ();
106     for my $mykey (@mandatory_keys ) {
107         push @missing_params, $mykey if !grep (/^$mykey/, keys %$params);
108     }
109     if (scalar @missing_params > 0 ) {
110         croak ("UpdateStats did not received mandatory param(s): ".join (", ",@missing_params ));
111     }
112     my @invalid_params = ();
113     for my $myparam (keys %$params ) {
114         push @invalid_params, $myparam unless grep (/^$myparam$/, @allowed_keys);
115     }
116     if (scalar @invalid_params > 0 ) {
117         croak ("UpdateStats received invalid param(s): ".join (", ",@invalid_params ));
118     }
119 # get the parameters
120     my $branch            = $params->{branch};
121     my $type              = $params->{type};
122     my $borrowernumber    = exists $params->{borrowernumber} ? $params->{borrowernumber} :'';
123     my $itemnumber        = exists $params->{itemnumber}     ? $params->{itemnumber} :'';
124     my $amount            = exists $params->{amount}         ? $params->{amount} :'';
125     my $other             = exists $params->{other}          ? $params->{other} :'';
126     my $itemtype          = exists $params->{itemtype}       ? $params->{itemtype} :'';
127     my $accountno         = exists $params->{accountno}      ? $params->{accountno} :'';
128     my $ccode             = exists $params->{ccode}          ? $params->{ccode} :'';
129
130     my $dbh = C4::Context->dbh;
131     my $sth = $dbh->prepare(
132         "INSERT INTO statistics
133         (datetime,
134          branch,          type,        value,
135          other,           itemnumber,  itemtype,
136          borrowernumber,  proccode,    ccode)
137          VALUES (now(),?,?,?,?,?,?,?,?,?)"
138     );
139     $sth->execute(
140         $branch,         $type,        $amount,
141         $other,          $itemnumber,  $itemtype,
142         $borrowernumber, $accountno,   $ccode
143     );
144 }
145
146 =head2 TotalPaid
147
148   @total = &TotalPaid ( $time, [$time2], [$spreadsheet ]);
149
150 Returns an array containing the payments and writeoffs made between two dates
151 C<$time> and C<$time2>, or on a specific one, or from C<$time> onwards.
152
153 C<$time> param is mandatory.
154 If C<$time> eq 'today', returns are limited to the current day
155 If C<$time2> eq '', results are returned from C<$time> onwards.
156 If C<$time2> is undef, returns are limited to C<$time>
157 C<$spreadsheet> param is optional and controls the sorting of the results.
158
159 Returns undef if no param is given
160
161 =cut
162
163 sub TotalPaid {
164     my ( $time, $time2, $spreadsheet ) = @_;
165     return () unless (defined $time);
166     $time2 = $time unless $time2;
167     my $dbh   = C4::Context->dbh;
168     my $query = "SELECT * FROM statistics 
169   LEFT JOIN borrowers ON statistics.borrowernumber= borrowers.borrowernumber
170   WHERE (statistics.type='payment' OR statistics.type='writeoff') ";
171     if ( $time eq 'today' ) {
172 # FIXME wrong condition. Now() will not get all the payments of the day but of a specific timestamp
173         $query .= " AND datetime = now()";
174     } else {
175         $query .= " AND datetime > '$time'";    # FIXME: use placeholders
176     }
177     if ( $time2 ne '' ) {
178         $query .= " AND datetime < '$time2'";   # FIXME: use placeholders
179     }
180 # FIXME if $time2 is undef, query will be "AND datetime > $time AND AND datetime < $time"
181 # Operators should probably be <= and >=
182     if ($spreadsheet) {
183         $query .= " ORDER BY branch, type";
184     }
185     $debug and warn "TotalPaid query: $query";
186     my $sth = $dbh->prepare($query);
187     $sth->execute();
188     return @{$sth->fetchall_arrayref({})};
189 }
190
191 1;
192 __END__
193
194 =head1 AUTHOR
195
196 Koha Development Team <http://koha-community.org/>
197
198 =cut
199