Added PODs.
[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 @ISA = qw(Exporter);
31 @EXPORT = qw(&UpdateStats &statsreport &Count &Overdues &TotalOwing
32 &TotalPaid &getcharges &Getpaidbranch &unfilledreserves);
33
34
35 sub UpdateStats {
36   #module to insert stats data into stats table
37   my ($env,$branch,$type,$amount,$other,$itemnum,$itemtype,$borrowernumber)=@_;
38   my $dbh=C4Connect();
39   if ($branch eq ''){
40     $branch=$env->{'branchcode'};
41   }
42   my $user = $env->{'usercode'};
43   print $borrowernumber;
44   my $sth=$dbh->prepare("Insert into statistics
45   (datetime,branch,type,usercode,value,
46   other,itemnumber,itemtype,borrowernumber) 
47   values (now(),'$branch','$type','$user','$amount',
48   '$other','$itemnum','$itemtype','$borrowernumber')");
49   $sth->execute;
50   $sth->finish;
51   $dbh->disconnect;
52 }
53
54 sub statsreport {
55   #module to return a list of stats for a given day,time,branch type
56   #or to return search stats
57   my ($type,$time)=@_;
58   my @data;
59 #  print "here";
60 #  if ($type eq 'issue'){
61     @data=circrep($time,$type);
62 #  }
63   return(@data);
64 }
65
66 sub circrep {
67   my ($time,$type)=@_;
68   my $dbh=C4Connect;
69   my $query="Select * from statistics";
70   if ($time eq 'today'){
71     $query=$query." where type='$type' and datetime
72     >=datetime('yesterday'::date)";
73   }
74   my $sth=$dbh->prepare($query);
75   $sth->execute;
76   my $i=0;
77   my @results;
78   while (my $data=$sth->fetchrow_hashref){
79     $results[$i]="$data->{'datetime'}\t$data->{'branch'}";
80     $i++;
81   }
82   $sth->finish;
83 #  print $query;
84   $dbh->disconnect;
85   return(@results);
86
87 }
88
89 sub Count {
90   my ($type,$branch,$time,$time2)=@_;
91   my $dbh=C4Connect;
92   my $query="Select count(*) from statistics where type='$type'";
93   $query.=" and datetime >= '$time' and datetime< '$time2' and branch='$branch'";
94   my $sth=$dbh->prepare($query);
95   $sth->execute;
96   my $data=$sth->fetchrow_hashref;
97   $sth->finish;
98 #  print $query;
99   $dbh->disconnect;
100   return($data->{'count(*)'});
101 }
102
103 sub Overdues{
104   my $dbh=C4Connect;
105   my $query="Select count(*) from issues where date_due >= now()";
106   my $sth=$dbh->prepare($query);
107   $sth->execute;
108   my $count=$sth->fetchrow_hashref;
109   $sth->finish;
110   $dbh->disconnect;
111   return($count->{'count(*)'});  
112 }
113
114 sub TotalOwing{
115   my ($type)=@_;
116   my $dbh=C4Connect;
117   my $query="Select sum(amountoutstanding) from accountlines";
118   if ($type eq 'fine'){
119     $query=$query." where accounttype='F' or accounttype='FN'";
120   }
121   my $sth=$dbh->prepare($query);
122 #  print $query;
123   $sth->execute;
124    my $total=$sth->fetchrow_hashref;
125    $sth->finish;
126   $dbh->disconnect; 
127   return($total->{'sum(amountoutstanding)'});
128 }
129
130 sub TotalPaid {
131   my ($time)=@_;
132   my $dbh=C4Connect;
133   my $query="Select * from accountlines,borrowers where (accounttype = 'Pay'
134 or accounttype ='W')
135   and accountlines.borrowernumber = borrowers.borrowernumber";
136   if ($time eq 'today'){
137     $query=$query." and date = now()";
138   } else {
139     $query.=" and date='$time'";
140   }
141 #  my $query="Select * from statistics,borrowers
142 #  where statistics.borrowernumber= borrowers.borrowernumber
143 #  and (statistics.type='payment' or statistics.type='writeoff') ";
144 #  if ($time eq 'today'){
145 #    $query=$query." and datetime = now()";
146 #  } else {
147 #    $query.=" and datetime > '$time'";
148 #  }
149   $query.=" order by timestamp";
150 #  print $query;
151   my $sth=$dbh->prepare($query);
152   $sth->execute;
153   my @results;
154   my $i=0;
155   while (my $data=$sth->fetchrow_hashref){
156     $results[$i]=$data;
157     $i++;
158   }
159    $sth->finish;
160   $dbh->disconnect; 
161 #  print $query;
162   return(@results);
163 }
164
165 sub getcharges{
166   my($borrowerno,$timestamp)=@_;
167   my $dbh=C4Connect;
168   my $timestamp2=$timestamp-1;
169   my $query="Select * from accountlines where borrowernumber=$borrowerno
170   and timestamp = '$timestamp' and accounttype <> 'Pay' and
171   accounttype <> 'W'";
172   my $sth=$dbh->prepare($query);
173 #  print $query,"<br>";
174   $sth->execute;
175   my $i=0;
176   my @results;
177   while (my $data=$sth->fetchrow_hashref){
178 #    if ($data->{'timestamp'} == $timestamp){
179       $results[$i]=$data;
180       $i++;
181 #    }
182   }
183   $dbh->disconnect;
184   return(@results);
185 }
186
187 sub Getpaidbranch{
188   my($date,$borrno)=@_;
189   my $dbh=C4Connect;
190   my $query="select * from statistics where type='payment' and datetime
191   >'$date' and  borrowernumber='$borrno'";
192   my $sth=$dbh->prepare($query);
193   $sth->execute;
194 #  print $query;
195   my $data=$sth->fetchrow_hashref;
196   $sth->finish;
197   $dbh->disconnect;
198   return($data->{'branch'});
199 }
200
201 sub unfilledreserves {
202   my $dbh=C4Connect;
203   my $query="select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where found <> 'F' and cancellationdate
204 is NULL and biblio.biblionumber=reserves.biblionumber and
205 reserves.constrainttype='o'
206 and (reserves.biblionumber=reserveconstraints.biblionumber
207 and reserves.borrowernumber=reserveconstraints.borrowernumber)
208 and
209 reserves.borrowernumber=borrowers.borrowernumber and
210 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
211 biblio.title,reserves.reservedate";
212   my $sth=$dbh->prepare($query);
213   $sth->execute;
214   my $i=0;
215   my @results;
216   while (my $data=$sth->fetchrow_hashref){
217     $results[$i]=$data;
218     $i++;
219   }
220   $sth->finish;
221   $query="select *,biblio.title from reserves,biblio,borrowers where found <> 'F' and cancellationdate
222 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
223 reserves.borrowernumber=borrowers.borrowernumber
224 order by
225 biblio.title,reserves.reservedate";
226   $sth=$dbh->prepare($query);
227   $sth->execute;
228   while (my $data=$sth->fetchrow_hashref){
229     $results[$i]=$data;
230     $i++;
231   }
232   $sth->finish;
233   $dbh->disconnect;
234   return($i,\@results);
235 }
236
237 END { }       # module clean-up code here (global destructor)
238   
239