php库存和订单管理系统

时间:2017-06-30 06:57:01

标签: php mysql

for($i=0; $i<count($_POST['order_item']); $i++) 
{   

$order_date=$_POST['order_date'];
$customer_name=$_POST['customer_name'];
$salesman_name=$_POST['salesman_name'];
$area_code=$_POST['area_code'];
$product_name=$_POST['product_name'][$i];
$item_qty=$_POST['item_qty'][$i];//product actual quality
$order_item=$_POST['order_item'][$i]; //order item number 
$billed_qty=$_POST['itqty'][$i]; // product billed quantity editable
$prod_mrps=$_POST['mrps'][$i];
$prices=$_POST['price'][$i];
$discount=$_POST['qty'][$i];
$amount=$_POST['total'][$i];    
$prod_qty = $_POST['prod_qty'][$i];//available quantity
$batch_no = $_POST['batch_no'][$i];//batch number
$qtyout = 0;
$result = $dbo->query("SELECT `batch_no`, `prod_id`, `prod_name`, `total_qty` FROM `sm_product_batch` where `prod_name` = '$product_name' and `batch_status` = 'Active_batch' order by batch_no asc");
        while ($row = $result->fetch(PDO::FETCH_ASSOC))
        {
            if($billed_qty > 0)
            {
                $batchout = 0;
                $rem = max($row['total_qty']-$billed_qty,0);
                if($rem == 0)
                    $batchout = $row['total_qty']; //This means there are no items of this cost remaining
                else
                    $batchout = $billed_qty; //This means there are items remaining and therefore our next loop (within the while) will check for the next expensive item

                $billed_qty -= $batchout;
                $qtyout += $batchout;
                $sql = "Update sm_product_batch set total_qty = (total_qty - $batchout) where prod_name='".$product_name."' AND batch_no = ".$row["batch_no"];
                $dbo->query($sql);
                $sql1 = "Update sm_product_batch SET batch_status=CASE WHEN total_qty='0' THEN 'Inactive_batch' ELSE 'Active_batch' END where prod_name='".$product_name."'";
                $dbo->query($sql1);
            }
        }
        $sql = "INSERT INTO sm_invoice (order_id,invoice_id,product_name,customer_name,salesman_name,order_item,area_code,order_date,invoice_date,item_qty,billed_qty,batch_no,prod_mrp,price,spl_dis,total) VALUES ('".$order_id."','".$order_id."','".$product_name."','".$customer_name."','".$salesman_name."','".$order_item."','".$area_code."','".$order_date."','".$invoice_date."','".$item_qty."','".$_POST['itqty'][$i]."','".$batch_no."','".$prod_mrps."','".$prices."','".$discount."','".$amount."')";
        $dbo->query($sql);
}
  

这是我用批量和发票表更新库存的代码。此代码仅适用于我的第一个产品。需要对订单的所有产品进行此操作。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

for($i=0; $i<count($_POST['order_item']); $i++) 
{   
$order_date=$_POST['order_date'];
$customer_name=$_POST['customer_name'];
$salesman_name=$_POST['salesman_name'];
$area_code=$_POST['area_code'];
$product_name=$_POST['product_name'][$i];
$item_qty=$_POST['item_qty'][$i];//product actual quality
$order_item=$_POST['order_item'][$i]; //order item number 
$billed_qty=$_POST['itqty'][$i]; // product billed quantity editable
$prod_mrps=$_POST['mrps'][$i];
$prices=$_POST['price'][$i];
$discount=$_POST['qty'][$i];
$amount=$_POST['total'][$i];    
$prod_qty = $_POST['prod_qty'][$i];//available quantity
$batch_no = $_POST['batch_no'][$i];//batch number
$qtyout = 0;
$result = $dbo->query("SELECT `batch_no`, `prod_id`, `prod_name`, `total_qty` FROM `sm_product_batch` where `prod_name` = '$product_name' and `batch_status` = 'Active_batch' order by batch_no asc");
        while ($row = $result->fetch(PDO::FETCH_ASSOC))
        {
            if($billed_qty > 0)
            {
                $batchout = 0;
                $rem = max($row['total_qty']-$billed_qty,0);
                if($rem == 0)
                    $batchout = $row['total_qty']; //This means there are no items of this cost remaining
                else
                    $batchout = $billed_qty; //This means there are items remaining and therefore our next loop (within the while) will check for the next expensive item
                $billed_qty -= $batchout;
                $qtyout += $batchout;
                $sql = "Update sm_product_batch set total_qty = (total_qty - $batchout) where prod_name='".$product_name."' AND batch_no = ".$row["batch_no"];
                $dbo->query($sql);
                $sql1 = "Update sm_product_batch SET batch_status=CASE WHEN total_qty='0' THEN 'Inactive_batch' ELSE 'Active_batch' END where prod_name='".$product_name."'";
                $dbo->query($sql1);
        $sql = "INSERT INTO sm_invoice (order_id,invoice_id,product_name,customer_name,salesman_name,order_item,area_code,order_date,invoice_date,item_qty,billed_qty,batch_no,prod_mrp,price,spl_dis,total) VALUES ('".$order_id."','".$order_id."','".$_POST['product_name'][$i]."','".$customer_name."','".$salesman_name."','".$_POST['order_item'][$i]."','".$area_code."','".$order_date."','".$invoice_date."','".$item_qty."','".$batchout."','".$row["batch_no"]."','".$_POST['mrps'][$i]."','".$_POST['price'][$i]."','".$_POST['qty'][$i]."','".$_POST['total'][$i]."')";
        $dbo->query($sql);
            }
        }
}
  

经过大量调试后找到答案。   感谢您的回复。