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'};
137 $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
138 push @accountlines,$line;
141 #get amount paid and update database
143 &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
145 &recordpayment($env,$bornumber,$dbh,$data);
146 #Check if the borrower still owes
147 $total=&checkaccount($env,$bornumber,$dbh);
153 # FIXME - This function is never used. Then again, it's not exported,
156 #here we update both the accountoffsets and the account lines
157 my ($env,$bornumber,$dbh,$data)=@_;
161 my $amountleft = $data;
163 # my $sth = $dbh->prepare("begin");
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)
170 my $sth = $dbh->prepare($query);
172 # offset transactions
173 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
174 if ($accdata->{'amountoutstanding'} < $amountleft) {
176 $amountleft = $amountleft - $accdata->{'amountoutstanding'};
179 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
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);
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);
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);
209 UpdateStats($env,'branch','payment',$data)
212 # $sth = $dbh->prepare;
219 $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
221 Returns the next unused account number for the patron with the given
224 C<$dbh> is a DBI::db handle to the Koha database.
229 # FIXME - Okay, so what does the above actually _mean_?
231 my ($env,$bornumber,$dbh)=@_;
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);
240 if (my $accdata=$sth->fetchrow_hashref){
241 $nextaccntno = $accdata->{'accountno'} + 1;
244 return($nextaccntno);
247 END { } # module clean-up code here (global destructor)
256 C4::Accounts2(3), DBI(3)