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

asked on

How to reduce the total after every iteration and check if value is enough to pay invoice

I have a $paymentsTotal variable which is the sum of all payments. I then want to loop through all outstanding invoices and reduce the $paymentsTotal variable for each outstanding amount. If the payment amount is enough to cover invoice 1, it must be marked as paid, if the same for invoice2, mark as paid etc.

I have this code which works to an extent.

                foreach ($invoices as $invoice) {
                   $something = $paymentTotal - $paymentTotal -= $invoice->amount;
                   if($paymentTotal >= $something) {
                       echo $invoice->amount . ' - PAID <br>';
                   }
                }

Open in new window


The problem is that if for example I have:

invoice 1 = 550
Invoice 2 = 400

If I input 500 it should fail on invoice 1 but pass on invoice 2 and mark it is paid. But this doesn't happen. I have to input a minimum of 550 for it to pass and 950 for both to pass. Ideally if I enter 500 it should mark Invoice 2 as paid as 500 is more than 400.
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America 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
Can you post entire code and how you  are calculating $paymentsTotal
Avatar of Crazy Horse

ASKER

Sorry Rajesh, I think I found the problem.