- }
-}
-
-=head2 manualinvoice
-
- &manualinvoice($borrowernumber, $itemnumber, $description, $type,
- $amount, $note);
-
-C<$borrowernumber> is the patron's borrower number.
-C<$description> is a description of the transaction.
-C<$type> may be one of C<CS>, C<CB>, C<CW>, C<CF>, C<CL>, C<N>, C<L>,
-or C<REF>.
-C<$itemnumber> is the item involved, if pertinent; otherwise, it
-should be the empty string.
-
-=cut
-
-#'
-# FIXME: In Koha 3.0 , the only account adjustment 'types' passed to this function
-# are :
-# 'C' = CREDIT
-# 'FOR' = FORGIVEN (Formerly 'F', but 'F' is taken to mean 'FINE' elsewhere)
-# 'N' = New Card fee
-# 'F' = Fine
-# 'A' = Account Management fee
-# 'M' = Sundry
-# 'L' = Lost Item
-#
-
-sub manualinvoice {
- my ( $borrowernumber, $itemnum, $desc, $type, $amount, $note ) = @_;
- my $manager_id = 0;
- $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
- my $dbh = C4::Context->dbh;
- my $notifyid = 0;
- my $insert;
- my $accountno = getnextacctno($borrowernumber);
- my $amountleft = $amount;
-
- if ( ( $type eq 'L' )
- or ( $type eq 'F' )
- or ( $type eq 'A' )
- or ( $type eq 'N' )
- or ( $type eq 'M' ) )
- {
- $notifyid = 1;
- }
-
- if ( $itemnum ) {
- $desc .= ' ' . $itemnum;
- my $sth = $dbh->prepare(
- 'INSERT INTO accountlines
- (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding, itemnumber,notify_id, note, manager_id)
- VALUES (?, ?, now(), ?,?, ?,?,?,?,?,?)');
- $sth->execute($borrowernumber, $accountno, $amount, $desc, $type, $amountleft, $itemnum,$notifyid, $note, $manager_id) || return $sth->errstr;
- } else {
- my $sth=$dbh->prepare("INSERT INTO accountlines
- (borrowernumber, accountno, date, amount, description, accounttype, amountoutstanding,notify_id, note, manager_id)
- VALUES (?, ?, now(), ?, ?, ?, ?,?,?,?)"
- );
- $sth->execute( $borrowernumber, $accountno, $amount, $desc, $type,
- $amountleft, $notifyid, $note, $manager_id );
- }
-
- if ( C4::Context->preference("FinesLog") ) {
- logaction("FINES", 'CREATE',$borrowernumber,Dumper({
- action => 'create_fee',
- borrowernumber => $borrowernumber,
- accountno => $accountno,
- amount => $amount,
- description => $desc,
- accounttype => $type,
- amountoutstanding => $amountleft,
- notify_id => $notifyid,
- note => $note,
- itemnumber => $itemnum,
- manager_id => $manager_id,
- }));
- }
-
- return 0;
-}
-
-sub getcharges {
- my ( $borrowerno, $timestamp, $accountno ) = @_;
- my $dbh = C4::Context->dbh;
- my $timestamp2 = $timestamp - 1;
- my $query = "";
- my $sth = $dbh->prepare(
- "SELECT * FROM accountlines WHERE borrowernumber=? AND accountno = ?"
- );
- $sth->execute( $borrowerno, $accountno );
-
- my @results;
- while ( my $data = $sth->fetchrow_hashref ) {
- push @results,$data;
- }
- return (@results);
-}
-
-sub ModNote {
- my ( $accountlines_id, $note ) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare('UPDATE accountlines SET note = ? WHERE accountlines_id = ?');
- $sth->execute( $note, $accountlines_id );
-}
-
-sub getcredits {
- my ( $date, $date2 ) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare(
- "SELECT * FROM accountlines,borrowers
- WHERE amount < 0 AND accounttype not like 'Pay%' AND accountlines.borrowernumber = borrowers.borrowernumber
- AND timestamp >=TIMESTAMP(?) AND timestamp < TIMESTAMP(?)"
- );
-
- $sth->execute( $date, $date2 );
- my @results;
- while ( my $data = $sth->fetchrow_hashref ) {
- $data->{'date'} = $data->{'timestamp'};
- push @results,$data;
- }
- return (@results);
-}
-
-
-sub getrefunds {
- my ( $date, $date2 ) = @_;
- my $dbh = C4::Context->dbh;
-
- my $sth = $dbh->prepare(
- "SELECT *,timestamp AS datetime
- FROM accountlines,borrowers
- WHERE (accounttype = 'REF'
- AND accountlines.borrowernumber = borrowers.borrowernumber
- AND date >=? AND date <?)"
- );
-
- $sth->execute( $date, $date2 );
-
- my @results;
- while ( my $data = $sth->fetchrow_hashref ) {
- push @results,$data;
-
- }
- return (@results);
-}
-
-sub ReversePayment {
- my ( $accountlines_id ) = @_;
- my $dbh = C4::Context->dbh;
-
- my $sth = $dbh->prepare('SELECT * FROM accountlines WHERE accountlines_id = ?');
- $sth->execute( $accountlines_id );
- my $row = $sth->fetchrow_hashref();
- my $amount_outstanding = $row->{'amountoutstanding'};
-
- if ( $amount_outstanding <= 0 ) {
- $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = amount * -1, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?');
- $sth->execute( $accountlines_id );
- } else {
- $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = 0, description = CONCAT( description, " Reversed -" ) WHERE accountlines_id = ?');
- $sth->execute( $accountlines_id );
- }
-
- if ( C4::Context->preference("FinesLog") ) {
- my $manager_id = 0;
- $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
-
- if ( $amount_outstanding <= 0 ) {
- $row->{'amountoutstanding'} *= -1;
- } else {
- $row->{'amountoutstanding'} = '0';
- }
- $row->{'description'} .= ' Reversed -';
- logaction("FINES", 'MODIFY', $row->{'borrowernumber'}, Dumper({
- action => 'reverse_fee_payment',
- borrowernumber => $row->{'borrowernumber'},
- old_amountoutstanding => $row->{'amountoutstanding'},
- new_amountoutstanding => 0 - $amount_outstanding,,
- accountlines_id => $row->{'accountlines_id'},
- accountno => $row->{'accountno'},
- manager_id => $manager_id,
- }));
-
- }
-
-}
-
-=head2 recordpayment_selectaccts
-
- recordpayment_selectaccts($borrowernumber, $payment,$accts);
-
-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. C<$accts> is an array ref to a list of
-accountnos which the payment can be recorded against
-
-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_selectaccts {
- my ( $borrowernumber, $amount, $accts, $note ) = @_;
-
- my $dbh = C4::Context->dbh;
- my $newamtos = 0;
- my $accdata = q{};
- my $branch = C4::Context->userenv->{branch};
- my $amountleft = $amount;
- my $manager_id = 0;
- $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;
- my $sql = 'SELECT * FROM accountlines WHERE (borrowernumber = ?) ' .
- 'AND (amountoutstanding<>0) ';
- if (@{$accts} ) {
- $sql .= ' AND accountlines_id IN ( ' . join ',', @{$accts};
- $sql .= ' ) ';
- }
- $sql .= ' ORDER BY date';
- # begin transaction
- my $nextaccntno = getnextacctno($borrowernumber);
-
- # get lines with outstanding amounts to offset
- my $rows = $dbh->selectall_arrayref($sql, { Slice => {} }, $borrowernumber);
-
- # offset transactions
- my $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding= ? ' .
- 'WHERE accountlines_id=?');
-
- my @ids;
- for my $accdata ( @{$rows} ) {
- if ($amountleft == 0) {
- last;
- }
- if ( $accdata->{amountoutstanding} < $amountleft ) {
- $newamtos = 0;
- $amountleft -= $accdata->{amountoutstanding};
- }
- else {
- $newamtos = $accdata->{amountoutstanding} - $amountleft;
- $amountleft = 0;