5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
26 use vars qw($VERSION @ISA @EXPORT);
28 # set the version for version checking
33 C4::Stats - Update Koha statistics (log)
41 The C<&UpdateStats> function adds an entry to the statistics table in
42 the Koha database, which acts as an activity log.
51 @EXPORT = qw(&UpdateStats &statsreport &Count &Overdues &TotalOwing
52 &TotalPaid &getcharges &Getpaidbranch &unfilledreserves);
56 &UpdateStats($env, $branch, $type, $value, $other, $itemnumber,
57 $itemtype, $borrowernumber);
59 Adds a line to the statistics table of the Koha database. In effect,
62 C<$branch>, C<$type>, C<$value>, C<$other>, C<$itemnumber>,
63 C<$itemtype>, and C<$borrowernumber> correspond to the fields of the
64 statistics table in the Koha database.
66 If C<$branch> is the empty string, the branch code will be taken from
67 C<$env-E<gt>{branchcode}>.
69 C<$env-E<gt>{usercode}> specifies the value of the C<usercode> field.
74 #module to insert stats data into stats table
75 my ($env,$branch,$type,$amount,$other,$itemnum,$itemtype,$borrowernumber)=@_;
76 my $dbh = C4::Context->dbh;
78 $branch=$env->{'branchcode'};
80 my $user = $env->{'usercode'};
81 print $borrowernumber;
82 # FIXME - Use $dbh->do() instead
83 my $sth=$dbh->prepare("Insert into statistics
84 (datetime,branch,type,usercode,value,
85 other,itemnumber,itemtype,borrowernumber)
86 values (now(),'$branch','$type','$user','$amount',
87 '$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.
96 # If not, it needs a POD.
98 #module to return a list of stats for a given day,time,branch type
99 #or to return search stats
103 # if ($type eq 'issue'){
104 @data=circrep($time,$type);
109 # Only used internally. Probably useless: see comment for
113 my $dbh = C4::Context->dbh;
114 my $query="Select * from statistics";
115 if ($time eq 'today'){
116 # FIXME - What is this supposed to do? MySQL 3.23.42 barfs on it.
117 $query=$query." where type='$type' and datetime
118 >=datetime('yesterday'::date)";
121 my $sth=$dbh->prepare($query);
125 while (my $data=$sth->fetchrow_hashref){
126 $results[$i]="$data->{'datetime'}\t$data->{'branch'}";
134 # FIXME - This is only used in stats.pl, which in turn is never used.
135 # Otherwise, this needs a POD.
137 my ($type,$branch,$time,$time2)=@_;
138 my $dbh = C4::Context->dbh;
139 my $query="Select count(*) from statistics where type='$type'";
140 $query.=" and datetime >= '$time' and datetime< '$time2' and branch='$branch'";
141 my $sth=$dbh->prepare($query);
143 my $data=$sth->fetchrow_hashref;
146 return($data->{'count(*)'});
149 # FIXME - This function doesn't appear to be used.
150 # If it is, it needs a POD.
152 my $dbh = C4::Context->dbh;
153 my $query="Select count(*) from issues where date_due >= now()";
154 my $sth=$dbh->prepare($query);
156 my $count=$sth->fetchrow_hashref;
158 return($count->{'count(*)'});
161 # FIXME - Never used.
162 # Otherwise, it'd need a POD.
165 my $dbh = C4::Context->dbh;
166 my $query="Select sum(amountoutstanding) from accountlines";
167 if ($type eq 'fine'){
168 $query .= " where accounttype='F' or accounttype='FN'";
170 my $sth=$dbh->prepare($query);
173 my $total=$sth->fetchrow_hashref;
175 return($total->{'sum(amountoutstanding)'});
178 # FIXME - Never used.
179 # Otherwise, it'd need a POD.
182 my $dbh = C4::Context->dbh;
183 my $query="Select * from accountlines,borrowers where (accounttype = 'Pay'
185 and accountlines.borrowernumber = borrowers.borrowernumber";
186 if ($time eq 'today'){
187 $query .= " and date = now()";
189 $query.=" and date='$time'";
191 # my $query="Select * from statistics,borrowers
192 # where statistics.borrowernumber= borrowers.borrowernumber
193 # and (statistics.type='payment' or statistics.type='writeoff') ";
194 # if ($time eq 'today'){
195 # $query=$query." and datetime = now()";
197 # $query.=" and datetime > '$time'";
199 $query.=" order by timestamp";
201 my $sth=$dbh->prepare($query);
205 while (my $data=$sth->fetchrow_hashref){
214 # FIXME - Only used in stats.pl, which in turn is never used.
215 # Otherwise, it needs a POD.
217 my($borrowerno,$timestamp)=@_;
218 my $dbh = C4::Context->dbh;
219 my $timestamp2=$timestamp-1;
220 my $query="Select * from accountlines where borrowernumber=$borrowerno
221 and timestamp = '$timestamp' and accounttype <> 'Pay' and
223 my $sth=$dbh->prepare($query);
224 # print $query,"<br>";
228 while (my $data=$sth->fetchrow_hashref){
229 # if ($data->{'timestamp'} == $timestamp){
237 # This is only used in stats.pl and stats2.pl, neither of which is
239 # Otherwise, this needs a POD.
241 my($date,$borrno)=@_;
242 my $dbh = C4::Context->dbh;
243 my $query="select * from statistics where type='payment' and datetime
244 >'$date' and borrowernumber='$borrno'";
245 my $sth=$dbh->prepare($query);
248 my $data=$sth->fetchrow_hashref;
250 return($data->{'branch'});
253 # FIXME - This is only used in reservereport.pl and reservereport.xls,
254 # neither of which is used.
255 # Otherwise, it needs a POD.
256 sub unfilledreserves {
257 my $dbh = C4::Context->dbh;
258 my $query="select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where found <> 'F' and cancellationdate
259 is NULL and biblio.biblionumber=reserves.biblionumber and
260 reserves.constrainttype='o'
261 and (reserves.biblionumber=reserveconstraints.biblionumber
262 and reserves.borrowernumber=reserveconstraints.borrowernumber)
264 reserves.borrowernumber=borrowers.borrowernumber and
265 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
266 biblio.title,reserves.reservedate";
267 my $sth=$dbh->prepare($query);
271 while (my $data=$sth->fetchrow_hashref){
276 $query="select *,biblio.title from reserves,biblio,borrowers where found <> 'F' and cancellationdate
277 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
278 reserves.borrowernumber=borrowers.borrowernumber
280 biblio.title,reserves.reservedate";
281 $sth=$dbh->prepare($query);
283 while (my $data=$sth->fetchrow_hashref){
288 return($i,\@results);
298 Koha Developement team <info@koha.org>