<?php

global $db;
$requestData= $_REQUEST;

	$cc = 1;

	$columns = array( 
	0 => 'po_id',
	1 => 'created_on',
	2 => 'order_no',
	3 => 'marking',
	4 => 'supplier_company_name',
	5 => 'forwarder_id'
	);
	
	if($_SESSION['group_id'] == 1){
		$data = $db->query("SELECT COUNT(company_id) as jumlah FROM purchase_orders WHERE forwarder_id = '" . $_SESSION['company_id'] . "'");
	}else{
		$data = $db->query("SELECT COUNT(company_id) as jumlah FROM purchase_orders WHERE company_id = '" . $_SESSION['company_id'] . "'");
	}
	$count = $data[0]["jumlah"];
	$totalData = $count;
	$totalFiltered = $totalData;

	if(get_data($_SESSION['user_id'], "group_id") == 1){
		$sql = "SELECT * FROM purchase_orders WHERE forwarder_id = '".$_SESSION['company_id']."'";
	}else{
		$sql = "SELECT * FROM purchase_orders 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(); 
		
		$nestedData[] = $cc;
		$nestedData[] = date('d/m/Y', strtotime($value["created_on"]));
		$nestedData[] = $value["order_no"];
		$nestedData[] = $value["marking"];
		$nestedData[] = $value["supplier_company_name"];
		$nestedData[] = getCompanyData($value['forwarder_id'], "company_id", "name");
		$nestedData[] = "(".$value["order_currency"] . ")" . number_format((float)getPOTotal($value["po_id"]), 2, '.', '');
		$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 pull-left" role="menu">
									<li>
										<a href="/purchase-order/details/'.$value["po_id"].'">
											Details </a>
									</li>  
									<li>
										<a href="/messenger/order/'.getRoomIDFromPO($value["po_id"]).'">Chat</a>
									</li>                                                              
								</ul>
							</div>';
		
		$qweqwe[] = $nestedData;
		
		$cc++;
	}
	$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);