<?php

global $db;
ini_set("display_errors", "0");
error_reporting(E_ALL);
$requestData= $_REQUEST;
$columns = array( 
	0 => 'so_id',
	1 => 'order_no',
	2 => 'marking_no',
	3 => 'created_on',
	4 => 'supplier_company_name',
	5 => 'warehouse_name'
	
);
$company_id = getCompany($_SESSION['user_id']);

if(get_data($_SESSION['user_id'], "group_id") == 1){
	$data = $db->query("SELECT COUNT(company_id) as jumlah FROM shipping_orders WHERE forwarder_id = '" . $company_id . "'");
}else{
	$data = $db->query("SELECT COUNT(company_id) as jumlah FROM shipping_orders WHERE company_id = '" . $company_id . "'");
}
$count = $data[0]["jumlah"];
$totalData = $count;
$totalFiltered = $totalData;

if(get_data($_SESSION['user_id'], "group_id") == 1){
	$sql = "SELECT * FROM shipping_orders WHERE forwarder_id = '".$company_id."'";
}else{
	$sql = "SELECT * FROM shipping_orders WHERE company_id = '".$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) {
    $nestedData = array(); 
	
	$nestedData[] = $value["so_id"];
	$nestedData[] = $value["order_no"]  . (getSOUnreadBySOID($value["so_id"]) == 0 ? " <span class='badge badge-danger'> new </span>" : "");
	$nestedData[] = $value["marking"];
	$nestedData[] = date('d-m-Y', strtotime($value["created_on"]));
	$nestedData[] = $value["supplier_company_name"]. "<br>". $value["supplier_contact_person"]. "<br>". $value["supplier_contact_person_no"];
	$nestedData[] = $value["warehouse_name"]; 
	$nestedData[] = ( checkPOExist($value["po_id"])? "<a href='/purchase-order/details/".$value["po_id"]."'>Yes" : "No");
	$nestedData[] = getSOTotalCBM($value["so_id"]) . "m³";
	$nestedData[] = getSOTotalWeight($value["so_id"]) . "kg";
	$nestedData[] = getSOTotalCarton($value["so_id"]);
	$nestedData[] = '<div class="btn-group">
						<button class="btn green btn-xs btn-circle btn-outline dropdown-toggle" data-toggle="dropdown"> Actions
							<i class="fa fa-angle-down"></i>
						</button>
						<ul class="dropdown-menu" role="menu">
							<li>
								<a href="/shipping-order/details/'.$value["so_id"].'">Details</a>
							</li>
							<li>
								<a href="/track-order?id='.$value["so_id"].'">Track Order</a>
							</li>
							<li>
								<a href="/messenger/order/'.getRoomIDFromSO($value["so_id"]).'">Chat</a>
							</li>
							<li>
								<a class="previewPL" data-id="'.$value["so_id"].'">Preview PL</a>
							</li>
							<li>
								<a class="previewMarking" data-id="'.$value["so_id"].'">Print Marking</a>
							</li>
						</ul>
					</div>';
	
	$qweqwe[] = $nestedData;
}

	$db->query("UPDATE shipping_order_unread set status = 1 where user_id = '". $_SESSION['user_id'] ."'");
$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);
