Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

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

Avatar of Nicolas Lecanu
Nicolas Lecanu
Flag of Guadeloupe image

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 ?
Avatar of Crazy Horse

ASKER

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.
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

ASKER CERTIFIED SOLUTION
Avatar of Nicolas Lecanu
Nicolas Lecanu
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome, thank you!
You are welcome  :-)