1 package C4::Accounts2; #assumes C4/Accounts2
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
27 use C4::Circulation::Circ2;
28 use vars qw($VERSION @ISA @EXPORT);
30 # set the version for version checking
31 $VERSION = 0.01; # FIXME - Should probably be different from
32 # the version for C4::Accounts
36 C4::Accounts - Functions for dealing with Koha accounts
44 The functions in this module deal with the monetary aspect of Koha,
45 including looking up and modifying the amount of money owed by a
55 @EXPORT = qw(&recordpayment &fixaccounts &makepayment &manualinvoice
65 &recordpayment($env, $borrowernumber, $payment);
67 Record payment by a patron. C<$borrowernumber> is the patron's
68 borrower number. C<$payment> is a floating-point number, giving the
69 amount that was paid. C<$env> is a reference-to-hash;
70 C<$env-E<gt>{branchcode}> is the code of the branch where payment was
73 Amounts owed are paid off oldest first. That is, if the patron has a
74 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
75 of $1.50, then the oldest fine will be paid off in full, and $0.50
76 will be credited to the next one.
81 #here we update both the accountoffsets and the account lines
82 my ($env,$bornumber,$data)=@_;
83 warn "in accounts2.pm";
84 my $dbh = C4::Context->dbh;
87 my $branch=$env->{'branchcode'};
89 my $amountleft = $data;
91 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
92 # get lines with outstanding amounts to offset
93 my $sth = $dbh->prepare("select * from accountlines
94 where (borrowernumber = ?) and (amountoutstanding<>0)
96 $sth->execute($bornumber);
98 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
99 if ($accdata->{'amountoutstanding'} < $amountleft) {
101 $amountleft -= $accdata->{'amountoutstanding'};
103 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
106 my $thisacct = $accdata->{accountno};
107 my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
108 where (borrowernumber = ?) and (accountno=?)");
109 $usth->execute($newamtos,$bornumber,$thisacct);
111 $usth = $dbh->prepare("insert into accountoffsets
112 (borrowernumber, accountno, offsetaccount, offsetamount)
114 $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
118 my $usth = $dbh->prepare("insert into accountlines
119 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
120 values (?,?,now(),?,'Payment,thanks','Pay',?)");
121 $usth->execute($bornumber,$nextaccntno,0-$data,0-$amountleft);
123 UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
129 &makepayment($borrowernumber, $acctnumber, $amount, $branchcode);
131 Records the fact that a patron has paid off the entire amount he or
134 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
135 the account that was credited. C<$amount> is the amount paid (this is
136 only used to record the payment. It is assumed to be equal to the
137 amount owed). C<$branchcode> is the code of the branch where payment
142 # FIXME - I'm not at all sure about the above, because I don't
143 # understand what the acct* tables in the Koha database are for.
145 #here we update both the accountoffsets and the account lines
146 #updated to check, if they are paying off a lost item, we return the item
147 # from their card, and put a note on the item record
148 my ($bornumber,$accountno,$amount,$user,$branch)=@_;
150 $env{'branchcode'}=$branch;
151 my $dbh = C4::Context->dbh;
153 my $nextaccntno = getnextacctno(\%env,$bornumber,$dbh);
155 my $sth=$dbh->prepare("Select * from accountlines where borrowernumber=? and accountno=?");
156 $sth->execute($bornumber,$accountno);
157 my $data=$sth->fetchrow_hashref;
162 SET amountoutstanding = 0
163 WHERE borrowernumber = $bornumber
164 AND accountno = $accountno
169 INSERT INTO accountoffsets
170 (borrowernumber, accountno, offsetaccount,
172 VALUES ($bornumber, $accountno, $nextaccntno, $newamtos)
176 my $payment=0-$amount;
178 INSERT INTO accountlines
179 (borrowernumber, accountno, date, amount,
180 description, accounttype, amountoutstanding)
181 VALUES ($bornumber, $nextaccntno, now(), $payment,
182 'Payment,thanks - $user', 'Pay', 0)
185 # FIXME - The second argument to &UpdateStats is supposed to be the
187 # UpdateStats is now being passed $accountno too. MTJ
188 UpdateStats(\%env,$user,'payment',$amount,'','','',$bornumber,$accountno);
190 #check to see what accounttype
191 if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
192 returnlost($bornumber,$data->{'itemnumber'});
198 $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
200 Returns the next unused account number for the patron with the given
203 C<$dbh> is a DBI::db handle to the Koha database.
209 # FIXME - Okay, so what does the above actually _mean_?
211 my ($env,$bornumber,$dbh)=@_;
213 my $sth = $dbh->prepare("select * from accountlines
214 where (borrowernumber = ?)
215 order by accountno desc");
216 $sth->execute($bornumber);
217 if (my $accdata=$sth->fetchrow_hashref){
218 $nextaccntno = $accdata->{'accountno'} + 1;
221 return($nextaccntno);
226 &fixaccounts($borrowernumber, $accountnumber, $amount);
230 # FIXME - I don't understand what this function does.
232 my ($borrowernumber,$accountno,$amount)=@_;
233 my $dbh = C4::Context->dbh;
234 my $sth=$dbh->prepare("Select * from accountlines where borrowernumber=?
236 $sth->execute($borrowernumber,$accountno);
237 my $data=$sth->fetchrow_hashref;
238 # FIXME - Error-checking
239 my $diff=$amount-$data->{'amount'};
240 my $outstanding=$data->{'amountoutstanding'}+$diff;
245 SET amount = '$amount',
246 amountoutstanding = '$outstanding'
247 WHERE borrowernumber = $borrowernumber
248 AND accountno = $accountno
252 # FIXME - Never used, but not exported, either.
254 my ($borrnum,$itemnum)=@_;
255 my $dbh = C4::Context->dbh;
256 my $borrower=borrdata('',$borrnum); #from C4::Search;
257 my $sth=$dbh->prepare("Update issues set returndate=now() where
258 borrowernumber=? and itemnumber=? and returndate is null");
259 $sth->execute($borrnum,$itemnum);
261 my @datearr = localtime(time);
262 my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
263 my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
264 $sth=$dbh->prepare("Update items set paidfor=? where itemnumber=?");
265 $sth->execute("Paid for by $bor $date",$itemnum);
271 &manualinvoice($borrowernumber, $itemnumber, $description, $type,
274 C<$borrowernumber> is the patron's borrower number.
275 C<$description> is a description of the transaction.
276 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
278 C<$itemnumber> is the item involved, if pertinent; otherwise, it
279 should be the empty string.
283 # FIXME - Okay, so what does this function do, really?
285 my ($bornum,$itemnum,$desc,$type,$amount,$user)=@_;
286 my $dbh = C4::Context->dbh;
290 my $accountno=getnextacctno('',$bornum,$dbh);
291 my $amountleft=$amount;
293 if ($type eq 'CS' || $type eq 'CB' || $type eq 'CW'
294 || $type eq 'CF' || $type eq 'CL'){
295 my $amount2=$amount*-1; # FIXME - $amount2 = -$amount
296 $amountleft=fixcredit(\%env,$bornum,$amount2,$itemnum,$type,$user);
301 if ($type eq 'L' && $desc eq ''){
305 $amountleft=refund('',$bornum,$amount);
308 #FIXME to use ? before uncommenting
309 # my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
311 # my $data=$sth->fetchrow_hashref;
314 my $sth=$dbh->prepare("INSERT INTO accountlines
315 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber)
316 VALUES (?, ?, now(), ?,?, ?,?,?)");
317 # $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft, $data->{'itemnumber'});
318 $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft, $itemnum);
320 $desc=$dbh->quote($desc);
321 my $sth=$dbh->prepare("INSERT INTO accountlines
322 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding)
323 VALUES (?, ?, now(), ?, ?, ?, ?)");
324 $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft);
329 # $amountleft = &fixcredit($env, $bornumber, $data, $barcode, $type, $user);
331 # This function is only used internally.
332 # FIXME - Figure out what this function does, and write it down.
334 #here we update both the accountoffsets and the account lines
335 my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
336 my $dbh = C4::Context->dbh;
339 my $amountleft = $data;
341 my $item=getiteminformation($env,'',$barcode);
342 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
343 my $query="Select * from accountlines where (borrowernumber=?
344 and itemnumber=? and amountoutstanding > 0)";
346 $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
347 } elsif ($type eq 'CF'){
348 $query.=" and (accounttype = 'F' or accounttype = 'FU' or
349 accounttype='Res' or accounttype='Rent')";
350 } elsif ($type eq 'CB'){
351 $query.=" and accounttype='A'";
354 my $sth=$dbh->prepare($query);
355 $sth->execute($bornumber,$item->{'itemnumber'});
356 $accdata=$sth->fetchrow_hashref;
358 if ($accdata->{'amountoutstanding'} < $amountleft) {
360 $amountleft -= $accdata->{'amountoutstanding'};
362 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
365 my $thisacct = $accdata->{accountno};
366 my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
367 where (borrowernumber = ?) and (accountno=?)");
368 $usth->execute($newamtos,$bornumber,$thisacct);
370 $usth = $dbh->prepare("insert into accountoffsets
371 (borrowernumber, accountno, offsetaccount, offsetamount)
373 $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
377 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
378 # get lines with outstanding amounts to offset
379 my $sth = $dbh->prepare("select * from accountlines
380 where (borrowernumber = ?) and (amountoutstanding >0)
382 $sth->execute($bornumber);
384 # offset transactions
385 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
386 if ($accdata->{'amountoutstanding'} < $amountleft) {
388 $amountleft -= $accdata->{'amountoutstanding'};
390 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
393 my $thisacct = $accdata->{accountno};
394 my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
395 where (borrowernumber = ?) and (accountno=?)");
396 $usth->execute($newamtos,$bornumber,$thisacct);
398 $usth = $dbh->prepare("insert into accountoffsets
399 (borrowernumber, accountno, offsetaccount, offsetamount)
401 $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
405 $env->{'branch'}=$user;
406 $type="Credit ".$type;
407 UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
413 # FIXME - Figure out what this function does, and write it down.
415 #here we update both the accountoffsets and the account lines
416 my ($env,$bornumber,$data)=@_;
417 my $dbh = C4::Context->dbh;
420 # my $branch=$env->{'branchcode'};
421 my $amountleft = $data *-1;
424 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
425 # get lines with outstanding amounts to offset
426 my $sth = $dbh->prepare("select * from accountlines
427 where (borrowernumber = ?) and (amountoutstanding<0)
429 $sth->execute($bornumber);
431 # offset transactions
432 while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
433 if ($accdata->{'amountoutstanding'} > $amountleft) {
435 $amountleft -= $accdata->{'amountoutstanding'};
437 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
441 my $thisacct = $accdata->{accountno};
442 my $usth = $dbh->prepare("update accountlines set amountoutstanding= ?
443 where (borrowernumber = ?) and (accountno=?)");
444 $usth->execute($newamtos,$bornumber,$thisacct);
446 $usth = $dbh->prepare("insert into accountoffsets
447 (borrowernumber, accountno, offsetaccount, offsetamount)
449 $usth->execute($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos);
456 END { } # module clean-up code here (global destructor)