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){
127 $tempvalue{'value'}="$data->{'datetime'}\t$data->{'branch'}";
128 push(@results, \%tempvalue);
135 # FIXME - This is only used in stats.pl, which in turn is never used.
136 # Otherwise, this needs a POD.
138 my ($type,$branch,$time,$time2)=@_;
139 my $dbh = C4::Context->dbh;
140 my $query="Select count(*) from statistics where type='$type'";
141 $query.=" and datetime >= '$time' and datetime< '$time2' and branch='$branch'";
142 my $sth=$dbh->prepare($query);
144 my $data=$sth->fetchrow_hashref;
147 return($data->{'count(*)'});
150 # FIXME - This function doesn't appear to be used.
151 # If it is, it needs a POD.
153 my $dbh = C4::Context->dbh;
154 my $query="Select count(*) from issues where date_due >= now()";
155 my $sth=$dbh->prepare($query);
157 my $count=$sth->fetchrow_hashref;
159 return($count->{'count(*)'});
162 # FIXME - Never used.
163 # Otherwise, it'd need a POD.
166 my $dbh = C4::Context->dbh;
167 my $query="Select sum(amountoutstanding) from accountlines";
168 if ($type eq 'fine'){
169 $query .= " where accounttype='F' or accounttype='FN'";
171 my $sth=$dbh->prepare($query);
174 my $total=$sth->fetchrow_hashref;
176 return($total->{'sum(amountoutstanding)'});
179 # FIXME - Never used.
180 # Otherwise, it'd need a POD.
183 my $dbh = C4::Context->dbh;
184 my $query="Select * from accountlines,borrowers where (accounttype = 'Pay'
186 and accountlines.borrowernumber = borrowers.borrowernumber";
187 if ($time eq 'today'){
188 $query .= " and date = now()";
190 $query.=" and date='$time'";
192 # my $query="Select * from statistics,borrowers
193 # where statistics.borrowernumber= borrowers.borrowernumber
194 # and (statistics.type='payment' or statistics.type='writeoff') ";
195 # if ($time eq 'today'){
196 # $query=$query." and datetime = now()";
198 # $query.=" and datetime > '$time'";
200 $query.=" order by timestamp";
202 my $sth=$dbh->prepare($query);
206 while (my $data=$sth->fetchrow_hashref){
215 # FIXME - Only used in stats.pl, which in turn is never used.
216 # Otherwise, it needs a POD.
218 my($borrowerno,$timestamp)=@_;
219 my $dbh = C4::Context->dbh;
220 my $timestamp2=$timestamp-1;
221 my $query="Select * from accountlines where borrowernumber=$borrowerno
222 and timestamp = '$timestamp' and accounttype <> 'Pay' and
224 my $sth=$dbh->prepare($query);
225 # print $query,"<br>";
229 while (my $data=$sth->fetchrow_hashref){
230 # if ($data->{'timestamp'} == $timestamp){
238 # This is only used in stats.pl and stats2.pl, neither of which is
240 # Otherwise, this needs a POD.
242 my($date,$borrno)=@_;
243 my $dbh = C4::Context->dbh;
244 my $query="select * from statistics where type='payment' and datetime
245 >'$date' and borrowernumber='$borrno'";
246 my $sth=$dbh->prepare($query);
249 my $data=$sth->fetchrow_hashref;
251 return($data->{'branch'});
254 # FIXME - This is only used in reservereport.pl and reservereport.xls,
255 # neither of which is used.
256 # Otherwise, it needs a POD.
257 sub unfilledreserves {
258 my $dbh = C4::Context->dbh;
259 my $query="select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where found <> 'F' and cancellationdate
260 is NULL and biblio.biblionumber=reserves.biblionumber and
261 reserves.constrainttype='o'
262 and (reserves.biblionumber=reserveconstraints.biblionumber
263 and reserves.borrowernumber=reserveconstraints.borrowernumber)
265 reserves.borrowernumber=borrowers.borrowernumber and
266 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
267 biblio.title,reserves.reservedate";
268 my $sth=$dbh->prepare($query);
272 while (my $data=$sth->fetchrow_hashref){
277 $query="select *,biblio.title from reserves,biblio,borrowers where found <> 'F' and cancellationdate
278 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
279 reserves.borrowernumber=borrowers.borrowernumber
281 biblio.title,reserves.reservedate";
282 $sth=$dbh->prepare($query);
284 while (my $data=$sth->fetchrow_hashref){
289 return($i,\@results);
299 Koha Developement team <info@koha.org>