<?php

global $db;
ini_set("display_errors", "0");
error_reporting(E_ALL);
$requestData= $_REQUEST;
$columns = array( 
	0 => 'receive_on',
	3 => 'warehouse_receive_note_no',
	4 => 'transport_company_name',
	5 => 'consigment_note_no',
	
);
if($_SESSION['company_id'] == 0){
	$data = $db->query("SELECT COUNT(id) as jumlah FROM warehouse_arrangements WHERE user_id = '" . $_SESSION['user_id'] . "'");
}else{
	$data = $db->query("SELECT COUNT(id) as jumlah FROM warehouse_arrangements WHERE company_id = '" . $_SESSION['company_id'] . "'");
}

$count = $data[0]["jumlah"];
$totalData = $count;
$totalFiltered = $totalData;
if($_SESSION['company_id'] == 0){
	$sql = "SELECT * FROM warehouse_arrangements WHERE user_id = '".$_SESSION['user_id']."'";
}else{
	$sql = "SELECT * FROM warehouse_arrangements WHERE 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) {
    $nestedData = array(); 
	if($value["status"] == 1){
		$status = "Decision Requested";
	}elseif($value["status"] == 2){
		$status = "Warehouse Received";
	}elseif($value["status"] == 3){
		$status = "Decision Declined";
	}else{
		$status = "Warehouse Received";
	}
	$nestedData[] = $value["receive_on"];
	$nestedData[] = getSOData($value["so_id"], "warehouse_name");
	$nestedData[] = $value["warehouse_receive_note_no"];
	$nestedData[] = getSOData($value["so_id"], "marking_no");
	$nestedData[] = $value["transport_company_name"];
	$nestedData[] = $value["consignment_note_no"];
	$nestedData[] = getWATotalCarton($value["id"]);
	$nestedData[] = getWATotalCBM($value["id"]);
	$nestedData[] = getWATotalWeight($value["id"]);
	$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="/warehouse-arrangements/details/'.$value["id"].'">
									<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);
