-=head2 recordpayment
-
- &recordpayment($borrowernumber, $payment);
-
-Record payment by a patron. C<$borrowernumber> is the patron's
-borrower number. C<$payment> is a floating-point number, giving the
-amount that was paid.
-
-Amounts owed are paid off oldest first. That is, if the patron has a
-$1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment
-of $1.50, then the oldest fine will be paid off in full, and $0.50
-will be credited to the next one.
-
-=cut
-
-#'
-sub recordpayment {
-
- #here we update the account lines
- my ( $borrowernumber, $data ) = @_;
- my $dbh = C4::Context->dbh;
- my $newamtos = 0;
- my $accdata = "";
- my $branch = C4::Context->userenv->{'branch'};
- my $amountleft = $data;
- my $manager_id = 0;
- $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
-
- # begin transaction
- my $nextaccntno = getnextacctno($borrowernumber);
-
- # get lines with outstanding amounts to offset
- my $sth = $dbh->prepare(
- "SELECT * FROM accountlines
- WHERE (borrowernumber = ?) AND (amountoutstanding<>0)
- ORDER BY date"
- );
- $sth->execute($borrowernumber);
-
- # offset transactions
- while ( ( $accdata = $sth->fetchrow_hashref ) and ( $amountleft > 0 ) ) {
- if ( $accdata->{'amountoutstanding'} < $amountleft ) {
- $newamtos = 0;
- $amountleft -= $accdata->{'amountoutstanding'};
- }
- else {
- $newamtos = $accdata->{'amountoutstanding'} - $amountleft;
- $amountleft = 0;
- }
- my $thisacct = $accdata->{accountno};
- my $usth = $dbh->prepare(
- "UPDATE accountlines SET amountoutstanding= ?
- WHERE (borrowernumber = ?) AND (accountno=?)"
- );
- $usth->execute( $newamtos, $borrowernumber, $thisacct );
- $usth->finish;
-# $usth = $dbh->prepare(
-# "INSERT INTO accountoffsets
-# (borrowernumber, accountno, offsetaccount, offsetamount)
-# VALUES (?,?,?,?)"
-# );
-# $usth->execute( $borrowernumber, $accdata->{'accountno'},
-# $nextaccntno, $newamtos );
- $usth->finish;
- }
-
- # create new line
- my $usth = $dbh->prepare(
- "INSERT INTO accountlines
- (borrowernumber, accountno,date,amount,description,accounttype,amountoutstanding,manager_id)
- VALUES (?,?,now(),?,'Payment,thanks','Pay',?,?)"
- );
- $usth->execute( $borrowernumber, $nextaccntno, 0 - $data, 0 - $amountleft, $manager_id );
- $usth->finish;
- UpdateStats( $branch, 'payment', $data, '', '', '', $borrowernumber, $nextaccntno );
- $sth->finish;
-}
-
-=head2 makepayment
-
- &makepayment($borrowernumber, $acctnumber, $amount, $branchcode);
-
-Records the fact that a patron has paid off the entire amount he or
-she owes.
-
-C<$borrowernumber> is the patron's borrower number. C<$acctnumber> is
-the account that was credited. C<$amount> is the amount paid (this is
-only used to record the payment. It is assumed to be equal to the
-amount owed). C<$branchcode> is the code of the branch where payment
-was made.
-
-=cut
-
-#'
-# FIXME - I'm not at all sure about the above, because I don't
-# understand what the acct* tables in the Koha database are for.
-sub makepayment {
-
- #here we update both the accountoffsets and the account lines
- #updated to check, if they are paying off a lost item, we return the item
- # from their card, and put a note on the item record
- my ( $borrowernumber, $accountno, $amount, $user, $branch ) = @_;
- my $dbh = C4::Context->dbh;
- my $manager_id = 0;
- $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
-
- # begin transaction
- my $nextaccntno = getnextacctno($borrowernumber);
- my $newamtos = 0;
- my $sth =
- $dbh->prepare(
- "SELECT * FROM accountlines WHERE borrowernumber=? AND accountno=?");
- $sth->execute( $borrowernumber, $accountno );
- my $data = $sth->fetchrow_hashref;
- $sth->finish;
-
- if($data->{'accounttype'} eq "Pay"){
- my $udp =
- $dbh->prepare(
- "UPDATE accountlines
- SET amountoutstanding = 0, description = 'Payment,thanks'
- WHERE borrowernumber = ?
- AND accountno = ?
- "
- );
- $udp->execute($borrowernumber, $accountno );
- $udp->finish;
- }else{
- my $udp =
- $dbh->prepare(
- "UPDATE accountlines
- SET amountoutstanding = 0
- WHERE borrowernumber = ?
- AND accountno = ?
- "
- );
- $udp->execute($borrowernumber, $accountno );
- $udp->finish;
-
- # create new line
- my $payment = 0 - $amount;
-
- my $ins =
- $dbh->prepare(
- "INSERT
- INTO accountlines (borrowernumber, accountno, date, amount, itemnumber, description, accounttype, amountoutstanding, manager_id)
- VALUES ( ?, ?, now(), ?, ?, 'Payment,thanks', 'Pay', 0, ?)"
- );
- $ins->execute($borrowernumber, $nextaccntno, $payment, $data->{'itemnumber'}, $manager_id);
- $ins->finish;
- }
-
- # FIXME - The second argument to &UpdateStats is supposed to be the
- # branch code.
- # UpdateStats is now being passed $accountno too. MTJ
- UpdateStats( $user, 'payment', $amount, '', '', '', $borrowernumber,
- $accountno );
- #from perldoc: for SELECT only #$sth->finish;
-
- #check to see what accounttype
- if ( $data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L' ) {
- C4::Circulation::ReturnLostItem( $borrowernumber, $data->{'itemnumber'} );
- }
-}
-