1 package C4::Stats; #assumes C4/Stats
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
27 # set the version for version checking
32 C4::Stats - Update Koha statistics (log)
40 The C<&UpdateStats> function adds an entry to the statistics table in
41 the Koha database, which acts as an activity log.
50 @EXPORT = qw(&UpdateStats &statsreport &Count &Overdues &TotalOwing
51 &TotalPaid &getcharges &Getpaidbranch &unfilledreserves);
55 &UpdateStats($env, $branch, $type, $value, $other, $itemnumber,
56 $itemtype, $borrowernumber);
58 Adds a line to the statistics table of the Koha database. In effect,
61 C<$branch>, C<$type>, C<$value>, C<$other>, C<$itemnumber>,
62 C<$itemtype>, and C<$borrowernumber> correspond to the fields of the
63 statistics table in the Koha database.
65 If C<$branch> is the empty string, the branch code will be taken from
66 C<$env-E<gt>{branchcode}>.
68 C<$env-E<gt>{usercode}> specifies the value of the C<usercode> field.
73 #module to insert stats data into stats table
74 my ($env,$branch,$type,$amount,$other,$itemnum,$itemtype,$borrowernumber)=@_;
75 my $dbh = C4::Context->dbh;
77 $branch=$env->{'branchcode'};
79 my $user = $env->{'usercode'};
80 print $borrowernumber;
81 # FIXME - Use $dbh->do() instead
82 my $sth=$dbh->prepare("Insert into statistics
83 (datetime,branch,type,usercode,value,
84 other,itemnumber,itemtype,borrowernumber)
85 values (now(),'$branch','$type','$user','$amount',
86 '$other','$itemnum','$itemtype','$borrowernumber')");
92 # FIXME - Why does this function exist? Why not just rename &circrep
94 # Then again, it only appears to be used in reports.pl which, in turn,
95 # doesn't appear to be used. So presumably this function is obsolete.
97 #module to return a list of stats for a given day,time,branch type
98 #or to return search stats
102 # if ($type eq 'issue'){
103 @data=circrep($time,$type);
108 # XXX - Doc. Only used internally. Probably useless: see comment for
112 my $dbh = C4::Context->dbh;
113 my $query="Select * from statistics";
114 if ($time eq 'today'){
115 # FIXME - What is this supposed to do? MySQL 3.23.42 barfs on it.
116 $query=$query." where type='$type' and datetime
117 >=datetime('yesterday'::date)";
119 my $sth=$dbh->prepare($query);
123 while (my $data=$sth->fetchrow_hashref){
124 $results[$i]="$data->{'datetime'}\t$data->{'branch'}";
133 # FIXME - This is only used in stats.pl, which in turn is never used.
135 my ($type,$branch,$time,$time2)=@_;
136 my $dbh = C4::Context->dbh;
137 my $query="Select count(*) from statistics where type='$type'";
138 $query.=" and datetime >= '$time' and datetime< '$time2' and branch='$branch'";
139 my $sth=$dbh->prepare($query);
141 my $data=$sth->fetchrow_hashref;
144 return($data->{'count(*)'});
147 # XXX - POD. Doesn't appear to be used
149 my $dbh = C4::Context->dbh;
150 my $query="Select count(*) from issues where date_due >= now()";
151 my $sth=$dbh->prepare($query);
153 my $count=$sth->fetchrow_hashref;
155 return($count->{'count(*)'});
158 # XXX - POD. Never used
161 my $dbh = C4::Context->dbh;
162 my $query="Select sum(amountoutstanding) from accountlines";
163 if ($type eq 'fine'){
164 $query=$query." where accounttype='F' or accounttype='FN'";
166 my $sth=$dbh->prepare($query);
169 my $total=$sth->fetchrow_hashref;
171 return($total->{'sum(amountoutstanding)'});
174 # XXX - POD. Never used
177 my $dbh = C4::Context->dbh;
178 my $query="Select * from accountlines,borrowers where (accounttype = 'Pay'
180 and accountlines.borrowernumber = borrowers.borrowernumber";
181 if ($time eq 'today'){
182 $query=$query." and date = now()";
184 $query.=" and date='$time'";
186 # my $query="Select * from statistics,borrowers
187 # where statistics.borrowernumber= borrowers.borrowernumber
188 # and (statistics.type='payment' or statistics.type='writeoff') ";
189 # if ($time eq 'today'){
190 # $query=$query." and datetime = now()";
192 # $query.=" and datetime > '$time'";
194 $query.=" order by timestamp";
196 my $sth=$dbh->prepare($query);
200 while (my $data=$sth->fetchrow_hashref){
209 # XXX - POD. Only used in stats.pl, which in turn is never used.
211 my($borrowerno,$timestamp)=@_;
212 my $dbh = C4::Context->dbh;
213 my $timestamp2=$timestamp-1;
214 my $query="Select * from accountlines where borrowernumber=$borrowerno
215 and timestamp = '$timestamp' and accounttype <> 'Pay' and
217 my $sth=$dbh->prepare($query);
218 # print $query,"<br>";
222 while (my $data=$sth->fetchrow_hashref){
223 # if ($data->{'timestamp'} == $timestamp){
231 # XXX - POD. This is only used in stats.pl and stats2.pl, neither of
234 my($date,$borrno)=@_;
235 my $dbh = C4::Context->dbh;
236 my $query="select * from statistics where type='payment' and datetime
237 >'$date' and borrowernumber='$borrno'";
238 my $sth=$dbh->prepare($query);
241 my $data=$sth->fetchrow_hashref;
243 return($data->{'branch'});
246 # XXX - POD. This is only used in reservereport.pl and
247 # reservereport.xls, neither of which is used.
248 sub unfilledreserves {
249 my $dbh = C4::Context->dbh;
250 my $query="select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where found <> 'F' and cancellationdate
251 is NULL and biblio.biblionumber=reserves.biblionumber and
252 reserves.constrainttype='o'
253 and (reserves.biblionumber=reserveconstraints.biblionumber
254 and reserves.borrowernumber=reserveconstraints.borrowernumber)
256 reserves.borrowernumber=borrowers.borrowernumber and
257 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
258 biblio.title,reserves.reservedate";
259 my $sth=$dbh->prepare($query);
263 while (my $data=$sth->fetchrow_hashref){
268 $query="select *,biblio.title from reserves,biblio,borrowers where found <> 'F' and cancellationdate
269 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
270 reserves.borrowernumber=borrowers.borrowernumber
272 biblio.title,reserves.reservedate";
273 $sth=$dbh->prepare($query);
275 while (my $data=$sth->fetchrow_hashref){
280 return($i,\@results);
283 END { } # module clean-up code here (global destructor)
291 Koha Developement team <info@koha.org>