Crazy Horse
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.
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.
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>';
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you post entire code and how you are calculating $paymentsTotal
ASKER
Sorry Rajesh, I think I found the problem.