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