<?php
function getWAOverallStatusFromSOID($so_id){
	global $db;

	$db->bind('so_id', $so_id);
	$data = $db->query("SELECT * FROM warehouse_arrangements WHERE so_id = :so_id");
	$overallstatus = "";
	$cawaiting = 0;
	foreach ($data as $key => $value) {
		$status = $value['status'];
		if($status == 1){
			$cawaiting++;
		}
	}
	if($cawaiting > 0){
		return "Waiting for Decision";
	}else{
		return "Warehouse Received";
	}
	
}
function getItemBalance($so_item_id, $so_id){
	global $db;
	
	$db->bind('so_item_id', $so_item_id);
	$data = $db->query("SELECT SUM(warehouse_arrangement_items.actual_ctn) as total FROM warehouse_arrangement_items join warehouse_arrangements on warehouse_arrangement_items.warehouse_arrangement_id = warehouse_arrangements.id WHERE warehouse_arrangement_items.so_item_id = :so_item_id and warehouse_arrangements.status <> 3");
	
	if($data[0]['total'] <> NULL){
		return getSOItem($so_item_id, "packaging_quantity") - $data[0]['total'];
	}else{
		return getSOItem($so_item_id, "packaging_quantity");
	}
}

function getItemBalanceFromSOID($so_id){
	global $db;
	$warehouse_arrangement_id = getWAIDs($so_id);
	$warehouse_arrangement_id = implode(',', array_map(function ($entry) {return $entry['id'];}, $warehouse_arrangement_id));
	if($warehouse_arrangement_id <> "" || $warehouse_arrangement_id <> NULL){
		$db->bind('warehouse_arrangement_id', $warehouse_arrangement_id);
		$data = $db->query("SELECT SUM(actual_ctn) as total FROM warehouse_arrangement_items WHERE warehouse_arrangement_id IN (".$warehouse_arrangement_id .")");
		if($data[0]['total'] <> NULL){
			return getSOTotalCtn($so_id) - $data[0]['total'];
		}else{
			return getSOTotalCtn($so_id);
		}
	}else{
		return getSOTotalCtn($so_id);
	}
}

function getAllSOWithoutWarehouse(){
	global $db;
	
	$db->bind('forwarder_id', $_SESSION['company_id']);
	$data = $db->query("SELECT * FROM shipping_orders WHERE forwarder_id = :forwarder_id");
	
	$PO  = "";
    foreach ($data as $key => $value) {
		// Fix display all shipping order 
		// if(getItemBalanceFromSOID($value['so_id']) <> 0){
		//	$PO .= "<option value='" . $value['so_id'] . "'>" . $value['order_no'] . " (" .  $value['marking'] . ')</option>';
		//	}
		$PO .= "<option value='" . $value['so_id'] . "'>" . $value['order_no'] . " (" .  $value['marking'] . ')</option>';
			 		 
	}
	return $PO;
}
function getAllSOWithoutWarehouseCount(){
	global $db;
	
	$db->bind('forwarder_id', $_SESSION['company_id']);
	$data = $db->query("SELECT * FROM shipping_orders WHERE forwarder_id = :forwarder_id");
	
	
	return count($data);
}


function getWATotalCBM($warehouse_arrangement_id){
	global $db;
	
    $db->bind('warehouse_arrangement_id', $warehouse_arrangement_id);
	$data = $db->query("SELECT SUM(actual_measurement_ctn) as total FROM warehouse_arrangement_items WHERE warehouse_arrangement_id = :warehouse_arrangement_id");
	
	return $data[0]['total'];
}
function getWATotalWeight($warehouse_arrangement_id){
	global $db;
	
    $db->bind('warehouse_arrangement_id', $warehouse_arrangement_id);
	$data = $db->query("SELECT SUM(actual_weight_ctn) as total FROM warehouse_arrangement_items WHERE warehouse_arrangement_id = :warehouse_arrangement_id");
	
	return $data[0]['total'];
}
function getWATotalCarton($warehouse_arrangement_id){
	global $db;
	
    $db->bind('warehouse_arrangement_id', $warehouse_arrangement_id);
	$data = $db->query("SELECT SUM(actual_ctn) as total FROM warehouse_arrangement_items WHERE warehouse_arrangement_id = :warehouse_arrangement_id");
	
	return $data[0]['total'];
}
function getWAID($so_id){
	global $db;
	
    $db->bind('so_id', $so_id);
	$data = $db->query("SELECT id FROM warehouse_arrangements WHERE so_id = :so_id ORDER BY `warehouse_arrangements`.`id` DESC");
	
	return $data[0]['id'];
}

function getWAIDs($so_id){
	global $db;
	
    $db->bind('so_id', $so_id);
	$data = $db->query("SELECT id FROM warehouse_arrangements WHERE so_id = :so_id and status <> 3 ORDER BY `warehouse_arrangements`.`id` DESC");
	
	return $data;
}

function getWAData($so_id, $data){
	global $db;
	
	$db->bind('so_id', $so_id);
	
	$warehouse_arrangements = $db->query("SELECT * FROM warehouse_arrangements WHERE so_id = :so_id");
	
	return (count($warehouse_arrangements) > 0 ? $warehouse_arrangements[0][$data] : "");
	
}

function getWADatafromWAID($WAID, $data){
	global $db;
	
	$db->bind('WAID', $WAID);
	
	$warehouse_arrangements = $db->query("SELECT * FROM warehouse_arrangements WHERE id = :WAID");
	
	return (count($warehouse_arrangements) > 0 ? $warehouse_arrangements[0][$data] : "");
	
}


function getWAItems($so_id){
	global $db;
	$warehouse_arrangement_id = getWAID($so_id);
	$db->bind('warehouse_arrangement_id', $warehouse_arrangement_id);
	$data = $db->query("SELECT * FROM warehouse_arrangement_items WHERE warehouse_arrangement_id = :warehouse_arrangement_id");
	
    return $data;	
}

function getWAItemsFromWAID($wa_id){
	global $db;
	$warehouse_arrangement_id = $wa_id;
	$db->bind('warehouse_arrangement_id', $warehouse_arrangement_id);
	$data = $db->query("SELECT * FROM warehouse_arrangement_items WHERE warehouse_arrangement_id = :warehouse_arrangement_id");
	
    return $data;	
}


function getWAItem($id, $kol){
	global $db;
	
	$db->bind('id', $id);
	$data = $db->query("SELECT * FROM warehouse_arrangement_items WHERE id = :id");
	
    return $data[0][$kol];	
}