1 package C4::Accounts; #assumes C4/Accounts
5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
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
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.
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
30 use C4::Interface::AccountsCDK;
31 use vars qw($VERSION @ISA @EXPORT);
33 # set the version for version checking
38 C4::Accounts - Functions for dealing with Koha accounts
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
57 @EXPORT = qw(&checkaccount &reconcileaccount &getnextacctno);
58 # FIXME - This is never used
65 $owed = &checkaccount($env, $borrowernumber, $dbh);
67 Looks up the total amount of money owed by a borrower (fines, etc.).
69 C<$borrowernumber> specifies the borrower to look up.
71 C<$dbh> is a DBI::db handle for the Koha database.
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");
85 while (my $data=$sth->fetchrow_hashref){
86 $total += $data->{'sum(amountoutstanding)'};
89 # output(1,2,"borrower owes $total");
91 # # output(1,2,"borrower owes $total");
93 # reconcileaccount($env,$dbh,$bornumber,$total);
100 # XXX - POD. Need to figure out C4/Interface/AccountsCDK.pm first,
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;
109 my $sth=$dbh->prepare("select * from borrowers
110 where borrowernumber=$bornumber");
112 my $borrower=$sth->fetchrow_hashref;
114 #get borrower information
115 $sth=$dbh->prepare("Select * from accountlines where
116 borrowernumber=$bornumber and amountoutstanding<>0 order by date");
118 #display account information
120 #&helptext('F11 quits');
121 output(20,0,"Accounts");
126 #output (1,2,"Account Info");
127 #output (1,3,"Item\tDate \tAmount\tDescription");
128 while (my $data=$sth->fetchrow_hashref){
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 $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
137 push @accountlines,$line;
140 #get amount paid and update database
142 &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
144 &recordpayment($env,$bornumber,$dbh,$data);
145 #Check if the borrower still owes
146 $total=&checkaccount($env,$bornumber,$dbh);
152 # FIXME - This function is never used. Then again, it's not exported,
155 #here we update both the accountoffsets and the account lines
156 my ($env,$bornumber,$dbh,$data)=@_;
160 my $amountleft = $data;
162 # my $sth = $dbh->prepare("begin");
164 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
165 # get lines with outstanding amounts to offset
166 my $query = "select * from accountlines
167 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
169 my $sth = $dbh->prepare($query);
171 # offset transactions
172 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
173 if ($accdata->{'amountoutstanding'} < $amountleft) {
175 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
177 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
180 my $thisacct = $accdata->{accountno};
181 $updquery = "update accountlines set amountoutstanding= '$newamtos'
182 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
183 my $usth = $dbh->prepare($updquery);
186 $updquery = "insert into accountoffsets
187 (borrowernumber, accountno, offsetaccount, offsetamount)
188 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
189 # FIXME - There's already a $usth in this scope.
190 my $usth = $dbh->prepare($updquery);
196 #$updquery = "insert into accountlines (borrowernumber,
197 #accountno,date,amount,description,accounttype,amountoutstanding) values
198 #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
199 #'Pay',0-$amountleft)";
200 $updquery = "insert into accountlines
201 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
202 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
203 'Pay',0-$amountleft)";
204 my $usth = $dbh->prepare($updquery);
207 UpdateStats($env,'branch','payment',$data)
210 # $sth = $dbh->prepare;
217 $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
219 Returns the next unused account number for the patron with the given
222 C<$dbh> is a DBI::db handle to the Koha database.
227 # FIXME - Okay, so what does the above actually _mean_?
229 my ($env,$bornumber,$dbh)=@_;
231 # FIXME - This could just be
232 # SELECT max(accountno)+1 from accountlines;
233 my $query = "select * from accountlines
234 where (borrowernumber = '$bornumber')
235 order by accountno desc";
236 my $sth = $dbh->prepare($query);
238 if (my $accdata=$sth->fetchrow_hashref){
239 $nextaccntno = $accdata->{'accountno'} + 1;
242 return($nextaccntno);
245 END { } # module clean-up code here (global destructor)