1 package C4::Accounts; #assumes C4/Accounts
3 # FIXME: This module uses the CDK modules, and crashes if called from a web script
4 # Hence the existence of Accounts2
6 # This module will be deprecated when we build a new curses/slang/character
11 # Copyright 2000-2002 Katipo Communications
13 # This file is part of Koha.
15 # Koha is free software; you can redistribute it and/or modify it under the
16 # terms of the GNU General Public License as published by the Free Software
17 # Foundation; either version 2 of the License, or (at your option) any later
20 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
21 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License along with
25 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
26 # Suite 330, Boston, MA 02111-1307 USA
35 #use C4::InterfaceCDK;
36 #use C4::Interface::AccountsCDK;
37 use vars qw($VERSION @ISA @EXPORT);
39 # set the version for version checking
44 C4::Accounts - Functions for dealing with Koha accounts
52 The functions in this module deal with the monetary aspect of Koha,
53 including looking up and modifying the amount of money owed by a
63 @EXPORT = qw(&checkaccount &reconcileaccount &getnextacctno);
67 $owed = &checkaccount($env, $borrowernumber, $dbh, $date);
69 Looks up the total amount of money owed by a borrower (fines, etc.).
71 C<$borrowernumber> specifies the borrower to look up.
73 C<$dbh> is a DBI::db handle for the Koha database.
81 #check accounts and list amounts owing
82 my ($env,$bornumber,$dbh,$date)=@_;
83 my $select="SELECT SUM(amountoutstanding) AS total
85 WHERE borrowernumber = ?
86 AND amountoutstanding<>0";
87 my @bind = ($bornumber);
89 $select.=" AND date < ?";
93 my $sth=$dbh->prepare($select);
95 my $data=$sth->fetchrow_hashref;
96 my $total = $data->{'total'};
98 # output(1,2,"borrower owes $total");
100 # # output(1,2,"borrower owes $total");
102 # reconcileaccount($env,$dbh,$bornumber,$total);
109 # XXX - POD. Need to figure out C4/Interface/AccountsCDK.pm first,
111 # FIXME - It looks as though this function really wants to be part of
112 # a curses-based script.
113 sub reconcileaccount {
114 #print put money owing give person opportunity to pay it off
115 my ($env,$dummy,$bornumber,$total)=@_;
116 my $dbh = C4::Context->dbh;
118 my $sth=$dbh->prepare("select * from borrowers
119 where borrowernumber=$bornumber");
121 my $borrower=$sth->fetchrow_hashref;
123 #get borrower information
124 $sth=$dbh->prepare("Select * from accountlines where
125 borrowernumber=$bornumber and amountoutstanding<>0 order by date");
127 #display account information
129 #&helptext('F11 quits');
130 output(20,0,"Accounts");
135 #output (1,2,"Account Info");
136 #output (1,3,"Item\tDate \tAmount\tDescription");
137 while (my $data=$sth->fetchrow_hashref){
139 my $amount=0+$data->{'amountoutstanding'};
140 my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
141 $line= $data->{'accountno'}." ".$data->{'date'}." ".$data->{'accounttype'}." ";
142 my $title = $itemdata->{'title'};
143 if (length($title) > 15 ) {$title = substr($title,0,15);}
144 $line .= $itemdata->{'barcode'}." $title ".$data->{'description'};
145 $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
146 push @accountlines,$line;
149 #get amount paid and update database
151 &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
153 &recordpayment($env,$bornumber,$dbh,$data);
154 #Check if the borrower still owes
155 $total=&checkaccount($env,$bornumber,$dbh);
161 # FIXME - This function is never used. Then again, it's not exported,
164 #here we update both the accountoffsets and the account lines
165 my ($env,$bornumber,$dbh,$data)=@_;
168 my $amountleft = $data;
170 # my $sth = $dbh->prepare("begin");
172 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
173 # get lines with outstanding amounts to offset
174 my $sth = $dbh->prepare("select * from accountlines
175 where (borrowernumber = ?) and (amountoutstanding<>0)
177 $sth->execute($bornumber);
178 # offset transactions
179 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
180 if ($accdata->{'amountoutstanding'} < $amountleft) {
182 $amountleft -= $accdata->{'amountoutstanding'};
184 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
187 my $thisacct = $accdata->{accountno};
188 my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
189 where (borrowernumber = ?) and (accountno=?)");
190 $usth->execute($newamtos,$bornumber,$thisacct);
193 $usth = $dbh->prepare("insert into accountoffsets
194 (borrowernumber, accountno, offsetaccount, offsetamount)
197 $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
201 #$updquery = "insert into accountlines (borrowernumber,
202 #accountno,date,amount,description,accounttype,amountoutstanding) values
203 #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
204 #'Pay',0-$amountleft)";
205 my $usth = $dbh->prepare("insert into accountlines
206 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
207 values (?,?,now(),?,?,'Payment,thanks','Pay')");
208 $usth->execute($bornumber,$nextaccntno,0-$data,0-$amountleft);
210 UpdateStats($env,'branch','payment',$data)
215 $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
217 Returns the next unused account number for the patron with the given
220 C<$dbh> is a DBI::db handle to the Koha database.
225 # FIXME - Okay, so what does the above actually _mean_?
227 my ($env,$bornumber,$dbh)=@_;
230 my $sth = $dbh->prepare("select max(accountno)+1 from accountlines");
232 if (my $accdata=$sth->fetchrow_hashref){
233 $nextaccntno = $accdata->{'accountno'} + 1;
239 END { } # module clean-up code here (global destructor)
248 C4::Accounts2(3), DBI(3)