1 package C4::Stats; #asummes C4/Stats
3 #requires DBI.pm to be installed
10 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
12 # set the version for version checking
16 @EXPORT = qw(&UpdateStats &statsreport &Count &Overdues &TotalOwing
17 &TotalPaid &getcharges &Getpaidbranch &unfilledreserves);
18 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
20 # your exported package globals go here,
21 # as well as any optionally exported functions
23 @EXPORT_OK = qw($Var1 %Hashit);
26 # non-exported package globals go here
27 use vars qw(@more $stuff);
29 # initalize package globals, first exported ones
35 # then the others (which are still accessible as $Some::Module::stuff)
39 # all file-scoped lexicals must be created before
40 # the functions below that use them.
42 # file-private lexicals go here
46 # here's a file-private function as a closure,
47 # callable as &$priv_func; it cannot be prototyped.
52 # make all your functions, whether exported or not;
55 #module to insert stats data into stats table
56 my ($env,$branch,$type,$amount,$other,$itemnum,$itemtype,$borrowernumber)=@_;
59 $branch=$env->{'branchcode'};
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')");
74 #module to return a list of stats for a given day,time,branch type
75 #or to return search stats
79 # if ($type eq 'issue'){
80 @data=circrep($time,$type);
88 my $query="Select * from statistics";
89 if ($time eq 'today'){
90 $query=$query." where type='$type' and datetime
91 >=datetime('yesterday'::date)";
93 my $sth=$dbh->prepare($query);
97 while (my $data=$sth->fetchrow_hashref){
98 $results[$i]="$data->{'datetime'}\t$data->{'branch'}";
109 my ($type,$branch,$time,$time2)=@_;
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);
115 my $data=$sth->fetchrow_hashref;
119 return($data->{'count(*)'});
124 my $query="Select count(*) from issues where date_due >= now()";
125 my $sth=$dbh->prepare($query);
127 my $count=$sth->fetchrow_hashref;
130 return($count->{'count(*)'});
136 my $query="Select sum(amountoutstanding) from accountlines";
137 if ($type eq 'fine'){
138 $query=$query." where accounttype='F' or accounttype='FN'";
140 my $sth=$dbh->prepare($query);
143 my $total=$sth->fetchrow_hashref;
146 return($total->{'sum(amountoutstanding)'});
152 my $query="Select * from accountlines,borrowers where (accounttype = 'Pay'
154 and accountlines.borrowernumber = borrowers.borrowernumber";
155 if ($time eq 'today'){
156 $query=$query." and date = now()";
158 $query.=" and date='$time'";
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()";
166 # $query.=" and datetime > '$time'";
168 $query.=" order by timestamp";
170 my $sth=$dbh->prepare($query);
174 while (my $data=$sth->fetchrow_hashref){
185 my($borrowerno,$timestamp)=@_;
187 my $timestamp2=$timestamp-1;
188 my $query="Select * from accountlines where borrowernumber=$borrowerno
189 and timestamp = '$timestamp' and accounttype <> 'Pay' and
191 my $sth=$dbh->prepare($query);
192 # print $query,"<br>";
196 while (my $data=$sth->fetchrow_hashref){
197 # if ($data->{'timestamp'} == $timestamp){
207 my($date,$borrno)=@_;
209 my $query="select * from statistics where type='payment' and datetime
210 >'$date' and borrowernumber='$borrno'";
211 my $sth=$dbh->prepare($query);
214 my $data=$sth->fetchrow_hashref;
217 return($data->{'branch'});
220 sub unfilledreserves {
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)
228 reserves.borrowernumber=borrowers.borrowernumber and
229 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
230 biblio.title,reserves.reservedate";
231 my $sth=$dbh->prepare($query);
235 while (my $data=$sth->fetchrow_hashref){
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
244 biblio.title,reserves.reservedate";
245 $sth=$dbh->prepare($query);
247 while (my $data=$sth->fetchrow_hashref){
253 return($i,\@results);
256 END { } # module clean-up code here (global destructor)