Sub renamed according to the coding guidelines
[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     print $borrowernumber;
93
94     # FIXME - Use $dbh->do() instead
95     my $sth = $dbh->prepare(
96         "Insert into statistics (datetime,branch,type,usercode,value,
97                                         other,itemnumber,itemtype,borrowernumber,proccode,associatedborrower) values (now(),?,?,?,?,?,?,?,?,?,?)"
98     );
99     $sth->execute(
100         $branch,    $type,    $user,     $amount,
101         $other,     $itemnum, $itemtype, $borrowernumber,
102         $accountno, $organisation
103     );
104     $sth->finish;
105 }
106
107 # Otherwise, it'd need a POD.
108 sub TotalPaid {
109     my ( $time, $time2, $spreadsheet ) = @_;
110     $time2 = $time unless $time2;
111     my $dbh   = C4::Context->dbh;
112     my $query = "SELECT * FROM statistics,borrowers
113   WHERE statistics.borrowernumber= borrowers.borrowernumber
114   AND (statistics.type='payment' OR statistics.type='writeoff') ";
115     if ( $time eq 'today' ) {
116         $query = $query . " AND datetime = now()";
117     }
118     else {
119         $query .= " AND datetime > '$time'";
120     }
121     if ( $time2 ne '' ) {
122         $query .= " AND datetime < '$time2'";
123     }
124     if ($spreadsheet) {
125         $query .= " ORDER BY branch, type";
126     }
127     my $sth = $dbh->prepare($query);
128     $sth->execute();
129     my @results;
130     while ( my $data = $sth->fetchrow_hashref ) {
131         push @results, $data;
132     }
133     $sth->finish;
134     return (@results);
135 }
136
137 # Otherwise, it needs a POD.
138 sub getcharges {
139     my ( $borrowerno, $timestamp, $accountno ) = @_;
140     my $dbh        = C4::Context->dbh;
141     my $timestamp2 = $timestamp - 1;
142     my $query      = "";
143     my $sth;
144
145     # getcharges is now taking accountno. as an argument
146     if ($accountno) {
147         $sth = $dbh->prepare(
148             "Select * from accountlines where borrowernumber=?
149               and accountno = ?"
150         );
151         $sth->execute( $borrowerno, $accountno );
152
153         # this bit left in for old 2 arg usage of getcharges
154     }
155     else {
156         $sth = $dbh->prepare(
157             "Select * from accountlines where borrowernumber=?
158               and timestamp = ? and accounttype <> 'Pay' and
159               accounttype <> 'W'"
160         );
161         $sth->execute( $borrowerno, $timestamp );
162     }
163
164     #  print $query,"<br>";
165     my $i = 0;
166     my @results;
167     while ( my $data = $sth->fetchrow_hashref ) {
168
169         #    if ($data->{'timestamp'} == $timestamp){
170         $results[$i] = $data;
171         $i++;
172
173         #    }
174     }
175     return (@results);
176 }
177
178 # Otherwise, it needs a POD.
179 sub getcredits {
180     my ( $date, $date2 ) = @_;
181     my $dbh = C4::Context->dbh;
182
183     #takes date converts to timestamps
184     my $padding = "000000";
185     ( my $a, my $b, my $c ) = unpack( "A4 x1 A2 x1 A2", $date );
186     ( my $x, my $y, my $z ) = unpack( "A4 x1 A2 x1 A2", $date2 );
187     my $timestamp  = $a . $b . $c . $padding;
188     my $timestamp2 = $x . $y . $z . $padding;
189
190     my $sth = $dbh->prepare(
191 "Select * from accountlines,borrowers where (((accounttype = 'LR')  or (accounttype <> 'Pay'))
192                                    and amount < 0  and accountlines.borrowernumber = borrowers.borrowernumber
193                                    and timestamp >=?  and timestamp <?)"
194     );
195     $sth->execute( $timestamp, $timestamp2 );
196
197     my $i = 0;
198     my @results;
199     while ( my $data = $sth->fetchrow_hashref ) {
200         $results[$i] = $data;
201         $i++;
202     }
203     return (@results);
204 }
205
206 sub getrefunds {
207     my ( $date, $date2 ) = @_;
208     my $dbh = C4::Context->dbh;
209
210     #takes date converts to timestamps
211     my $padding = "000000";
212     ( my $a, my $b, my $c ) = unpack( "A4 x1 A2 x1 A2", $date );
213     ( my $x, my $y, my $z ) = unpack( "A4 x1 A2 x1 A2", $date2 );
214     my $timestamp  = $a . $b . $c . $padding;
215     my $timestamp2 = $x . $y . $z . $padding;
216
217     my $sth = $dbh->prepare(
218 "Select * from accountlines,borrowers where (accounttype = 'REF'                                                                
219                                           and accountlines.borrowernumber = borrowers.borrowernumber                                                                          
220                                                    and timestamp >=?  and timestamp <?)"
221     );
222     $sth->execute( $timestamp, $timestamp2 );
223
224     my @results;
225     while ( my $data = $sth->fetchrow_hashref ) {
226         push @results, $data;
227     }
228     return (@results);
229 }
230
231 # Otherwise, this needs a POD.
232 sub Getpaidbranch {
233     my ( $date, $borrno ) = @_;
234     my $dbh = C4::Context->dbh;
235     my $sth =
236       $dbh->prepare(
237 "select * from statistics where type='payment' and datetime >? and  borrowernumber=?"
238       );
239     $sth->execute( $date, $borrno );
240
241     #  print $query;
242     my $data = $sth->fetchrow_hashref;
243     $sth->finish;
244     return ( $data->{'branch'} );
245 }
246
247 # FIXME - This is only used in reservereport.pl and reservereport.xls,
248 # neither of which is used.
249 # Otherwise, it needs a POD.
250 sub unfilledreserves {
251     my $dbh = C4::Context->dbh;
252     my $sth = $dbh->prepare(
253 "select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where (found <> 'F' or
254             found is NULL) and cancellationdate
255                                                                 is NULL and biblio.biblionumber=reserves.biblionumber and
256                                                                 reserves.constrainttype='o'
257                                                                 and (reserves.biblionumber=reserveconstraints.biblionumber
258                                                                 and reserves.borrowernumber=reserveconstraints.borrowernumber)
259                                                                 and
260                                                                 reserves.borrowernumber=borrowers.borrowernumber and
261                                                                 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
262                                                                 biblio.title,reserves.reservedate"
263     );
264     $sth->execute;
265     my $i = 0;
266     my @results;
267     while ( my $data = $sth->fetchrow_hashref ) {
268         $results[$i] = $data;
269         $i++;
270     }
271     $sth->finish;
272     $sth = $dbh->prepare(
273 "select *,biblio.title from reserves,biblio,borrowers where (found <> 'F' or found is NULL) and cancellationdate
274                 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
275                 reserves.borrowernumber=borrowers.borrowernumber
276                 order by
277                 biblio.title,reserves.reservedate"
278     );
279     $sth->execute;
280     while ( my $data = $sth->fetchrow_hashref ) {
281         $results[$i] = $data;
282         $i++;
283     }
284     $sth->finish;
285     return ( $i, \@results );
286 }
287
288 1;
289 __END__
290
291 =back
292
293 =head1 AUTHOR
294
295 Koha Developement team <info@koha.org>
296
297 =cut
298