| Lines 72-78
          patron.
      
      
        Link Here | 
        
          | 72 |  | 72 |  | 
        
          | 73 | Record payment by a patron. C<$borrowernumber> is the patron's | 73 | Record payment by a patron. C<$borrowernumber> is the patron's | 
        
          | 74 | borrower number. C<$payment> is a floating-point number, giving the | 74 | borrower number. C<$payment> is a floating-point number, giving the | 
          
            
              | 75 | amount that was paid.  | 75 | amount that was paid. | 
        
          | 76 |  | 76 |  | 
        
          | 77 | Amounts owed are paid off oldest first. That is, if the patron has a | 77 | Amounts owed are paid off oldest first. That is, if the patron has a | 
        
          | 78 | $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment | 78 | $1 fine from Feb. 1, another $1 fine from Mar. 1, and makes a payment | 
  
    | Lines 113-124
          sub recordpayment {
      
      
        Link Here | 
        
          | 113 |             $newamtos   = $accdata->{'amountoutstanding'} - $amountleft; | 113 |             $newamtos   = $accdata->{'amountoutstanding'} - $amountleft; | 
        
          | 114 |             $amountleft = 0; | 114 |             $amountleft = 0; | 
        
          | 115 |         } | 115 |         } | 
          
            
              | 116 |         my $thisacct = $accdata->{accountno}; | 116 |         my $thisacct = $accdata->{accountlinesid}; | 
        
          | 117 |         my $usth     = $dbh->prepare( | 117 |         my $usth     = $dbh->prepare( | 
        
          | 118 |             "UPDATE accountlines SET amountoutstanding= ? | 118 |             "UPDATE accountlines SET amountoutstanding= ? | 
          
            
              | 119 |      WHERE (borrowernumber = ?) AND (accountno=?)" | 119 |      WHERE (accountlinesid = ?)" | 
        
          | 120 |         ); | 120 |         ); | 
          
            
              | 121 |         $usth->execute( $newamtos, $borrowernumber, $thisacct ); | 121 |         $usth->execute( $newamtos, $thisacct ); | 
        
          | 122 |         $usth->finish; | 122 |         $usth->finish; | 
        
          | 123 | #        $usth = $dbh->prepare( | 123 | #        $usth = $dbh->prepare( | 
        
          | 124 | #            "INSERT INTO accountoffsets | 124 | #            "INSERT INTO accountoffsets | 
  
    | Lines 144-150
          sub recordpayment {
      
      
        Link Here | 
        
          | 144 |  | 144 |  | 
        
          | 145 | =head2 makepayment | 145 | =head2 makepayment | 
        
          | 146 |  | 146 |  | 
          
            
              | 147 |   &makepayment($borrowernumber, $acctnumber, $amount, $branchcode); | 147 |   &makepayment($accountlinesid, $borrowernumber, $acctnumber, $amount, $branchcode); | 
        
          | 148 |  | 148 |  | 
        
          | 149 | Records the fact that a patron has paid off the entire amount he or | 149 | Records the fact that a patron has paid off the entire amount he or | 
        
          | 150 | she owes. | 150 | she owes. | 
  
    | Lines 165-171
          sub makepayment {
      
      
        Link Here | 
        
          | 165 |     #here we update both the accountoffsets and the account lines | 165 |     #here we update both the accountoffsets and the account lines | 
        
          | 166 |     #updated to check, if they are paying off a lost item, we return the item | 166 |     #updated to check, if they are paying off a lost item, we return the item | 
        
          | 167 |     # from their card, and put a note on the item record | 167 |     # from their card, and put a note on the item record | 
          
            
              | 168 |     my ( $borrowernumber, $accountno, $amount, $user, $branch ) = @_; | 168 |     my ( $accountlinesid, $borrowernumber, $accountno, $amount, $user, $branch ) = @_; | 
        
          | 169 |     my $dbh = C4::Context->dbh; | 169 |     my $dbh = C4::Context->dbh; | 
        
          | 170 |     my $manager_id = 0; | 170 |     my $manager_id = 0; | 
        
          | 171 |     $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;  | 171 |     $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv;  | 
  
    | Lines 173-182
          sub makepayment {
      
      
        Link Here | 
        
          | 173 |     # begin transaction | 173 |     # begin transaction | 
        
          | 174 |     my $nextaccntno = getnextacctno($borrowernumber); | 174 |     my $nextaccntno = getnextacctno($borrowernumber); | 
        
          | 175 |     my $newamtos    = 0; | 175 |     my $newamtos    = 0; | 
          
            
              | 176 |     my $sth = | 176 |     my $sth         = $dbh->prepare("SELECT * FROM accountlines WHERE accountlinesid=?"); | 
            
              | 177 |       $dbh->prepare( | 177 |     $sth->execute( $accountlinesid ); | 
            
              | 178 |         "SELECT * FROM accountlines WHERE  borrowernumber=? AND accountno=?"); |  |  | 
            
              | 179 |     $sth->execute( $borrowernumber, $accountno ); | 
        
          | 180 |     my $data = $sth->fetchrow_hashref; | 178 |     my $data = $sth->fetchrow_hashref; | 
        
          | 181 |     $sth->finish; | 179 |     $sth->finish; | 
        
          | 182 |  | 180 |  | 
  
    | Lines 185-206
          sub makepayment {
      
      
        Link Here | 
        
          | 185 |             $dbh->prepare( | 183 |             $dbh->prepare( | 
        
          | 186 |                 "UPDATE accountlines | 184 |                 "UPDATE accountlines | 
        
          | 187 |                     SET amountoutstanding = 0, description = 'Payment,thanks' | 185 |                     SET amountoutstanding = 0, description = 'Payment,thanks' | 
          
            
              | 188 |                     WHERE borrowernumber = ? | 186 |                     WHERE accountlinesid = ? | 
            
              | 189 |                     AND accountno = ? |  |  | 
        
          | 190 |                 " | 187 |                 " | 
        
          | 191 |             ); | 188 |             ); | 
          
            
              | 192 |         $udp->execute($borrowernumber, $accountno ); | 189 |         $udp->execute($accountlinesid); | 
        
          | 193 |         $udp->finish; | 190 |         $udp->finish; | 
        
          | 194 |     }else{ | 191 |     }else{ | 
        
          | 195 |         my $udp = 		 | 192 |         my $udp = 		 | 
        
          | 196 |             $dbh->prepare( | 193 |             $dbh->prepare( | 
        
          | 197 |                 "UPDATE accountlines | 194 |                 "UPDATE accountlines | 
        
          | 198 |                     SET amountoutstanding = 0 | 195 |                     SET amountoutstanding = 0 | 
          
            
              | 199 |                     WHERE borrowernumber = ? | 196 |                     WHERE accountlinesid = ? | 
            
              | 200 |                     AND accountno = ? |  |  | 
        
          | 201 |                 " | 197 |                 " | 
        
          | 202 |             ); | 198 |             ); | 
          
            
              | 203 |         $udp->execute($borrowernumber, $accountno ); | 199 |         $udp->execute($accountlinesid); | 
        
          | 204 |         $udp->finish; | 200 |         $udp->finish; | 
        
          | 205 |  | 201 |  | 
        
          | 206 |          # create new line | 202 |          # create new line | 
  
    | Lines 227-232
          sub makepayment {
      
      
        Link Here | 
        
          | 227 |     if ( $data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L' ) { | 223 |     if ( $data->{'accounttype'} eq 'Rep' || $data->{'accounttype'} eq 'L' ) { | 
        
          | 228 |         C4::Circulation::ReturnLostItem( $borrowernumber, $data->{'itemnumber'} ); | 224 |         C4::Circulation::ReturnLostItem( $borrowernumber, $data->{'itemnumber'} ); | 
        
          | 229 |     } | 225 |     } | 
            
              |  |  | 226 |     my $sthr = $dbh->prepare("SELECT max(accountlinesid) AS lastinsertid FROM accountlines"); | 
            
              | 227 |     $sthr->execute(); | 
            
              | 228 |     my $datalastinsertid = $sthr->fetchrow_hashref; | 
            
              | 229 |     $sthr->finish; | 
            
              | 230 |     return $datalastinsertid->{'lastinsertid'}; | 
        
          | 230 | } | 231 | } | 
        
          | 231 |  | 232 |  | 
        
          | 232 | =head2 getnextacctno | 233 | =head2 getnextacctno | 
  
    | Lines 254-271
          sub getnextacctno ($) {
      
      
        Link Here | 
        
          | 254 |  | 255 |  | 
        
          | 255 | =head2 fixaccounts (removed) | 256 | =head2 fixaccounts (removed) | 
        
          | 256 |  | 257 |  | 
          
            
              | 257 |   &fixaccounts($borrowernumber, $accountnumber, $amount); | 258 |   &fixaccounts($accountlinesid, $borrowernumber, $accountnumber, $amount); | 
        
          | 258 |  | 259 |  | 
        
          | 259 | #' | 260 | #' | 
        
          | 260 | # FIXME - I don't understand what this function does. | 261 | # FIXME - I don't understand what this function does. | 
        
          | 261 | sub fixaccounts { | 262 | sub fixaccounts { | 
          
            
              | 262 |     my ( $borrowernumber, $accountno, $amount ) = @_; | 263 |     my ( $accountlinesid, $borrowernumber, $accountno, $amount ) = @_; | 
        
          | 263 |     my $dbh = C4::Context->dbh; | 264 |     my $dbh = C4::Context->dbh; | 
        
          | 264 |     my $sth = $dbh->prepare( | 265 |     my $sth = $dbh->prepare( | 
          
            
              | 265 |         "SELECT * FROM accountlines WHERE borrowernumber=? | 266 |         "SELECT * FROM accountlines WHERE accountlinesid=?" | 
            
              | 266 |      AND accountno=?" |  |  | 
        
          | 267 |     ); | 267 |     ); | 
          
            
              | 268 |     $sth->execute( $borrowernumber, $accountno ); | 268 |     $sth->execute( $accountlinesid ); | 
        
          | 269 |     my $data = $sth->fetchrow_hashref; | 269 |     my $data = $sth->fetchrow_hashref; | 
        
          | 270 |  | 270 |  | 
        
          | 271 |     # FIXME - Error-checking | 271 |     # FIXME - Error-checking | 
  
    | Lines 277-284
          sub fixaccounts {
      
      
        Link Here | 
        
          | 277 |         UPDATE  accountlines | 277 |         UPDATE  accountlines | 
        
          | 278 |         SET     amount = '$amount', | 278 |         SET     amount = '$amount', | 
        
          | 279 |                 amountoutstanding = '$outstanding' | 279 |                 amountoutstanding = '$outstanding' | 
          
            
              | 280 |         WHERE   borrowernumber = $borrowernumber | 280 |         WHERE   accountlinesid = $accountlinesid | 
            
              | 281 |           AND   accountno = $accountno |  |  | 
        
          | 282 | EOT | 281 | EOT | 
        
          | 283 | 	# FIXME: exceedingly bad form.  Use prepare with placholders ("?") in query and execute args. | 282 | 	# FIXME: exceedingly bad form.  Use prepare with placholders ("?") in query and execute args. | 
        
          | 284 | } | 283 | } | 
  
    | Lines 456-467
          sub fixcredit {
      
      
        Link Here | 
        
          | 456 |             $newamtos   = $accdata->{'amountoutstanding'} - $amountleft; | 455 |             $newamtos   = $accdata->{'amountoutstanding'} - $amountleft; | 
        
          | 457 |             $amountleft = 0; | 456 |             $amountleft = 0; | 
        
          | 458 |         } | 457 |         } | 
          
            
              | 459 |         my $thisacct = $accdata->{accountno}; | 458 |         my $thisacct = $accdata->{accountlinesid}; | 
        
          | 460 |         my $usth     = $dbh->prepare( | 459 |         my $usth     = $dbh->prepare( | 
        
          | 461 |             "UPDATE accountlines SET amountoutstanding= ? | 460 |             "UPDATE accountlines SET amountoutstanding= ? | 
          
            
              | 462 |      WHERE (borrowernumber = ?) AND (accountno=?)" | 461 |      WHERE (accountlinesid = ?)" | 
        
          | 463 |         ); | 462 |         ); | 
          
            
              | 464 |         $usth->execute( $newamtos, $borrowernumber, $thisacct ); | 463 |         $usth->execute( $newamtos, $thisacct ); | 
        
          | 465 |         $usth->finish; | 464 |         $usth->finish; | 
        
          | 466 |         $usth = $dbh->prepare( | 465 |         $usth = $dbh->prepare( | 
        
          | 467 |             "INSERT INTO accountoffsets | 466 |             "INSERT INTO accountoffsets | 
  
    | Lines 495-506
          sub fixcredit {
      
      
        Link Here | 
        
          | 495 |             $newamtos   = $accdata->{'amountoutstanding'} - $amountleft; | 494 |             $newamtos   = $accdata->{'amountoutstanding'} - $amountleft; | 
        
          | 496 |             $amountleft = 0; | 495 |             $amountleft = 0; | 
        
          | 497 |         } | 496 |         } | 
          
            
              | 498 |         my $thisacct = $accdata->{accountno}; | 497 |         my $thisacct = $accdata->{accountlinesid}; | 
        
          | 499 |         my $usth     = $dbh->prepare( | 498 |         my $usth     = $dbh->prepare( | 
        
          | 500 |             "UPDATE accountlines SET amountoutstanding= ? | 499 |             "UPDATE accountlines SET amountoutstanding= ? | 
          
            
              | 501 |      WHERE (borrowernumber = ?) AND (accountno=?)" | 500 |      WHERE (accountlinesid = ?)" | 
        
          | 502 |         ); | 501 |         ); | 
          
            
              | 503 |         $usth->execute( $newamtos, $borrowernumber, $thisacct ); | 502 |         $usth->execute( $newamtos, $thisacct ); | 
        
          | 504 |         $usth->finish; | 503 |         $usth->finish; | 
        
          | 505 |         $usth = $dbh->prepare( | 504 |         $usth = $dbh->prepare( | 
        
          | 506 |             "INSERT INTO accountoffsets | 505 |             "INSERT INTO accountoffsets | 
  
    | Lines 561-572
          sub refund {
      
      
        Link Here | 
        
          | 561 |         } | 560 |         } | 
        
          | 562 |  | 561 |  | 
        
          | 563 |         #     print $amountleft; | 562 |         #     print $amountleft; | 
          
            
              | 564 |         my $thisacct = $accdata->{accountno}; | 563 |         my $thisacct = $accdata->{accountlinesid}; | 
        
          | 565 |         my $usth     = $dbh->prepare( | 564 |         my $usth     = $dbh->prepare( | 
        
          | 566 |             "UPDATE accountlines SET amountoutstanding= ? | 565 |             "UPDATE accountlines SET amountoutstanding= ? | 
          
            
              | 567 |      WHERE (borrowernumber = ?) AND (accountno=?)" | 566 |      WHERE (accountlinesid = ?)" | 
        
          | 568 |         ); | 567 |         ); | 
          
            
              | 569 |         $usth->execute( $newamtos, $borrowernumber, $thisacct ); | 568 |         $usth->execute( $newamtos, $thisacct ); | 
        
          | 570 |         $usth->finish; | 569 |         $usth->finish; | 
        
          | 571 |         $usth = $dbh->prepare( | 570 |         $usth = $dbh->prepare( | 
        
          | 572 |             "INSERT INTO accountoffsets | 571 |             "INSERT INTO accountoffsets | 
  
    | Lines 599-608
          sub getcharges {
      
      
        Link Here | 
        
          | 599 | } | 598 | } | 
        
          | 600 |  | 599 |  | 
        
          | 601 | sub ModNote { | 600 | sub ModNote { | 
          
            
              | 602 |     my ( $borrowernumber, $accountno, $note ) = @_; | 601 |     my ( $accountlinesid, $note ) = @_; | 
        
          | 603 |     my $dbh = C4::Context->dbh; | 602 |     my $dbh = C4::Context->dbh; | 
          
            
              | 604 |     my $sth = $dbh->prepare('UPDATE accountlines SET note = ? WHERE borrowernumber = ? AND accountno = ?'); | 603 |     my $sth = $dbh->prepare('UPDATE accountlines SET note = ? WHERE accountlinesid = ?'); | 
            
              | 605 |     $sth->execute( $note, $borrowernumber, $accountno ); | 604 |     $sth->execute( $note, $accountlinesid ); | 
        
          | 606 | } | 605 | } | 
        
          | 607 |  | 606 |  | 
        
          | 608 | sub getcredits { | 607 | sub getcredits { | 
  
    | Lines 647-667
          sub getrefunds {
      
      
        Link Here | 
        
          | 647 | } | 646 | } | 
        
          | 648 |  | 647 |  | 
        
          | 649 | sub ReversePayment { | 648 | sub ReversePayment { | 
          
            
              | 650 |   my ( $borrowernumber, $accountno ) = @_; | 649 |     my ( $accountlinesid ) = @_; | 
            
              | 651 |   my $dbh = C4::Context->dbh; | 650 |     my $dbh = C4::Context->dbh; | 
            
              | 652 |    | 651 |  | 
            
              | 653 |   my $sth = $dbh->prepare('SELECT amountoutstanding FROM accountlines WHERE borrowernumber = ? AND accountno = ?'); | 652 |     my $sth = $dbh->prepare('SELECT amountoutstanding FROM accountlines WHERE accountlinesid = ?'); | 
            
              | 654 |   $sth->execute( $borrowernumber, $accountno ); | 653 |     $sth->execute( $accountlinesid ); | 
            
              | 655 |   my $row = $sth->fetchrow_hashref(); | 654 |     my $row = $sth->fetchrow_hashref(); | 
            
              | 656 |   my $amount_outstanding = $row->{'amountoutstanding'}; | 655 |     my $amount_outstanding = $row->{'amountoutstanding'}; | 
            
              | 657 |    | 656 |  | 
            
              | 658 |   if ( $amount_outstanding <= 0 ) { | 657 |     if ( $amount_outstanding <= 0 ) { | 
            
              | 659 |     $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = amount * -1, description = CONCAT( description, " Reversed -" ) WHERE borrowernumber = ? AND accountno = ?'); | 658 |         $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = amount * -1, description = CONCAT( description, " Reversed -" ) WHERE accountlinesid = ?'); | 
            
              | 660 |     $sth->execute( $borrowernumber, $accountno ); | 659 |         $sth->execute( $accountlinesid ); | 
            
              | 661 |   } else { | 660 |     } else { | 
            
              | 662 |     $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = 0, description = CONCAT( description, " Reversed -" ) WHERE borrowernumber = ? AND accountno = ?'); | 661 |         $sth = $dbh->prepare('UPDATE accountlines SET amountoutstanding = 0, description = CONCAT( description, " Reversed -" ) WHERE accountlinesid = ?'); | 
            
              | 663 |     $sth->execute( $borrowernumber, $accountno ); | 662 |         $sth->execute( $accountlinesid ); | 
            
              | 664 |   } | 663 |     } | 
        
          | 665 | } | 664 | } | 
        
          | 666 |  | 665 |  | 
        
          | 667 | =head2 recordpayment_selectaccts | 666 | =head2 recordpayment_selectaccts | 
  
    | Lines 703-709
          sub recordpayment_selectaccts {
      
      
        Link Here | 
        
          | 703 |  | 702 |  | 
        
          | 704 |     # offset transactions | 703 |     # offset transactions | 
        
          | 705 |     my $sth     = $dbh->prepare('UPDATE accountlines SET amountoutstanding= ? ' . | 704 |     my $sth     = $dbh->prepare('UPDATE accountlines SET amountoutstanding= ? ' . | 
          
            
              | 706 |         'WHERE (borrowernumber = ?) AND (accountno=?)'); | 705 |         'WHERE accountlinesid=?'); | 
        
          | 707 |     for my $accdata ( @{$rows} ) { | 706 |     for my $accdata ( @{$rows} ) { | 
        
          | 708 |         if ($amountleft == 0) { | 707 |         if ($amountleft == 0) { | 
        
          | 709 |             last; | 708 |             last; | 
  
    | Lines 716-723
          sub recordpayment_selectaccts {
      
      
        Link Here | 
        
          | 716 |             $newamtos   = $accdata->{amountoutstanding} - $amountleft; | 715 |             $newamtos   = $accdata->{amountoutstanding} - $amountleft; | 
        
          | 717 |             $amountleft = 0; | 716 |             $amountleft = 0; | 
        
          | 718 |         } | 717 |         } | 
          
            
              | 719 |         my $thisacct = $accdata->{accountno}; | 718 |         my $thisacct = $accdata->{accountlinesid}; | 
            
              | 720 |         $sth->execute( $newamtos, $borrowernumber, $thisacct ); | 719 |         $sth->execute( $newamtos, $thisacct ); | 
        
          | 721 |     } | 720 |     } | 
        
          | 722 |  | 721 |  | 
        
          | 723 |     # create new line | 722 |     # create new line | 
  
    | Lines 732-738
          sub recordpayment_selectaccts {
      
      
        Link Here | 
        
          | 732 | # makepayment needs to be fixed to handle partials till then this separate subroutine | 731 | # makepayment needs to be fixed to handle partials till then this separate subroutine | 
        
          | 733 | # fills in | 732 | # fills in | 
        
          | 734 | sub makepartialpayment { | 733 | sub makepartialpayment { | 
          
            
              | 735 |     my ( $borrowernumber, $accountno, $amount, $user, $branch ) = @_; | 734 |     my ( $accountlinesid, $borrowernumber, $accountno, $amount, $user, $branch ) = @_; | 
        
          | 736 |     my $manager_id = 0; | 735 |     my $manager_id = 0; | 
        
          | 737 |     $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; | 736 |     $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; | 
        
          | 738 |     if (!$amount || $amount < 0) { | 737 |     if (!$amount || $amount < 0) { | 
  
    | Lines 744-755
          sub makepartialpayment {
      
      
        Link Here | 
        
          | 744 |     my $newamtos    = 0; | 743 |     my $newamtos    = 0; | 
        
          | 745 |  | 744 |  | 
        
          | 746 |     my $data = $dbh->selectrow_hashref( | 745 |     my $data = $dbh->selectrow_hashref( | 
          
            
              | 747 |         'SELECT * FROM accountlines WHERE  borrowernumber=? AND accountno=?',undef,$borrowernumber,$accountno); | 746 |         'SELECT * FROM accountlines WHERE  accountlinesid=?',undef,$accountlinesid); | 
        
          | 748 |     my $new_outstanding = $data->{amountoutstanding} - $amount; | 747 |     my $new_outstanding = $data->{amountoutstanding} - $amount; | 
        
          | 749 |  | 748 |  | 
          
            
              | 750 |     my $update = 'UPDATE  accountlines SET amountoutstanding = ?  WHERE   borrowernumber = ? ' | 749 |     my $update = 'UPDATE  accountlines SET amountoutstanding = ?  WHERE   accountlinesid = ? '; | 
            
              | 751 |     . ' AND   accountno = ?'; | 750 |     $dbh->do( $update, undef, $new_outstanding, $accountlinesid); | 
            
              | 752 |     $dbh->do( $update, undef, $new_outstanding, $borrowernumber, $accountno); |  |  | 
        
          | 753 |  | 751 |  | 
        
          | 754 |     # create new line | 752 |     # create new line | 
        
          | 755 |     my $insert = 'INSERT INTO accountlines (borrowernumber, accountno, date, amount, ' | 753 |     my $insert = 'INSERT INTO accountlines (borrowernumber, accountno, date, amount, ' |