<?php
    include_once 'opendb.php';
    
    if (isset($_POST["WebEno"]) && isset($_POST["Status"]) && isset($_POST["SubCode"]) && isset($_POST["OrderNo"]))
    {
        $WebEno=$_POST["WebEno"] ;
        $SubCode=$_POST["SubCode"] ;
        $OrderNo=$_POST["OrderNo"] ;
        $Status=$_POST["Status"] ;
       
        $sql = "update delivery_sub set is_Delivered=".$Status." , deliv_time=now() WHERE WEntry_No = $WebEno and sub_code like '$SubCode' and Order_No=$OrderNo";
        
       
       if ($conn->query($sql) === TRUE)
       {
          $mess="OK";
       } else
       {
          $mess= "Error: " . $sql . "<br>" . $conn->error;
       } 
       $Qry="SELECT is_Delivered FROM delivery_sub WHERE WEntry_No = $WebEno and is_Delivered=0";
   
        $result = $conn->query($Qry) or die(json_encode($response));
        if ($result->num_rows == 0) {  
              $sql = "update delivery_master set is_Finished=1 WHERE WebEntry_No = $WebEno ";
        } else {
             $sql = "update delivery_master set is_Finished=0 WHERE WebEntry_No = $WebEno ";
        }
       $conn->query($sql);
       mysqli_free_result($result);
    }
    else
    {
        $mess= "Error : parameters missing!";
    }
    echo $mess;
    $conn->close();
?>