removing warnings
[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 = 0.01;
30
31 =head1 NAME
32
33 C4::Stats - Update Koha statistics (log)
34
35 =head1 SYNOPSIS
36
37   use C4::Stats;
38
39 =head1 DESCRIPTION
40
41 The C<&UpdateStats> function adds an entry to the statistics table in
42 the Koha database, which acts as an activity log.
43
44 =head1 FUNCTIONS
45
46 =over 2
47
48 =cut
49
50 @ISA = qw(Exporter);
51 @EXPORT = qw(&UpdateStats &statsreport &Count &Overdues &TotalOwing
52 &TotalPaid &getcharges &Getpaidbranch &unfilledreserves);
53
54 =item UpdateStats
55
56   &UpdateStats($env, $branch, $type, $value, $other, $itemnumber,
57                $itemtype, $borrowernumber);
58
59 Adds a line to the statistics table of the Koha database. In effect,
60 it logs an event.
61
62 C<$branch>, C<$type>, C<$value>, C<$other>, C<$itemnumber>,
63 C<$itemtype>, and C<$borrowernumber> correspond to the fields of the
64 statistics table in the Koha database.
65
66 If C<$branch> is the empty string, the branch code will be taken from
67 C<$env-E<gt>{branchcode}>.
68
69 C<$env-E<gt>{usercode}> specifies the value of the C<usercode> field.
70
71 =cut
72 #'
73 sub UpdateStats {
74   #module to insert stats data into stats table
75   my ($env,$branch,$type,$amount,$other,$itemnum,$itemtype,$borrowernumber)=@_;
76   my $dbh = C4::Context->dbh;
77   if ($branch eq ''){
78     $branch=$env->{'branchcode'};
79   }
80   my $user = $env->{'usercode'};
81   print $borrowernumber;
82   # FIXME - Use $dbh->do() instead
83   my $sth=$dbh->prepare("Insert into statistics
84   (datetime,branch,type,usercode,value,
85   other,itemnumber,itemtype,borrowernumber)
86   values (now(),'$branch','$type','$user','$amount',
87   '$other','$itemnum','$itemtype','$borrowernumber')");
88   $sth->execute;
89   $sth->finish;
90 }
91
92 # FIXME - Why does this function exist? Why not just rename &circrep
93 # to &statsreport?
94 # Then again, it only appears to be used in reports.pl which, in turn,
95 # doesn't appear to be used. So presumably this function is obsolete.
96 # If not, it needs a POD.
97 sub statsreport {
98   #module to return a list of stats for a given day,time,branch type
99   #or to return search stats
100   my ($type,$time)=@_;
101   my @data;
102 #  print "here";
103 #  if ($type eq 'issue'){
104     @data=circrep($time,$type);
105 #  }
106   return(@data);
107 }
108
109 # Only used internally. Probably useless: see comment for
110 # &statsreport.
111 sub circrep {
112   my ($time,$type)=@_;
113   my $dbh = C4::Context->dbh;
114   my $query="Select * from statistics";
115   if ($time eq 'today'){
116     # FIXME - What is this supposed to do? MySQL 3.23.42 barfs on it.
117     $query=$query." where type='$type' and datetime
118     >=datetime('yesterday'::date)";
119                         # FIXME - .= <<EOT;
120   }
121   my $sth=$dbh->prepare($query);
122   $sth->execute;
123   my $i=0;
124   my @results;
125   while (my $data=$sth->fetchrow_hashref){
126     $results[$i]="$data->{'datetime'}\t$data->{'branch'}";
127     $i++;
128   }
129   $sth->finish;
130 #  print $query;
131   return(@results);
132 }
133
134 # FIXME - This is only used in stats.pl, which in turn is never used.
135 # Otherwise, this needs a POD.
136 sub Count {
137   my ($type,$branch,$time,$time2)=@_;
138   my $dbh = C4::Context->dbh;
139   my $query="Select count(*) from statistics where type='$type'";
140   $query.=" and datetime >= '$time' and datetime< '$time2' and branch='$branch'";
141   my $sth=$dbh->prepare($query);
142   $sth->execute;
143   my $data=$sth->fetchrow_hashref;
144   $sth->finish;
145 #  print $query;
146   return($data->{'count(*)'});
147 }
148
149 # FIXME - This function doesn't appear to be used.
150 # If it is, it needs a POD.
151 sub Overdues{
152   my $dbh = C4::Context->dbh;
153   my $query="Select count(*) from issues where date_due >= now()";
154   my $sth=$dbh->prepare($query);
155   $sth->execute;
156   my $count=$sth->fetchrow_hashref;
157   $sth->finish;
158   return($count->{'count(*)'});
159 }
160
161 # FIXME - Never used.
162 # Otherwise, it'd need a POD.
163 sub TotalOwing{
164   my ($type)=@_;
165   my $dbh = C4::Context->dbh;
166   my $query="Select sum(amountoutstanding) from accountlines";
167   if ($type eq 'fine'){
168     $query .= " where accounttype='F' or accounttype='FN'";
169   }
170   my $sth=$dbh->prepare($query);
171 #  print $query;
172   $sth->execute;
173    my $total=$sth->fetchrow_hashref;
174    $sth->finish;
175   return($total->{'sum(amountoutstanding)'});
176 }
177
178 # FIXME - Never used.
179 # Otherwise, it'd need a POD.
180 sub TotalPaid {
181   my ($time)=@_;
182   my $dbh = C4::Context->dbh;
183   my $query="Select * from accountlines,borrowers where (accounttype = 'Pay'
184 or accounttype ='W')
185   and accountlines.borrowernumber = borrowers.borrowernumber";
186   if ($time eq 'today'){
187     $query .= " and date = now()";
188   } else {
189     $query.=" and date='$time'";
190   }
191 #  my $query="Select * from statistics,borrowers
192 #  where statistics.borrowernumber= borrowers.borrowernumber
193 #  and (statistics.type='payment' or statistics.type='writeoff') ";
194 #  if ($time eq 'today'){
195 #    $query=$query." and datetime = now()";
196 #  } else {
197 #    $query.=" and datetime > '$time'";
198 #  }
199   $query.=" order by timestamp";
200 #  print $query;
201   my $sth=$dbh->prepare($query);
202   $sth->execute;
203   my @results;
204   my $i=0;
205   while (my $data=$sth->fetchrow_hashref){
206     $results[$i]=$data;
207     $i++;
208   }
209    $sth->finish;
210 #  print $query;
211   return(@results);
212 }
213
214 # FIXME - Only used in stats.pl, which in turn is never used.
215 # Otherwise, it needs a POD.
216 sub getcharges{
217   my($borrowerno,$timestamp)=@_;
218   my $dbh = C4::Context->dbh;
219   my $timestamp2=$timestamp-1;
220   my $query="Select * from accountlines where borrowernumber=$borrowerno
221   and timestamp = '$timestamp' and accounttype <> 'Pay' and
222   accounttype <> 'W'";
223   my $sth=$dbh->prepare($query);
224 #  print $query,"<br>";
225   $sth->execute;
226   my $i=0;
227   my @results;
228   while (my $data=$sth->fetchrow_hashref){
229 #    if ($data->{'timestamp'} == $timestamp){
230       $results[$i]=$data;
231       $i++;
232 #    }
233   }
234   return(@results);
235 }
236
237 # This is only used in stats.pl and stats2.pl, neither of which is
238 # used.
239 # Otherwise, this needs a POD.
240 sub Getpaidbranch{
241   my($date,$borrno)=@_;
242   my $dbh = C4::Context->dbh;
243   my $query="select * from statistics where type='payment' and datetime
244   >'$date' and  borrowernumber='$borrno'";
245   my $sth=$dbh->prepare($query);
246   $sth->execute;
247 #  print $query;
248   my $data=$sth->fetchrow_hashref;
249   $sth->finish;
250   return($data->{'branch'});
251 }
252
253 # FIXME - This is only used in reservereport.pl and reservereport.xls,
254 # neither of which is used.
255 # Otherwise, it needs a POD.
256 sub unfilledreserves {
257   my $dbh = C4::Context->dbh;
258   my $query="select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where found <> 'F' and cancellationdate
259 is NULL and biblio.biblionumber=reserves.biblionumber and
260 reserves.constrainttype='o'
261 and (reserves.biblionumber=reserveconstraints.biblionumber
262 and reserves.borrowernumber=reserveconstraints.borrowernumber)
263 and
264 reserves.borrowernumber=borrowers.borrowernumber and
265 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
266 biblio.title,reserves.reservedate";
267   my $sth=$dbh->prepare($query);
268   $sth->execute;
269   my $i=0;
270   my @results;
271   while (my $data=$sth->fetchrow_hashref){
272     $results[$i]=$data;
273     $i++;
274   }
275   $sth->finish;
276   $query="select *,biblio.title from reserves,biblio,borrowers where found <> 'F' and cancellationdate
277 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
278 reserves.borrowernumber=borrowers.borrowernumber
279 order by
280 biblio.title,reserves.reservedate";
281   $sth=$dbh->prepare($query);
282   $sth->execute;
283   while (my $data=$sth->fetchrow_hashref){
284     $results[$i]=$data;
285     $i++;
286   }
287   $sth->finish;
288   return($i,\@results);
289 }
290
291 1;
292 __END__
293
294 =back
295
296 =head1 AUTHOR
297
298 Koha Developement team <info@koha.org>
299
300 =cut