一付减少两张票据的总数

时间:2018-11-29 05:27:11

标签: php mysql

我有类似的情况,我有2个音符,第一个总计100.000,第二个50.000。 我支付120.000。所以我想第一音符变为0,第二音符变为30.000。 他先付款,直到0,然后再付款。我曾尝试for和foreach更新。 我的代码的这一部分

$query      = "SELECT GROUP_CONCAT(DCTN_NBR) as DCTN_NBR, SUM(TND_AMT) as TND_AMT, GROUP_CONCAT(ORD_NBR_RT) as ORD_NBR_RT FROM $RTL.RTL_STK_DCTN WHERE DEL_NBR = 0";
// echo $query;
$result     = mysqli_query($connect, $query);
while($row  = mysqli_fetch_array($result))
{
    $totdctnamt =  $row['TND_AMT'];
   // this result is total of paid = 120.000
}


foreach ($PrnNbr as $index => $PrnNbrStrs) 
    {
     $querystknote  = " SELECT TOT_AMT FROM $RTL.RTL_STK_HEAD WHERE ORD_NBR =".$PrnNbr[$index];
            $resultstknote = mysqli_query($connect, $querystknote);
            $rowstknote    = mysqli_fetch_array($resultstknote);
            $rowstknote['TOT_AMT'];
            // this result from 2 note 100.000 and 50.000
            $querytotrem  = " UPDATE $RTL.RTL_STK_HEAD SET TOT_REM = 0 WHERE ORD_NBR =".$PrnNbr[$index];
            // echo $querytotrem;
    }

现在如何将第一个nota更新为0,然后将其余的第一张钞票更新为第二张钞票

1 个答案:

答案 0 :(得分:1)

在循环中,请始终减少总数,如果当前音符小于总数,则将当前音符设置为零。

直到总数变得小于当前的票据数量,然后执行另一种方法:设置票据数量=票据-数量,并更新总数。

foreach ($PrnNbr as $index => $PrnNbrStrs) 
{
 $querystknote  = " SELECT TOT_AMT FROM $RTL.RTL_STK_HEAD WHERE ORD_NBR =".$PrnNbr[$index];
        $resultstknote = mysqli_query($connect, $querystknote);
        $rowstknote    = mysqli_fetch_array($resultstknote);

        $note = $rowstknote['TOT_AMT'];   // assuming this will be 100,000 then 50,000
        if($totdctnamt>=$note) {
          $totdctnamt -= $note;      //reduce the total amount
                                     // it was 120k
                                     // now it is 120k-100k = 20k
          $note = 0;
        } else {
           $note -= $totdctnamt;     // the second iteration, 
                                     // note is 50k, and total is 20k
                                     // so note will be 50k-20k = 30k

          $totdctnamt = 0;           // to avoid other notes being affected

        }

        // finally, we update the table
        $querytotrem  = " UPDATE $RTL.RTL_STK_HEAD SET TOT_REM = $note WHERE ORD_NBR =".$PrnNbr[$index];
        // echo $querytotrem;
}