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