Insert the filename of the token into the TmplToken object too
[koha.git] / C4 / Stats.pm
1 package C4::Stats;
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
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
12 # version.
13 #
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.
17 #
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
21
22 use strict;
23 require Exporter;
24 use DBI;
25 use C4::Context;
26 use vars qw($VERSION @ISA @EXPORT);
27
28 # set the version for version checking
29 $VERSION = 0.01;
30
31 =head1 NAME
32
33 C4::Stats - Update Koha statistics (log)
34
35 =head1 SYNOPSIS
36
37   use C4::Stats;
38
39 =head1 DESCRIPTION
40
41 The C<&UpdateStats> function adds an entry to the statistics table in
42 the Koha database, which acts as an activity log.
43
44 =head1 FUNCTIONS
45
46 =over 2
47
48 =cut
49
50 @ISA = qw(Exporter);
51 @EXPORT = qw(&UpdateStats &statsreport &TotalOwing
52 &TotalPaid &getcharges &Getpaidbranch &unfilledreserves);
53
54 =item UpdateStats
55
56   &UpdateStats($env, $branch, $type, $value, $other, $itemnumber,
57                $itemtype, $borrowernumber);
58
59 Adds a line to the statistics table of the Koha database. In effect,
60 it logs an event.
61
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.
65
66 If C<$branch> is the empty string, the branch code will be taken from
67 C<$env-E<gt>{branchcode}>.
68
69 C<$env-E<gt>{usercode}> specifies the value of the C<usercode> field.
70
71 =cut
72 #'
73 sub UpdateStats {
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;
77         if ($branch eq ''){
78                 $branch=$env->{'branchcode'};
79         }
80         my $user = $env->{'usercode'};
81         print $borrowernumber;
82         # FIXME - Use $dbh->do() instead
83         my $sth=$dbh->prepare("Insert into statistics (datetime,branch,type,usercode,value,
84                                         other,itemnumber,itemtype,borrowernumber) values (now(),?,?,?,?,?,?,?,?)");
85         $sth->execute($branch,$type,$user,$amount,$other,$itemnum,$itemtype,$borrowernumber);
86         $sth->finish;
87 }
88
89 # Otherwise, it'd need a POD.
90 sub TotalPaid {
91         my ($time,$time2)=@_;
92         $time2=$time unless $time2;
93         my $dbh = C4::Context->dbh;
94         my $query="Select * from accountlines,borrowers where (accounttype = 'Pay' or accounttype ='W')
95                                         and accountlines.borrowernumber = borrowers.borrowernumber";
96         my @bind = ();
97         if ($time eq 'today'){
98                 $query .= " and date = now()";
99         } else {
100                 $query.=" and date>=? and date<=?";
101                 @bind = ($time,$time2);
102         }
103         #  my $query="Select * from statistics,borrowers
104         #  where statistics.borrowernumber= borrowers.borrowernumber
105         #  and (statistics.type='payment' or statistics.type='writeoff') ";
106         #  if ($time eq 'today'){
107         #    $query=$query." and datetime = now()";
108         #  } else {
109         #    $query.=" and datetime > '$time'";
110         #  }
111         $query.=" order by timestamp";
112         #  print $query;
113         my $sth=$dbh->prepare($query);
114         $sth->execute(@bind);
115         my @results;
116         my $i=0;
117         while (my $data=$sth->fetchrow_hashref){
118                 $results[$i]=$data;
119                 $i++;
120         }
121         $sth->finish;
122         #  print $query;
123         return(@results);
124 }
125
126 # Otherwise, it needs a POD.
127 sub getcharges{
128         my($borrowerno,$timestamp)=@_;
129         my $dbh = C4::Context->dbh;
130         my $timestamp2=$timestamp-1;
131         my $query="";
132         my $sth=$dbh->prepare("Select * from accountlines where borrowernumber=?
133         and timestamp = ? and accounttype <> 'Pay' and
134         accounttype <> 'W'");
135         #  print $query,"<br>";
136         $sth->execute($borrowerno,$timestamp);
137         my $i=0;
138         my @results;
139         while (my $data=$sth->fetchrow_hashref){
140         #    if ($data->{'timestamp'} == $timestamp){
141                 $results[$i]=$data;
142                 $i++;
143         #    }
144         }
145         return(@results);
146 }
147
148 # Otherwise, this needs a POD.
149 sub Getpaidbranch{
150         my($date,$borrno)=@_;
151         my $dbh = C4::Context->dbh;
152         my $sth=$dbh->prepare("select * from statistics where type='payment' and datetime >? and  borrowernumber=?");
153         $sth->execute($date,$borrno);
154         #  print $query;
155         my $data=$sth->fetchrow_hashref;
156         $sth->finish;
157         return($data->{'branch'});
158 }
159
160 # FIXME - This is only used in reservereport.pl and reservereport.xls,
161 # neither of which is used.
162 # Otherwise, it needs a POD.
163 sub unfilledreserves {
164         my $dbh = C4::Context->dbh;
165         my $sth=$dbh->prepare("select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where found <> 'F' and cancellationdate
166                                                                 is NULL and biblio.biblionumber=reserves.biblionumber and
167                                                                 reserves.constrainttype='o'
168                                                                 and (reserves.biblionumber=reserveconstraints.biblionumber
169                                                                 and reserves.borrowernumber=reserveconstraints.borrowernumber)
170                                                                 and
171                                                                 reserves.borrowernumber=borrowers.borrowernumber and
172                                                                 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
173                                                                 biblio.title,reserves.reservedate");
174         $sth->execute;
175         my $i=0;
176         my @results;
177         while (my $data=$sth->fetchrow_hashref){
178                 $results[$i]=$data;
179                 $i++;
180         }
181         $sth->finish;
182         $sth=$dbh->prepare("select *,biblio.title from reserves,biblio,borrowers where found <> 'F' and cancellationdate
183                 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
184                 reserves.borrowernumber=borrowers.borrowernumber
185                 order by
186                 biblio.title,reserves.reservedate");
187         $sth->execute;
188         while (my $data=$sth->fetchrow_hashref){
189                 $results[$i]=$data;
190                 $i++;
191         }
192         $sth->finish;
193         return($i,\@results);
194 }
195
196 1;
197 __END__
198
199 =back
200
201 =head1 AUTHOR
202
203 Koha Developement team <info@koha.org>
204
205 =cut