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

	$db->bind('so_id', $so_id);
	$db->bind('user_id', $_SESSION['user_id']);
	
	$data = $db->query("SELECT * FROM shipping_order_unread WHERE so_id = :so_id and user_id = :user_id");

	return $data[0]['status'];
}
function getAllPOWithoutSO(){
	global $db;
	
	$db->bind('company_id', $_SESSION['company_id']);
	$data = $db->query("SELECT * FROM purchase_orders WHERE po_id not in (select po_id from shipping_orders where po_id is not null) and company_id = :company_id");
	
	$PO  = "";
    foreach ($data as $key => $value) {
		 $PO .= "<option value='" . $value['po_id']  . "' " .  ($_GET['id'] == $value['po_id'] ? "selected" : "")  .">" . $value['marking_no'] . '</option>';
	}
	return $PO;
}

function getLastForwarder(){
	global $db;
	$db->bind('company_id', $_SESSION['company_id']);
	$db->bind('branch_id', $_SESSION['branch_id']);
	$data = $db->query("SELECT * FROM shipping_orders WHERE company_id = :company_id and branch_id = :branch_id  ORDER BY so_id DESC LIMIT 1");
	
	if(count($data > 0)){
		return $data[0]['forwarder_id'];
	}else{
		return "";
	}
	
	
}

function getSOTotalCtn($so_id){

	global $db;
	$db->bind('so_id', $so_id);
	$data = $db->query("SELECT SUM(packaging_quantity) as total FROM shipping_order_items WHERE so_id = :so_id");
	
	return $data[0]['total'];
}

function checkPOExist($so_id){
	global $db;
	$db->bind('so_id', $so_id);
	$data = $db->query("SELECT * FROM shipping_orders WHERE so_id = :so_id");
	if($data[0]['po_id'] == null){
		return false;
	}else{
		return true;
	}
	
}

function getSOTotalCBM($so_id){
	global $db;
	
    $db->bind('so_id', $so_id);
	$data = $db->query("SELECT SUM(packaging_cbm) as total FROM shipping_order_items WHERE so_id = :so_id");
	
	return $data[0]['total'];
}

function getSOTotalWeight($so_id){
	global $db;
	
    $db->bind('so_id', $so_id);
	$data = $db->query("SELECT SUM(packaging_gross) as total FROM shipping_order_items WHERE so_id = :so_id");
	
	return $data[0]['total'];
}
function getSOTotalCarton($so_id){
	global $db;
	
    $db->bind('so_id', $so_id);
	$data = $db->query("SELECT SUM(packaging_quantity) as total FROM shipping_order_items WHERE so_id = :so_id");
	
	return $data[0]['total'];
}

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


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

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

function isSOMine($so_id){
	global $db;
	$company_id = getCompany($_SESSION['user_id']);
	
	$db->bind('so_id', $so_id);
	$data = $db->query("SELECT * FROM shipping_orders WHERE so_id = :so_id");
	if ($company_id  == $data[0]['forwarder_id'] || $company_id  == $data[0]['company_id']){
		return true;
	}else{
		return false;
	}
}


function getModalPackingList($so_id){
	global $db;
	$html = "";
	$x = 1;
		$html .= '<div class="portlet light">
				<table id="packinglists" class="table packing-list table-striped table-hover table-bordered dataTable">
					<thead>
						<tr>
							<th> No   		      </th>
							<th> Description	  </th>
							<th> Model            </th>
							<th> Qty              </th>
							<th> UOM              </th>
							<th> Height           </th>
							<th> Width            </th>
							<th> Depth            </th>
							<th> Gross		      </th>
							<th> Nett		      </th>
							<th> Photo   	      </th>
						</tr>
					</thead>
					<tbody>';
					$SOItems = getSOItems($so_id);
					foreach($SOItems as $key => $SOItem){
						$html .= '<tr>
									<td>'. $x .'</td>
									<td>'. $SOItem['description'] .'</td>
									<td>'. $SOItem['model_no'] .'</td>
									<td>'. $SOItem['packaging_quantity'] .'</td>
									<td>'. $SOItem['packaging_type'] .'</td>
									<td>'. $SOItem['packaging_height'] .'</td>
									<td>'. $SOItem['packaging_width'] .'</td>
									<td>'. $SOItem['packaging_depth'] .'</td>
									<td>'. $SOItem['packaging_gross'] .'</td>
									<td>'. $SOItem['packaging_nett'] .'</td>
									<td><img style="max-width: 100px; max-height: 100px;" src="'.($SOItem['image'] <> null ? $SOItem['image'] : "http://www.placehold.it/200/EFEFEF/AAAAAA&amp;text=no+image") .'"></td>
								  </tr>';
						$x++;
												
					}
			$html .= '</tbody></table>';
					
	return $html;
}
function getModalMarking($so_id){
	global $db;
	$html = "";
	$urlencode = urlencode("http://". $_SERVER['HTTP_HOST'] . "/shipping-order/details/" . $so_id);
	
	$html .= '<center><table class="marking" id="markings"  frame="box" style="margin-left: 15px; margin-right: 15px; margin-top: 15px; margin-bottom: 15px; min-width:95%; ">
				<tr>
					<td rowspan="4"><img src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl='. $urlencode .'&choe=UTF-8" style="max-width:80%;"></th>
					<td colspan="4"><h1 style="margin-top: 0px;margin-bottom: 0px;" align="center"><strong>'.getSOData($so_id, 'marking_no').'</strong></h1></th>
				</tr>
				<tr>
					<td colspan="2" style="padding-right: 20px;">仓库地址</td>
					<td colspan="2">'.getSOData($so_id, "warehouse_address").'</td>
				</tr>
				<tr>
					<td colspan="2" style="padding-right: 20px;">收件人</td>
					<td colspan="2">'.get_warehouse(getSOData($so_id, "warehouse_id"), "contact_person").'</td>
				</tr>
				<tr>
					<td colspan="2" style="padding-right: 20px;">联系号码</td>
					<td colspan="2">'.get_warehouse(getSOData($so_id, "warehouse_id"), "contact_person_no").'</td>
				</tr>
			</table> </center>';
	return $html;
} 