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