rel_3_0 moved to HEAD
[koha.git] / C4 / Stats.pm
1 package C4::Stats;
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 require Exporter;
24 use DBI;
25 use C4::Context;
26 use vars qw($VERSION @ISA @EXPORT);
27
28 # set the version for version checking
29 $VERSION = $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
30     shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
31 };
32
33 =head1 NAME
34
35 C4::Stats - Update Koha statistics (log)
36
37 =head1 SYNOPSIS
38
39   use C4::Stats;
40
41 =head1 DESCRIPTION
42
43 The C<&UpdateStats> function adds an entry to the statistics table in
44 the Koha database, which acts as an activity log.
45
46 =head1 FUNCTIONS
47
48 =over 2
49
50 =cut
51
52 @ISA    = qw(Exporter);
53 @EXPORT = qw(&UpdateStats &statsreport &TotalOwing
54   &TotalPaid &getcharges &Getpaidbranch &unfilledreserves &getcredits
55   getrefunds);
56
57 =item UpdateStats
58
59   &UpdateStats($env, $branch, $type, $value, $other, $itemnumber,
60                $itemtype, $borrowernumber);
61
62 Adds a line to the statistics table of the Koha database. In effect,
63 it logs an event.
64
65 C<$branch>, C<$type>, C<$value>, C<$other>, C<$itemnumber>,
66 C<$itemtype>, and C<$borrowernumber> correspond to the fields of the
67 statistics table in the Koha database.
68
69 If C<$branch> is the empty string, the branch code will be taken from
70 C<$env-E<gt>{branchcode}>.
71
72 C<$env-E<gt>{usercode}> specifies the value of the C<usercode> field.
73
74 =cut
75
76 #'
77 sub UpdateStats {
78
79     #module to insert stats data into stats table
80     my (
81         $env,      $branch,         $type,
82         $amount,   $other,          $itemnum,
83         $itemtype, $borrowernumber, $accountno
84       )
85       = @_;
86     my $dbh = C4::Context->dbh;
87     if ( $branch eq '' ) {
88         $branch = $env->{'branchcode'};
89     }
90     my $user         = $env->{'usercode'};
91     my $organisation = $env->{'organisation'};
92
93     # FIXME - Use $dbh->do() instead
94     my $sth = $dbh->prepare(
95         "Insert into statistics (datetime,branch,type,usercode,value,
96                                         other,itemnumber,itemtype,borrowernumber,proccode,associatedborrower) values (now(),?,?,?,?,?,?,?,?,?,?)"
97     );
98     $sth->execute(
99         $branch,    $type,    $user,     $amount,
100         $other,     $itemnum, $itemtype, $borrowernumber,
101         $accountno, $organisation
102     );
103     $sth->finish;
104 }
105
106 # Otherwise, it'd need a POD.
107 sub TotalPaid {
108     my ( $time, $time2, $spreadsheet ) = @_;
109     $time2 = $time unless $time2;
110     my $dbh   = C4::Context->dbh;
111     my $query = "SELECT * FROM statistics,borrowers
112   WHERE statistics.borrowernumber= borrowers.borrowernumber
113   AND (statistics.type='payment' OR statistics.type='writeoff') ";
114     if ( $time eq 'today' ) {
115         $query = $query . " AND datetime = now()";
116     }
117     else {
118         $query .= " AND datetime > '$time'";
119     }
120     if ( $time2 ne '' ) {
121         $query .= " AND datetime < '$time2'";
122     }
123     if ($spreadsheet) {
124         $query .= " ORDER BY branch, type";
125     }
126     my $sth = $dbh->prepare($query);
127     $sth->execute();
128     my @results;
129     while ( my $data = $sth->fetchrow_hashref ) {
130         push @results, $data;
131     }
132     $sth->finish;
133     return (@results);
134 }
135
136 # Otherwise, it needs a POD.
137 sub getcharges {
138     my ( $borrowerno, $timestamp, $accountno ) = @_;
139     my $dbh        = C4::Context->dbh;
140     my $timestamp2 = $timestamp - 1;
141     my $query      = "";
142     my $sth;
143
144     # getcharges is now taking accountno. as an argument
145     if ($accountno) {
146         $sth = $dbh->prepare(
147             "Select * from accountlines where borrowernumber=?
148               and accountno = ?"
149         );
150         $sth->execute( $borrowerno, $accountno );
151
152         # this bit left in for old 2 arg usage of getcharges
153     }
154     else {
155         $sth = $dbh->prepare(
156             "Select * from accountlines where borrowernumber=?
157               and timestamp = ? and accounttype <> 'Pay' and
158               accounttype <> 'W'"
159         );
160         $sth->execute( $borrowerno, $timestamp );
161     }
162
163     #  print $query,"<br>";
164     my $i = 0;
165     my @results;
166     while ( my $data = $sth->fetchrow_hashref ) {
167
168         #    if ($data->{'timestamp'} == $timestamp){
169         $results[$i] = $data;
170         $i++;
171
172         #    }
173     }
174     return (@results);
175 }
176
177 # Otherwise, it needs a POD.
178 sub getcredits {
179     my ( $date, $date2 ) = @_;
180     my $dbh = C4::Context->dbh;
181
182     #takes date converts to timestamps
183     my $padding = "000000";
184     ( my $a, my $b, my $c ) = unpack( "A4 x1 A2 x1 A2", $date );
185     ( my $x, my $y, my $z ) = unpack( "A4 x1 A2 x1 A2", $date2 );
186     my $timestamp  = $a . $b . $c . $padding;
187     my $timestamp2 = $x . $y . $z . $padding;
188
189     my $sth = $dbh->prepare(
190 "Select * from accountlines,borrowers where (((accounttype = 'LR')  or (accounttype <> 'Pay'))
191                                    and amount < 0  and accountlines.borrowernumber = borrowers.borrowernumber
192                                    and timestamp >=?  and timestamp <?)"
193     );
194     $sth->execute( $timestamp, $timestamp2 );
195
196     my $i = 0;
197     my @results;
198     while ( my $data = $sth->fetchrow_hashref ) {
199         $results[$i] = $data;
200         $i++;
201     }
202     return (@results);
203 }
204
205 sub getrefunds {
206     my ( $date, $date2 ) = @_;
207     my $dbh = C4::Context->dbh;
208
209     #takes date converts to timestamps
210     my $padding = "000000";
211     ( my $a, my $b, my $c ) = unpack( "A4 x1 A2 x1 A2", $date );
212     ( my $x, my $y, my $z ) = unpack( "A4 x1 A2 x1 A2", $date2 );
213     my $timestamp  = $a . $b . $c . $padding;
214     my $timestamp2 = $x . $y . $z . $padding;
215
216     my $sth = $dbh->prepare(
217 "Select * from accountlines,borrowers where (accounttype = 'REF'                                                                
218                                           and accountlines.borrowernumber = borrowers.borrowernumber                                                                          
219                                                    and timestamp >=?  and timestamp <?)"
220     );
221     $sth->execute( $timestamp, $timestamp2 );
222
223     my @results;
224     while ( my $data = $sth->fetchrow_hashref ) {
225         push @results, $data;
226     }
227     return (@results);
228 }
229
230 # Otherwise, this needs a POD.
231 sub Getpaidbranch {
232     my ( $date, $borrno ) = @_;
233     my $dbh = C4::Context->dbh;
234     my $sth =
235       $dbh->prepare(
236 "select * from statistics where type='payment' and datetime >? and  borrowernumber=?"
237       );
238     $sth->execute( $date, $borrno );
239
240     #  print $query;
241     my $data = $sth->fetchrow_hashref;
242     $sth->finish;
243     return ( $data->{'branch'} );
244 }
245
246 # FIXME - This is only used in reservereport.pl and reservereport.xls,
247 # neither of which is used.
248 # Otherwise, it needs a POD.
249 sub unfilledreserves {
250     my $dbh = C4::Context->dbh;
251     my $sth = $dbh->prepare(
252 "select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where (found <> 'F' or
253             found is NULL) and cancellationdate
254                                                                 is NULL and biblio.biblionumber=reserves.biblionumber and
255                                                                 reserves.constrainttype='o'
256                                                                 and (reserves.biblionumber=reserveconstraints.biblionumber
257                                                                 and reserves.borrowernumber=reserveconstraints.borrowernumber)
258                                                                 and
259                                                                 reserves.borrowernumber=borrowers.borrowernumber and
260                                                                 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
261                                                                 biblio.title,reserves.reservedate"
262     );
263     $sth->execute;
264     my $i = 0;
265     my @results;
266     while ( my $data = $sth->fetchrow_hashref ) {
267         $results[$i] = $data;
268         $i++;
269     }
270     $sth->finish;
271     $sth = $dbh->prepare(
272 "select *,biblio.title from reserves,biblio,borrowers where (found <> 'F' or found is NULL) and cancellationdate
273                 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
274                 reserves.borrowernumber=borrowers.borrowernumber
275                 order by
276                 biblio.title,reserves.reservedate"
277     );
278     $sth->execute;
279     while ( my $data = $sth->fetchrow_hashref ) {
280         $results[$i] = $data;
281         $i++;
282     }
283     $sth->finish;
284     return ( $i, \@results );
285 }
286
287 1;
288 __END__
289
290 =back
291
292 =head1 AUTHOR
293
294 Koha Developement team <info@koha.org>
295
296 =cut
297