Savannah seems out of sync. reloading.Mainly code cleaning and removing of Date:...
[koha.git] / C4 / Stats.pm
1 package C4::Stats;
2
3 # $Id$
4 # Modified by TG
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
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 &getcredits &getinvoices);
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,$accountno)=@_;
76         my $dbh = C4::Context->dbh;
77         $env=C4::Context->userenv unless $env;
78         if ($branch eq ''){
79                 $branch=$env->{'branchcode'};
80         }
81         my $user = C4::Context->userenv;
82 #        print $borrowernumber;
83         my $userid=$user->{'cardnumber'} if $user;
84         # FIXME - Use $dbh->do() instead
85         my $sth=$dbh->prepare("Insert into statistics (datetime,branch,type,usercode,value,
86                                         other,itemnumber,itemtype,borrowernumber,proccode) values (now(),?,?,?,?,?,?,?,?,?)");
87         $sth->execute($branch,$type,$userid,$amount,$other,$itemnum,$itemtype,$borrowernumber,$accountno);
88         $sth->finish;
89 }
90
91 # Otherwise, it'd need a POD.
92 sub TotalPaid {
93         my ($time,$time2)=@_;
94         $time2=$time unless $time2;
95         my $dbh = C4::Context->dbh;
96
97
98         my $query="Select * from accountlines,borrowers where (accounttype = 'Pay' or accounttype='W')
99                                         and accountlines.borrowernumber = borrowers.borrowernumber";
100         my @bind = ();
101         if ($time eq 'today'){
102                 $query .= " and date = now()";
103         } else {
104                 $query.=" and date>=? and date<=?";
105                 @bind = ($time,$time2);
106         }
107
108       
109
110
111            $query.=" order by timestamp";
112
113           # print $query;
114
115         my $sth=$dbh->prepare($query);
116
117        # $sth->execute();
118          $sth->execute(@bind);
119         my @results;
120         my $i=0;
121         while (my $data=$sth->fetchrow_hashref){
122                 $results[$i]=$data;
123                 $i++;
124         }
125         $sth->finish;
126         #  print $query;
127         return(@results);
128 }
129
130 # Otherwise, it needs a POD.
131 sub getcharges{
132         my($borrowerno,$offset,$accountno)=@_;
133         my $dbh = C4::Context->dbh;
134         my $query="";
135         my $sth;
136
137         # getcharges is now taking accountno. as an argument
138         if ($offset){
139               $sth=$dbh->prepare("Select * from accountlines where borrowernumber=?
140               and accountno = ? and amount>0");
141               $sth->execute($borrowerno,$offset);
142
143         # this bit left in for old 2 arg usage of getcharges
144         } else {
145                  $sth=$dbh->prepare("Select * from accountlines where borrowernumber=?
146               and accountno = ?");
147               $sth->execute($borrowerno,$accountno);
148         }
149
150         #  print $query,"<br>";
151         my $i=0;
152         my @results;
153         while (my $data=$sth->fetchrow_hashref){
154         #    if ($data->{'timestamp'} == $timestamp){
155                 $results[$i]=$data;
156                 $i++;
157         #    }
158         }
159         return(@results);
160 }
161
162 # Otherwise, it needs a POD.
163 sub getcredits{
164         my ($date,$date2)=@_;
165         my $dbh = C4::Context->dbh;
166
167
168
169         my $sth=$dbh->prepare("Select * from accountlines,borrowers where (( (accounttype <> 'Pay'))
170                                    and amount < 0  and accountlines.borrowernumber = borrowers.borrowernumber
171                                    and date >=?  and date <=?)");
172         $sth->execute($date, $date2);
173
174         my $i=0;
175         my @results;
176         while (my $data=$sth->fetchrow_hashref){
177                 $results[$i]=$data;
178                 $i++;
179         }
180         return(@results);
181 }
182
183 sub getinvoices{
184         my ($date,$date2)=@_;
185         my $dbh = C4::Context->dbh;
186         my $sth=$dbh->prepare("Select * from accountlines,borrowers where amount>0 and amountoutstanding > 0  and accountlines.borrowernumber = borrowers.borrowernumber
187                                    and (date >=?  and date <=?)");
188         $sth->execute($date, $date2);
189
190         my $i=0;
191         my @results;
192         while (my $data=$sth->fetchrow_hashref){
193                 $results[$i]=$data;
194                 $i++;
195         }
196         return(@results);
197 }
198
199
200 # Otherwise, this needs a POD.
201 sub Getpaidbranch{
202         my($date,$borrno)=@_;
203         my $dbh = C4::Context->dbh;
204         my $sth=$dbh->prepare("select * from statistics where type='payment' and datetime >? and  borrowernumber=?");
205         $sth->execute($date,$borrno);
206         #  print $query;
207         my $data=$sth->fetchrow_hashref;
208         $sth->finish;
209         return($data->{'branch'});
210 }
211
212 # FIXME - This is only used in reservereport.pl and reservereport.xls,
213 # neither of which is used.
214 # Otherwise, it needs a POD.
215 sub unfilledreserves {
216         my $dbh = C4::Context->dbh;
217       
218         my $i=0;
219         my @results;
220        
221    my     $sth=$dbh->prepare("select *,biblio.title from reserves,biblio,borrowers where (found <> '1' or found is NULL) and cancellationdate
222                 is NULL and biblio.biblionumber=reserves.biblionumber  and
223                 reserves.borrowernumber=borrowers.borrowernumber
224                 order by
225                 reserves.reservedate,biblio.title");
226         $sth->execute;
227         while (my $data=$sth->fetchrow_hashref){
228                 $results[$i]=$data;
229                 $i++;
230         }
231         $sth->finish;
232         return($i,\@results);
233 }
234
235 1;
236 __END__
237
238 =back
239
240 =head1 AUTHOR
241
242 Koha Developement team <info@koha.org>
243
244 =cut
245