reduce inputted amount by another amount after every iteration

I have an invoice for 300 and an invoice for 500 therefore my balance is 800. I make a payment of 500 and my balance is therefore 300 and I only pay off 1 invoice out of 2. I want that one invoice payment to be inserted into a table.

I was looping through to see where the invoice amount was less than or equal to the payment amount but the problem is both are true because 300 is less than or equal to 500 and 500 is equal than or less than 500. So, both records are moving to the table instead of one.

I need to check against one, reduce the 500 by that amount and then check again to see if 500 - 300 is enough to pay 500 which it isn't so only one record should be inserted.

            $stmt = $db->prepare("INSERT INTO `payment_link` (`amount`) VALUES (:amount)");
            $stmt->bindParam("amount", $amount, PDO::PARAM_STR);

            foreach ($invoices as $invoice) {
                if ($invoice->amount <= $this->amount) {
                    $amount = $invoice->amount;
                    $stmt->execute();  
                }
            }

Open in new window

LVL 1
Black SulfurAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nicolas LecanuAnalyst developerCommented:
Hi Black Sulfur,

your question is a little bit hard to understand.

you want to separete  2 case ? :
     1 - you make a first invoice of 500 (so you have 300 left) = first invoice
     2 - then another invoice of 300 ( and you have nothing left)

that right ?
Black SulfurAuthor Commented:
Sorry, it's hard to explain in words.

I have 2 invoices for example (could be any number of invoices) and in the database that is 2 rows.

invoice 1: 300
invoice 2: 500

The customer therefore has an outstanding balance of 800.

I then have an input field (amount variable) where I can input any value. So, I input 500.

The 500 is only enough to pay one invoice, not both. So, if the 500 pays off the records in order, it will start at invoice 1 and that invoice 1 needs to be inserted into another table, like a payments table. So, the payments table will have a value of 300 for this user and their balance will reduce by 300.

The invoice for 500 should not move to the payment table because the 500 was not enough to cover both invoices.

My loop however is adding both records because it is just checking the individual amount of each invoice vs the inputted amount ie:

300 is less than or equal to 500 so move the record
loop to next record
500 is less than or equal to 500 so move the record

I shouldn't have it doing that. It should say:

300 is less than or equal to 500 so move the record.
Reduce 500 by 300 = 200.
500 is not less than or equal to 200
Stop the loop.
Black SulfurAuthor Commented:
Perhaps something like this:

            foreach ($invoices as $invoice) {
                
                $test = $this->amount - $invoice->amount;
                if ($test > 0) {
                    $amount = $invoice->amount;
                    $stmt->execute();
                }

Open in new window

 Acronis Global Cyber Summit 2019 in Miami

The Acronis Global Cyber Summit 2019 will be held at the Fontainebleau Miami Beach Resort on October 13–16, 2019, and it promises to be the must-attend event for IT infrastructure managers, CIOs, service providers, value-added resellers, ISVs, and developers.

Nicolas LecanuAnalyst developerCommented:
<?php
$stmt = $db->prepare("INSERT INTO `payment_link` (`amount`) VALUES (:amount)");
$stmt->bindParam("amount", $amount, PDO::PARAM_STR);


foreach ($invoices as $invoice) {
    if ($invoice->amount <= $this->amount) { // 300 <= 500 ? yes 
        $amount = $invoice->amount;
        $this->amount = $this->amount - $invoice->amount; // so 500-300 = 200
        $stmt->execute();
    }
}

Open in new window


in the next loop  $this->amount will be 200

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Black SulfurAuthor Commented:
Awesome, thank you!
Nicolas LecanuAnalyst developerCommented:
You are welcome  :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.