Templating reservereport et shelves.
[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         my %tempvalue;
127     $tempvalue{'value'}="$data->{'datetime'}\t$data->{'branch'}";
128     push(@results, \%tempvalue);
129   }
130   $sth->finish;
131 #  print $query;
132   return(@results);
133 }
134
135 # FIXME - This is only used in stats.pl, which in turn is never used.
136 # Otherwise, this needs a POD.
137 sub Count {
138   my ($type,$branch,$time,$time2)=@_;
139   my $dbh = C4::Context->dbh;
140   my $query="Select count(*) from statistics where type='$type'";
141   $query.=" and datetime >= '$time' and datetime< '$time2' and branch='$branch'";
142   my $sth=$dbh->prepare($query);
143   $sth->execute;
144   my $data=$sth->fetchrow_hashref;
145   $sth->finish;
146 #  print $query;
147   return($data->{'count(*)'});
148 }
149
150 # FIXME - This function doesn't appear to be used.
151 # If it is, it needs a POD.
152 sub Overdues{
153   my $dbh = C4::Context->dbh;
154   my $query="Select count(*) from issues where date_due >= now()";
155   my $sth=$dbh->prepare($query);
156   $sth->execute;
157   my $count=$sth->fetchrow_hashref;
158   $sth->finish;
159   return($count->{'count(*)'});
160 }
161
162 # FIXME - Never used.
163 # Otherwise, it'd need a POD.
164 sub TotalOwing{
165   my ($type)=@_;
166   my $dbh = C4::Context->dbh;
167   my $query="Select sum(amountoutstanding) from accountlines";
168   if ($type eq 'fine'){
169     $query .= " where accounttype='F' or accounttype='FN'";
170   }
171   my $sth=$dbh->prepare($query);
172 #  print $query;
173   $sth->execute;
174    my $total=$sth->fetchrow_hashref;
175    $sth->finish;
176   return($total->{'sum(amountoutstanding)'});
177 }
178
179 # FIXME - Never used.
180 # Otherwise, it'd need a POD.
181 sub TotalPaid {
182   my ($time)=@_;
183   my $dbh = C4::Context->dbh;
184   my $query="Select * from accountlines,borrowers where (accounttype = 'Pay'
185 or accounttype ='W')
186   and accountlines.borrowernumber = borrowers.borrowernumber";
187   if ($time eq 'today'){
188     $query .= " and date = now()";
189   } else {
190     $query.=" and date='$time'";
191   }
192 #  my $query="Select * from statistics,borrowers
193 #  where statistics.borrowernumber= borrowers.borrowernumber
194 #  and (statistics.type='payment' or statistics.type='writeoff') ";
195 #  if ($time eq 'today'){
196 #    $query=$query." and datetime = now()";
197 #  } else {
198 #    $query.=" and datetime > '$time'";
199 #  }
200   $query.=" order by timestamp";
201 #  print $query;
202   my $sth=$dbh->prepare($query);
203   $sth->execute;
204   my @results;
205   my $i=0;
206   while (my $data=$sth->fetchrow_hashref){
207     $results[$i]=$data;
208     $i++;
209   }
210    $sth->finish;
211 #  print $query;
212   return(@results);
213 }
214
215 # FIXME - Only used in stats.pl, which in turn is never used.
216 # Otherwise, it needs a POD.
217 sub getcharges{
218   my($borrowerno,$timestamp)=@_;
219   my $dbh = C4::Context->dbh;
220   my $timestamp2=$timestamp-1;
221   my $query="Select * from accountlines where borrowernumber=$borrowerno
222   and timestamp = '$timestamp' and accounttype <> 'Pay' and
223   accounttype <> 'W'";
224   my $sth=$dbh->prepare($query);
225 #  print $query,"<br>";
226   $sth->execute;
227   my $i=0;
228   my @results;
229   while (my $data=$sth->fetchrow_hashref){
230 #    if ($data->{'timestamp'} == $timestamp){
231       $results[$i]=$data;
232       $i++;
233 #    }
234   }
235   return(@results);
236 }
237
238 # This is only used in stats.pl and stats2.pl, neither of which is
239 # used.
240 # Otherwise, this needs a POD.
241 sub Getpaidbranch{
242   my($date,$borrno)=@_;
243   my $dbh = C4::Context->dbh;
244   my $query="select * from statistics where type='payment' and datetime
245   >'$date' and  borrowernumber='$borrno'";
246   my $sth=$dbh->prepare($query);
247   $sth->execute;
248 #  print $query;
249   my $data=$sth->fetchrow_hashref;
250   $sth->finish;
251   return($data->{'branch'});
252 }
253
254 # FIXME - This is only used in reservereport.pl and reservereport.xls,
255 # neither of which is used.
256 # Otherwise, it needs a POD.
257 sub unfilledreserves {
258   my $dbh = C4::Context->dbh;
259   my $query="select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where found <> 'F' and cancellationdate
260 is NULL and biblio.biblionumber=reserves.biblionumber and
261 reserves.constrainttype='o'
262 and (reserves.biblionumber=reserveconstraints.biblionumber
263 and reserves.borrowernumber=reserveconstraints.borrowernumber)
264 and
265 reserves.borrowernumber=borrowers.borrowernumber and
266 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
267 biblio.title,reserves.reservedate";
268   my $sth=$dbh->prepare($query);
269   $sth->execute;
270   my $i=0;
271   my @results;
272   while (my $data=$sth->fetchrow_hashref){
273     $results[$i]=$data;
274     $i++;
275   }
276   $sth->finish;
277   $query="select *,biblio.title from reserves,biblio,borrowers where found <> 'F' and cancellationdate
278 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
279 reserves.borrowernumber=borrowers.borrowernumber
280 order by
281 biblio.title,reserves.reservedate";
282   $sth=$dbh->prepare($query);
283   $sth->execute;
284   while (my $data=$sth->fetchrow_hashref){
285     $results[$i]=$data;
286     $i++;
287   }
288   $sth->finish;
289   return($i,\@results);
290 }
291
292 1;
293 __END__
294
295 =back
296
297 =head1 AUTHOR
298
299 Koha Developement team <info@koha.org>
300
301 =cut