3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
28 use vars qw($VERSION @ISA @EXPORT);
31 # set the version for version checking
36 &recordpayment &fixaccounts &makepayment &manualinvoice
37 &getnextacctno &reconcileaccount
43 C4::Accounts - Functions for dealing with Koha accounts
51 The functions in this module deal with the monetary aspect of Koha,
52 including looking up and modifying the amount of money owed by a
59 &recordpayment($borrowernumber, $payment);
61 Record payment by a patron. C<$borrowernumber> is the patron's
62 borrower number. C<$payment> is a floating-point number, giving the
65 Amounts owed are paid off oldest first. That is, if the patron has a
66 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
67 of $1.50, then the oldest fine will be paid off in full, and $0.50
68 will be credited to the next one.
75 #here we update both the accountoffsets and the account lines
76 my ( $borrowernumber, $data ) = @_;
77 my $dbh = C4::Context->dbh;
80 my $branch = C4::Context->userenv->{'branch'};
81 my $amountleft = $data;
84 my $nextaccntno = getnextacctno($borrowernumber);
86 # get lines with outstanding amounts to offset
87 my $sth = $dbh->prepare(
88 "SELECT * FROM accountlines
89 WHERE (borrowernumber = ?) AND (amountoutstanding<>0)
92 $sth->execute($borrowernumber);
95 while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) {
96 if ( $accdata->{'amountoutstanding'} < $amountleft ) {
98 $amountleft -= $accdata->{'amountoutstanding'};
101 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
104 my $thisacct = $accdata->{accountno};
105 my $usth = $dbh->prepare(
106 "UPDATE accountlines SET amountoutstanding= ?
107 WHERE (borrowernumber = ?) AND (accountno=?)"
109 $usth->execute( $newamtos, $borrowernumber, $thisacct );
111 $usth = $dbh->prepare(
112 "INSERT INTO accountoffsets
113 (borrowernumber, accountno, offsetaccount, offsetamount)
116 $usth->execute( $borrowernumber, $accdata->{'accountno'},
117 $nextaccntno, $newamtos );
122 my $usth = $dbh->prepare(
123 "INSERT INTO accountlines
124 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
125 VALUES (?,?,now(),?,'Payment,thanks','Pay',?)"
127 $usth->execute( $borrowernumber, $nextaccntno, 0 - $data, 0 - $amountleft );
129 UpdateStats( $branch, 'payment', $data, '', '', '', $borrowernumber );
135 &makepayment($borrowernumber, $acctnumber, $amount, $branchcode);
137 Records the fact that a patron has paid off the entire amount he or
140 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
141 the account that was credited. C<$amount> is the amount paid (this is
142 only used to record the payment. It is assumed to be equal to the
143 amount owed). C<$branchcode> is the code of the branch where payment
149 # FIXME - I'm not at all sure about the above, because I don't
150 # understand what the acct* tables in the Koha database are for.
153 #here we update both the accountoffsets and the account lines
154 #updated to check, if they are paying off a lost item, we return the item
155 # from their card, and put a note on the item record
156 my ( $borrowernumber, $accountno, $amount, $user, $branch ) = @_;
157 my $dbh = C4::Context->dbh;
160 my $nextaccntno = getnextacctno($borrowernumber);
164 "SELECT * FROM accountlines WHERE borrowernumber=? AND accountno=?");
165 $sth->execute( $borrowernumber, $accountno );
166 my $data = $sth->fetchrow_hashref;
171 SET amountoutstanding = 0
172 WHERE borrowernumber = $borrowernumber
173 AND accountno = $accountno
179 INSERT INTO accountoffsets
180 (borrowernumber, accountno, offsetaccount,
182 VALUES ($borrowernumber, $accountno, $nextaccntno, $newamtos)
186 my $payment = 0 - $amount;
188 INSERT INTO accountlines
189 (borrowernumber, accountno, date, amount,
190 description, accounttype, amountoutstanding)
191 VALUES ($borrowernumber, $nextaccntno, now(), $payment,
192 'Payment,thanks - $user', 'Pay', 0)
195 # FIXME - The second argument to &UpdateStats is supposed to be the
197 # UpdateStats is now being passed $accountno too. MTJ
198 UpdateStats( $user, 'payment', $amount, '', '', '', $borrowernumber,
202 #check to see what accounttype
203 if ( $data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L' ) {
204 returnlost( $borrowernumber, $data->{'itemnumber'} );
210 $nextacct = &getnextacctno($borrowernumber);
212 Returns the next unused account number for the patron with the given
218 # FIXME - Okay, so what does the above actually _mean_?
220 my ($borrowernumber) = @_;
222 my $dbh = C4::Context->dbh;
223 my $sth = $dbh->prepare(
224 "SELECT * FROM accountlines
225 WHERE (borrowernumber = ?)
226 ORDER BY accountno DESC"
228 $sth->execute($borrowernumber);
229 if ( my $accdata = $sth->fetchrow_hashref ) {
230 $nextaccntno = $accdata->{'accountno'} + 1;
233 return ($nextaccntno);
238 &fixaccounts($borrowernumber, $accountnumber, $amount);
243 # FIXME - I don't understand what this function does.
245 my ( $borrowernumber, $accountno, $amount ) = @_;
246 my $dbh = C4::Context->dbh;
247 my $sth = $dbh->prepare(
248 "SELECT * FROM accountlines WHERE borrowernumber=?
251 $sth->execute( $borrowernumber, $accountno );
252 my $data = $sth->fetchrow_hashref;
254 # FIXME - Error-checking
255 my $diff = $amount - $data->{'amount'};
256 my $outstanding = $data->{'amountoutstanding'} + $diff;
261 SET amount = '$amount',
262 amountoutstanding = '$outstanding'
263 WHERE borrowernumber = $borrowernumber
264 AND accountno = $accountno
268 # FIXME - Never used, but not exported, either.
270 my ( $borrowernumber, $itemnum ) = @_;
271 my $dbh = C4::Context->dbh;
272 my $borrower = C4::Members::GetMember( $borrowernumber, 'borrowernumber' );
273 my $sth = $dbh->prepare(
274 "UPDATE issues SET returndate=now() WHERE
275 borrowernumber=? AND itemnumber=? AND returndate IS NULL"
277 $sth->execute( $borrowernumber, $itemnum );
279 my @datearr = localtime(time);
281 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
283 "$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
284 ModItem({ paidfor => "Paid for by $bor $date" }, undef, $itemnum);
289 &manualinvoice($borrowernumber, $itemnumber, $description, $type,
292 C<$borrowernumber> is the patron's borrower number.
293 C<$description> is a description of the transaction.
294 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
296 C<$itemnumber> is the item involved, if pertinent; otherwise, it
297 should be the empty string.
302 # FIXME - Okay, so what does this function do, really?
304 my ( $borrowernumber, $itemnum, $desc, $type, $amount, $user ) = @_;
305 my $dbh = C4::Context->dbh;
309 my $accountno = getnextacctno($borrowernumber);
310 my $amountleft = $amount;
318 my $amount2 = $amount * -1; # FIXME - $amount2 = -$amount
320 fixcredit( $borrowernumber, $amount2, $itemnum, $type, $user );
322 if ( $type eq 'N' ) {
325 if ( $type eq 'F' ) {
328 if ( $type eq 'A' ) {
329 $desc .= "Account Management fee";
331 if ( $type eq 'M' ) {
335 if ( $type eq 'L' && $desc eq '' ) {
339 if ( $type eq 'REF' ) {
340 $desc .= "Cash Refund";
341 $amountleft = refund( '', $borrowernumber, $amount );
343 if ( ( $type eq 'L' )
347 or ( $type eq 'M' ) )
352 if ( $itemnum ne '' ) {
353 $desc .= " " . $itemnum;
354 my $sth = $dbh->prepare(
355 "INSERT INTO accountlines
356 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id)
357 VALUES (?, ?, now(), ?,?, ?,?,?,?)");
358 $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid) || return $sth->errstr;
360 my $sth=$dbh->prepare("INSERT INTO accountlines
361 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id)
362 VALUES (?, ?, now(), ?, ?, ?, ?,?)"
364 $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type,
365 $amountleft, $notifyid );
372 $amountleft = &fixcredit($borrowernumber, $data, $barcode, $type, $user);
374 This function is only used internally, not exported.
375 FIXME - Figure out what this function does, and write it down.
381 #here we update both the accountoffsets and the account lines
382 my ( $borrowernumber, $data, $barcode, $type, $user ) = @_;
383 my $dbh = C4::Context->dbh;
386 my $amountleft = $data;
387 if ( $barcode ne '' ) {
388 my $item = GetBiblioFromItemNumber( '', $barcode );
389 my $nextaccntno = getnextacctno($borrowernumber);
390 my $query = "SELECT * FROM accountlines WHERE (borrowernumber=?
391 AND itemnumber=? AND amountoutstanding > 0)";
392 if ( $type eq 'CL' ) {
393 $query .= " AND (accounttype = 'L' OR accounttype = 'Rep')";
395 elsif ( $type eq 'CF' ) {
396 $query .= " AND (accounttype = 'F' OR accounttype = 'FU' OR
397 accounttype='Res' OR accounttype='Rent')";
399 elsif ( $type eq 'CB' ) {
400 $query .= " and accounttype='A'";
404 my $sth = $dbh->prepare($query);
405 $sth->execute( $borrowernumber, $item->{'itemnumber'} );
406 $accdata = $sth->fetchrow_hashref;
408 if ( $accdata->{'amountoutstanding'} < $amountleft ) {
410 $amountleft -= $accdata->{'amountoutstanding'};
413 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
416 my $thisacct = $accdata->{accountno};
417 my $usth = $dbh->prepare(
418 "UPDATE accountlines SET amountoutstanding= ?
419 WHERE (borrowernumber = ?) AND (accountno=?)"
421 $usth->execute( $newamtos, $borrowernumber, $thisacct );
423 $usth = $dbh->prepare(
424 "INSERT INTO accountoffsets
425 (borrowernumber, accountno, offsetaccount, offsetamount)
428 $usth->execute( $borrowernumber, $accdata->{'accountno'},
429 $nextaccntno, $newamtos );
434 my $nextaccntno = getnextacctno($borrowernumber);
436 # get lines with outstanding amounts to offset
437 my $sth = $dbh->prepare(
438 "SELECT * FROM accountlines
439 WHERE (borrowernumber = ?) AND (amountoutstanding >0)
442 $sth->execute($borrowernumber);
445 # offset transactions
446 while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) {
447 if ( $accdata->{'amountoutstanding'} < $amountleft ) {
449 $amountleft -= $accdata->{'amountoutstanding'};
452 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
455 my $thisacct = $accdata->{accountno};
456 my $usth = $dbh->prepare(
457 "UPDATE accountlines SET amountoutstanding= ?
458 WHERE (borrowernumber = ?) AND (accountno=?)"
460 $usth->execute( $newamtos, $borrowernumber, $thisacct );
462 $usth = $dbh->prepare(
463 "INSERT INTO accountoffsets
464 (borrowernumber, accountno, offsetaccount, offsetamount)
467 $usth->execute( $borrowernumber, $accdata->{'accountno'},
468 $nextaccntno, $newamtos );
472 $type = "Credit " . $type;
473 UpdateStats( $user, $type, $data, $user, '', '', $borrowernumber );
475 return ($amountleft);
481 # FIXME - Figure out what this function does, and write it down.
487 #here we update both the accountoffsets and the account lines
488 my ( $borrowernumber, $data ) = @_;
489 my $dbh = C4::Context->dbh;
492 my $amountleft = $data * -1;
495 my $nextaccntno = getnextacctno($borrowernumber);
497 # get lines with outstanding amounts to offset
498 my $sth = $dbh->prepare(
499 "SELECT * FROM accountlines
500 WHERE (borrowernumber = ?) AND (amountoutstanding<0)
503 $sth->execute($borrowernumber);
506 # offset transactions
507 while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft < 0 ) ) {
508 if ( $accdata->{'amountoutstanding'} > $amountleft ) {
510 $amountleft -= $accdata->{'amountoutstanding'};
513 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
518 my $thisacct = $accdata->{accountno};
519 my $usth = $dbh->prepare(
520 "UPDATE accountlines SET amountoutstanding= ?
521 WHERE (borrowernumber = ?) AND (accountno=?)"
523 $usth->execute( $newamtos, $borrowernumber, $thisacct );
525 $usth = $dbh->prepare(
526 "INSERT INTO accountoffsets
527 (borrowernumber, accountno, offsetaccount, offsetamount)
530 $usth->execute( $borrowernumber, $accdata->{'accountno'},
531 $nextaccntno, $newamtos );
535 return ($amountleft);
538 END { } # module clean-up code here (global destructor)