HTL mod for till reconciliation.
[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 &getcredits);
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         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,proccode) values (now(),?,?,?,?,?,?,?,?,?)");
85         $sth->execute($branch,$type,$user,$amount,$other,$itemnum,$itemtype,$borrowernumber,$accountno);
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
95
96         #my $query="Select * from accountlines,borrowers where (accounttype = 'Pay' or accounttype ='W')
97         #                                and accountlines.borrowernumber = borrowers.borrowernumber";
98         #my @bind = ();
99         #if ($time eq 'today'){
100         #        $query .= " and date = now()";
101         #} else {
102         #        $query.=" and date>=? and date<=?";
103         #        @bind = ($time,$time2);
104         #}
105
106           my $query="Select * from statistics,borrowers
107           where statistics.borrowernumber= borrowers.borrowernumber
108           and (statistics.type='payment' or statistics.type='writeoff') ";
109           if ($time eq 'today'){
110             $query=$query." and datetime = now()";
111           } else {
112             $query.=" and datetime > '$time'";
113           }
114
115
116           # $query.=" order by timestamp";
117
118           # print $query;
119
120         my $sth=$dbh->prepare($query);
121
122         $sth->execute();
123         # $sth->execute(@bind);
124         my @results;
125         my $i=0;
126         while (my $data=$sth->fetchrow_hashref){
127                 $results[$i]=$data;
128                 $i++;
129         }
130         $sth->finish;
131         #  print $query;
132         return(@results);
133 }
134
135 # Otherwise, it needs a POD.
136 sub getcharges{
137         my($borrowerno,$timestamp,$accountno)=@_;
138         my $dbh = C4::Context->dbh;
139         my $timestamp2=$timestamp-1;
140         my $query="";
141         my $sth;
142
143         # getcharges is now taking accountno. as an argument
144         if ($accountno){
145               $sth=$dbh->prepare("Select * from accountlines where borrowernumber=?
146               and accountno = ?");
147               $sth->execute($borrowerno,$accountno);
148
149         # this bit left in for old 2 arg usage of getcharges
150         } else {
151               $sth=$dbh->prepare("Select * from accountlines where borrowernumber=?
152               and timestamp = ? and accounttype <> 'Pay' and
153               accounttype <> 'W'");
154               $sth->execute($borrowerno,$timestamp);
155         }
156
157         #  print $query,"<br>";
158         my $i=0;
159         my @results;
160         while (my $data=$sth->fetchrow_hashref){
161         #    if ($data->{'timestamp'} == $timestamp){
162                 $results[$i]=$data;
163                 $i++;
164         #    }
165         }
166         return(@results);
167 }
168
169 # Otherwise, it needs a POD.
170 sub getcredits{
171         my ($date,$date2)=@_;
172         my $dbh = C4::Context->dbh;
173
174         #takes date converts to timestamps
175         my $padding="000000";
176         (my $a, my $b, my $c) =  unpack("A4 x1 A2 x1 A2", $date);
177         (my $x, my $y, my $z) =  unpack("A4 x1 A2 x1 A2", $date2);
178         my $timestamp = $a.$b.$c.$padding;
179         my $timestamp2 = $x.$y.$z.$padding;
180
181         my $sth;
182
183         $sth=$dbh->prepare("Select * from accountlines where (accounttype =  'CR' or accounttype = 'LR') and timestamp >=? and timestamp <?");
184         $sth->execute($timestamp,$timestamp2);
185
186         my $i=0;
187         my @results;
188         while (my $data=$sth->fetchrow_hashref){
189                 $results[$i]=$data;
190                 $i++;
191         }
192         return(@results);
193 }
194
195
196
197 # Otherwise, this needs a POD.
198 sub Getpaidbranch{
199         my($date,$borrno)=@_;
200         my $dbh = C4::Context->dbh;
201         my $sth=$dbh->prepare("select * from statistics where type='payment' and datetime >? and  borrowernumber=?");
202         $sth->execute($date,$borrno);
203         #  print $query;
204         my $data=$sth->fetchrow_hashref;
205         $sth->finish;
206         return($data->{'branch'});
207 }
208
209 # FIXME - This is only used in reservereport.pl and reservereport.xls,
210 # neither of which is used.
211 # Otherwise, it needs a POD.
212 sub unfilledreserves {
213         my $dbh = C4::Context->dbh;
214         my $sth=$dbh->prepare("select *,biblio.title from reserves,reserveconstraints,biblio,borrowers,biblioitems where found <> 'F' and cancellationdate
215                                                                 is NULL and biblio.biblionumber=reserves.biblionumber and
216                                                                 reserves.constrainttype='o'
217                                                                 and (reserves.biblionumber=reserveconstraints.biblionumber
218                                                                 and reserves.borrowernumber=reserveconstraints.borrowernumber)
219                                                                 and
220                                                                 reserves.borrowernumber=borrowers.borrowernumber and
221                                                                 biblioitems.biblioitemnumber=reserveconstraints.biblioitemnumber order by
222                                                                 biblio.title,reserves.reservedate");
223         $sth->execute;
224         my $i=0;
225         my @results;
226         while (my $data=$sth->fetchrow_hashref){
227                 $results[$i]=$data;
228                 $i++;
229         }
230         $sth->finish;
231         $sth=$dbh->prepare("select *,biblio.title from reserves,biblio,borrowers where found <> 'F' and cancellationdate
232                 is NULL and biblio.biblionumber=reserves.biblionumber and reserves.constrainttype='a' and
233                 reserves.borrowernumber=borrowers.borrowernumber
234                 order by
235                 biblio.title,reserves.reservedate");
236         $sth->execute;
237         while (my $data=$sth->fetchrow_hashref){
238                 $results[$i]=$data;
239                 $i++;
240         }
241         $sth->finish;
242         return($i,\@results);
243 }
244
245 1;
246 __END__
247
248 =back
249
250 =head1 AUTHOR
251
252 Koha Developement team <info@koha.org>
253
254 =cut
255