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 my $dbh = C4::Context->dbh;
87 my $branch=$env->{'branchcode'};
88 my $amountleft = $data;
90 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
91 # get lines with outstanding amounts to offset
92 my $query = "select * from accountlines
93 where (borrowernumber = '$bornumber') and (amountoutstanding<>0)
95 my $sth = $dbh->prepare($query);
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 $updquery = "update accountlines set amountoutstanding= '$newamtos'
108 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
109 my $usth = $dbh->prepare($updquery);
112 $updquery = "insert into accountoffsets
113 (borrowernumber, accountno, offsetaccount, offsetamount)
114 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
115 $usth = $dbh->prepare($updquery);
120 $updquery = "insert into accountlines
121 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
122 values ($bornumber,$nextaccntno,now(),0-$data,'Payment,thanks',
123 'Pay',0-$amountleft)";
124 my $usth = $dbh->prepare($updquery);
127 UpdateStats($env,$branch,'payment',$data,'','','',$bornumber);
133 &makepayment($borrowernumber, $acctnumber, $amount, $branchcode);
135 Records the fact that a patron has paid off the entire amount he or
138 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
139 the account that was credited. C<$amount> is the amount paid (this is
140 only used to record the payment. It is assumed to be equal to the
141 amount owed). C<$branchcode> is the code of the branch where payment
146 # FIXME - I'm not at all sure about the above, because I don't
147 # understand what the acct* tables in the Koha database are for.
149 #here we update both the accountoffsets and the account lines
150 #updated to check, if they are paying off a lost item, we return the item
151 # from their card, and put a note on the item record
152 my ($bornumber,$accountno,$amount,$user)=@_;
154 my $dbh = C4::Context->dbh;
156 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
158 my $sel="Select * from accountlines where borrowernumber=$bornumber and
159 accountno=$accountno";
160 my $sth=$dbh->prepare($sel);
162 my $data=$sth->fetchrow_hashref;
167 SET amountoutstanding = 0
168 WHERE borrowernumber = $bornumber
169 AND accountno = $accountno
174 INSERT INTO accountoffsets
175 (borrowernumber, accountno, offsetaccount,
177 VALUES ($bornumber, $accountno, $nextaccntno, $newamtos)
181 my $payment=0-$amount;
183 INSERT INTO accountlines
184 (borrowernumber, accountno, date, amount,
185 description, accounttype, amountoutstanding)
186 VALUES ($bornumber, $nextaccntno, now(), $payment,
187 'Payment,thanks - $user', 'Pay', 0)
190 # FIXME - The second argument to &UpdateStats is supposed to be the
192 UpdateStats($env,$user,'payment',$amount,'','','',$bornumber);
194 #check to see what accounttype
195 if ($data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L'){
196 returnlost($bornumber,$data->{'itemnumber'});
202 $nextacct = &getnextacctno($env, $borrowernumber, $dbh);
204 Returns the next unused account number for the patron with the given
207 C<$dbh> is a DBI::db handle to the Koha database.
213 # FIXME - Okay, so what does the above actually _mean_?
215 my ($env,$bornumber,$dbh)=@_;
217 my $query = "select * from accountlines
218 where (borrowernumber = '$bornumber')
219 order by accountno desc";
220 my $sth = $dbh->prepare($query);
222 if (my $accdata=$sth->fetchrow_hashref){
223 $nextaccntno = $accdata->{'accountno'} + 1;
226 return($nextaccntno);
231 &fixaccounts($borrowernumber, $accountnumber, $amount);
235 # FIXME - I don't understand what this function does.
237 my ($borrowernumber,$accountno,$amount)=@_;
238 my $dbh = C4::Context->dbh;
239 my $query="Select * from accountlines where borrowernumber=$borrowernumber
240 and accountno=$accountno";
241 my $sth=$dbh->prepare($query);
243 my $data=$sth->fetchrow_hashref;
244 # FIXME - Error-checking
245 my $diff=$amount-$data->{'amount'};
246 my $outstanding=$data->{'amountoutstanding'}+$diff;
251 SET amount = '$amount',
252 amountoutstanding = '$outstanding'
253 WHERE borrowernumber = $borrowernumber
254 AND accountno = $accountno
258 # FIXME - Never used, but not exported, either.
260 my ($borrnum,$itemnum)=@_;
261 my $dbh = C4::Context->dbh;
262 my $borrower=borrdata('',$borrnum); #from C4::Search;
263 my $upiss="Update issues set returndate=now() where
264 borrowernumber='$borrnum' and itemnumber='$itemnum' and returndate is null";
265 my $sth=$dbh->prepare($upiss);
268 my @datearr = localtime(time);
269 my $date = (1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
270 my $bor="$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
271 # FIXME - Use $dbh->do();
272 my $upitem="Update items set paidfor='Paid for by $bor $date' where itemnumber='$itemnum'";
273 $sth=$dbh->prepare($upitem);
280 &manualinvoice($borrowernumber, $itemnumber, $description, $type,
283 C<$borrowernumber> is the patron's borrower number.
284 C<$description> is a description of the transaction.
285 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
287 C<$itemnumber> is the item involved, if pertinent; otherwise, it
288 should be the empty string.
292 # FIXME - Okay, so what does this function do, really?
294 my ($bornum,$itemnum,$desc,$type,$amount,$user)=@_;
295 my $dbh = C4::Context->dbh;
299 my $accountno=getnextacctno('',$bornum,$dbh);
300 my $amountleft=$amount;
302 if ($type eq 'CS' || $type eq 'CB' || $type eq 'CW'
303 || $type eq 'CF' || $type eq 'CL'){
304 my $amount2=$amount*-1; # FIXME - $amount2 = -$amount
305 $amountleft=fixcredit(\%env,$bornum,$amount2,$itemnum,$type,$user);
310 if ($type eq 'L' && $desc eq ''){
314 $amountleft=refund('',$bornum,$amount);
317 # my $sth=$dbh->prepare("Select * from items where barcode='$itemnum'");
319 # my $data=$sth->fetchrow_hashref;
322 my $sth=$dbh->prepare("INSERT INTO accountlines
323 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber)
324 VALUES (?, ?, now(), ?,?, ?,?,?)");
325 # $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft, $data->{'itemnumber'});
326 $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft, $itemnum);
328 $desc=$dbh->quote($desc);
329 my $sth=$dbh->prepare("INSERT INTO accountlines
330 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding)
331 VALUES (?, ?, now(), ?, ?, ?, ?)");
332 $sth->execute($bornum, $accountno, $amount, $desc, $type, $amountleft);
337 # $amountleft = &fixcredit($env, $bornumber, $data, $barcode, $type, $user);
339 # This function is only used internally.
340 # FIXME - Figure out what this function does, and write it down.
342 #here we update both the accountoffsets and the account lines
343 my ($env,$bornumber,$data,$barcode,$type,$user)=@_;
344 my $dbh = C4::Context->dbh;
348 my $amountleft = $data;
350 my $item=getiteminformation($env,'',$barcode);
351 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
352 my $query="Select * from accountlines where (borrowernumber='$bornumber'
353 and itemnumber='$item->{'itemnumber'}' and amountoutstanding > 0)";
355 $query.=" and (accounttype = 'L' or accounttype = 'Rep')";
356 } elsif ($type eq 'CF'){
357 $query.=" and (accounttype = 'F' or accounttype = 'FU' or
358 accounttype='Res' or accounttype='Rent')";
359 } elsif ($type eq 'CB'){
360 $query.=" and accounttype='A'";
363 my $sth=$dbh->prepare($query);
365 $accdata=$sth->fetchrow_hashref;
367 if ($accdata->{'amountoutstanding'} < $amountleft) {
369 $amountleft -= $accdata->{'amountoutstanding'};
371 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
374 my $thisacct = $accdata->{accountno};
375 my $updquery = "update accountlines set amountoutstanding= '$newamtos'
376 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
377 my $usth = $dbh->prepare($updquery);
380 $updquery = "insert into accountoffsets
381 (borrowernumber, accountno, offsetaccount, offsetamount)
382 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
383 $usth = $dbh->prepare($updquery);
388 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
389 # get lines with outstanding amounts to offset
390 my $query = "select * from accountlines
391 where (borrowernumber = '$bornumber') and (amountoutstanding >0)
393 my $sth = $dbh->prepare($query);
396 # offset transactions
397 while (($accdata=$sth->fetchrow_hashref) and ($amountleft>0)){
398 if ($accdata->{'amountoutstanding'} < $amountleft) {
400 $amountleft -= $accdata->{'amountoutstanding'};
402 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
405 my $thisacct = $accdata->{accountno};
406 $updquery = "update accountlines set amountoutstanding= '$newamtos'
407 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
408 my $usth = $dbh->prepare($updquery);
411 $updquery = "insert into accountoffsets
412 (borrowernumber, accountno, offsetaccount, offsetamount)
413 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
414 $usth = $dbh->prepare($updquery);
419 $env->{'branch'}=$user;
420 $type="Credit ".$type;
421 UpdateStats($env,$user,$type,$data,$user,'','',$bornumber);
427 # FIXME - Figure out what this function does, and write it down.
429 #here we update both the accountoffsets and the account lines
430 my ($env,$bornumber,$data)=@_;
431 my $dbh = C4::Context->dbh;
435 # my $branch=$env->{'branchcode'};
436 my $amountleft = $data *-1;
439 my $nextaccntno = getnextacctno($env,$bornumber,$dbh);
440 # get lines with outstanding amounts to offset
441 my $query = "select * from accountlines
442 where (borrowernumber = '$bornumber') and (amountoutstanding<0)
444 my $sth = $dbh->prepare($query);
448 # offset transactions
449 while (($accdata=$sth->fetchrow_hashref) and ($amountleft<0)){
450 if ($accdata->{'amountoutstanding'} > $amountleft) {
452 $amountleft -= $accdata->{'amountoutstanding'};
454 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
458 my $thisacct = $accdata->{accountno};
459 $updquery = "update accountlines set amountoutstanding= '$newamtos'
460 where (borrowernumber = '$bornumber') and (accountno='$thisacct')";
461 my $usth = $dbh->prepare($updquery);
464 $updquery = "insert into accountoffsets
465 (borrowernumber, accountno, offsetaccount, offsetamount)
466 values ($bornumber,$accdata->{'accountno'},$nextaccntno,$newamtos)";
467 $usth = $dbh->prepare($updquery);
475 END { } # module clean-up code here (global destructor)