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