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
29 $VERSION = $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
30 shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
35 C4::Stats - Update Koha statistics (log)
43 The C<&UpdateStats> function adds an entry to the statistics table in
44 the Koha database, which acts as an activity log.
53 @EXPORT = qw(&UpdateStats &statsreport &TotalOwing
54 &TotalPaid &getcharges &Getpaidbranch &unfilledreserves &getcredits
59 &UpdateStats($env, $branch, $type, $value, $other, $itemnumber,
60 $itemtype, $borrowernumber);
62 Adds a line to the statistics table of the Koha database. In effect,
65 C<$branch>, C<$type>, C<$value>, C<$other>, C<$itemnumber>,
66 C<$itemtype>, and C<$borrowernumber> correspond to the fields of the
67 statistics table in the Koha database.
69 If C<$branch> is the empty string, the branch code will be taken from
70 C<$env-E<gt>{branchcode}>.
72 C<$env-E<gt>{usercode}> specifies the value of the C<usercode> field.
79 #module to insert stats data into stats table
82 $amount, $other, $itemnum,
83 $itemtype, $borrowernumber, $accountno
86 my $dbh = C4::Context->dbh;
87 if ( $branch eq '' ) {
88 $branch = $env->{'branchcode'};
90 my $user = $env->{'usercode'};
91 my $organisation = $env->{'organisation'};
93 # FIXME - Use $dbh->do() instead
94 my $sth = $dbh->prepare(
95 "Insert into statistics (datetime,branch,type,usercode,value,
96 other,itemnumber,itemtype,borrowernumber,proccode,associatedborrower) values (now(),?,?,?,?,?,?,?,?,?,?)"
99 $branch, $type, $user, $amount,
100 $other, $itemnum, $itemtype, $borrowernumber,
101 $accountno, $organisation
106 # Otherwise, it'd need a POD.
108 my ( $time, $time2, $spreadsheet ) = @_;
109 $time2 = $time unless $time2;
110 my $dbh = C4::Context->dbh;
111 my $query = "SELECT * FROM statistics,borrowers
112 WHERE statistics.borrowernumber= borrowers.borrowernumber
113 AND (statistics.type='payment' OR statistics.type='writeoff') ";
114 if ( $time eq 'today' ) {
115 $query = $query . " AND datetime = now()";
118 $query .= " AND datetime > '$time'";
120 if ( $time2 ne '' ) {
121 $query .= " AND datetime < '$time2'";
124 $query .= " ORDER BY branch, type";
126 my $sth = $dbh->prepare($query);
129 while ( my $data = $sth->fetchrow_hashref ) {
130 push @results, $data;
136 # Otherwise, it needs a POD.
138 my ( $borrowerno, $timestamp, $accountno ) = @_;
139 my $dbh = C4::Context->dbh;
140 my $timestamp2 = $timestamp - 1;
144 # getcharges is now taking accountno. as an argument
146 $sth = $dbh->prepare(
147 "Select * from accountlines where borrowernumber=?
150 $sth->execute( $borrowerno, $accountno );
152 # this bit left in for old 2 arg usage of getcharges
155 $sth = $dbh->prepare(
156 "Select * from accountlines where borrowernumber=?
157 and timestamp = ? and accounttype <> 'Pay' and
160 $sth->execute( $borrowerno, $timestamp );
163 # print $query,"<br>";
166 while ( my $data = $sth->fetchrow_hashref ) {
168 # if ($data->{'timestamp'} == $timestamp){
169 $results[$i] = $data;
177 # Otherwise, it needs a POD.
179 my ( $date, $date2 ) = @_;
180 my $dbh = C4::Context->dbh;
182 #takes date converts to timestamps
183 my $padding = "000000";
184 ( my $a, my $b, my $c ) = unpack( "A4 x1 A2 x1 A2", $date );
185 ( my $x, my $y, my $z ) = unpack( "A4 x1 A2 x1 A2", $date2 );
186 my $timestamp = $a . $b . $c . $padding;
187 my $timestamp2 = $x . $y . $z . $padding;
189 my $sth = $dbh->prepare(
190 "Select * from accountlines,borrowers where (((accounttype = 'LR') or (accounttype <> 'Pay'))
191 and amount < 0 and accountlines.borrowernumber = borrowers.borrowernumber
192 and timestamp >=? and timestamp <?)"
194 $sth->execute( $timestamp, $timestamp2 );
198 while ( my $data = $sth->fetchrow_hashref ) {
199 $results[$i] = $data;
206 my ( $date, $date2 ) = @_;
207 my $dbh = C4::Context->dbh;
209 #takes date converts to timestamps
210 my $padding = "000000";
211 ( my $a, my $b, my $c ) = unpack( "A4 x1 A2 x1 A2", $date );
212 ( my $x, my $y, my $z ) = unpack( "A4 x1 A2 x1 A2", $date2 );
213 my $timestamp = $a . $b . $c . $padding;
214 my $timestamp2 = $x . $y . $z . $padding;
216 my $sth = $dbh->prepare(
217 "Select * from accountlines,borrowers where (accounttype = 'REF'
218 and accountlines.borrowernumber = borrowers.borrowernumber
219 and timestamp >=? and timestamp <?)"
221 $sth->execute( $timestamp, $timestamp2 );
224 while ( my $data = $sth->fetchrow_hashref ) {
225 push @results, $data;
230 # Otherwise, this needs a POD.
232 my ( $date, $borrno ) = @_;
233 my $dbh = C4::Context->dbh;
236 "select * from statistics where type='payment' and datetime >? and borrowernumber=?"
238 $sth->execute( $date, $borrno );
241 my $data = $sth->fetchrow_hashref;
243 return ( $data->{'branch'} );
246 # FIXME - This is only used in reservereport.pl and reservereport.xls,
247 # neither of which is used.
248 # Otherwise, it needs a POD.
249 sub unfilledreserves {
250 my $dbh = C4::Context->dbh;
251 my $sth = $dbh->prepare(
252 "select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where (found <> 'F' or
253 found is NULL) and cancellationdate
254 is NULL and biblio.biblionumber=reserves.biblionumber and
255 reserves.constrainttype='o'
256 and (reserves.biblionumber=reserveconstraints.biblionumber
257 and reserves.borrowernumber=reserveconstraints.borrowernumber)
259 reserves.borrowernumber=borrowers.borrowernumber and
260 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
261 biblio.title,reserves.reservedate"
266 while ( my $data = $sth->fetchrow_hashref ) {
267 $results[$i] = $data;
271 $sth = $dbh->prepare(
272 "select *,biblio.title from reserves,biblio,borrowers where (found <> 'F' or found is NULL) and cancellationdate
273 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
274 reserves.borrowernumber=borrowers.borrowernumber
276 biblio.title,reserves.reservedate"
279 while ( my $data = $sth->fetchrow_hashref ) {
280 $results[$i] = $data;
284 return ( $i, \@results );
294 Koha Developement team <info@koha.org>