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
29 use vars qw($VERSION @ISA @EXPORT);
31 # set the version for version checking
32 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
33 shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
38 C4::Accounts - Functions for dealing with Koha accounts
46 The functions in this module deal with the monetary aspect of Koha,
47 including looking up and modifying the amount of money owed by a
55 @EXPORT = qw(&recordpayment &fixaccounts &makepayment &manualinvoice
56 &getnextacctno &reconcileaccount);
60 &recordpayment($borrowernumber, $payment);
62 Record payment by a patron. C<$borrowernumber> is the patron's
63 borrower number. C<$payment> is a floating-point number, giving the
66 Amounts owed are paid off oldest first. That is, if the patron has a
67 $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
68 of $1.50, then the oldest fine will be paid off in full, and $0.50
69 will be credited to the next one.
76 #here we update both the accountoffsets and the account lines
77 my ( $borrowernumber, $data ) = @_;
78 my $dbh = C4::Context->dbh;
81 my $branch = C4::Context->userenv->{'branch'};
82 my $amountleft = $data;
85 my $nextaccntno = getnextacctno($borrowernumber);
87 # get lines with outstanding amounts to offset
88 my $sth = $dbh->prepare(
89 "SELECT * FROM accountlines
90 WHERE (borrowernumber = ?) AND (amountoutstanding<>0)
93 $sth->execute($borrowernumber);
96 while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) {
97 if ( $accdata->{'amountoutstanding'} < $amountleft ) {
99 $amountleft -= $accdata->{'amountoutstanding'};
102 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
105 my $thisacct = $accdata->{accountno};
106 my $usth = $dbh->prepare(
107 "UPDATE accountlines SET amountoutstanding= ?
108 WHERE (borrowernumber = ?) AND (accountno=?)"
110 $usth->execute( $newamtos, $borrowernumber, $thisacct );
112 $usth = $dbh->prepare(
113 "INSERT INTO accountoffsets
114 (borrowernumber, accountno, offsetaccount, offsetamount)
117 $usth->execute( $borrowernumber, $accdata->{'accountno'},
118 $nextaccntno, $newamtos );
123 my $usth = $dbh->prepare(
124 "INSERT INTO accountlines
125 (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding)
126 VALUES (?,?,now(),?,'Payment,thanks','Pay',?)"
128 $usth->execute( $borrowernumber, $nextaccntno, 0 - $data, 0 - $amountleft );
130 UpdateStats( $branch, 'payment', $data, '', '', '', $borrowernumber );
136 &makepayment($borrowernumber, $acctnumber, $amount, $branchcode);
138 Records the fact that a patron has paid off the entire amount he or
141 C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
142 the account that was credited. C<$amount> is the amount paid (this is
143 only used to record the payment. It is assumed to be equal to the
144 amount owed). C<$branchcode> is the code of the branch where payment
150 # FIXME - I'm not at all sure about the above, because I don't
151 # understand what the acct* tables in the Koha database are for.
154 #here we update both the accountoffsets and the account lines
155 #updated to check, if they are paying off a lost item, we return the item
156 # from their card, and put a note on the item record
157 my ( $borrowernumber, $accountno, $amount, $user, $branch ) = @_;
158 my $dbh = C4::Context->dbh;
161 my $nextaccntno = getnextacctno($borrowernumber);
165 "SELECT * FROM accountlines WHERE borrowernumber=? AND accountno=?");
166 $sth->execute( $borrowernumber, $accountno );
167 my $data = $sth->fetchrow_hashref;
172 SET amountoutstanding = 0
173 WHERE borrowernumber = $borrowernumber
174 AND accountno = $accountno
180 INSERT INTO accountoffsets
181 (borrowernumber, accountno, offsetaccount,
183 VALUES ($borrowernumber, $accountno, $nextaccntno, $newamtos)
187 my $payment = 0 - $amount;
189 INSERT INTO accountlines
190 (borrowernumber, accountno, date, amount,
191 description, accounttype, amountoutstanding)
192 VALUES ($borrowernumber, $nextaccntno, now(), $payment,
193 'Payment,thanks - $user', 'Pay', 0)
196 # FIXME - The second argument to &UpdateStats is supposed to be the
198 # UpdateStats is now being passed $accountno too. MTJ
199 UpdateStats( $user, 'payment', $amount, '', '', '', $borrowernumber,
203 #check to see what accounttype
204 if ( $data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L' ) {
205 returnlost( $borrowernumber, $data->{'itemnumber'} );
211 $nextacct = &getnextacctno($borrowernumber);
213 Returns the next unused account number for the patron with the given
219 # FIXME - Okay, so what does the above actually _mean_?
221 my ($borrowernumber) = @_;
223 my $dbh = C4::Context->dbh;
224 my $sth = $dbh->prepare(
225 "SELECT * FROM accountlines
226 WHERE (borrowernumber = ?)
227 ORDER BY accountno DESC"
229 $sth->execute($borrowernumber);
230 if ( my $accdata = $sth->fetchrow_hashref ) {
231 $nextaccntno = $accdata->{'accountno'} + 1;
234 return ($nextaccntno);
239 &fixaccounts($borrowernumber, $accountnumber, $amount);
244 # FIXME - I don't understand what this function does.
246 my ( $borrowernumber, $accountno, $amount ) = @_;
247 my $dbh = C4::Context->dbh;
248 my $sth = $dbh->prepare(
249 "SELECT * FROM accountlines WHERE borrowernumber=?
252 $sth->execute( $borrowernumber, $accountno );
253 my $data = $sth->fetchrow_hashref;
255 # FIXME - Error-checking
256 my $diff = $amount - $data->{'amount'};
257 my $outstanding = $data->{'amountoutstanding'} + $diff;
262 SET amount = '$amount',
263 amountoutstanding = '$outstanding'
264 WHERE borrowernumber = $borrowernumber
265 AND accountno = $accountno
269 # FIXME - Never used, but not exported, either.
271 my ( $borrowernumber, $itemnum ) = @_;
272 my $dbh = C4::Context->dbh;
273 my $borrower = C4::Members::GetMember( $borrowernumber, 'borrowernumber' );
274 my $sth = $dbh->prepare(
275 "UPDATE issues SET returndate=now() WHERE
276 borrowernumber=? AND itemnumber=? AND returndate IS NULL"
278 $sth->execute( $borrowernumber, $itemnum );
280 my @datearr = localtime(time);
282 ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
284 "$borrower->{'firstname'} $borrower->{'surname'} $borrower->{'cardnumber'}";
285 $sth = $dbh->prepare("UPDATE items SET paidfor=? WHERE itemnumber=?");
286 $sth->execute( "Paid for by $bor $date", $itemnum );
292 &manualinvoice($borrowernumber, $itemnumber, $description, $type,
295 C<$borrowernumber> is the patron's borrower number.
296 C<$description> is a description of the transaction.
297 C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
299 C<$itemnumber> is the item involved, if pertinent; otherwise, it
300 should be the empty string.
305 # FIXME - Okay, so what does this function do, really?
307 my ( $borrowernumber, $itemnum, $desc, $type, $amount, $user ) = @_;
308 my $dbh = C4::Context->dbh;
312 my $accountno = getnextacctno($borrowernumber);
313 my $amountleft = $amount;
321 my $amount2 = $amount * -1; # FIXME - $amount2 = -$amount
323 fixcredit( $borrowernumber, $amount2, $itemnum, $type, $user );
325 if ( $type eq 'N' ) {
328 if ( $type eq 'F' ) {
331 if ( $type eq 'A' ) {
332 $desc .= "Account Management fee";
334 if ( $type eq 'M' ) {
338 if ( $type eq 'L' && $desc eq '' ) {
342 if ( $type eq 'REF' ) {
343 $desc .= "Cash Refund";
344 $amountleft = refund( '', $borrowernumber, $amount );
346 if ( ( $type eq 'L' )
350 or ( $type eq 'M' ) )
355 if ( $itemnum ne '' ) {
356 $desc .= " " . $itemnum;
357 my $sth = $dbh->prepare(
358 "INSERT INTO accountlines
359 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id)
360 VALUES (?, ?, now(), ?,?, ?,?,?,?)");
361 $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid) || return $sth->errstr;
363 my $sth=$dbh->prepare("INSERT INTO accountlines
364 (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id)
365 VALUES (?, ?, now(), ?, ?, ?, ?,?)"
367 $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type,
368 $amountleft, $notifyid );
375 $amountleft = &fixcredit($borrowernumber, $data, $barcode, $type, $user);
377 This function is only used internally, not exported.
378 FIXME - Figure out what this function does, and write it down.
384 #here we update both the accountoffsets and the account lines
385 my ( $borrowernumber, $data, $barcode, $type, $user ) = @_;
386 my $dbh = C4::Context->dbh;
389 my $amountleft = $data;
390 if ( $barcode ne '' ) {
391 my $item = GetBiblioFromItemNumber( '', $barcode );
392 my $nextaccntno = getnextacctno($borrowernumber);
393 my $query = "SELECT * FROM accountlines WHERE (borrowernumber=?
394 AND itemnumber=? AND amountoutstanding > 0)";
395 if ( $type eq 'CL' ) {
396 $query .= " AND (accounttype = 'L' OR accounttype = 'Rep')";
398 elsif ( $type eq 'CF' ) {
399 $query .= " AND (accounttype = 'F' OR accounttype = 'FU' OR
400 accounttype='Res' OR accounttype='Rent')";
402 elsif ( $type eq 'CB' ) {
403 $query .= " and accounttype='A'";
407 my $sth = $dbh->prepare($query);
408 $sth->execute( $borrowernumber, $item->{'itemnumber'} );
409 $accdata = $sth->fetchrow_hashref;
411 if ( $accdata->{'amountoutstanding'} < $amountleft ) {
413 $amountleft -= $accdata->{'amountoutstanding'};
416 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
419 my $thisacct = $accdata->{accountno};
420 my $usth = $dbh->prepare(
421 "UPDATE accountlines SET amountoutstanding= ?
422 WHERE (borrowernumber = ?) AND (accountno=?)"
424 $usth->execute( $newamtos, $borrowernumber, $thisacct );
426 $usth = $dbh->prepare(
427 "INSERT INTO accountoffsets
428 (borrowernumber, accountno, offsetaccount, offsetamount)
431 $usth->execute( $borrowernumber, $accdata->{'accountno'},
432 $nextaccntno, $newamtos );
437 my $nextaccntno = getnextacctno($borrowernumber);
439 # get lines with outstanding amounts to offset
440 my $sth = $dbh->prepare(
441 "SELECT * FROM accountlines
442 WHERE (borrowernumber = ?) AND (amountoutstanding >0)
445 $sth->execute($borrowernumber);
448 # offset transactions
449 while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) {
450 if ( $accdata->{'amountoutstanding'} < $amountleft ) {
452 $amountleft -= $accdata->{'amountoutstanding'};
455 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
458 my $thisacct = $accdata->{accountno};
459 my $usth = $dbh->prepare(
460 "UPDATE accountlines SET amountoutstanding= ?
461 WHERE (borrowernumber = ?) AND (accountno=?)"
463 $usth->execute( $newamtos, $borrowernumber, $thisacct );
465 $usth = $dbh->prepare(
466 "INSERT INTO accountoffsets
467 (borrowernumber, accountno, offsetaccount, offsetamount)
470 $usth->execute( $borrowernumber, $accdata->{'accountno'},
471 $nextaccntno, $newamtos );
475 $type = "Credit " . $type;
476 UpdateStats( $user, $type, $data, $user, '', '', $borrowernumber );
478 return ($amountleft);
484 # FIXME - Figure out what this function does, and write it down.
490 #here we update both the accountoffsets and the account lines
491 my ( $borrowernumber, $data ) = @_;
492 my $dbh = C4::Context->dbh;
495 my $amountleft = $data * -1;
498 my $nextaccntno = getnextacctno($borrowernumber);
500 # get lines with outstanding amounts to offset
501 my $sth = $dbh->prepare(
502 "SELECT * FROM accountlines
503 WHERE (borrowernumber = ?) AND (amountoutstanding<0)
506 $sth->execute($borrowernumber);
509 # offset transactions
510 while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft < 0 ) ) {
511 if ( $accdata->{'amountoutstanding'} > $amountleft ) {
513 $amountleft -= $accdata->{'amountoutstanding'};
516 $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
521 my $thisacct = $accdata->{accountno};
522 my $usth = $dbh->prepare(
523 "UPDATE accountlines SET amountoutstanding= ?
524 WHERE (borrowernumber = ?) AND (accountno=?)"
526 $usth->execute( $newamtos, $borrowernumber, $thisacct );
528 $usth = $dbh->prepare(
529 "INSERT INTO accountoffsets
530 (borrowernumber, accountno, offsetaccount, offsetamount)
533 $usth->execute( $borrowernumber, $accdata->{'accountno'},
534 $nextaccntno, $newamtos );
538 return ($amountleft);
541 END { } # module clean-up code here (global destructor)