Initial revision
[koha.git] / C4 / Stats.pm
1 package C4::Stats; #asummes C4/Stats
2
3 #requires DBI.pm to be installed
4 #uses DBD:Pg
5
6 use strict;
7 require Exporter;
8 use DBI;
9 use C4::Database;
10 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
11
12 # set the version for version checking
13 $VERSION = 0.01;
14
15 @ISA = qw(Exporter);
16 @EXPORT = qw(&UpdateStats &statsreport &Count &Overdues &TotalOwing
17 &TotalPaid &getcharges &Getpaidbranch &unfilledreserves);
18 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
19
20 # your exported package globals go here,
21 # as well as any optionally exported functions
22
23 @EXPORT_OK   = qw($Var1 %Hashit);
24
25
26 # non-exported package globals go here
27 use vars qw(@more $stuff);
28
29 # initalize package globals, first exported ones
30
31 my $Var1   = '';
32 my %Hashit = ();
33
34
35 # then the others (which are still accessible as $Some::Module::stuff)
36 my $stuff  = '';
37 my @more   = ();
38
39 # all file-scoped lexicals must be created before
40 # the functions below that use them.
41
42 # file-private lexicals go here
43 my $priv_var    = '';
44 my %secret_hash = ();
45
46 # here's a file-private function as a closure,
47 # callable as &$priv_func;  it cannot be prototyped.
48 my $priv_func = sub {
49   # stuff goes here.
50   };
51   
52 # make all your functions, whether exported or not;
53
54 sub UpdateStats {
55   #module to insert stats data into stats table
56   my ($env,$branch,$type,$amount,$other,$itemnum,$itemtype)=@_;
57   my $dbh=C4Connect();
58   my $branch=$env->{'branchcode'};
59   my $user = $env->{'usercode'};
60   my $sth=$dbh->prepare("Insert into statistics
61      (datetime,branch,type,usercode,value,other,itemnumber,itemtype) 
62      values (now(),'$branch',
63      '$type','$user','$amount','$other','$itemnum','$itemtype')");
64   $sth->execute;
65   $sth->finish;
66   $dbh->disconnect;
67 }
68
69 sub statsreport {
70   #module to return a list of stats for a given day,time,branch type
71   #or to return search stats
72   my ($type,$time)=@_;
73   my @data;
74 #  print "here";
75 #  if ($type eq 'issue'){
76     @data=circrep($time,$type);
77 #  }
78   return(@data);
79 }
80
81 sub circrep {
82   my ($time,$type)=@_;
83   my $dbh=C4Connect;
84   my $query="Select * from statistics";
85   if ($time eq 'today'){
86     $query=$query." where type='$type' and datetime
87     >=datetime('yesterday'::date)";
88   }
89   my $sth=$dbh->prepare($query);
90   $sth->execute;
91   my $i=0;
92   my @results;
93   while (my $data=$sth->fetchrow_hashref){
94     $results[$i]="$data->{'datetime'}\t$data->{'branch'}";
95     $i++;
96   }
97   $sth->finish;
98 #  print $query;
99   $dbh->disconnect;
100   return(@results);
101
102 }
103
104 sub Count {
105   my ($type,$branch,$time,$time2)=@_;
106   my $dbh=C4Connect;
107   my $query="Select count(*) from statistics where type='$type'";
108   $query.=" and datetime >= '$time' and datetime< '$time2' and branch='$branch'";
109   my $sth=$dbh->prepare($query);
110   $sth->execute;
111   my $data=$sth->fetchrow_hashref;
112   $sth->finish;
113 #  print $query;
114   $dbh->disconnect;
115   return($data->{'count(*)'});
116 }
117
118 sub Overdues{
119   my $dbh=C4Connect;
120   my $query="Select count(*) from issues where date_due >= now()";
121   my $sth=$dbh->prepare($query);
122   $sth->execute;
123   my $count=$sth->fetchrow_hashref;
124   $sth->finish;
125   $dbh->disconnect;
126   return($count->{'count(*)'});  
127 }
128
129 sub TotalOwing{
130   my ($type)=@_;
131   my $dbh=C4Connect;
132   my $query="Select sum(amountoutstanding) from accountlines";
133   if ($type eq 'fine'){
134     $query=$query." where accounttype='F' or accounttype='FN'";
135   }
136   my $sth=$dbh->prepare($query);
137 #  print $query;
138   $sth->execute;
139    my $total=$sth->fetchrow_hashref;
140    $sth->finish;
141   $dbh->disconnect; 
142   return($total->{'sum(amountoutstanding)'});
143 }
144
145 sub TotalPaid {
146   my ($time)=@_;
147   my $dbh=C4Connect;
148   my $query="Select * from accountlines,borrowers where accounttype = 'Pay' 
149   and accountlines.borrowernumber = borrowers.borrowernumber";
150   if ($time eq 'today'){
151     $query=$query." and date = now()";
152   } else {
153     $query.=" and date='$time'";
154   }
155 #  $query.=" order by timestamp";
156   my $sth=$dbh->prepare($query);
157   $sth->execute;
158   my @results;
159   my $i=0;
160   while (my $data=$sth->fetchrow_hashref){
161     $results[$i]=$data;
162     $i++;
163   }
164    $sth->finish;
165   $dbh->disconnect; 
166 #  print $query;
167   return(@results);
168 }
169
170 sub getcharges{
171   my($borrowerno,$timestamp)=@_;
172   my $dbh=C4Connect;
173   my $timestamp2=$timestamp-1;
174   my $query="Select * from accountlines where borrowernumber=$borrowerno
175   and timestamp <= '$timestamp' and accounttype <> 'Pay' and
176   accounttype <> 'W'";
177   my $sth=$dbh->prepare($query);
178 #  print $query,"<br>";
179   $sth->execute;
180   my $i=0;
181   my @results;
182   while (my $data=$sth->fetchrow_hashref){
183     if ($data->{'timestamp'} == $timestamp){
184       $results[$i]=$data;
185       $i++;
186     }
187   }
188   $dbh->disconnect;
189   return(@results);
190 }
191
192 sub Getpaidbranch{
193   my($date)=@_;
194   my $dbh=C4Connect;
195   my $query="select * from statistics where type='payment' and datetime='$date'";
196   my $sth=$dbh->prepare($query);
197   $sth->execute;
198 #  print $query;
199   my $data=$sth->fetchrow_hashref;
200   $sth->finish;
201   $dbh->disconnect;
202   return($data->{'branch'});
203 }
204
205 sub unfilledreserves {
206   my $dbh=C4Connect;
207   my $query="select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where found <> 'F' and cancellationdate
208 is NULL and biblio.biblionumber=reserves.biblionumber and
209 reserves.constrainttype='o'
210 and (reserves.biblionumber=reserveconstraints.biblionumber
211 and reserves.borrowernumber=reserveconstraints.borrowernumber)
212 and
213 reserves.borrowernumber=borrowers.borrowernumber and
214 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
215 biblio.title,reserves.reservedate";
216   my $sth=$dbh->prepare($query);
217   $sth->execute;
218   my $i=0;
219   my @results;
220   while (my $data=$sth->fetchrow_hashref){
221     $results[$i]=$data;
222     $i++;
223   }
224   $sth->finish;
225   $query="select *,biblio.title from reserves,biblio,borrowers where found <> 'F' and cancellationdate
226 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
227 reserves.borrowernumber=borrowers.borrowernumber
228 order by
229 biblio.title,reserves.reservedate";
230   $sth=$dbh->prepare($query);
231   $sth->execute;
232   while (my $data=$sth->fetchrow_hashref){
233     @results[$i]=$data;
234     $i++;
235   }
236   $sth->finish;
237   $dbh->disconnect;
238   return($i,\@results);
239 }
240
241 END { }       # module clean-up code here (global destructor)
242   
243