<?php
    include_once 'opendb.php';
    $filter="";
    $delman_code=0;
    
     $CompID=0;
    if (isset($_POST["comp_code"]))
    {
        $Qry="SELECT comp_id from company where comp_code like '". $_POST["comp_code"] ."'";
        if ($result = mysqli_query ($conn,$Qry)){
            if (mysqli_num_rows($result) > 0){
                if($srow = $result->fetch_assoc()) {
					$CompID=$srow["comp_id"];
                }
            }else {
             $response["message"] = "Error: co code not found";
                die( json_encode($response));
            }
            
        } else {
             $response["message"] = "Error: in select co id ";
                die( json_encode($response));
            
        }
        $filter.=" where comp_id=" . $CompID;
        if ($CompID==0){
            $response["message"] = "Error: in getting co id ";
                die( json_encode($response));
        }
    } else {
         $response["message"] = "Error: Web Co code required";
            // echo no users JSON
            die( json_encode($response));
    }
    
    
    if(isset($_POST["delman_code"])){
        $delman_code=$_POST["delman_code"];
    } else {
         $response["message"] = "Error: Deliverer Code required.";
            // echo no users JSON
            die( json_encode($response));
    }
  
    $Qry="SELECT DM.webentry_no,DM.entry_date,DM.voucher_no,DM.remark,DM.delman_code,DM.delman_name,DM.route_code,DM.route_name,DM.is_finished";
    $Qry.= ",DS.sub_code,DS.order_no,DS.amount,DS.route_slno,DS.notes,DS.is_delivered,DS.pkey"; 
    $Qry.= ",SB.sub_name,SB.address,SB.mobile,SB.latitude,SB.longitude,SB.area_name from delivery_sub DS "; 
    $Qry.= " join delivery_master DM on DS.WEntry_No=DM.WebEntry_No join subsidiary SB on DS.sub_code=SB.Sub_code ";
    $Qry.= " where DM.comp_id=$CompID and DS.compid=$CompID and SB.comp_id=$CompID and DM.delman_code='$delman_code'";
    $cutDate=date('Y-m-d',strtotime('-5 day'));
    $Qry.= " and Entry_date > '$cutDate' order by webentry_no,route_slno";
   // file_put_contents("temp.txt", $Qry);
   // die($Qry);
    if ($result = mysqli_query ($conn,$Qry)){
        if (mysqli_num_rows($result) > 0){
            $rows = array();
            while ($r = mysqli_fetch_assoc($result)) {   
                 $rows[] = $r;
            }
            echo json_encode($rows);
         } else {   
            $response["message"] = "NoData";
            echo json_encode($response);
         }
    }
    else {
        $response["message"] = "Error:".$Qry;
        echo json_encode($response);
    }
 $conn->close();
 mysqli_free_result($result);
?>