<?php

global $db;
ini_set("display_errors", "0");
error_reporting(E_ALL);
$requestData= $_REQUEST;
$columns = array( 
	0 => 'created_on',
	1 => 'delivery_type',
	2 => 'so_id',
	6 => 'user_id',
	8 => 'status',
	
);

$data = $db->query("SELECT COUNT(delivery_arrangement_items.id) as jumlah FROM delivery_arrangement_items left join delivery_arrangements on delivery_arrangement_items.delivery_arrangement_id = delivery_arrangements.id WHERE delivery_arrangements.company_id = '" . $_SESSION['company_id'] . "'");

$count = $data[0]["jumlah"];
$totalData = $count;
$totalFiltered = $totalData;

$sql = "SELECT delivery_arrangements.delivery_type as delivery_type, delivery_arrangements.delivery_date as delivery_date, delivery_arrangements.driver_contact_person as driver_contact_person, delivery_arrangements.driver_contact_person_no as driver_contact_person_no, delivery_arrangements.vehicle_no as vehicle_no, delivery_arrangements.courier_service_name as courier_service_name, delivery_arrangements.tracking_no as tracking_no, delivery_arrangements.warehouse_id as warehouse_id, delivery_arrangements.warehouse_address as warehouse_address, delivery_arrangements.warehouse_zip as warehouse_zip, delivery_arrangements.warehouse_city as warehouse_city, delivery_arrangements.warehouse_state as warehouse_state, delivery_arrangements.warehouse_country as warehouse_country, delivery_arrangements.warehouse_contact_person as warehouse_contact_person, delivery_arrangements.warehouse_contact_person_no as warehouse_contact_person_no, delivery_arrangements.user_id as user_id, delivery_arrangements.company_id as company_id, delivery_arrangements.status as status, delivery_arrangements.created_on as created_on, delivery_arrangement_items.so_id as so_id  FROM delivery_arrangement_items left join delivery_arrangements on delivery_arrangement_items.delivery_arrangement_id = delivery_arrangements.id WHERE delivery_arrangements.company_id = '".$_SESSION['company_id']."'";



$data = $db->query($sql);
$totalFiltered = count($data);
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
$data = $db->query($sql);
$qweqwe = array();
foreach ($data as $key => $value) {
	if($value["status"] == "1"){
		$status = "Delivery <br/> Request <br/> <span class='sent'>Sent</span>";
	}elseif($value["status"] == "2"){
		$status = "Delivery <br/> Request <br/> Accepted"; 
	}elseif($value["status"] == "3"){
		$status = "Delivery <br/> Request <br/> Completed";
	}elseif($value["status"] == "4"){
		$status = "Order <br/> Closed";
	}elseif($value["status"] == "5"){
		$status = "Delivery <br/> Request <br/> Rejected";
	}
	if($value["delivery_type"] == "1"){
		$delivery_type = "Inhouse Services"; 
		$delivery_details = "Delivery Date : " . date('d/m/Y', strtotime($value["delivery_date"]));
		$delivery_details .= "<br/>Vehicle No : " . $value["vehicle_no"];
		$delivery_details .= "<br/>Contact Person : " . $value["driver_contact_person"];
		$delivery_details .= "<br/>Contact Person No :" . $value["driver_contact_person_no"];			
	}elseif($value["delivery_type"] == "2"){
		$delivery_type = "Courier Services";
		$delivery_details = "Courier Service : " . $value["courier_service_name"];
		$delivery_details .= "<br/>Courier Service : " . $value["tracking_no"];
	}elseif($value["delivery_type"] == "3"){
		$delivery_type = "Outsource Services";
		$delivery_details = "Delivery Date : " . date('d/m/Y', strtotime($value["delivery_date"]));
		$delivery_details .= "<br/>Vehicle No : " . $value["vehicle_no"];
		$delivery_details .= "<br/>Contact Person : " . $value["driver_contact_person"];
		$delivery_details .= "<br/>Contact Person No : " . $value["driver_contact_person_no"];
	}elseif($value["delivery_type"] == "4"){
		$delivery_type = "Self Collect";
		$delivery_details = "Warehouse : " . get_warehouse($value["vehicle_no"], "name");
		$delivery_details .= "<br/>Address : " . $value["warehouse_address"] . ", ". $value["warehouse_zip"]. " ". $value["warehouse_city"]. ", ". $value["warehouse_state"]. ", ". $value["warehouse_country"];
		$delivery_details .= "<br/>Contact Person : " . $value["warehouse_contact_person"];
		$delivery_details .= "<br/>Contact Person No : " . $value["warehouse_contact_person_no"];
	}
	
	
    $nestedData = array(); 
	
	$nestedData[] = date('d/m/Y', strtotime($value["created_on"]));
	$nestedData[] = "Type : " . $delivery_type . "</br>". $delivery_details;
	$nestedData[] = getSOData($value["so_id"], "marking_no");
	$nestedData[] = getSAData($value["so_id"], "shipment_no");
	$nestedData[] = getSOData($value["so_id"], "delivery_address");  
	$nestedData[] = getSAData($value["so_id"], "container_no");
	$nestedData[] = $status;
	$nestedData[] = '<div class="btn-group">
						<button class="btn green btn-xs btn-circle btn-outline dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false"> Actions
							<i class="fa fa-angle-down"></i>
						</button>
						<ul class="dropdown-menu" role="menu">
							<li>
								<a href="/shipping-order/details/'.$value["so_id"].'#arrangements">
									<i class="icon-docs"></i> Details </a>
							</li>                                                                
						</ul>
					</div>';
	
	$qweqwe[] = $nestedData;
}
$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),  // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $qweqwe   // total data array
			);
echo json_encode($json_data);
