在一个查询中支付多张发票

时间:2018-10-01 23:58:28

标签: php mysql pdo sql-update

当我尝试生成付款(取决于金额)取消“ X”金额发票时,我遇到了问题。

例如,客户总共有6笔“信用”发票,总计155美元。当客户接近付款或已付款(例如135美元)时,代码必须减去每张发票的总额从最早到完成为止,这可能导致三件事:

  
      
  1. 取消欠款总额。
  2.   
  3. 保留在发票上(按照上一个示例,已支付了四张发票,第五张发票剩下$ 5,   第六,他的$ 20待定)
  4.   
  5. 或支付总额,其余的作为借方用于以后的其他购买。
  6.   

我有一个表,可以跟踪每个客户的所有未结发票,金额,付款以及一行信息,以了解它是未结付款,部分付款,已付款还是对客户有利的借方。

当我进行 UPDATE 时,与其一一对应,但要支付的相同值却保存在所有“信用”发票中……我感到迷路了

希望你能帮助我。

这是UPDATED脚本:

      $conn->beginTransaction();

        $id = $_POST['idCliente'];   // Client id

     //  now only select the invoices with pending payments 0, 4, 5, 6

        $sql = $conn->prepare("SELECT COUNT(refId) AS fMora FROM VENTAS WHERE idCliente = :idCliente
        AND contacredito IN (0, 4, 5, 6) AND activo = '1'");
        $sql->bindParam(':idCliente',$id);
        $sql->execute();

   // Now is calling the rows that need to change

        while($row = $sql->fetch(PDO::FETCH_ASSOC))  $fMora = $row['fMora'];
            $sth = $conn->prepare("SELECT idV, refVenta, refId, total, pagado
            FROM VENTAS WHERE idCliente = :idCliente AND contacredito IN (0, 4, 5, 6) AND activo = '1'");
            $sth->bindParam(':idCliente',$id);
            $sth->execute();

   // fMora run if is more or equal than one
            if($fMora >= '1') {

  // each time if are more than 1

                for($i=0; $i< $fMora; $i++){
                    while($row2 = $sth->fetch(PDO::FETCH_ASSOC)) {

                        $percibido = $_POST['abono']; // the client amount
                        $percibido2 = $resta;  // variable with the subtracted of the amount
                        $abona = $percibido - $percibido2;  // variable make an subtract for making each time smaller amount

                        $idV = $row2['idV'];  // table id
                        $refVenta = $row2['refVenta'];  // ref id equal to printed invoice
                        $refId = $row2['refId']; // id for print
                        $total = $row2['total'];  // invoice total
                        $pagado = $row2['pagado'];  //previously paid

   // if the amount is less than the total

                        if($abona < $total && $abona > '0'){ 
                            $paga = $abona;  // add the amount
                            $contaCred = '4';  // change to partial payment

                            $sql = "UPDATE VENTAS SET
                            contacredito = :contacredito,
                            pagado = :pagado
                            WHERE refId = :refId";
                            $stmt = $conn->prepare($sql);
                            $stmt->bindParam(':contacredito', $contaCred, PDO::PARAM_STR);
                            $stmt->bindParam(':pagado', $paga, PDO::PARAM_STR);
                            $stmt->bindParam(':refId', $refId, PDO::PARAM_STR);
                            $stmt->execute();
                        } 

     // if is greater of the total amount

                        else if($abona >= $total){
                            $paga = $total;  // add the same value of total in pagado
                            $pagado = $total-$pagado;  // make the subtraction to generate the amount that will be subtracted
                            $contaCred = '1';  // change to paid
                            $resta = $abona-$pagado;  // make the subtraction to generate the new amount to the new invoice

                            $sql = "UPDATE VENTAS SET
                            contacredito = :contacredito,
                            pagado = :pagado
                            WHERE refId = :refId";
                            $stmt = $conn->prepare($sql);
                            $stmt->bindParam(':contacredito', $contaCred, PDO::PARAM_STR);
                            $stmt->bindParam(':pagado', $paga, PDO::PARAM_STR);
                            $stmt->bindParam(':refId', $refId, PDO::PARAM_STR);
                            $stmt->execute();
                        };


                    }
                }
            }

            $conn->commit();

1 个答案:

答案 0 :(得分:0)

此行:

$percibido = $_POST['abono']

应该超出您的循环范围-您需要减少每一行的贷记金额。