1 package C4::Accounts; #assumes C4/Accounts
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
29 use C4::Interface::AccountsCDK;
30 use vars qw($VERSION @ISA @EXPORT);
32 # set the version for version checking
56 @EXPORT = qw(&checkaccount &reconcileaccount &getnextacctno);
57 # FIXME - This is never used
64 $owed = &checkaccount($env, $borrowernumber, $dbh);
66 Looks up the total amount of money owed by a borrower (fines, etc.).
68 C<$borrowernumber> specifies the borrower to look up.
70 C<$dbh> is a DBI::db handle for the Koha database.
78 #check accounts and list amounts owing
79 my ($env,$bornumber,$dbh)=@_;
80 my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
81 borrowernumber=$bornumber and amountoutstanding<>0");
84 while (my $data=$sth->fetchrow_hashref){
85 $total += $data->{'sum(amountoutstanding)'};
88 # output(1,2,"borrower owes $total");
90 # # output(1,2,"borrower owes $total");
92 # reconcileaccount($env,$dbh,$bornumber,$total);
99 # XXX - POD. Need to figure out C4/Interface/AccountsCDK.pm first,
101 # FIXME - It looks as though this function really wants to be part of
102 # a curses-based script.
103 sub reconcileaccount {
104 #print put money owing give person opportunity to pay it off
105 my ($env,$dummy,$bornumber,$total)=@_;
106 my $dbh = &C4Connect;
108 my $sth=$dbh->prepare("select * from borrowers
109 where borrowernumber=$bornumber");
111 my $borrower=$sth->fetchrow_hashref;
113 #get borrower information
114 $sth=$dbh->prepare("Select * from accountlines where
115 borrowernumber=$bornumber and amountoutstanding<>0 order by date");
117 #display account information
119 #&helptext('F11 quits');
120 output(20,0,"Accounts");
125 #output (1,2,"Account Info");
126 #output (1,3,"Item\tDate \tAmount\tDescription");
127 while (my $data=$sth->fetchrow_hashref){
129 my $amount=0+$data->{'amountoutstanding'};
130 my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
131 $line= $data->{'accountno'}." ".$data->{'date'}." ".$data->{'accounttype'}." ";
132 my $title = $itemdata->{'title'};
133 if (length($title) > 15 ) {$title = substr($title,0,15);}
134 $line= $line.$itemdata->{'barcode'}." $title ".$data->{'description'};
135 $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
136 push @accountlines,$line;
139 #get amount paid and update database
141 &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
143 &recordpayment($env,$bornumber,$dbh,$data);
144 #Check if the borrower still owes
145 $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 my $usth = $dbh->prepare($updquery);
195 #$updquery = "insert into accountlines (borrowernumber,
196 #accountno,date,amount,description,accounttype,amountoutstanding) values
197 #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
198 #'Pay',0-$amountleft)";
199 $updquery = "insert into accountlines
200 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
201 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
202 'Pay',0-$amountleft)";
203 my $usth = $dbh->prepare($updquery);
206 UpdateStats($env,'branch','payment',$data)
209 # $sth = $dbh->prepare;
216 $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
218 Returns the next unused account number for the patron with the given
221 C<$dbh> is a DBI::db handle to the Koha database.
226 # FIXME - Okay, so what does the above actually _mean_?
228 my ($env,$bornumber,$dbh)=@_;
230 # FIXME - This could just be
231 # SELECT max(accountno)+1 from accountlines;
232 my $query = "select * from accountlines
233 where (borrowernumber = '$bornumber')
234 order by accountno desc";
235 my $sth = $dbh->prepare($query);
237 if (my $accdata=$sth->fetchrow_hashref){
238 $nextaccntno = $accdata->{'accountno'} + 1;
241 return($nextaccntno);
244 END { } # module clean-up code here (global destructor)