Stores an associated borrowernumber now with issue data, if the system pref is set.
[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 = $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
30     shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };;
31
32 =head1 NAME
33
34 C4::Stats - Update Koha statistics (log)
35
36 =head1 SYNOPSIS
37
38   use C4::Stats;
39
40 =head1 DESCRIPTION
41
42 The C<&UpdateStats> function adds an entry to the statistics table in
43 the Koha database, which acts as an activity log.
44
45 =head1 FUNCTIONS
46
47 =over 2
48
49 =cut
50
51 @ISA = qw(Exporter);
52 @EXPORT = qw(&UpdateStats &statsreport &TotalOwing
53 &TotalPaid &getcharges &Getpaidbranch &unfilledreserves &getcredits);
54
55 =item UpdateStats
56
57   &UpdateStats($env, $branch, $type, $value, $other, $itemnumber,
58                $itemtype, $borrowernumber);
59
60 Adds a line to the statistics table of the Koha database. In effect,
61 it logs an event.
62
63 C<$branch>, C<$type>, C<$value>, C<$other>, C<$itemnumber>,
64 C<$itemtype>, and C<$borrowernumber> correspond to the fields of the
65 statistics table in the Koha database.
66
67 If C<$branch> is the empty string, the branch code will be taken from
68 C<$env-E<gt>{branchcode}>.
69
70 C<$env-E<gt>{usercode}> specifies the value of the C<usercode> field.
71
72 =cut
73 #'
74 sub UpdateStats {
75         #module to insert stats data into stats table
76         my ($env,$branch,$type,$amount,$other,$itemnum,$itemtype,$borrowernumber,$accountno)=@_;
77         my $dbh = C4::Context->dbh;
78         if ($branch eq ''){
79                 $branch=$env->{'branchcode'};
80         }
81         my $user = $env->{'usercode'};
82         my $organisation = $env->{'organisation'};
83         print $borrowernumber;
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,associatedborrower) values (now(),?,?,?,?,?,?,?,?,?,?)");
87         $sth->execute($branch,$type,$user,$amount,$other,$itemnum,$itemtype,$borrowernumber,$accountno,$organisation);
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           my $query="Select * from statistics,borrowers
109           where statistics.borrowernumber= borrowers.borrowernumber
110           and (statistics.type='payment' or statistics.type='writeoff') ";
111           if ($time eq 'today'){
112             $query=$query." and datetime = now()";
113           } else {
114             $query.=" and datetime > '$time'";
115           }
116
117
118           # $query.=" order by timestamp";
119
120           # print $query;
121
122         my $sth=$dbh->prepare($query);
123
124         $sth->execute();
125         # $sth->execute(@bind);
126         my @results;
127         my $i=0;
128         while (my $data=$sth->fetchrow_hashref){
129                 $results[$i]=$data;
130                 $i++;
131         }
132         $sth->finish;
133         #  print $query;
134         return(@results);
135 }
136
137 # Otherwise, it needs a POD.
138 sub getcharges{
139         my($borrowerno,$timestamp,$accountno)=@_;
140         my $dbh = C4::Context->dbh;
141         my $timestamp2=$timestamp-1;
142         my $query="";
143         my $sth;
144
145         # getcharges is now taking accountno. as an argument
146         if ($accountno){
147               $sth=$dbh->prepare("Select * from accountlines where borrowernumber=?
148               and accountno = ?");
149               $sth->execute($borrowerno,$accountno);
150
151         # this bit left in for old 2 arg usage of getcharges
152         } else {
153               $sth=$dbh->prepare("Select * from accountlines where borrowernumber=?
154               and timestamp = ? and accounttype <> 'Pay' and
155               accounttype <> 'W'");
156               $sth->execute($borrowerno,$timestamp);
157         }
158
159         #  print $query,"<br>";
160         my $i=0;
161         my @results;
162         while (my $data=$sth->fetchrow_hashref){
163         #    if ($data->{'timestamp'} == $timestamp){
164                 $results[$i]=$data;
165                 $i++;
166         #    }
167         }
168         return(@results);
169 }
170
171 # Otherwise, it needs a POD.
172 sub getcredits{
173         my ($date,$date2)=@_;
174         my $dbh = C4::Context->dbh;
175
176         #takes date converts to timestamps
177         my $padding="000000";
178         (my $a, my $b, my $c) =  unpack("A4 x1 A2 x1 A2", $date);
179         (my $x, my $y, my $z) =  unpack("A4 x1 A2 x1 A2", $date2);
180         my $timestamp = $a.$b.$c.$padding;
181         my $timestamp2 = $x.$y.$z.$padding;
182
183         my $sth=$dbh->prepare("Select * from accountlines,borrowers where (((accounttype = 'LR')  or (accounttype <> 'Pay'))
184                                    and amount < 0  and accountlines.borrowernumber = borrowers.borrowernumber
185                                    and timestamp >=?  and timestamp <?)");
186         $sth->execute($timestamp, $timestamp2);
187
188         my $i=0;
189         my @results;
190         while (my $data=$sth->fetchrow_hashref){
191                 $results[$i]=$data;
192                 $i++;
193         }
194         return(@results);
195 }
196
197
198
199 # Otherwise, this needs a POD.
200 sub Getpaidbranch{
201         my($date,$borrno)=@_;
202         my $dbh = C4::Context->dbh;
203         my $sth=$dbh->prepare("select * from statistics where type='payment' and datetime >? and  borrowernumber=?");
204         $sth->execute($date,$borrno);
205         #  print $query;
206         my $data=$sth->fetchrow_hashref;
207         $sth->finish;
208         return($data->{'branch'});
209 }
210
211 # FIXME - This is only used in reservereport.pl and reservereport.xls,
212 # neither of which is used.
213 # Otherwise, it needs a POD.
214 sub unfilledreserves {
215         my $dbh = C4::Context->dbh;
216         my $sth=$dbh->prepare("select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where (found <> 'F' or
217             found is NULL) and cancellationdate
218                                                                 is NULL and biblio.biblionumber=reserves.biblionumber and
219                                                                 reserves.constrainttype='o'
220                                                                 and (reserves.biblionumber=reserveconstraints.biblionumber
221                                                                 and reserves.borrowernumber=reserveconstraints.borrowernumber)
222                                                                 and
223                                                                 reserves.borrowernumber=borrowers.borrowernumber and
224                                                                 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
225                                                                 biblio.title,reserves.reservedate");
226         $sth->execute;
227         my $i=0;
228         my @results;
229         while (my $data=$sth->fetchrow_hashref){
230                 $results[$i]=$data;
231                 $i++;
232         }
233         $sth->finish;
234         $sth=$dbh->prepare("select *,biblio.title from reserves,biblio,borrowers where (found <> 'F' or found is NULL) and cancellationdate
235                 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
236                 reserves.borrowernumber=borrowers.borrowernumber
237                 order by
238                 biblio.title,reserves.reservedate");
239         $sth->execute;
240         while (my $data=$sth->fetchrow_hashref){
241                 $results[$i]=$data;
242                 $i++;
243         }
244         $sth->finish;
245         return($i,\@results);
246 }
247
248 1;
249 __END__
250
251 =back
252
253 =head1 AUTHOR
254
255 Koha Developement team <info@koha.org>
256
257 =cut
258