【问题标题】:Paid multiple invoice in one query一次查询支付多张发票
【发布时间】:2019-03-07 02:39:11
【问题描述】:

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

例如,客户总共有 6 张“赊账”发票,总金额为 155 美元。当客户即将付款或已支付(例如 135 美元)时,代码必须减去每张发票的总和从最早的到完成金额,这可能会导致三件事:

  1. 取消欠款总额。
  2. 发票上的剩余金额(按照前面的示例,支付了四张发票,第五张发票剩余 5 美元, 第六总是有他的 20 美元待处理)
  3. 或支付全部款项,其余部分作为借方用于未来的其他购买。

我有一个表格,可以跟踪每个客户的所有待处理发票、金额、付款和一行以了解它是待付款、部分付款、已付款还是以客户为受益人的借方。

当我进行更新时,而不是一一进行,相同的支付价值被保存在所有“赊账”发票中......我觉得我迷路了

我希望你能帮助我。

这是更新后的脚本:

      $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();

【问题讨论】:

  • 这一行:$percibido = $_POST['abono'] 应该在您的循环之外 - 您需要在贷记每一行时减少付款中剩余的金额。
  • 不工作,总是在每张发票中减去相同的债务
  • @Jerry ,忘记之前的评论......你给了我解决方案!谢谢你。如果可以,请添加解决方案以给您积分并标记为已解决

标签: php mysql pdo sql-update


【解决方案1】:

这一行:

$percibido = $_POST['abono']

应该在您的循环之外 - 您需要在贷记每一行时减少付款中剩余的金额。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    相关资源
    最近更新 更多