Added an incomplete POD, as well as some FIXME comments (which, as it
[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::Database;
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=C4Connect();
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   $dbh->disconnect;
90 }
91
92 # XXX - POD
93 # FIXME - Why does this function exist? Why not just rename &circrep
94 # to &statsreport?
95 # Then again, it only appears to be used in reports.pl which, in turn,
96 # doesn't appear to be used. So presumably this function is obsolete.
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 # XXX - Doc. Only used internally. Probably useless: see comment for
110 # &statsreport.
111 sub circrep {
112   my ($time,$type)=@_;
113   my $dbh=C4Connect;
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   }
120   my $sth=$dbh->prepare($query);
121   $sth->execute;
122   my $i=0;
123   my @results;
124   while (my $data=$sth->fetchrow_hashref){
125     $results[$i]="$data->{'datetime'}\t$data->{'branch'}";
126     $i++;
127   }
128   $sth->finish;
129 #  print $query;
130   $dbh->disconnect;
131   return(@results);
132
133 }
134
135 # XXX - POD
136 # FIXME - This is only used in stats.pl, which in turn is never used.
137 sub Count {
138   my ($type,$branch,$time,$time2)=@_;
139   my $dbh=C4Connect;
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   $dbh->disconnect;
148   return($data->{'count(*)'});
149 }
150
151 # XXX - POD. Doesn't appear to be used
152 sub Overdues{
153   my $dbh=C4Connect;
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   $dbh->disconnect;
160   return($count->{'count(*)'});
161 }
162
163 # XXX - POD. Never used
164 sub TotalOwing{
165   my ($type)=@_;
166   my $dbh=C4Connect;
167   my $query="Select sum(amountoutstanding) from accountlines";
168   if ($type eq 'fine'){
169     $query=$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   $dbh->disconnect;
177   return($total->{'sum(amountoutstanding)'});
178 }
179
180 # XXX - POD. Never used
181 sub TotalPaid {
182   my ($time)=@_;
183   my $dbh=C4Connect;
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=$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   $dbh->disconnect;
212 #  print $query;
213   return(@results);
214 }
215
216 # XXX - POD. Only used in stats.pl, which in turn is never used.
217 sub getcharges{
218   my($borrowerno,$timestamp)=@_;
219   my $dbh=C4Connect;
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   $dbh->disconnect;
236   return(@results);
237 }
238
239 # XXX - POD. This is only used in stats.pl and stats2.pl, neither of
240 # which is used.
241 sub Getpaidbranch{
242   my($date,$borrno)=@_;
243   my $dbh=C4Connect;
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   $dbh->disconnect;
252   return($data->{'branch'});
253 }
254
255 # XXX - POD. This is only used in reservereport.pl and
256 # reservereport.xls, neither of which is used.
257 sub unfilledreserves {
258   my $dbh=C4Connect;
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   $dbh->disconnect;
290   return($i,\@results);
291 }
292
293 END { }       # module clean-up code here (global destructor)
294
295 1;
296 __END__
297 =back
298
299 =head1 AUTHOR
300
301 Koha Developement team <info@koha.org>
302
303 =cut