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