<?php
include("../config.php");
$query = "SELECT * FROM lr_mst WHERE ";
if($_POST["is_date_search"] == "yes"){
  $query .= 'DATE(created_datetime) BETWEEN "'.$_POST["start_date"].'" AND "'.$_POST["end_date"].'" AND ';
}

if($_POST["vendor_id"] != ""){
  $query .= 'consignor_name ="'.$_POST['vendor_id'].'" AND ';
}


if(isset($_POST["search"]["value"])){
  $query .= '
    (id LIKE "%'.$_POST["search"]["value"].'%" 
    OR id LIKE "%'.$_POST["search"]["value"].'%")';
}

if(isset($_POST["order"])){
  $query .= 'GROUP BY id ';
}
$query1 = '';
if($_POST["length"] != -1){
  $query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}

$number_filter_row = mysqli_num_rows(mysqli_query($conn, $query));
$result        = mysqli_query($conn, $query . $query1);
$data          = array();
$cou               = 1;

while($row = mysqli_fetch_array($result)){
  $sub_array   = array();
  $con_nor      = "SELECT * FROM `vendor_master` WHERE `id` = '".$row["consignor_name"]."'";
  $con_nor_res  = mysqli_query($conn,$con_nor);
  $con_nor_rows = mysqli_fetch_array($con_nor_res);

  $con_nee      = "SELECT * FROM `vendor_master` WHERE `id` = '".$row["consignee_name"]."'";
  $con_nee_res  = mysqli_query($conn,$con_nee);
  $con_nee_rows = mysqli_fetch_array($con_nee_res);

  $truck      = "SELECT * FROM `lhc` WHERE `lr_no` = '".$row["lr_no"]."'";
  $truck_res  = mysqli_query($conn,$truck);
  $truck_rows = mysqli_fetch_array($truck_res);

  $bill      = "SELECT * FROM `billing` WHERE `lr_no` = '".$row["lr_no"]."'";
  $bill_res  = mysqli_query($conn,$bill);
  $bill_rows = mysqli_fetch_array($bill_res);

  $sub_array[]  = $cou++;
  $sub_array[]  = $row["lr_no"];
  $sub_array[]  = date("d-M-Y", strtotime($row["l_date"]));
  $sub_array[]  = $con_nor_rows["name"];
  $sub_array[]  = $con_nee_rows["name"];
  if($row["pcs"] == ''){
    $sub_array[]  = '-';  
  }else{
    $sub_array[]  = $row["pcs"];
  }
  
  if($truck_rows["lhc_no"] == ''){
    $sub_array[]  = '-';  
  }else{
    $sub_array[]  = $truck_rows["lhc_no"];
  }

  if($truck_rows["truck_no"] == ''){
    $sub_array[]  = '-';  
  }else{
    $sub_array[]  = $truck_rows["truck_no"];
  }
  if($row["freight"] == ''){
    $sub_array[]  = '-';  
  }else{
    $sub_array[]  = $row["freight"];
  }
  if($row["rate"] == ''){
    $sub_array[]  = '-';  
  }else{
    $sub_array[]  = $row["rate"];
  }
  if($row["billing_party"] == 'CONSIGNEE'){
    $sub_array[]  = $con_nee_rows["name"];
  }else{
    $sub_array[]  = $con_nor_rows["name"];
  }
  $sub_array[]  = $con_nee_rows["name"];
  $sub_array[]  = $row["total_amnt"];

  if($bill_rows["bill_date"] == ''){
    $sub_array[]  = '-';  
  }else{
    $sub_array[]  = date("d-M-Y", strtotime($bill_rows["bill_date"]));
  }

  if($bill_rows["payment_mode"] == 'CASH'){
    $sub_array[]  = 'CASH';  
  }else if($bill_rows["payment_mode"] == 'CREDIT'){
     $sub_array[]  = 'CREDIT';
  }else{
     $sub_array[]  = 'CHEQUE';
  }
  if($row["remark"] == ''){
    $sub_array[]  = '-';  
  }else{
    $sub_array[]  = $row["remark"];
  }
  
  $data[]       = $sub_array;
}

function get_all_data($conn){
  $query   = "SELECT * FROM lr_mst";
  $result  = mysqli_query($conn, $query);
  return mysqli_num_rows($result);
}

$output = array(
 "draw"         => intval($_POST["draw"]),
 "recordsTotal"   =>  get_all_data($conn),
 "recordsFiltered"  => $number_filter_row,
 "data"         => $data
);

echo json_encode($output);

?>