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