1 package C4::Accounts; #assumes C4/Accounts
3 # 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
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);
64 # FIXME - This is never used
71 $owed = &checkaccount($env, $borrowernumber, $dbh);
73 Looks up the total amount of money owed by a borrower (fines, etc.).
75 C<$borrowernumber> specifies the borrower to look up.
77 C<$dbh> is a DBI::db handle for the Koha database.
85 #check accounts and list amounts owing
86 my ($env,$bornumber,$dbh)=@_;
87 my $sth=$dbh->prepare("Select sum(amountoutstanding) from accountlines where
88 borrowernumber=$bornumber and amountoutstanding<>0");
91 while (my $data=$sth->fetchrow_hashref){
92 $total += $data->{'sum(amountoutstanding)'};
95 # output(1,2,"borrower owes $total");
97 # # output(1,2,"borrower owes $total");
99 # reconcileaccount($env,$dbh,$bornumber,$total);
106 # XXX - POD. Need to figure out C4/Interface/AccountsCDK.pm first,
108 # FIXME - It looks as though this function really wants to be part of
109 # a curses-based script.
110 sub reconcileaccount {
111 #print put money owing give person opportunity to pay it off
112 my ($env,$dummy,$bornumber,$total)=@_;
113 my $dbh = C4::Context->dbh;
115 my $sth=$dbh->prepare("select * from borrowers
116 where borrowernumber=$bornumber");
118 my $borrower=$sth->fetchrow_hashref;
120 #get borrower information
121 $sth=$dbh->prepare("Select * from accountlines where
122 borrowernumber=$bornumber and amountoutstanding<>0 order by date");
124 #display account information
126 #&helptext('F11 quits');
127 output(20,0,"Accounts");
132 #output (1,2,"Account Info");
133 #output (1,3,"Item\tDate \tAmount\tDescription");
134 while (my $data=$sth->fetchrow_hashref){
136 my $amount=0+$data->{'amountoutstanding'};
137 my $itemdata = itemnodata($env,$dbh,$data->{'itemnumber'});
138 $line= $data->{'accountno'}." ".$data->{'date'}." ".$data->{'accounttype'}." ";
139 my $title = $itemdata->{'title'};
140 if (length($title) > 15 ) {$title = substr($title,0,15);}
141 $line .= $itemdata->{'barcode'}." $title ".$data->{'description'};
142 $line = fmtstr($env,$line,"L65")." ".fmtdec($env,$amount,"52");
143 push @accountlines,$line;
146 #get amount paid and update database
148 &accountsdialog($env,"Payment Entry",$borrower,\@accountlines,$total);
150 &recordpayment($env,$bornumber,$dbh,$data);
151 #Check if the borrower still owes
152 $total=&checkaccount($env,$bornumber,$dbh);
158 # FIXME - This function is never used. Then again, it's not exported,
161 #here we update both the accountoffsets and the account lines
162 my ($env,$bornumber,$dbh,$data)=@_;
166 my $amountleft = $data;
168 # my $sth = $dbh->prepare("begin");
170 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
171 # get lines with outstanding amounts to offset
172 my $query = "select * from accountlines
173 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
175 my $sth = $dbh->prepare($query);
177 # offset transactions
178 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
179 if ($accdata->{'amountoutstanding'} < $amountleft) {
181 $amountleft -= $accdata->{'amountoutstanding'};
183 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
186 my $thisacct = $accdata->{accountno};
187 $updquery = "update accountlines set amountoutstanding= '$newamtos'
188 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
189 my $usth = $dbh->prepare($updquery);
192 $updquery = "insert into accountoffsets
193 (borrowernumber, accountno, offsetaccount, offsetamount)
194 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
195 # FIXME - There's already a $usth in this scope.
196 my $usth = $dbh->prepare($updquery);
202 #$updquery = "insert into accountlines (borrowernumber,
203 #accountno,date,amount,description,accounttype,amountoutstanding) values
204 #($bornumber,$nextaccntno,datetime('now'::abstime),0-$data,'Payment,thanks',
205 #'Pay',0-$amountleft)";
206 $updquery = "insert into accountlines
207 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
208 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
209 'Pay',0-$amountleft)";
210 my $usth = $dbh->prepare($updquery);
213 UpdateStats($env,'branch','payment',$data)
216 # $sth = $dbh->prepare;
223 $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
225 Returns the next unused account number for the patron with the given
228 C<$dbh> is a DBI::db handle to the Koha database.
233 # FIXME - Okay, so what does the above actually _mean_?
235 my ($env,$bornumber,$dbh)=@_;
237 # FIXME - This could just be
238 # SELECT max(accountno)+1 from accountlines;
239 my $query = "select * from accountlines
240 where (borrowernumber = '$bornumber')
241 order by accountno desc";
242 my $sth = $dbh->prepare($query);
244 if (my $accdata=$sth->fetchrow_hashref){
245 $nextaccntno = $accdata->{'accountno'} + 1;
248 return($nextaccntno);
251 END { } # module clean-up code here (global destructor)
260 C4::Accounts2(3), DBI(3)