У меня есть логическая проблема в том, как мне выполнить это в PHP, ваша поддержка очень ценится.
У меня есть два массива:
- Счет
- Оплата
Массивы выглядят так
$invoice = array
(
array("id"=>1,"amt"=>2000.00),
array("id"=>2,"amt"=>3000.00),
array("id"=>3,"amt"=>4000.00)
);
$payments = array
(
array("id"=>11111,"amt"=>500.00),
array("id"=>22222,"amt"=>3000.00),
array("id"=>33333,"amt"=>4500.00),
array("id"=>44444,"amt"=>1500.00)
);
UPDATE
это код, который я написал.
// count how many elements available
$sc = count($iSetl);
$ic = count($payments);
/*
Three types of flags
$flag = 0 // first execution
$flag = 1 // settlement amount is higher than the payment amount
$flag = 2 // payment amount is higher than the settlement amount
*/
// available balances after transaction
$setBal = 0; // settlement balance
$paymBal = 0; // payment balance
$flag = 0;
// our priority is to disburse all payments
// therefore our initial loop would be payments
foreach($payments as $p){
// round 2:
// settlement value is higher there for we are approaching to the next invoice
if($flag == 1){
if($setBal > 0){
if($setData <= $p["amt"]){
$setData = $p["amt"] - $setData;
//echo($setData)."<br />";
$output = "invoice number #".$s["id"]." | invoice value ".$s["amt"]." Payment Number " . $p["id"] . " - Payment Amount ". $p["amt"]."<br />";
$output .= "after settlement the invoice value is ". $setData;
echo($output);
echo "<br />the payment value is more than the invoice settlement <br /><br />";
$setBal = $setData;
$flag = 2;
}
}
}
// start looping each settlements
foreach($iSetl as $s){
// check if this is the initial transaction
if($flag == 0 ){
// if the settlement balance is equal to 0 (or less)
// or settlement is complete
if($setBal <= 0) {
// this case: Settlement amount is more than the payment amount therefore
// deduct the settlement from payment
$setData = $p["amt"];
$setData = $s["amt"] - $setData;
$output = "invoice number #".$s["id"]." | invoice value ".$s["amt"]." Payment Number " . $p["id"] . " - Payment Amount ". $p["amt"]."<br />";
$output .= "after settlement the invoice value is ". $setData;
echo($output);
//echo($setData)."<br />";
echo "<br />the invoice settlement value is more than the payment value<br /><br />";
// assign the balance amount to be settled
$setBal = $setData;
// set the flag as 1 since the settlement value is higher than payment value
$flag = 1;
}
}
if($flag == 2 ){
// if the settlement balance is equal to 0 (or less)
// or settlement is complete
if($setBal > 0) {
echo("stage3 <br />");
// this case: Settlement amount is more than the payment amount therefore
// deduct the settlement from payment
$setData = $s["amt"] - $setData;
//echo($setData)."<br />";
$output = "invoice number #".$s["id"]." | invoice value ".$s["amt"]." Payment Number " . $p["id"] . " - Payment Amount ". $p["amt"]."<br />";
$output .= "after settlement the invoice value is ". $setData;
echo($output);
echo "<br />the invoice settlement value is more than the payment value<br /><br />";
// assign the balance amount to be settled
$setBal = $setData;
// set the flag as 1 since the settlement value is higher than payment value
$flag = 1;
}
}
}
}
Я хочу рассчитать счет-фактуру «amt» с платежом «amt» в тех случаях, когда счет-фактура «amt» превышает сумму «amt» платежа, а в других случаях платеж превышает сумму счета-фактуры.
Результат должен отображаться как
действительно запутался, как подойти к этому, помогите пожалуйста.