Added some FIXME comments.
[koha.git] / C4 / Accounts.pm
1 package C4::Accounts; #assumes C4/Accounts
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 C4::Format;
27 use C4::Search;
28 use C4::Stats;
29 use C4::InterfaceCDK;
30 use C4::Interface::AccountsCDK;
31 use vars qw($VERSION @ISA @EXPORT);
32
33 # set the version for version checking
34 $VERSION = 0.01;
35
36 =head1 NAME
37
38 C4::Accounts - Functions for dealing with Koha accounts
39
40 =head1 SYNOPSIS
41
42   use C4::Accounts;
43
44 =head1 DESCRIPTION
45
46 The functions in this module deal with the monetary aspect of Koha,
47 including looking up and modifying the amount of money owed by a
48 patron.
49
50 =head1 FUNCTIONS
51
52 =over 2
53
54 =cut
55
56 @ISA = qw(Exporter);
57 @EXPORT = qw(&checkaccount &reconcileaccount &getnextacctno);
58 # FIXME - This is never used
59 sub displayaccounts{
60   my ($env)=@_;
61 }
62
63 =item checkaccount
64
65   $owed = &checkaccount($env, $borrowernumber, $dbh);
66
67 Looks up the total amount of money owed by a borrower (fines, etc.).
68
69 C<$borrowernumber> specifies the borrower to look up.
70
71 C<$dbh> is a DBI::db handle for the Koha database.
72
73 C<$env> is ignored.
74
75 =cut
76 #'
77 sub checkaccount  {
78   #take borrower number
79   #check accounts and list amounts owing
80   my ($env,$bornumber,$dbh)=@_;
81   my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
82   borrowernumber=$bornumber and amountoutstanding<>0");
83   $sth->execute;
84   my $total=0;
85   while (my $data=$sth->fetchrow_hashref){
86     $total += $data->{'sum(amountoutstanding)'};
87   }
88   $sth->finish;
89   # output(1,2,"borrower owes $total");
90   #if ($total > 0){
91   #  # output(1,2,"borrower owes $total");
92   #  if ($total > 5){
93   #    reconcileaccount($env,$dbh,$bornumber,$total);
94   #  }
95   #}
96   #  pause();
97   return($total);
98 }
99
100 # XXX - POD. Need to figure out C4/Interface/AccountsCDK.pm first,
101 # though
102 # FIXME - It looks as though this function really wants to be part of
103 # a curses-based script.
104 sub reconcileaccount {
105   #print put money owing give person opportunity to pay it off
106   my ($env,$dummy,$bornumber,$total)=@_;
107   my $dbh = C4::Context->dbh;
108   #get borrower record
109   my $sth=$dbh->prepare("select * from borrowers
110     where borrowernumber=$bornumber");
111   $sth->execute;
112   my $borrower=$sth->fetchrow_hashref;
113   $sth->finish();
114   #get borrower information
115   $sth=$dbh->prepare("Select * from accountlines where
116   borrowernumber=$bornumber and amountoutstanding<>0 order by date");
117   $sth->execute;
118   #display account information
119   &clearscreen();
120   #&helptext('F11 quits');
121   output(20,0,"Accounts");
122   my @accountlines;
123   my $row=4;
124   my $i=0;
125   my $text;
126   #output (1,2,"Account Info");
127   #output (1,3,"Item\tDate      \tAmount\tDescription");
128   while (my $data=$sth->fetchrow_hashref){
129     my $line=$i+1;
130     my $amount=0+$data->{'amountoutstanding'};
131     my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
132     $line= $data->{'accountno'}." ".$data->{'date'}." ".$data->{'accounttype'}." ";
133     my $title = $itemdata->{'title'};
134     if (length($title) > 15 ) {$title = substr($title,0,15);}
135     $line= $line.$itemdata->{'barcode'}." $title ".$data->{'description'};
136                                 # FIXME - .=
137     $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
138     push @accountlines,$line;
139     $i++;
140   }
141   #get amount paid and update database
142   my ($data,$reason)=
143     &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
144   if ($data>0) {
145     &recordpayment($env,$bornumber,$dbh,$data);
146     #Check if the borrower still owes
147     $total=&checkaccount($env,$bornumber,$dbh);
148   }
149   return($total);
150
151 }
152
153 # FIXME - This function is never used. Then again, it's not exported,
154 # either.
155 sub recordpayment{
156   #here we update both the accountoffsets and the account lines
157   my ($env,$bornumber,$dbh,$data)=@_;
158   my $updquery = "";
159   my $newamtos = 0;
160   my $accdata = "";
161   my $amountleft = $data;
162   # begin transaction
163 #  my $sth = $dbh->prepare("begin");
164 #  $sth->execute;
165   my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
166   # get lines with outstanding amounts to offset
167   my $query = "select * from accountlines
168   where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
169   order by date";
170   my $sth = $dbh->prepare($query);
171   $sth->execute;
172   # offset transactions
173   while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
174      if ($accdata->{'amountoutstanding'} < $amountleft) {
175         $newamtos = 0;
176         $amountleft = $amountleft - $accdata->{'amountoutstanding'};
177                                 # FIXME - -=
178      }  else {
179         $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
180         $amountleft = 0;
181      }
182      my $thisacct = $accdata->{accountno};
183      $updquery = "update accountlines set amountoutstanding= '$newamtos'
184      where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
185      my $usth = $dbh->prepare($updquery);
186      $usth->execute;
187      $usth->finish;
188      $updquery = "insert into accountoffsets
189      (borrowernumber, accountno, offsetaccount,  offsetamount)
190      values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
191      # FIXME - There's already a $usth in this scope.
192      my $usth = $dbh->prepare($updquery);
193 #     print $updquery
194      $usth->execute;
195      $usth->finish;
196   }
197   # create new line
198   #$updquery = "insert into accountlines (borrowernumber,
199   #accountno,date,amount,description,accounttype,amountoutstanding) values
200   #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
201   #'Pay',0-$amountleft)";
202   $updquery = "insert into accountlines
203   (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
204   values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
205   'Pay',0-$amountleft)";
206   my $usth = $dbh->prepare($updquery);
207   $usth->execute;
208   $usth->finish;
209   UpdateStats($env,'branch','payment',$data)
210 #  $sth->finish;
211 #  $query = "commit";
212 #  $sth = $dbh->prepare;
213 #  $sth->execute;
214 #  $sth-finish;
215 }
216
217 =item getnextacctno
218
219   $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
220
221 Returns the next unused account number for the patron with the given
222 borrower number.
223
224 C<$dbh> is a DBI::db handle to the Koha database.
225
226 C<$env> is ignored.
227
228 =cut
229 # FIXME - Okay, so what does the above actually _mean_?
230 sub getnextacctno {
231   my ($env,$bornumber,$dbh)=@_;
232   my $nextaccntno = 1;
233   # FIXME - This could just be
234   #     SELECT max(accountno)+1 from accountlines;
235   my $query = "select * from accountlines
236   where (borrowernumber = '$bornumber')
237   order by accountno desc";
238   my $sth = $dbh->prepare($query);
239   $sth->execute;
240   if (my $accdata=$sth->fetchrow_hashref){
241     $nextaccntno = $accdata->{'accountno'} + 1;
242   }
243   $sth->finish;
244   return($nextaccntno);
245 }
246
247 END { }       # module clean-up code here (global destructor)
248
249 1;
250 __END__
251
252 =back
253
254 =head1 SEE ALSO
255
256 C4::Accounts2(3), DBI(3)
257
258 =cut