<?php
class log{
	/*********************
	** LOGGER
	*********************/
	public $logger;

	public function dumpLog(){
		
		echo $this->logger;
				
	}
	
	public function debugLog($log = ''){
		$this->logger .= "<script>console.log('".$log."')</script>" ;
	}
	public function slowAndHeavyOperation($text = '') {
		//sleep(1);
		$this->logger .= $this->debugLog(microtime(true).'>>>'.$text);
	}
	/*********************
	** LOGGER end
	*********************/
}
// mysqli escape string
function escapeString($value){
	global $mysqli ;
	$value = trimData($value) ;
	$value = $mysqli->real_escape_string($value) ; 
	return $value ;
}

// check spam email
function spamcheck($field){
	$field=filter_var($field, FILTER_SANITIZE_EMAIL);
	if(filter_var($field, FILTER_VALIDATE_EMAIL)){
		return TRUE;
	}
	else{
		return FALSE;
	}
}

// remove last comma
function removeLastComma($value){
	$value = rtrim(trimData($value), ',') ;
	return $value ;
}

// stripslashes data
function dataFilter($value){
	$value = trimData($value) ;
	$value = stripslashes($value) ;
	//$value = html_entity_decode($value) ;
	return $value ;
}

// function with - if value = null
function dataFilterDash($value, $dash = '-'){
	$value = dataFilter($value) ;
	$value = ($value != '' ? $value : $dash) ;
	return $value ;
}


// entity decode
function entityDecode($value){
	$value = html_entity_decode($value) ;
	return $value ;
}

// trim data
function trimData($value){
	$value = trim($value) ;
	return $value ;
}

// insert database filter string
function resetString($value){
	$value = htmlspecialchars($value, ENT_COMPAT) ;	
	return $value ;
}

// check array is it true
function arrayCheck($array){
	$boolean_array = false ;
	if (count($array) > 0 && is_array($array)){
		$boolean_array = true ;	
	}
	return $boolean_array ;
}


// trash page
function trashPage($page, $mysqli, $query, $trash){
	$multiple_trash = array() ;
	$multiple_trash = $trash ;
	$boolean = true ;
	if (count($multiple_trash) > 0 && is_array($multiple_trash)){
		foreach($multiple_trash as $key => $value){
			if ($value == 1 && $mysqli->query($query . $key)){
				
			}else{
				$boolean = false ;
			}
		}
	}else{
		$boolean = false ;	
	}
	return $boolean ;
}

function checkCookieLogin(){
   // check if user has been remembered
   if ($_COOKIE['system_id'] != '' && $_COOKIE['system_name'] != '' && $_COOKIE['system_permission'] != ''){
		// keep in session
		$_SESSION['system_id'] = $_COOKIE['system_id'] ;
		$_SESSION['system_name'] = $_COOKIE['system_name'] ;
		$_SESSION['system_permission'] = $_COOKIE['system_permission'] ;
		// refresh page to check session
		header('Refresh: 0') ;
		exit ;
   }
}

// return selected category name
function getSelectedCategory($parent, $type, $link){
	global $mysqli, $prefixPost ;

	// default variable
	$sub_category = '' ;
	// remove "<filter-" to "" and change ">" to ","
	$parent = substr_replace(str_replace('<filter-', '', str_replace('>', ',', $parent)), '', -1) ;
	// explode with comma
	$parent = explode(',', $parent) ;
	// check if array exsits
	if (count($parent) > 0 && is_array($parent)){
		// loop all parent category
		foreach($parent as $key => $value){
			// query for category
			$mysqli_category = $mysqli->query("SELECT post_id, post_title FROM $prefixPost
												WHERE post_id = '".$value."' AND post_title != '' AND post_type = '".$type."' AND post_categories = '".$type."' AND post_trash = '0' LIMIT 1") ;

			// check if category exist
			if ($mysqli_category->num_rows > 0){
				// set query as array
				$row_category = $mysqli_category->fetch_array(MYSQLI_ASSOC) ;
				if ($link == 'link'){
					$sub_category .= '<a href="category.php?page_mode=edit&page='.dataFilter($row_category['post_id']).'">'.dataFilter($row_category['post_title']).'</a>, ' ;
				}else{
					$sub_category .= dataFilter($row_category['post_title']).', ' ;
				}
					
			}
		}
	}	
	// remove last string
	return substr_replace(trimData($sub_category), '', -1) ;
}

// get all category via array
function getSelectedCategoryArray($main_type, $parent, $type, $array){
	
	global $mysqli, $prefixPost ;
	
	// default variable
	$sub_category = $content = '' ;
	
	if ( $main_type == 'main' ){
		// remove "<filter-" to "" and change ">" to ","
		$parent = substr_replace(str_replace('<filter-', '', str_replace('>', ',', $parent)), '', -1) ;
		
		// explode with comma
		$temp_parent = explode(',', $parent) ;

	}else{
		$temp_parent[] = $parent ;
	}
	
	// check if array exsits
	if (count($temp_parent) > 0 && is_array($temp_parent)){
		
		// loop all parent category
		foreach($temp_parent as $key => $value){

			// query for category
			$mysqli_category = $mysqli->query("SELECT post_id, post_parent, post_title FROM $prefixPost
											   WHERE post_id = '".$value."' AND post_title != '' AND post_type = '".$type."' AND post_categories = '".$type."' AND post_trash = '0' LIMIT 1") ;

			
			// check if category exist
			if ($mysqli_category->num_rows > 0){
				
				// set query as array
				$row_category = $mysqli_category->fetch_array(MYSQLI_ASSOC) ;
				if ( $row_category['post_parent'] > 0 ){
					$content .= $value.','.getSelectedCategoryArray('sub', $row_category['post_parent'], $type, $array) ;
				}else{
					$content .= $value ;	
				}
				
			}
			
		}
		
	}

	return $content ;	
}

// get all sub category 
function getSubCategory($search_category){
	global $mysqli, $prefixPost ;
	$search_query .= " AND post_parent = '".$search_category."'" ;
	// page query
	$mysqli_query = "SELECT post_id FROM $prefixPost
					WHERE post_title != '' AND post_type = 'category' AND post_categories = 'category' AND post_trash = '0'".$search_query ;
	$mysqli_category = $mysqli->query($mysqli_query);
	if ($mysqli_category->num_rows > 0){
		$return = " AND (post_parent LIKE '%<filter-".$search_category.">%' OR";
		while ( $row = $mysqli_category->fetch_array()) {
			$return .= " post_parent LIKE '%<filter-".$row['post_id'].">%' OR";
		}
		$return .= substr($return, 0, -2); 
		$return .= " )";
	}
	return $return;
}

// check the parent id
function postParentID($category_id, $type){
	global $mysqli, $prefixPost, $array_product ;
	$mysqli_product = $mysqli->query("SELECT * FROM $prefixPost WHERE post_type = 'category' AND post_categories = 'category' AND post_parent = '".$category_id."' AND post_trash = '0'");
	// if has subcategory
	if ($type == 'main'){
		$array_product[] = $category_id ;
	}
	if ($mysqli_product->num_rows > 0){
		while ($row_product = $mysqli_product->fetch_array()){
			$array_product[] = $row_product['post_id'] ;
			postParentID($row_product['post_id'], 'sub') ;
		}
	}
	if ($type == 'main'){
		$total = count($array_product) ;
		$result = '' ;
		if ($total > 0 && is_array($array_product)){
			foreach ($array_product as $key => $value){
				//$result .= ($key != 0 ? ' || ' : 'AND (') . "post_parent = '".$value."'" . (($key+1) == $total ? ')' : '') ;
				$result .= ($key != 0 ? ' || ' : 'AND (') . "post_parent LIKE '%<filter-".$value.">%'" . (($key+1) == $total ? ')' : '') ;
			}
		}
		return $result ;
	}else{
		return $array_product ;
	}
}

// total service charge amount
function totalServiceCharge($array){
	$sum_service_my = $sum_service = 0 ;
	if (count($array) > 0 && is_array($array)){
		// loop all array
		foreach($array as $key => $value){
			$sum_service_my += $value['charge_my'] ;
			$sum_service += $value['charge'] ;
		}
	}
	// keep value in array
	$array['sum_service_my'] = $sum_service_my ;
	$array['sum_service'] = $sum_service ;
	// return result
	return $array ;
}

// sort by subarray
function cmpBySortable($a, $b) {
  return $a["cart_sortable"] - $b["cart_sortable"];
	//if ($a["cart_sortable"] == $b["cart_sortable"]) return 0;
	//return (($a["cart_sortable"] > $b["cart_sortable"]) ? 1 : -1);
}

// for so number
function cmpBySortableQty($a, $b) {
	return $b["Quantity"] - $a["Quantity"];
}

// change number to wording
function numtowords($num, $currency){ 
	$decones = array( 
					'01' => "One", 
					'02' => "Two", 
					'03' => "Three", 
					'04' => "Four", 
					'05' => "Five", 
					'06' => "Six", 
					'07' => "Seven", 
					'08' => "Eight", 
					'09' => "Nine", 
					10 => "Ten", 
					11 => "Eleven", 
					12 => "Twelve", 
					13 => "Thirteen", 
					14 => "Fourteen", 
					15 => "Fifteen", 
					16 => "Sixteen", 
					17 => "Seventeen", 
					18 => "Eighteen", 
					19 => "Nineteen" 
				) ;
	$ones = array( 
				0 => " ",
				1 => "One",     
				2 => "Two", 
				3 => "Three", 
				4 => "Four", 
				5 => "Five", 
				6 => "Six", 
				7 => "Seven", 
				8 => "Eight", 
				9 => "Nine", 
				10 => "Ten", 
				11 => "Eleven", 
				12 => "Twelve", 
				13 => "Thirteen", 
				14 => "Fourteen", 
				15 => "Fifteen", 
				16 => "Sixteen", 
				17 => "Seventeen", 
				18 => "Eighteen", 
				19 => "Nineteen" 
			) ; 
	$tens = array( 
				0 => "",
				1 => "Ten", 
				2 => "Twenty", 
				3 => "Thirty", 
				4 => "Forty", 
				5 => "Fifty", 
				6 => "Sixty", 
				7 => "Seventy", 
				8 => "Eighty", 
				9 => "Ninety" 
			) ; 
	$hundreds = array( 
				"Hundred", 
				"Thousand", 
				"Million", 
				"Billion", 
				"Trillion", 
				"Quadrillion" 
            ) ; 
			// limit t quadrillion 
	$num = number_format($num,2,".",",") ; 
	$num_arr = explode(".", $num) ; 
	$wholenum = $num_arr[0] ; 
	$decnum = $num_arr[1] ; 
	$whole_arr = array_reverse(explode(",", $wholenum)) ; 
	krsort($whole_arr) ; 
	$rettxt = "" ; 
	foreach($whole_arr as $key => $i){ 
		if($i < 20){ 
			$length_id = strlen($i) ;
			if ($length_id == 3){ 
				$i = substr($i, 1) ;	
			}
			$rettxt .= $ones[$i] ; 
		}
		elseif($i < 100){ 
			// remove 0 infront
			$reset_value = ltrim($i, '0') ;
			// continue wording
			$rettxt .= $tens[substr($reset_value,0,1)] ; 
			$rettxt .= " ".$ones[substr($reset_value,1,1)] ; 
		}
		else{
			$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0] ;
			
			if ($decnum > 0){
				$rettxt .= ' ' ;
			}else{
				$rettxt .= ' and ' ;
			}
			
			$splite_txt = (substr($i,1,1).substr($i,2,1)) ;
			if ($splite_txt >= 10 && $splite_txt < 20){
				$rettxt .= $ones[$splite_txt] ; 
			}else{
				$rettxt .= $tens[substr($i,1,1)] ; 
				$rettxt .= ' '.$ones[substr($i,2,1)] ; 
			}
			
		} 
		if($key > 0){ 
			$rettxt .= " ".$hundreds[$key]." " ; 
		} 
	
	} 
	$rettxt = $rettxt." ".$currency ;
	
	if($decnum > 0){ 
		$rettxt .= " and " ; 
		if($decnum < 20){ 
			$rettxt .= $decones[$decnum] ; 
		}
		elseif($decnum < 100){ 
			$rettxt .= $tens[substr($decnum,0,1)] ; 
			$rettxt .= " ".$ones[substr($decnum,1,1)] ; 
		}
		$rettxt = $rettxt . ($decnum == 1 ? " cent" : " cents") ; 
	} 
	return $rettxt . ' ONLY' ;
}

// Product pagination
function nextPrevious($current_page, $limit_page, $search,  $query) {
	global $mysqli ;
	// today date
	$today_date = date('Y-m-d', time()) ;
	// get query
	$mysqli_product = $mysqli->query($query) ;
	// total page
	$total = $mysqli_product->num_rows ;
	$total_page = ceil($total / $limit_page) ;
	$end_number = ($total - (($current_page - 1) * $limit_page)) ;
	// start pagination
	if ($total_page <= 1){
		$search_result = '
		<div class="row">
			<div class="col-sm-12">
				Total result: <strong>'.$total.'</strong>
			</div>
		</div>';	
	}
	else{
		// explode url
		$explode_url = explode('&page=', $url, 2);
		// Calculating the starting and endign values for the loop
		if ($current_page >= 7) {
			$start_loop = $current_page - 3;
			if ($total_page > $current_page + 3)
				$end_loop = $current_page + 3;
			else if ($current_page <= $total_page && $current_page > $total_page - 6) {
				$start_loop = $total_page - 6;
				$end_loop = $total_page;
			} else {
				$end_loop = $total_page;
			}
		}
		else {
			$start_loop = 1;
			if ($total_page > 7)
				$end_loop = 7;
			else
				$end_loop = $total_page;
		}
		// End calculating the starting and endign values for the loop
		$search_result = '
		<div class="row">
			<div class="col-sm-3">
				Total result: <strong>'.$total.'</strong>
			</div>
			<div class="col-sm-9">
				<div class="dataTables_paginate paging_simple_numbers" id="basic-datatable_paginate">
					<ul class="pagination no-margn pull-right">';
						// previous linking
						if ($current_page > 1) {
							$prev = $current_page - 1;
							$search_result .= '
							<li class="paginate_button previous" aria-controls="basic-datatable" tabindex="0" id="basic-datatable_previous">
								<a href="?'.$search.'&pagination=true&page='.$prev.'">Previous</a>
							</li>' ;
						}
						// pagination number
						for ($i = $start_loop; $i <= $end_loop; $i++) {
							if ($current_page == $i)
								$search_result .= '
								<li class="paginate_button active" aria-controls="basic-datatable" tabindex="0">
									<a href="?'.$search.'&pagination=true&page='.$i.'">'.$i.'</a>
								</li>' ;
							else
								$search_result .= '
								<li class="paginate_button" aria-controls="basic-datatable" tabindex="0">
									<a href="?'.$search.'&pagination=true&page='.$i.'">'.$i.'</a>
								</li>' ;
						}
						// next linking
						if ($current_page < $total_page) {
							$next = $current_page + 1;
							$search_result .= '
							<li class="paginate_button next" aria-controls="basic-datatable" tabindex="0" id="basic-datatable_next">
								<a href="?'.$search.'&pagination=true&page='.$next.'">Next</a>
							</li>' ;
						}
					$search_result .= '
					</ul>
				</div>
			</div>
		</div>' ;
	}
	$array['page_total'] = ($total != '' ? $total : 0) ;
	$array['page_pagination'] = $search_result ;
	$array['page_end'] = $end_number ;
	return $array;
}

/*************************************************************************************
						Resize Image
*************************************************************************************/
function reCreateImage($path, $page, $title, $position, $image, $type, $source, $status = ''){
	// Image uploads when exists
	$image = $image ;
	$imagetype = $type ;
	$source_file = $source ;
	$explode_type = pathinfo($image, PATHINFO_EXTENSION);
	$flag = false ;
	switch($imagetype){
		case 'image/jpg':
		case 'image/jpeg':
			$ext_type = 'jpg' ;
			$flag = true ;
		break ;
		case 'image/png':
			$ext_type = 'png' ;
			$flag = true ;
		break ;
	}
	// check if image not exists
	if ($image != ''){
		// check if image type not jpeg or png
		if ($flag){
			
			// image
			$image_name = $title.($status != '' ? '-'.$status : '').'-'.$page.'.'.$ext_type;
			$dir_img = $_SERVER['DOCUMENT_ROOT'].'/Uploads/'.$path.'/m/'.$image_name ;
			$dir_img_ori = $_SERVER['DOCUMENT_ROOT'].'/Uploads/'.$path.'/b/'.$image_name ;
			
			$dir_img_app = '/home/seowexport/public_html/app/Uploads/'.$path.'/m/'.$image_name ;
			$dir_img_ori_app = '/home/seowexport/public_html/app/Uploads/'.$path.'/b/'.$image_name ;
			$dir_img_crop_app = '/home/seowexport/public_html/app/Uploads/'.$path.'/'.$image_name ;
			
			// move file to selected directory
			if (move_uploaded_file($source_file, $dir_img_ori)){
				list($img_width,$img_height) = getimagesize($dir_img_ori) ;
				
				// resize image 2000 x height ?
				$modwidth_2000 = 1000 ;
				$diff_2000 = $img_width / $modwidth_2000 ;
				$modheight_2000 = $img_height / $diff_2000 ;
				
				// resize image 500 x height ?
				$modwidth = 500; 
				$diff = $img_width / $modwidth; 
				$modheight = $img_height / $diff;
				
				// resize image 500 x height ?
				if ($position == 'no-crop'){
					$modwidth_400 = 400; 
					$diff = $img_width / $modwidth_400; 
					$modheight_400 = $img_height / $diff;
				}else{
					$modwidth_400 = 400 ;
					$modheight_400 = 400 ;
				}
				
				// keep resize image into array
				$dir_img_crop = 
					array(
						array(
							'width' => $modwidth_2000, 
							'height' => $modheight_2000, 
							'type' => 'auto',
							'watermark' => $_SERVER['DOCUMENT_ROOT'].'/Images/watermark_800.png', 
							'source' => $dir_img_ori
						),
						array(
							'width' => $modwidth, 
							'height' => $modheight, 
							'type' => 'auto',
							'watermark' => $_SERVER['DOCUMENT_ROOT'].'/Images/watermark_800.png', 
							'source' => $dir_img
						),
						array(
							'width' => $modwidth_400, 
							'height' => $modheight_400, 
							'type' => 'crop', 
							'watermark' => $_SERVER['DOCUMENT_ROOT'].'/Images/watermark_100.png', 
							'source' => $_SERVER['DOCUMENT_ROOT'].'/Uploads/'.$path.'/'.$image_name
						),
						array(
							'width' => $modwidth_2000, 
							'height' => $modheight_2000, 
							'type' => 'auto',
							'watermark' => $_SERVER['DOCUMENT_ROOT'].'/Images/watermark_800.png', 
							'source' => $dir_img_ori_app
						),
						array(
							'width' => $modwidth, 
							'height' => $modheight, 
							'type' => 'auto',
							'watermark' => $_SERVER['DOCUMENT_ROOT'].'/Images/watermark_800.png', 
							'source' => $dir_img_app
						),
						array(
							'width' => $modwidth_400, 
							'height' => $modheight_400, 
							'type' => 'crop', 
							'watermark' => $_SERVER['DOCUMENT_ROOT'].'/Images/watermark_100.png', 
							'source' => $dir_img_crop_app
						)
						
					) ;
				// return result as array
				$array['result'] = true ;
				$array['image'] = $image_name ;
				$array['original'] = $dir_img_ori ;
				$array['extension'] = $ext_type ;
				$array['position'] = $position ;
				$array['merge_watermark'] = $status ;
				$array['crop'] = $dir_img_crop ;
			}
		}else{
			$array['result'] = false ;	
			$array['result_message'] = 'error-type' ;	
		}
	}
	return $array ;
}

// add watermark
function addWatermark($source, $watermark, $type, $position, $merge_watermark){
	// set flag false
	$flag = false ;
	if ($position == 'none') { $merge_watermark = '1'; }
	if ($merge_watermark == ''){
		// image source
		list($modwidth,$modheight) = getimagesize($source);
		// watermark
		$newWatermark = imagecreatefrompng($watermark) ;
		// get watermark size
		list($w_width, $w_height) = getimagesize($watermark);
		// watermark position
		$margin = 15 ;
		switch($position){
			case 'top':
				$position_width = ($modwidth - $w_width) / 2 ;
				$position_height = $margin ;
			break;
			case 'bottom':
				$position_width = ($modwidth - $w_width) / 2 ;
				$position_height = ($modheight - $w_height) - $margin ;
			break;
			case 'left':
				$position_width = $margin ;
				$position_height = ($modheight - $w_height) / 2 ;
			break;
			case 'right':
				$position_width = ($modwidth - $w_width) - $margin ;
				$position_height = ($modheight - $w_height) / 2 ;
			break;
			case 'center':
			default:
				$position_width = ($modwidth - $w_width) / 2 ;
				$position_height = ($modheight - $w_height) / 2 ;
			break;
		}
		// check the type |- jpg, gif, png
		switch($type){
			case 'jpg' ;
				$im = imagecreatefromjpeg($source) ;
				imagecopy($im, $newWatermark, $position_width, $position_height, 0, 0, $w_width, $w_height);
				imagejpeg($im, $source) ;
				$flag = true ;
			break;
			case 'png' ;
				$im = imagecreatefrompng($source) ;
				imagecopy($im, $newWatermark, $position_width, $position_height, 0, 0, $w_width, $w_height);
				imagepng($im, $source) ;
				$flag = true ;
			break;
			case 'gif' ;
			break;
		}
	}
	return $flag ;
}

// check title
function checkTitle($title, $page){
	global $mysqli, $prefixPost ;
	$title = escapeString($title) ;
	$title = stripNonAlphaNumeric($title) ;
	$title = $title != '' ? $title : 'No Title' ;
	$title = strtolower(str_replace("  ", "-", $title)) ;
	$title = strtolower(str_replace(" ", "-", $title)) ;
	if (substr($title, -1) == '-'){ $title = substr_replace($title, '', -1) ; }
	$mysqli_check = $mysqli->query("SELECT * FROM $prefixPost
									WHERE post_id != '".$page."' AND post_link = '".$title."' AND post_trash = '0' ORDER BY (post_status+0) DESC");
	if ($mysqli_check->num_rows > 0){
		$status = 1;
		$check_title = 0;
		while($check_title == 0){
			$new_title = $title.$status;
			$new_title = stripNonAlphaNumeric($new_title);
			$new_title = strtolower(str_replace("  ", "-", $new_title));
			$new_title = strtolower(str_replace(" ", "-", $new_title));
			if (substr($title, -1) == '-'){ $title = substr_replace($title, '', -1) ; }
			$mysqli_check = $mysqli->query("SELECT * FROM $prefixPost
											WHERE post_id != '".$page."' AND post_link = '".$new_title."' AND post_trash = '0' ORDER BY (post_status+0) DESC");
			if ($mysqli_check->num_rows > 0){
				$check_title = 0;
			}
			else{
				$check_title = 1;
			}
			$status++;
		}
		$title = $new_title;
	}
	else{
		$status = 0;	
	}
	$array['title'] = $title ;
	$array['status'] = $status ;
	return $array ;
}

// check product title if exists
function titleExists($mysqli, $prefixPost, $id, $title){
	$boolean_result = false ;
	if ($id != '' && $title != ''){
		// query for product
		$mysqli_product = $mysqli->query("SELECT post_title FROM $prefixPost
										WHERE post_id != '".$id."' AND post_title = '".$title."' AND post_type = 'product' AND post_categories = 'product' AND post_trash = '0' LIMIT 1") ;
		// check if product exist
		if ($mysqli_product->num_rows == 0){
			$boolean_result = true ;
		}
	}
	return $boolean_result ;
}

// loop all sub category
function subCategory($parentID, $type, $id) {
	global $mysqli, $prefixPost, $prefixJob ;
	$mysqli_category = $mysqli->query("SELECT * FROM $prefixPost
									WHERE post_title != '' AND post_type = 'category' AND post_parent = '".$parentID."' AND post_trash = '0' ORDER BY post_order") ;
	$category = '' ;
	if ($mysqli_category->num_rows > 0) {
		$category .= '
		<ul class="list-unstyled">' ;
			while($row_category = $mysqli_category->fetch_array(MYSQLI_ASSOC)) {
				$title = dataFilter($row_category['post_title']) ;
				$category .= '
				<li class="'.($row_category['post_id'] == $id ? 'active' : '').'">' ;
					switch($type){
						case 'none' :
							$category .= '<a href="job-category.php?id='.$row_category['post_id'].'">'.$title.'</a>' ;
						break ;
						case 'input' :
							// get job query
							$mysqli_job = $mysqli->query("SELECT * FROM " . $prefixJob . "
															WHERE job_quotation_id = '".$id."' AND job_post_id = '".$row_category['post_id']."' LIMIT 1") ;
							// check if job id exsits
							if ($mysqli_job->num_rows > 0){
								// set query as variable
								$row_job = $mysqli_job->fetch_array(MYSQLI_ASSOC) ;
								// keep in variable
								$category .= '
								<div class="quotation_job_checkbox">
									<input type="hidden" name="quotation_id[]" value="'.$row_job['job_id'].'" />
									<label><input type="checkbox" name="quotation_status['.$row_job['job_id'].']" value="1" '.($row_job['job_check'] == '1' ? 'checked' : '').' />'.$title.'</label>
									<input type="text" name="quotation_description['.$row_job['job_id'].']" class="form-control ui-search-input" value="'.$row_job['job_description'].'" placeholder="Description" />
								</div>' ;
							}
						break ;
					}
				$category .= '
				</li>' ;
			}
		$category .= '
		</ul>' ;
	}
	return $category ;
}

// category title
function categoryTitle($parentID) {
	global $mysqli, $prefixPost;
	$mysqli_category = $mysqli->query("SELECT * FROM $prefixPost
									WHERE post_type = 'category' AND post_categories = 'category' AND post_id = '".$parentID."' AND post_trash = '0' ORDER BY post_title");
	if ($mysqli_category->num_rows > 0) {
		$row_category = $mysqli_category->fetch_array(MYSQLI_ASSOC) ;
		$title = dataFilter($row_category['post_title']) ;
		return ' &mdash; '.$title.categoryTitle($row_category['post_parent']);
	}
	else{
		return ' &mdash; Main';
	}
}

// replace numeric string
function stripNonAlphaNumeric($string) {
    return  preg_replace("/[^a-zA-Z0-9\s]/", "", $string);
}

// number format with 2 decimal
function numberFormat($price){
	$price = number_format($price, 2, '.', '') ;
	return $price ;
}

// number format with 2 decimal and comma
function numberFormatComma($price, $digit = 2){
	$price = number_format($price, $digit, '.', ',') ;
	return $price ;
}

// custom number format
function numberFormatDigit($price, $digit){
	$price = number_format($price, $digit, '.', ',') ;
	return $price ;
}

// check if number
function numberCheck($value){
	$value = ($value > 0 ? $value : 0) ;	
	return $value ;
}

// shipping container list
function shippingContainer($mysqli, $prefixQuotation, $combine_head_quantity, $sub_quantity, $page_mode, $container_cart_checkbox, $container_cart_m3, $container_cart_kg){
	global $prefixQuotationHold, $rate_type ;
	// default variable
	$array_container = $array_update = array() ;
	$count = $count_all_item = 0 ;
	// check if is array
	if (arrayCheck($sub_quantity)){
		// loop all array
		foreach($sub_quantity as $key => $value){
			// query for quotation
			$mysqli_sub = $mysqli->query("SELECT quotation_product, quotation_id,quotation_so2_manual, quotation_so2, quotation_company_id FROM $prefixQuotation
											WHERE quotation_id = '".$key."' AND quotation_trash = '0' LIMIT 1") ;
			// if query exsits
			if ($mysqli_sub->num_rows > 0){
				// set query as array
				$row_sub = $mysqli_sub->fetch_array(MYSQLI_ASSOC) ;
				// serialize product
				$quotation_details = unserialize(base64_decode($row_sub['quotation_product'])) ;
				$quotation_id = $row_sub['quotation_id'] ;
				$quotation_so = ($row_sub['quotation_so2_manual'] == 1 ? '' : 'P').$row_sub['quotation_so2'] ;
				
				// check if hold table exists
				$mysqli_hold_quantity = $mysqli->query("SELECT quotation_hold_id FROM $prefixQuotationHold
														WHERE quotation_hold_quotation_id = '".$quotation_id."' AND quotation_hold_company_id = '".$row_sub['quotation_company_id']."' LIMIT 1") ;
				$release_status = 'pending' ;
				if ($mysqli_hold_quantity->num_rows > 0){
					$release_status = 'hold' ;
				}
				
				// check if is array
				if (arrayCheck($quotation_details)){
					// loop all array
					foreach($quotation_details as $key_details => $value_details){
						// default varible
						$key_item = $value[$key_details] ;
						// check if is array
						if (arrayCheck($key_item)){
							// loop all array
							foreach($key_item as $key_container => $value_container){
								// check if quantity not 0
								if ($value_container > 0){
									// set in array container
									$array_container[$key_container][$count_all_item] = $value_details ;
									$array_container[$key_container][$count_all_item]['cart_quantity'] = $value_container ;
									
									$temp_m3 = $container_cart_m3[$quotation_id][$key_details] ;
									$temp_kg = $container_cart_kg[$quotation_id][$key_details] ;
									
									$array_container[$key_container][$count_all_item]['cart_m3'] = ($temp_m3 > 0 ? $temp_m3 : $array_container[$key_container][$count_all_item]['cart_m3']) ;
									$array_container[$key_container][$count_all_item]['cart_kg'] = ($temp_kg > 0 ? $temp_kg : $array_container[$key_container][$count_all_item]['cart_kg']) ;
									
									$array_container[$key_container][$count_all_item]['cart_quotation_id'] = $quotation_id ;
									$array_container[$key_container][$count_all_item]['cart_quotation_so2'] = $quotation_so ;
									$array_container[$key_container][$count_all_item]['cart_quotation_status'] = $release_status ;
									$array_container[$key_container][$count_all_item]['cart_this_key'] = $count_all_item ;
								}
							}
							$count_all_item++ ;
						}
					}
				}
			}
		}
	}
 
 	// check if array exists
	if (arrayCheck($array_container)){
		// default variable
		$total_no_of_carton = $total_m3_single = $total_m3 = $total_kgs_gw_single = $total_kgs_gw = $total_kgs_nw = 0 ;
		// loop all array
		foreach($array_container as $key => $value){
			// check if query exists
			if (arrayCheck($value)){
				$sub_quantity = $sub_no_of_carton = $sub_m3_single = $sub_m3 = $sub_kgs_gw_single = $sub_kgs_gw = $sub_kgs_nw = $sub_total = 0 ;
				$array_temp = array() ;
				// $default_td_break = 'border-bottom:1px solid #000;' ;
				// loop all array
				foreach($value as $key_item => $cart){
					// default value
					$quantity = dataFilter($cart['cart_quantity']) ;
					// count number of carton | m3 | kgs | nw
					$number_of_box_temp = $cart['cart_number_of_box'] ;
					$number_of_box_temp = ($number_of_box_temp == 0 ? 1 : $number_of_box_temp) ;
					$number_of_carton = ($number_of_box_temp * $quantity) ;
					
					$m3_single = ($cart['cart_m3']) ;
					$m3 = ($m3_single * $quantity) ;
					
					$kgs_gw_single = ($cart['cart_kg']) ;
					$kgs_gw = ($kgs_gw_single * $quantity) ;
					
					$kgs_nw = ($kgs_gw - $number_of_carton) ;
					$sub_total_price = ($cart['cart_price'] * $quantity) ;
					// count sub value | quantity | number of carton | m3 | kgs-gw | kgs-nw
					$sub_quantity += $quantity ;
					$sub_no_of_carton += $number_of_carton ;
					$sub_m3_single += $m3_single ;
					$sub_m3 += $m3 ;
					$sub_kgs_gw_single += $kgs_gw_single ;
					$sub_kgs_gw += $kgs_gw ;
					$sub_kgs_nw += $kgs_nw ;
					$sub_total += $sub_total_price ;
					$number_of_box = countNumberOfBox($number_of_box_temp) ;
					// insert into array
					$array_temp[$key_item]['cart_id'] = dataFilter($cart['cart_id']) ;
					$array_temp[$key_item]['cart_type'] = dataFilter($cart['cart_type']) ;
					$array_temp[$key_item]['cart_parent_key'] = dataFilter($cart['cart_parent_key']) ;
					$array_temp[$key_item]['cart_title'] = dataFilter($cart['cart_title']) ;
					$array_temp[$key_item]['cart_reference_number'] = dataFilter($cart['cart_reference_number']) ;
					$array_temp[$key_item]['cart_buyer_code'] = dataFilter($cart['cart_buyer_code']) ;
					$array_temp[$key_item]['cart_hs_code'] = dataFilter($cart['cart_hs_code']) ;
					$array_temp[$key_item]['cart_dimension'] = dataFilter($cart['cart_dimension']) ;
					$array_temp[$key_item]['cart_colour'] = dataFilter($cart['cart_colour']) ;
					$array_temp[$key_item]['cart_specification'] = dataFilter($cart['cart_specification']) ;
					$array_temp[$key_item]['cart_description'] = dataFilter($cart['cart_description']) ;
					$array_temp[$key_item]['cart_image'] = 
					$array_temp[$key_item]['cart_quantity'] = $quantity ;
					$array_temp[$key_item]['cart_number_of_box'] = ($number_of_box != '' ? 'BOX '.$number_of_box : '') ;
					$array_temp[$key_item]['cart_number_of_carton'] = $number_of_carton ;
					$array_temp[$key_item]['cart_m3'] = $m3_single ;
					$array_temp[$key_item]['cart_sub_m3'] = $m3 ;
					$array_temp[$key_item]['cart_kg'] = $kgs_gw_single ;
					$array_temp[$key_item]['cart_sub_kg'] = $kgs_gw ;
					$array_temp[$key_item]['cart_sub_kg_nw'] = $kgs_nw ;
					$array_temp[$key_item]['cart_serially_numbered'] = '1 TO ' . $number_of_carton ;
					$array_temp[$key_item]['cart_price'] = numberFormat($cart['cart_price']) ;
					$array_temp[$key_item]['cart_sub_total'] = numberFormat($sub_total_price) ;
					$array_temp[$key_item]['cart_quotation_id'] = dataFilter($cart['cart_quotation_id']) ;
					$array_temp[$key_item]['cart_quotation_so2'] = dataFilter($cart['cart_quotation_so2']) ;
					$array_temp[$key_item]['cart_quotation_status'] = dataFilter($cart['cart_quotation_status']) ;
					$array_temp[$key_item]['cart_this_key'] = dataFilter($cart['cart_this_key']) ;
				}
				// count total value | number of carton | m3 | kgs-gw | kgs-nw
				$total_no_of_carton += $sub_no_of_carton ;
				$total_m3_single += $sub_m3_single ;
				$total_m3 += $sub_m3 ;
				$total_kgs_gw_single += $sub_kgs_gw_single ;
				$total_kgs_gw += $sub_kgs_gw ;
				$total_kgs_nw += $sub_kgs_nw ;
				// check page mode | update to update the container and all content
				$array_update[$key]['item'] = $array_temp ;
				$array_update[$key]['sub_quantity'] = $sub_quantity ;
				$array_update[$key]['sub_number_of_carton'] = $sub_no_of_carton ;
				$array_update[$key]['sub_m3_single'] = $sub_m3_single ;
				$array_update[$key]['sub_m3'] = $sub_m3 ;
				$array_update[$key]['sub_kgs_gw_single'] = $sub_kgs_gw_single ;
				$array_update[$key]['sub_kgs_gw'] = $sub_kgs_gw ;
				$array_update[$key]['sub_kgs_nw'] = $sub_kgs_nw ;
				$array_update[$key]['sub_total'] = $sub_total ;
				$array_update[$key]['sub_total_wording'] = 'TOTAL AMOUNT FOR EACH CONTAINER: '.($rate_type == 'eur' ? 'EUR' : 'USD').' ' . numberFormatDigit($sub_total, 2) . ' FOB MALAYSIAN PORT.' ;
			}
		}
	}
	// keep all result in array
	$array['content'] = $array_update ;
	// return result
	return $array ;
}

// shipping container list for tax invoice
function shippingContainerInvoice($mysqli, $prefixQuotation, $combine_head_id, $query_sub, $container_all_information, $array_container, $array_seal, $rate_type = 'myr'){
	global $prefixQuotationHold, $rate_type ;
	// query for combine sub
	$mysqli_query_sub = $mysqli->query($query_sub) ;
	$count = $total = $sub_m3 = $sub_kg = $sub_kg_nw = $sub_total = $total_grand = 0 ;
	
	// loop all container
	$array_invoice = $array_invoice_update = array() ;
	if (arrayCheck($container_all_information)){
		// first loop is container
		foreach($container_all_information as $key => $value){
			// second loop is item
			foreach($value['item'] as $key_item => $value_item){
				$key_status = $value_item['cart_quotation_status'] ;
				// check before insert
				$boolean_insert = true ;
				foreach($array_invoice[$key_status] as $key_invoice => $value_invoice){
					// if ($value_invoice['cart_quotation_id'] == $value_item['cart_quotation_id'] && $value_invoice['cart_this_key'] == $value_item['cart_this_key']){
					if ($value_invoice['cart_id'] == $value_item['cart_id']){
						
						$quantity_new = ($value_invoice['cart_quantity'] + $value_item['cart_quantity']) ;
						$sub_m3_new = ($value_invoice['cart_sub_m3'] + $value_item['cart_sub_m3']) ;
						$sub_kg_new = ($value_invoice['cart_sub_kg'] + $value_item['cart_sub_kg']) ;
						$sub_kg_nw_new = ($value_invoice['cart_sub_kg_nw'] + $value_item['cart_sub_kg_nw']) ;
						$sub_total_new = ($value_invoice['cart_sub_total'] + $value_item['cart_sub_total']) ;
						
						$array_invoice[$key_status][$key_invoice]['cart_quantity'] = $quantity_new ;
						$array_invoice[$key_status][$key_invoice]['cart_sub_m3'] = $sub_m3_new ;
						$array_invoice[$key_status][$key_invoice]['cart_sub_kg'] = $sub_kg_new ;
						$array_invoice[$key_status][$key_invoice]['cart_sub_kg_nw'] = $sub_kg_nw_new ;
						$array_invoice[$key_status][$key_invoice]['cart_sub_total'] = $sub_total_new ;
						
						$boolean_insert = false ;
						break ;
					}
				}
				// check status pending or hold
				if ($boolean_insert){
					$array_invoice[$key_status][] = $value_item ;
				}
				$sub_quantity += $value_item['cart_quantity'] ;
				$sub_m3 += $value_item['cart_sub_m3'] ;
				$sub_kg += $value_item['cart_sub_kg'] ;
				$sub_kg_nw += $value_item['cart_sub_kg_nw'] ;
				$sub_total += $value_item['cart_sub_total'] ;
			}
		}
	}
	if (arrayCheck($array_invoice)){
		foreach($array_invoice as $key => $value){
			foreach($value as $key1 => $value1){
				$array_invoice_update['item'][] = $value1 ;
			}
		}
	}

	$gst_charge = numberFormat(checkZeroValue($sub_total * ($rate_type == 'myr' ? GSTCHARGE : 0) / 100)) ;
	$total_grand = ($sub_total + $gst_charge) ;
	$array_service_charge = array() ;
	
	if ($mysqli_query_sub->num_rows > 0){
		while ($row_sub = $mysqli_query_sub->fetch_array(MYSQLI_ASSOC)){
			// serialize product
			$quotation_details = unserialize(base64_decode($row_sub['quotation_product'])) ;
			// check if hold table exists
			$mysqli_hold_quantity = $mysqli->query("SELECT quotation_hold_id FROM $prefixQuotationHold
													WHERE quotation_hold_quotation_id = '".$row_sub['quotation_id']."' AND quotation_hold_company_id = '".$row_sub['quotation_company_id']."' LIMIT 1") ;
			$release_status = 'pending' ;
			if ($mysqli_hold_quantity->num_rows > 0){
				$release_status = 'hold' ;
			}
			// check if is array
			if (arrayCheck($quotation_details)){
				// loop all array
				foreach($quotation_details as $key_details => $value_details){
					$array_update[$key_details]= $value_details ;
				}
			}
			$quotation_details_service = unserialize(base64_decode($row_sub['quotation_service'])) ;
			$quotation_details_total = unserialize(base64_decode($row_sub['quotation_total'])) ;
			$quotation_details_total['cart_grand_total_wording'] = ($rate_type == 'myr' ? 'RINGGIT MALAYSIA' : ($rate_type == 'eur' ? 'EURO' : 'US DOLLAR')).' ' . strtoupper(numtowords($quotation_details_total['cart_grand_total'], '')) . '.' ;
			// keep in array
			if ($release_status == 'pending'){
				$array_update_all[$count] = array(  'item' => $array_update,
													'service' => $quotation_details_service,
													'total' => $quotation_details_total) ;
													
				// update invocie
				
				if (arrayCheck($quotation_details_service)){
					foreach($quotation_details_service as $key => $value){
						$total_grand += $value['charge'] ;
						$array_service_charge[] = $value ;
					}
				}
				
				$count++ ;
			}
			// counting value
			$total += $quotation_details_total['cart_grand_total'] ;
			
		}
	}
	
	$array_invoice_update['container_no'] = implode(",", $array_container) ;
	$array_invoice_update['seal_no'] = implode(",", $array_seal) ;
	$array_invoice_update['service'] = $array_service_charge ;
	$array_invoice_update['total'] = array( 'cart_total_quantity' => $sub_quantity,
											'cart_total_m3' => $sub_m3,
											'cart_total_kg' => $sub_kg,
											'cart_total_kg_nw' => $sub_kg_nw,
											'cart_total' => $sub_total,
											'cart_gst_charge' => $gst_charge,
											'cart_grand_total' => $total_grand,
											'cart_grand_total_wording' => ($rate_type == 'myr' ? 'RINGGIT MALAYSIA' : ($rate_type == 'eur' ? 'EURO' : 'US DOLLAR')).' ' . strtoupper(numtowords($total_grand, '')) . '.') ;

	// keep all result in array
	$array['item'] = $array_update_all ;
	$array['total'] = $total ;
	$array['invoice'] = $array_invoice_update ;
	// return result
	return $array ;
}

// shipping container list
function shippingContainer2($mysqli, $prefixQuotation, $company_name, $array_container, $current_type, $page_mode){
	
	global $rate_type, $array_container_title ;
	$rate_type = ($rate_type == 'myr' ? 'myr' : ($rate_type == 'eur' ? 'eur' : 'usd')) ;
	$boolean_rate = ($rate_type == 'myr' ? true : false) ;
	
	$boolean_edit = ($page_mode == 'edit' ? true : false) ;
	
	// total value
	$all_container = $array_container['all_container'] ;
	$total_number_of_carton = $array_container['total_number_of_carton'] ;
	$total_m3_single = $array_container['total_m3_single'] ;
	$total_m3 = $array_container['total_m3'] ;
	$total_kgs_gw_single = $array_container['total_kgs_gw_single'] ;
	$total_kgs_gw = $array_container['total_kgs_gw'] ;
	$total_kgs_nw = $array_container['total_kgs_nw'] ;
	$total = $array_container['total'] ;
	$total_grand = $array_container['total_grand'] ;
	$total_wording = $array_container['total_wording'] ;
	$setting = $array_container['setting'] ;
	$company_name = ($array_container['company_name'] != '' ? $array_container['company_name'] : $company_name) ;
	$array_service = $array_container['service'] ;	
	$boolean_invoice = ($current_type == 'invoice' ? true : false) ;
	
	$check_sub_total = 0 ;
	$check_grand_total = 0 ;
	$check_gst_charge = 0 ;
	$check_boolean_sub_total = true ;
	$check_boolean_grand_total = true ;
	$check_boolean_gst_charge = true ;
	
	// check if array exists
	if (arrayCheck($all_container)){
		
		// if page mode = print
		if ($page_mode == 'print'){
			$default_table = 'text-transform:uppercase; border-collapse:separate; border:1px solid #000; border-bottom:0;  border-collapse: collapse;' ;
			$default_table .= ($boolean_invoice ? '' : 'font-size:18px;') ;
			$default_th = 'border-bottom:1px solid #000; vertical-align:middle;' ;
			$default_style = 'text-transform:uppercase; border:0; padding:5px; vertical-align:top;' ;
			$default_style1 = $default_style1_add = $default_style ;
			$default_style1_add .= 'padding:0 5px 5px;' ;
			$default_style2 = $default_style2_add = $default_style.'border-left:1px solid #000;' ;
			$default_style2_add .= 'padding:0 5px 5px;' ;
			$default_style3 = $default_style.'border-left:1px solid #000; padding:0 5px;' ;
			$default_style4 = $default_style.'border-left:1px solid #000; border-top:1px solid #000; font-weight:bold;' ;
			$default_style5 = 'border-bottom:1px solid #000;' ;
			$default_style6 = $default_style.' font-weight:bold;' ;
		}
		
		// keep content in variable
		if ($boolean_edit){ 
			$table_content .= '
			<table width="100%" border="0" cellpadding="0" cellspacing="0" class="doc_loading_details">
				<tr>
					<td width="180">COMPANY NAME</td>
					<td width="10">:</td>
					<td><input type="text" name="company_name" class="form-control" placeholder="COMPANY NAME" value="'.$company_name.'"></td>
				</tr>
				<tr>
					<td colspan="3"><input type="text" name="container_no_title" class="form-control" value="'.shippingNameType($array_container_title['cn'], 'cn').'" required '.$input_readonly.' />
				</tr>
				<tr>
					<td colspan="3"><input type="text" name="seal_no_title" class="form-control" value="'.shippingNameType($array_container_title['sn'], 'sn').'" required '.$input_readonly.' />
				</tr>
			</table>' ;
		}
		
		$table_content .= '
		<span style="display:none;" id="current_page_type">list</span>
		<table border="0" cellpadding="0" cellspacing="0" width="100%" class="table_shipping_list" style="'.$default_table.'">' ;
			
			// loop all array
			foreach($all_container as $key => $value){
				// check boolean edit
				if ($boolean_edit){ 
					$table_content .= '
					<tr class="shipping_list_main_tr" id="container_key_'.$key.'">
						<td>
							<table border="0" cellpadding="0" cellspacing="0" width="100%" class="table-striped table_shipping_list_sub">' ;
				}
				
				// page mode edit loop multiple time | print only print 1 time
				if ($page_mode == 'edit' || ($page_mode == 'print' && $count == 0)){
					// keep content in variable when in edit mode
					$count_main_tr = 4 ;
					$table_content .= '
					<thead style="display:table-header-group">
						<tr>
							<th style="'.$default_style1.' '.$default_th.'">ITEM</th>
							<th style="'.$default_style2.' '.$default_th.'">'.($current_type == 'packing' || $current_type == 'invoice' ? 'ITEM DESCRIPTION' : 'MODEL').'</th>' ;
							if ($boolean_edit){ }
							else{
								$table_content .= '
								<th style="'.$default_style2.' '.$default_th.'">DESCRIPTION & DIMENSION (mm)</th>' ;
							}
							$table_content .= '
							<th style="'.$default_style2.' '.$default_th.'">'.($current_type == 'packing' || $current_type == 'invoice' ? 'QTY PCS' : 'QTY').'</th>';
							if ($boolean_edit || (!$boolean_edit && !$boolean_invoice)){
								$count_main_tr += 6 ;
								$table_content .= '
								<th style="'.$default_style2.' '.$default_th.'">NO OF CARTON</th>
								<th style="'.$default_style2.' '.$default_th.'">CBM</th>
								<th style="'.$default_style2.' '.$default_th.'">TOTAL<br />CBM</th>
								<th style="'.$default_style2.' '.$default_th.'">GW (KGS)</th>
								<th style="'.$default_style2.' '.$default_th.'">TOTAL<br />KGS (GW)</th>
								<th style="'.$default_style2.' '.$default_th.'">TOTAL<br />KGS (NW)</th>' ;
							}
							if ($boolean_edit || $boolean_invoice){
								$count_main_tr += 2 ;
								if ($boolean_edit || ($boolean_invoice && $setting['serially_numbered'] == '1')){
									$count_main_tr++ ;
									$table_content .= '<th style="'.$default_style2.' '.$default_th.'">SERIALLY<br />NUMBERED</th>' ;
								}
								$table_content .= '
								<th style="'.$default_style2.' '.$default_th.' '.($boolean_rate ? 'display:none;' : '').'">UNIT PRICE<br />'.strtoupper($rate_type).'/PC</th>
								<th style="'.$default_style2.' '.$default_th.' '.($boolean_rate ? 'display:none;' : '').'">AMOUNT<br />'.strtoupper($rate_type).'</th>' ;
							}
						$table_content .= '
						</tr>
					</thead>' ;
					// check boolean edit
					
					if (!$boolean_edit){ 
						$table_content .= '
						<tfoot>
							<tr>
								<td height="1px" style="border:0;border-top:1px solid #000;" colspan="'.$count_main_tr.'"></td>
							</tr>
						</tfoot>' ;
					}else{
						$count_main_tr = ($boolean_rate ? ($count_main_tr - 2) : $count_main_tr) ;	
					}
					$count++ ;
				}
				
				// check if query exists
				$item_array = $value['item'] ;
				$number_of_carton_value = $value['sub_number_of_carton'] ;
				$sub_quantity = $value['sub_quantity'] ;
				$sub_m3_single_value = $value['sub_m3_single'] ;
				$sub_m3_value = $value['sub_m3'] ;
				$sub_kgs_gw_single_value = $value['sub_kgs_gw_single'] ;
				$sub_kgs_gw_value = $value['sub_kgs_gw'] ;
				$sub_kgs_nw_value = $value['sub_kgs_nw'] ;
				$sub_total_value = $value['sub_total'] ;
				$sub_total_wording = $value['sub_total_wording'] ;
				$container_value = $value['container'] ;
				$seal_value = $value['seal'] ;
				
				// check if item exists
				if (arrayCheck($item_array)){
					
					$count_item = $sub_no_of_carton = $sub_m3 = $sub_kgs_gw = $sub_kgs_nw = 0 ;
					
					// $default_td_break = 'border-bottom:1px solid #000;' ;
					// loop all array
					foreach($item_array as $key_item => $cart){
						
						// title name
						$title = dataFilter($cart['cart_title']) ;
						$cart_type = dataFilter($cart['cart_type']) ;
						$boolean_type_product = ($cart_type == 'product' || $cart_type == 'remark' ? true : false) ;
						$boolean_type_remark = ($cart_type == 'remark' ? true : false) ;
						$hide_input_remark = ($cart_type == 'product' || $cart_type == 'additional' ? '' : 'display_none') ;
						
						// check show td mode
						$default_style1_show = ($boolean_type_product ? $default_style1 : $default_style1_add) ;
						$default_style2_show = ($boolean_type_product ? $default_style2 : $default_style2_add) ;
						
						// count value
						($boolean_type_product ? $count_item++ : '') ;
						
						// quantity
						$quantity = dataFilter($cart['cart_quantity']) ;
						// check boolean edit
						
						// count sub total
						if ( $boolean_type_product ){
							$check_sub_total = ( $quantity * dataFilter($cart['cart_price']) ) ;
							$check_grand_total += $check_sub_total ;
							$current_cart_sub_total = dataFilter($cart['cart_sub_total']) ;
							$current_cart_sub_total = ( $current_cart_sub_total != '' ? $current_cart_sub_total : 0) ;
							if ( numberFormat($check_sub_total) != numberFormat($current_cart_sub_total) ){
								$check_boolean_sub_total = false ;
							}
						}
						
						if ($boolean_edit){
							$array_key = '['.$key.']['.$key_item.']' ;
							$cart_type = dataFilter($cart['cart_type']) ;
							$input_type_remark = ($cart_type == 'remark' ? 'hidden' : 'text') ;
							$cart_text_title = ($cart_type == 'product' ? 'MODEL' : ($cart_type == 'remark' ? 'REMARK' : 'ADDITIONAL')) ;
							
							// keep content in variable
							$table_content .= '
							<tr class="add_remark_box">
								<td width="25" class="text_center">
									<label>
										'.$count_item.'<br />
										'.($cart['cart_type'] == 'remark' ? '<input type="checkbox" name="cart_remark_checkbox'.$array_key.'" value="1" checked />' : '').'
									</label>
									<br />
									<a href="#" class="btn btn-default width_38px remove_shipping_list"><i class="fa fa-times fa-3"></i></a>
									<input type="hidden" name="cart_hidden_text'.$array_key.'" class="cart_hidden_text" value="1" />
									<input type="hidden" name="cart_id'.$array_key.'" value="'.dataFilter($cart['cart_id']).'" />
									<input type="hidden" name="cart_type'.$array_key.'" value="'.$cart_type.'" />
									<input type="hidden" name="cart_specification'.$array_key.'" value="'.dataFilter($cart['cart_specification']).'" />
									<input type="hidden" name="cart_image'.$array_key.'" value="'.dataFilter($cart['cart_image']).'" />
									<br />
									<input type="text" name="cart_sortable'.$array_key.'" class="width_80 text_center margin_top_3px" value="'.$key_item.'" />
								</td>
								<td width="200">
									<input type="text" name="cart_title'.$array_key.'" class="form-control" placeholder="'.$cart_text_title.'" value="'.$title.'" />
									<input type="text" name="cart_description'.$array_key.'" class="form-control margin_top_3px" placeholder="DESCRIPTION" value="'.dataFilter($cart['cart_description']).'" />
									<input type="text" name="cart_dimension'.$array_key.'" class="form-control margin_top_3px" placeholder="DIMENSION" value="'.dataFilter($cart['cart_dimension']).'" />
									'.($boolean_type_product ? '
										<input type="text" name="cart_reference_number'.$array_key.'" class="form-control margin_top_3px" placeholder="REFERENCE NUMBER" value="'.dataFilter($cart['cart_reference_number']).'" />
										<input type="text" name="cart_buyer_code'.$array_key.'" class="form-control margin_top_3px" placeholder="BUYER CODE" value="'.dataFilter($cart['cart_buyer_code']).'" />
										<input type="text" name="cart_hs_code'.$array_key.'" class="form-control margin_top_3px" placeholder="HS CODE" value="'.dataFilter($cart['cart_hs_code']).'" />
										<input type="text" name="cart_number_of_box'.$array_key.'" class="form-control margin_top_3px" placeholder="NUMBER OF BOX" value="'.dataFilter($cart['cart_number_of_box']).'" />
										<input type="text" name="cart_colour'.$array_key.'" class="form-control margin_top_3px" placeholder="COLOUR" value="'.dataFilter($cart['cart_colour']).'" />
										<textarea name="cart_box_add_on'.$array_key.'" class="form-control" placeholder="Box Add On">'.dataFilter($cart['cart_box_add_on']).'</textarea>
									' : '').'
								</td>
								<td width="75"><input type="text" name="cart_quantity'.$array_key.'" class="form-control width_100_percent shipping_live_change shipping_cart_quantity" id="shipping_cart_quantity" value="'.$quantity.'" /></td>
								<td width="75"><input type="text" name="cart_number_of_carton'.$array_key.'" class="form-control width_100_percent shipping_live_change shipping_cart_number_of_carton" id="shipping_cart_number_of_carton" value="'.dataFilter($cart['cart_number_of_carton']).'" /></td>
								<td width="75"><input type="text" name="cart_m3'.$array_key.'" class="form-control width_100_percent shipping_live_change shipping_cart_m3" id="shipping_cart_m3" value="'.dataFilter($cart['cart_m3']).'" /></td>
								<td width="75"><input type="text" name="cart_sub_m3'.$array_key.'" class="form-control width_100_percent shipping_live_change shipping_cart_sub_m3" id="shipping_cart_sub_m3" value="'.dataFilter($cart['cart_sub_m3']).'" /></td>
								<td width="75"><input type="text" name="cart_kg'.$array_key.'" class="form-control width_100_percent shipping_live_change shipping_cart_kg" id="shipping_cart_kg" value="'.dataFilter($cart['cart_kg']).'" /></td>
								<td width="75"><input type="text" name="cart_sub_kg'.$array_key.'" class="form-control width_100_percent shipping_live_change shipping_cart_sub_kg" id="shipping_cart_sub_kg" value="'.dataFilter($cart['cart_sub_kg']).'" /></td>
								<td width="75"><input type="text" name="cart_sub_kg_nw'.$array_key.'" class="form-control width_100_percent shipping_live_change shipping_cart_sub_kg_nw" id="shipping_cart_sub_kg_nw" value="'.dataFilter($cart['cart_sub_kg_nw']).'" /></td>
								<td width="75"><input type="text" name="cart_serially_numbered'.$array_key.'" class="form-control width_100_percent '.$hide_input_remark.' shipping_cart_serially_numbered" id="shipping_cart_serially_numbered" value="'.dataFilter($cart['cart_serially_numbered']).'" /></td>
								<td width="75" '.($boolean_rate ? 'style="display:none;"' : '').'><input type="text" name="cart_price'.$array_key.'" class="form-control width_100_percent '.$hide_input_remark.' shipping_live_change shipping_cart_price" id="shipping_cart_price" value="'.dataFilter($cart['cart_price']).'" /></td>
								<td width="75" '.($boolean_rate ? 'style="display:none;"' : '').'>
									<input type="text" name="cart_sub_total'.$array_key.'" class="form-control width_100_percent '.$hide_input_remark.' shipping_live_change shipping_cart_sub_total" id="shipping_cart_sub_total" value="'.dataFilter($cart['cart_sub_total']).'" />
									<label><input type="checkbox" name="cart_price_setting'.$array_key.'" value="foc" '.($cart['cart_price_setting'] == 'foc' ? 'checked' : '').' /> Show FOC</label>
								</td>
							</tr>' ;
						}else{
							// keep content in variable
							$table_content .= '
							<tr>
								<td width="30" style="text-align:center; '.$default_style1_show.' '.$default_td_break.'">'.($boolean_type_product ? $count_item : '&nbsp;').'</td>
								<td width="140" style="'.$default_style2_show.' '.$default_td_break.'">
									<strong>'.($boolean_type_product ? $title : '&nbsp;').'</strong>
									'.($boolean_type_product ? ($cart['cart_reference_number'] != '' ? '<div>'.dataFilterDash($cart['cart_reference_number']).'</div>' : '') : '').'
									'.($boolean_type_product ? ($cart['cart_buyer_code'] != '' ? '<div>'.dataFilterDash($cart['cart_buyer_code']).'</div>' : '') : '').'
								</td>
								<td width="290" style="'.$default_style2_show.' '.$default_td_break.'">
									'.($boolean_type_product ? '' : '<div><strong><u>** '.$title.'</u></strong></div>').'
									'.($cart['cart_description'] != '' ? '<div><strong>'.dataFilterDash($cart['cart_description']).'</strong></div>' : '').'
									'.($boolean_type_product ? '
										'.($cart['cart_dimension'] != '' ? '<div>'.dataFilterDash($cart['cart_dimension']).'</div>' : '').'
										'.($cart['cart_number_of_box'] != '' && $cart['cart_number_of_box'] != 'BOX A' ? '<div><strong style="text-decoration:underline;">'.$cart['cart_number_of_box'].'</strong></div>' : '').'
										'.($cart['cart_box_add_on'] != '' ? '<pre>'.dataFilterDash($cart['cart_box_add_on']).'</pre>' : '').'
										'.($cart['cart_hs_code'] != '' ? '<div>'.dataFilterDash($cart['cart_hs_code']).'</div>' : '').'
										'.($cart['cart_colour'] != '' ? '<div>COLOUR : '.dataFilterDash($cart['cart_colour']).'</div>' : '').'
									' : '').'
								</td>
								<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:center; font-weight:bold;">'.$quantity.'</td>' ;
								if ($boolean_invoice){
									$table_content .= '
									'.($setting['serially_numbered'] == '1' ? '<td width="100" style="'.$default_style2.' '.$default_td_break.' text-align:center;">'.dataFilter($cart['cart_serially_numbered']).'</td>' : '').'
									<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:center; font-weight:bold;">
										'.($boolean_type_product ? ($cart['cart_price'] > 0 ? numberFormatDigit(dataFilter($cart['cart_price']), 2) : ($cart['cart_price_setting'] == 'foc' ? 'FOC' : '')) : '&nbsp;').'
									</td>
									<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:center;">
										'.($boolean_type_product ? ($cart['cart_price'] > 0 ? numberFormatDigit(dataFilter($cart['cart_sub_total']), 2) : ($cart['cart_price_setting'] == 'foc' ? 'FOC' : '')) : '&nbsp;').'
									</td>' ;
								}else{
									$table_content .= '
									<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:center;">'.dataFilter($cart['cart_number_of_carton']).'</td>
									<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:center;">'.numberFormatDigit(dataFilter($cart['cart_m3']), 4).'</td>
									<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:center;">'.numberFormatDigit(dataFilter($cart['cart_sub_m3']), 4).'</td>
									<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:center;">'.numberFormatDigit(dataFilter($cart['cart_kg']), 2).'</td>
									<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:center;">'.numberFormatDigit(dataFilter($cart['cart_sub_kg']), 2).'</td>
									<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:center;">'.numberFormatDigit(dataFilter($cart['cart_sub_kg_nw']), 2).'</td>' ;
								}
							$table_content .= '
							</tr>' ;
						}
					}
					
					// check boolean edit
					if ($boolean_edit){
						
						// tr for container and seal no
						$table_content .= '
						<tr class="add_before_remark">
							<td><input type="text" name="container_sortable['.$key.']" class="form-control" value="'.$key.'" /></td>
							<td>
								<span class="display_none container_key">'.$key.'</span>
								<a href="#" class="btn btn-purple add_item_remark">Add Remark</a>
								<div class="margin_top_7px"><b>'.shippingNameType($array_container_title['cn'], 'cn').':</b><input type="text" name="container['.$key.']" class="form-control" value="'.$container_value.'" /></div>
								<div class="margin_top_3px"><b>'.shippingNameType($array_container_title['sn'], 'sn').':</b><input type="text" name="seal['.$key.']" class="form-control" value="'.$seal_value.'" /></div>
							</td>
							<td class="v_align_bottom"><input type="text" name="sub_quantity['.$key.']" class="form-control width_100_percent shipping_sub_quantity" value="'.$sub_quantity.'" /></td>
							<td class="v_align_bottom"><input type="text" name="sub_number_of_carton['.$key.']" class="form-control width_100_percent shipping_sub_number_of_carton" value="'.$number_of_carton_value.'" /></td>
							<td class="v_align_bottom"><span class="display_none"><input type="text" name="sub_m3_single['.$key.']" class="form-control width_100_percent shipping_sub_m3_single" value="'.$sub_m3_single_value.'" /></span></td>
							<td class="v_align_bottom"><input type="text" name="sub_m3['.$key.']" class="form-control width_100_percent shipping_sub_m3" value="'.$sub_m3_value.'" /></td>
							<td class="v_align_bottom"><span class="display_none"><input type="text" name="sub_kgs_gw_single['.$key.']" class="form-control width_100_percent shipping_sub_kgs_gw_single" value="'.$sub_kgs_gw_single_value.'" /></span></td>
							<td class="v_align_bottom"><input type="text" name="sub_kgs_gw['.$key.']" class="form-control width_100_percent shipping_sub_kgs_gw" value="'.$sub_kgs_gw_value.'" /></td>
							<td class="v_align_bottom"><input type="text" name="sub_kgs_nw['.$key.']" class="form-control width_100_percent shipping_sub_kgs_nw" value="'.$sub_kgs_nw_value.'" /></td>
							<td class="v_align_bottom">&nbsp;</td>
							<td class="v_align_bottom"  '.($boolean_rate ? 'style="display:none;"' : '').'>&nbsp;</td>
							<td class="v_align_bottom"  '.($boolean_rate ? 'style="display:none;"' : '').'><input type="text" name="sub_total['.$key.']" class="form-control width_100_percent shipping_sub_total" value="'.$sub_total_value.'" /></td>
						</tr>
						<tr style="display:none;"><td colspan="11"><input type="text" name="sub_total_wording['.$key.']" class="form-control width_100_percent shipping_sub_total_wording" value="'.$sub_total_wording.'" /></div></tr>' ;
					}else{
						if ($boolean_invoice){
							$table_content .= '
							<tr>
								<td style="'.$default_style1.' border-bottom:1px solid #000;">&nbsp;</td>
								<td style="'.$default_style3.' border-bottom:1px solid #000;">&nbsp;</td>
								<td style="'.$default_style3.' border-bottom:1px solid #000;">&nbsp;</td>
								<td style="'.$default_style3.' border-bottom:1px solid #000;">&nbsp;</td>
								<td style="'.$default_style3.' border-bottom:1px solid #000;">&nbsp;</td>
								<td style="'.$default_style3.' border-bottom:1px solid #000;">&nbsp;</td>
								'.($setting['serially_numbered'] == '1' ? '<td style="'.$default_style3.' border-bottom:1px solid #000;">&nbsp;</td>' : '').'
							</tr>' ;	
						}else{
							// tr for container and seal no
							$table_content .= '
							<tr>
								<td style="'.$default_style1.' padding:0 5px;">&nbsp;</td>
								<td style="'.$default_style3.'">&nbsp;</td>
								<td style="'.$default_style3.' color:#f00;">'.($boolean_invoice ? '&nbsp;' : '<strong>'.shippingNameType($array_container_title['cn'], 'cn').'</strong>: '.$container_value).'</td>
								<td style="'.$default_style3.'">&nbsp;</td>
								<td style="'.$default_style3.'">&nbsp;</td>
								<td style="'.$default_style3.'">&nbsp;</td>' ;
								if ($boolean_invoice){
									if ($setting['serially_numbered'] == '1'){
										$table_content .= '
										<td style="'.$default_style3.'">&nbsp;</td>' ;
									}
								}else{
									$table_content .= '
									<td style="'.$default_style3.'">&nbsp;</td>
									<td style="'.$default_style3.'">&nbsp;</td>
									<td style="'.$default_style3.'">&nbsp;</td>
									<td style="'.$default_style3.'">&nbsp;</td>' ;
								}
							$table_content .= '
							</tr>
							<tr>
								<td style="'.$default_style1.' '.$default_style5.'">&nbsp;</td>
								<td style="'.$default_style2.' '.$default_style5.'">&nbsp;</td>
								<td style="'.$default_style2.' '.$default_style5.' color:#f00;">'.($boolean_invoice ? '&nbsp;' : '<strong>'.shippingNameType($array_container_title['sn'], 'sn').'</strong>: '.$seal_value).'</td>
								<td style="'.$default_style4.' '.$default_style5.' text-align:center;">'.($boolean_invoice ? $sub_quantity : '<b>SUB TOTAL</b>').'</td>' ;
								if ($boolean_invoice){
									$table_content .= '
									'.($setting['serially_numbered'] == '1' ? '<td style="'.$default_style4.' '.$default_style5.' text-align:center;">&nbsp;</td>' : '').'
									<td style="'.$default_style4.' '.$default_style5.' text-align:center;"><b>SUB TOTAL</b></td>
									<td style="'.$default_style4.' '.$default_style5.' text-align:center;">'.numberFormatDigit($sub_total_value, 2).'</td>' ;
								}else{
									$table_content .= '
									<td style="'.$default_style4.' '.$default_style5.' text-align:center;">'.$number_of_carton_value.'</td>
									<td style="'.$default_style4.' '.$default_style5.' text-align:center;"><!--'.numberFormatDigit($sub_m3_single_value, 4).'--></td>
									<td style="'.$default_style4.' '.$default_style5.' text-align:center;">'.numberFormatDigit($sub_m3_value, 4).'</td>
									<td style="'.$default_style4.' '.$default_style5.' text-align:center;"><!--'.numberFormatDigit($sub_kgs_gw_single_value, 2).'--></td>
									<td style="'.$default_style4.' '.$default_style5.' text-align:center;">'.numberFormatDigit($sub_kgs_gw_value, 2).'</td>
									<td style="'.$default_style4.' '.$default_style5.' text-align:center;">'.numberFormatDigit($sub_kgs_nw_value, 2).'</td>' ;
								}
							$table_content .= '
							</tr>' ;
						}
					}
					
					// keep result in container no and seal no array
					$continue_container_no .= $container_value . ', ' ;
					$continue_seal_no .= $seal_value . ', ' ;
				}
				
				// check boolean edit
				if ($boolean_edit){
							$table_content .= '
							</td>
						</tr>
					</table>' ;
				}
			}
			
			// check boolean edit
			if ($boolean_edit){
				
				// count total container amount
				$table_content .= '
				<tr>
					<td>
						<table border="0" cellpadding="0" cellspacing="0" width="100%" class="table-striped table_shipping_list_sub">
							<tr>
								<td class="v_align_middle text_right"><b>TOTAL NUMBER OF CARTON</b></td>
								<td width="150"><input type="text" name="total_number_of_carton" class="form-control width_100_percent shipping_total_number_of_carton" value="'.$total_number_of_carton.'" /></td>
							</tr>
							<tr class="display_none">
								<td class="v_align_middle text_right"><b>TOTAL CBM</b></td>
								<td><input type="text" name="total_m3_single" class="form-control width_100_percent shipping_total_m3_single" value="'.$total_m3_single.'" /></td>
							</tr>
							<tr>
								<td class="v_align_middle text_right"><b>TOTAL CBM (ALL)</b></td>
								<td><input type="text" name="total_m3" class="form-control width_100_percent shipping_total_m3" value="'.$total_m3.'" /></td>
							</tr>
							<tr class="display_none">
								<td class="v_align_middle text_right"><b>TOTAL GW (KGS)</b></td>
								<td><input type="text" name="total_kgs_gw_single" class="form-control width_100_percent shipping_total_kgs_gw_single" value="'.$total_kgs_gw_single.'" /></td>
							</tr>
							<tr>
								<td class="v_align_middle text_right"><b>TOTAL KGS (GW)</b></td>
								<td><input type="text" name="total_kgs_gw" class="form-control width_100_percent shipping_total_kgs_gw" value="'.$total_kgs_gw.'" /></td>
							</tr>
							<tr>
								<td class="v_align_middle text_right"><b>TOTAL KGS (NW)</b></td>
								<td><input type="text" name="total_kgs_nw" class="form-control width_100_percent shipping_total_kgs_nw" value="'.$total_kgs_nw.'" /></td>
							</tr>
							<tr '.($boolean_rate ? 'style="display:none;"' : '').'>
								<td class="v_align_middle text_right"><b>TOTAL PRICE</b></td>
								<td><input type="text" name="total" class="form-control width_100_percent shipping_total" value="'.$total.'" /></td>
							</tr>
							<tr '.($boolean_rate ? 'style="display:none;"' : '').'>
								<td colspan="2"><input type="text" name="total_wording" class="form-control width_100_percent shipping_total_wording" value="'.$total_wording.'" /></td>
							</tr>
						</table>
					</td>
				</tr>
				<tr '.($boolean_rate ? 'style="display:none;"' : '').'>
					<td>
						<table border="0" cellpadding="0" cellspacing="0" width="100%" class="table-striped table_shipping_list_sub">
							<tr class="service_charge_invoice_tr" id="add_charge_invoice_tr">
								<td class="v_align_middle text_right"></td>
								<td width="150"><a href="#" id="add_charge_invoice">
									Add Charge</a>
									<span class="display_none invoice_current_td">1</td>
								</td>
							</tr>' ;
							if (arrayCheck($array_service)){
								foreach($array_service as $key => $value){
									$table_content .= '
									<tr class="service_charge_invoice_tr service_charge_invoice_append">
										<td class="v_align_middle text_right">
											<button class="btn btn-purple charge_invoice_remove" id="charge_invoice_remove">Remove</button>
											<input type="text" name="service_name['.$key.']" class="form-control width_100_percent float_right" style="width:150px;" value="'.dataFilter(strtoupper($value['name'])).'">
										</td>
										<td>
											<input type="hidden" name="service_charge_id[]" class="service_charge_id" value="'.$key.'">
											<input type="hidden" name="service_charge_my['.$key.']" class="form-control width_100_percent" value="'.$value['charge_my'].'">
											
											<input type="text" name="service_charge['.$key.']" class="form-control width_100_percent shipping_live_change" value="'.$value['charge'].'">
										</td>
									</tr>' ;
									$check_grand_total += $value['charge'] ;
								}
							}
							$table_content .= '
							<tr>
								<td class="v_align_middle text_right"><b>GRAND TOTAL</b></td>
								<td><input type="text" name="total_grand" class="form-control width_100_percent shipping_total_grand" value="'.$total_grand.'" /></td>
							</tr>
						</table>
					</td>
				</tr>' ;
			}else{
				if ($boolean_invoice){
					// count total container amount
					$table_content .= '
					<tr>
						<td style="'.$default_style4.' border-top:0; border-left:0; border-bottom:1px solid #000; text-align:left;" colspan="3"><b>'.$total_wording.'</b></td>
						<td style="'.$default_style4.' border-top:0; text-align:right; vertical-align:middle" colspan="'.($setting['serially_numbered'] == '1' ? '3' : '2').'">TOTAL :</td>
						<td style="'.$default_style4.' border-top:0; text-align:center; vertical-align:middle">'.numberFormatDigit($total, 2).'</td>
					</tr>' ;
					if (arrayCheck($array_service)){
						foreach($array_service as $key => $value){
							$table_content .= '
							<tr>
								<td style="'.$default_style4.' border-top:0; border-left:3px solid #fff; text-align:left;" colspan="3">&nbsp;</td>
								<td style="'.$default_style4.' border-top:0; border-left:0; text-align:right; vertical-align:middle" colspan="'.($setting['serially_numbered'] == '1' ? '3' : '2').'">'.strtoupper($value['name']).' : </td>
								<td style="'.$default_style4.' border-top:0; text-align:center; border-top:1px solid #000; vertical-align:middle">'.numberFormatDigit($value['charge'], 2).'</td>
							</tr>' ;
							$check_grand_total += $value['charge'] ;
						}
					}
					$table_content .= '
					<tr>
						<td style="'.$default_style6.' text-align:left; border-left:3px solid #fff; border-bottom:3px solid #fff;" colspan="3">&nbsp;</td>
						<td style="'.$default_style6.' text-align:right; border-bottom:3px solid #fff;" colspan="'.($setting['serially_numbered'] == '1' ? '3' : '2').'">GRAND TOTAL :</td>
						<td style="'.$default_style4.' text-align:center; vertical-align:middle">'.numberFormatDigit($total_grand, 2).'</td>
					</tr>' ;
				}else{
					// count total container amount
					$table_content .= '
					<tr>
						<td style="'.$default_style4.' border-top:0; text-align:right; border-left:0;" colspan="4"><b>GRAND TOTAL</b></td>
						<td style="'.$default_style4.' border-top:0; text-align:center;">'.$total_number_of_carton.'</td>
						<td style="'.$default_style4.' border-top:0; text-align:center;"><!--'.numberFormatDigit($total_m3_single, 4).'--></td>
						<td style="'.$default_style4.' border-top:0; text-align:center;">'.numberFormatDigit($total_m3, 4).'</td>
						<td style="'.$default_style4.' border-top:0; text-align:center;"><!--'.numberFormatDigit($total_kgs_gw_single, 2).'--></td>
						<td style="'.$default_style4.' border-top:0; text-align:center;">'.numberFormatDigit($total_kgs_gw, 2).'</td>
						<td style="'.$default_style4.' border-top:0; text-align:center;">'.numberFormatDigit($total_kgs_nw, 2).'</td>
					</tr>' ;
				}
			}
			
			if ( numberFormat($check_grand_total) != numberFormat($total_grand) ){
				$check_boolean_grand_total = false ;
			}
			
		// close the table
		$table_content .= '
		</table>' ;
	}
	// keep all result in array
	$array['content'] = $table_content ;
	$array['container_no'] = substr(trim($continue_container_no), 0, -1) ;
	$array['seal_no'] = substr(trim($continue_seal_no), 0, -1) ;
	
	$array['boolean_sub_total'] = $check_boolean_sub_total ;
	$array['boolean_grand_total'] = $check_boolean_grand_total ;
	
	// return result
	return $array ;
}

// shipping container list for tax invoice
function shippingContainerInvoice2($company_name, $array_invoice, $setting, $page_mode, $page_type, $array_key, $quotation_rate_type = 'usd'){
	global $table_content_container, $table_content_seal, $array_container_title ;
	
	$boolean_edit = ($page_mode == 'edit' ? true : false) ;
	// split to 3 variable
	$company_name = ($array_invoice['company_name'] != '' ? $array_invoice['company_name'] : $company_name) ;
	$array_product = $array_invoice['item'] ;
	$array_service = $array_invoice['service'] ;
	$array_total = $array_invoice['total'] ;
	$container_no = $array_invoice['container_no'] ;
	$seal_no = $array_invoice['seal_no'] ;
	
	$table_content = '' ;
	
	$text_price_type = ($quotation_rate_type == 'myr' ? 'MYR' : ($quotation_rate_type == 'eur' ? 'EUR' : 'USD')) ;
	$text_price_wording = ($quotation_rate_type == 'myr' ? str_replace('US DOLLAR', 'SAY TOTAL IN MYR', $array_total['cart_grand_total_wording']) : $array_total['cart_grand_total_wording']) ;
	$text_price_wording = ($quotation_rate_type == 'eur' ? str_replace('US DOLLAR', 'SAY TOTAL IN EURO', $array_total['cart_grand_total_wording']) : $array_total['cart_grand_total_wording']) ;
	
	// check if array exsits
	if (arrayCheck($array_product)){
		// if page mode = print
		if ($page_mode == 'print'){
			$default_table = 'text-transform:uppercase; border-collapse:separate; border:1px solid #000; border-bottom:0; font-size:14px; border-collapse: collapse;' ;
			$default_th = 'border-bottom:1px solid #000; vertical-align:middle;' ;
			$default_style = 'text-transform:uppercase; border:0; padding:5px 5px 0 5px; vertical-align:top;' ;
			$default_style1 = $default_style1_add = $default_style ;
			// $default_style1_add .= 'padding:0 5px 5px;' ;
			$default_style2 = $default_style2_add = $default_style.'border-left:1px solid #000;' ;
			// $default_style2_add .= 'padding:0 5px 5px;' ;
			$default_style3 = $default_style.'border-left:1px solid #000; padding:0 5px;' ;
			$default_style4 = $default_style.'border-left:1px solid #000; border-top:1px solid #000; font-weight:bold;' ;
			$default_style5 = 'border-bottom:1px solid #000;' ;
			$default_style6 = $default_style.' font-weight:bold;' ;
		}
		// keep content in variable
		$count_main_tr = 6 ;
		// keep content in variable
		if ($boolean_edit){ 
			$table_content .= '
			<table width="100%" border="0" cellpadding="0" cellspacing="0" class="doc_loading_details">
				<tr>
					<td width="180">COMPANY NAME</td>
					<td width="10">:</td>
					<td><input type="text" name="company_name" class="form-control" placeholder="COMPANY NAME" value="'.$company_name.'"></td>
				</tr>
				<tr>
					<td><input type="text" name="container_no_title" class="form-control" value="'.shippingNameType($array_container_title['cn'], 'cn').'" required '.$input_readonly.' />
					<td>:</td>
					<td><input type="text" name="container_no" class="form-control width_100_percent" value="'.($container_no != '' ? $container_no : $table_content_container).'"></td>
				</tr>
				<tr>
					<td><input type="text" name="seal_no_title" class="form-control" value="'.shippingNameType($array_container_title['sn'], 'sn').'" required '.$input_readonly.' />
					<td>:</td>
					<td><input type="text" name="seal_no" class="form-control width_100_percent" value="'.($seal_no != '' ? $seal_no : $table_content_seal).'"></td>
				</tr>
			</table>' ;
		}
		$table_content .= '
		<span style="display:none;" id="current_page_type">invoice</span>
		<table border="0" cellpadding="0" cellspacing="0" width="100%" class="table_shipping_list table-striped table_shipping_list_sub" style="'.$default_table.'">
			<thead style="display:table-header-group">
				<tr>
					<th style="'.$default_style1.' '.$default_th.'">ITEM</th>
					<th style="'.$default_style2.' '.$default_th.'">ITEM DESCRIPTION</th>
					<th style="'.$default_style2.' '.$default_th.'">DESCRIPTION & DIMENSION (mm)</th>
					<th style="'.$default_style2.' '.$default_th.'">QTY PCS</th>' ;
					if ($boolean_edit || $setting == '1'){
						$table_content .= 
						'<th style="'.$default_style2.' '.$default_th.'">SERIALLY<br />NUMBERED</th>' ;
						$count_main_tr++ ;
					}
					$table_content .= '
					<th style="'.$default_style2.' '.$default_th.'">UNIT PRICE<br />'.$text_price_type.' / PC</th>
					<th style="'.$default_style2.' '.$default_th.'">AMOUNT<br />'.$text_price_type.'</th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<td height="1px" style="border:0;border-top:1px solid #000;" colspan="'.$count_main_tr.'"></td>
				</tr>
			</tfoot>' ;
			
			// loop all array
			$count_item = 0 ;
			$count_key = 0 ;
			$check_sub_total = 0 ;
 			$check_grand_total = 0 ;
			$check_gst_charge = 0 ;
			$check_boolean_sub_total = true ;
			$check_boolean_grand_total = true ;
			$check_boolean_gst_charge = true ;
			
			foreach($array_product as $key => $cart){
				
				$title = dataFilter($cart['cart_title']) ;
				$cart_type = dataFilter($cart['cart_type']) ;
				
				// check show td mode
				$default_style1_show = ($boolean_type_product ? $default_style1 : $default_style1_add) ;
				$default_style2_show = ($boolean_type_product ? $default_style2 : $default_style2_add) ;
				$boolean_type_product = ($cart_type == 'product' ? true : false) ;
				
				// count value
				($boolean_type_product ? $count_item++ : '') ;
				
				// count sub total
				$check_sub_total = ( $cart['cart_quantity'] * dataFilter($cart['cart_price']) ) ;
				$check_grand_total += $check_sub_total ;
				$current_cart_sub_total = (dataFilter($cart['cart_sub_total'])) ;
				$current_cart_sub_total = ($current_cart_sub_total != '' ? $current_cart_sub_total : '0') ;
				if ( numberFormat($check_sub_total) != numberFormat(dataFilter($current_cart_sub_total)) ){
					$check_boolean_sub_total = false ;
				}
				
				// page mode edit loop multiple time | print only print 1 time
				if ($boolean_edit){
					
					$array_key = '['.$count_key.']' ;
					$count_key++ ;
					
					// keep content in variable
					$table_content .= '
					<tr class="add_remark_box">
						<td width="25" class="text_center">
							<label>'.($boolean_type_product ? $count_item : '&nbsp;').'</label>
							<input type="hidden" name="cart_hidden_text'.$array_key.'" value="1" />
							<input type="hidden" name="cart_id'.$array_key.'" value="'.dataFilter($cart['cart_id']).'" />
							<input type="hidden" name="cart_type'.$array_key.'" value="'.$cart_type.'" />
							<input type="hidden" name="cart_specification'.$array_key.'" value="'.dataFilter($cart['cart_specification']).'" />
							<input type="hidden" name="cart_image'.$array_key.'" value="'.dataFilter($cart['cart_image']).'" />
							<input type="hidden" name="cart_parent_key'.$array_key.'" value="'.dataFilter($cart['cart_parent_key']).'" />
							<input type="hidden" name="cart_kg'.$array_key.'" value="'.dataFilter($cart['cart_kg']).'" />
							<input type="hidden" name="cart_sub_kg'.$array_key.'" value="'.dataFilter($cart['cart_sub_kg']).'" />
							<input type="hidden" name="cart_m3'.$array_key.'" value="'.dataFilter($cart['cart_m3']).'" />
							<input type="hidden" name="cart_sub_m3'.$array_key.'" value="'.dataFilter($cart['cart_sub_m3']).'" />
							<input type="hidden" name="cart_number_of_box'.$array_key.'" value="'.dataFilter($cart['cart_number_of_box']).'" />
							<input type="hidden" name="cart_default_price'.$array_key.'" value="'.dataFilter($cart['cart_default_price']).'" />
							<input type="hidden" name="cart_price_my'.$array_key.'" value="'.dataFilter($cart['cart_price_my']).'" />
							<input type="hidden" name="cart_sub_total_my'.$array_key.'" value="'.dataFilter($cart['cart_sub_total_my']).'" />
							<input type="hidden" name="cart_ori_price'.$array_key.'" value="'.dataFilter($cart['cart_ori_price']).'" />
							
							<input type="hidden" name="cart_quotation_id'.$array_key.'" value="'.dataFilter($cart['cart_quotation_id']).'" />
							<input type="hidden" name="cart_quotation_so2'.$array_key.'" value="'.dataFilter($cart['cart_quotation_so2']).'" />
							<input type="hidden" name="cart_quotation_status'.$array_key.'" value="'.dataFilter($cart['cart_quotation_status']).'" />
							<input type="hidden" name="cart_this_key'.$array_key.'" value="'.dataFilter($cart['cart_this_key']).'" />
							<br />
							<input type="text" name="cart_sortable'.$array_key.'" class="width_80 text_center margin_top_3px" value="'.$count_key.'" />
						</td>
						<td width="200">
							<input type="text" name="cart_title'.$array_key.'" class="form-control" placeholder="MODEL" value="'.$title.'" />
							<input type="text" name="cart_reference_number'.$array_key.'" class="form-control" placeholder="REFERENCE NUMBER" value="'.dataFilter($cart['cart_reference_number']).'" />
							<input type="text" name="cart_buyer_code'.$array_key.'" class="form-control" placeholder="BUYER CODE" value="'.dataFilter($cart['cart_buyer_code']).'" />
							<input type="text" name="cart_hs_code'.$array_key.'" class="form-control" placeholder="HS CODE" value="'.dataFilter($cart['cart_hs_code']).'" />
							<input type="text" name="cart_material'.$array_key.'" class="form-control" placeholder="MATERIAL" value="'.dataFilter($cart['cart_material']).'" />
						</td>
						<td width="200">
							<input type="text" name="cart_description'.$array_key.'" class="form-control margin_top_3px" placeholder="DESCRIPTION" value="'.dataFilter($cart['cart_description']).'" />
							<input type="'.$input_type_remark.'" name="cart_dimension'.$array_key.'" class="form-control margin_top_3px" placeholder="DIMENSION" value="'.dataFilter($cart['cart_dimension']).'" />
							<input type="'.$input_type_remark.'" name="cart_colour'.$array_key.'" class="form-control margin_top_3px" placeholder="COLOUR" value="'.dataFilter($cart['cart_colour']).'" />
						</td>
						<td width="75"><input type="text" name="cart_quantity'.$array_key.'" class="form-control width_100_percent shipping_live_change shipping_cart_quantity" id="shipping_cart_quantity" value="'.$cart['cart_quantity'].'" /></td>
						<td width="75"><input type="text" name="cart_serially_numbered'.$array_key.'" class="form-control width_100_percent '.($boolean_type_product ? '' : 'display_none').' cart_serially_numbered" value="'.dataFilter($cart['cart_serially_numbered']).'" /></td>
						<td width="75"><input type="text" name="cart_price'.$array_key.'" class="form-control width_100_percent shipping_live_change shipping_cart_price" id="shipping_cart_price" value="'.dataFilter($cart['cart_price']).'" /></td>
						<td width="75">
							<input type="text" name="cart_sub_total'.$array_key.'" class="form-control width_100_percent shipping_live_change shipping_cart_sub_total" id="shipping_cart_sub_total" value="'.dataFilter($cart['cart_sub_total']).'" />
							<label><input type="checkbox" name="cart_price_setting'.$array_key.'" value="foc" '.($cart['cart_price_setting'] == 'foc' ? 'checked' : '').' /> Show FOC</label>
						</td>
					</tr>' ;
					
					// extra description
					if ($boolean_type_product){
						$table_content .= '
						<tr>
							<td width="25" class="text_center">&nbsp;</td>
							<td width="200"><textarea name="cart_title_extra'.$array_key.'" class="width_100_percent height_100">'.dataFilter($cart['cart_title_extra']).'</textarea></td>
							<td width="200"><textarea name="cart_description_extra'.$array_key.'" class="width_100_percent height_100">'.dataFilter($cart['cart_description_extra']).'</textarea></td>
							<td width="75"><textarea name="cart_quantity_extra'.$array_key.'" class="width_100_percent height_100">'.dataFilter($cart['cart_quantity_extra']).'</textarea></td>
							<td width="75"><textarea name="cart_serially_numbered_extra'.$array_key.'" class="width_100_percent height_100">'.dataFilter($cart['cart_serially_numbered_extra']).'</textarea></td>
							<td width="75"><textarea name="cart_price_extra'.$array_key.'" class="width_100_percent height_100">'.dataFilter($cart['cart_price_extra']).'</textarea></td>
							<td width="75"><textarea name="cart_sub_total_extra'.$array_key.'" class="width_100_percent height_100">'.dataFilter($cart['cart_sub_total_extra']).'</textarea></td>
						</tr>' ;
					}
					
				}else{
					if ($boolean_type_product){
						$cart_price = numberFormatDigit(dataFilter($cart['cart_price']), 2) ;
						$cart_sub_total = numberFormatDigit(dataFilter($cart['cart_sub_total']), 2) ;
						
						// keep content in variable
						$table_content .= '
						<tr>
							<td width="30" style="text-align:center; '.$default_style1_show.' '.$default_td_break.'">'.($boolean_type_product ? $count_item : '&nbsp;').'</td>
							<td width="100" style="'.$default_style2_show.' '.$default_td_break.'">
								<strong>'.($boolean_type_product ? $title : '&nbsp;').'</strong>
								'.($boolean_type_product ? ($cart['cart_reference_number'] != '' ? '<div>'.dataFilterDash($cart['cart_reference_number']).'</div>' : '') : '').'
								'.($boolean_type_product ? ($cart['cart_buyer_code'] != '' ? '<div>'.dataFilterDash($cart['cart_buyer_code']).'</div>' : '') : '').'
							</td>
							<td width="330" style="'.$default_style2_show.' '.$default_td_break.'">
								'.($boolean_type_product ? '' : '<div><strong><u>** '.$title.'</u></strong></div>').'
								'.($cart['cart_description'] != '' ? '<div><strong>'.dataFilterDash($cart['cart_description']).'</strong></div>' : '').'
								'.($boolean_type_product ? '
									'.($cart['cart_dimension'] != '' ? '<div>'.dataFilterDash($cart['cart_dimension']).'</div>' : '').'
									'.($cart['cart_hs_code'] != '' ? '<div>'.dataFilterDash($cart['cart_hs_code']).'</div>' : '').'
									'.($cart['cart_material'] != '' ? '<div>'.dataFilterDash($cart['cart_material']).'</div>' : '').'
									'.($cart['cart_colour'] != '' ? '<div>COLOUR : '.dataFilterDash($cart['cart_colour']).'</div>' : '').'
								' : '').'
							</td>
							<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:center;">'.dataFilter($cart['cart_quantity']).'</td>
							'.($setting == '1' ? '<td width="100" style="'.$default_style2.' '.$default_td_break.' text-align:center;">'.dataFilter($cart['cart_serially_numbered']).'</td>' : '').'
							<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:right; font-weight:bold;">
								'.($boolean_type_product ? ($cart_price > 0 ? $cart_price : ($cart['cart_price_setting'] == 'foc' ? 'FOC' : '')) : '&nbsp;').'
							</td>
							<td width="100" style="'.$default_style2_show.' '.$default_td_break.' text-align:right;">
								'.($boolean_type_product ? ($cart_sub_total > 0 ? $cart_sub_total : ($cart['cart_price_setting'] == 'foc' ? 'FOC' : '')) : '&nbsp;').'
							</td>
						</tr>' ;
						// extra description
						if ($boolean_type_product){
							// check if extra title null
							if (trim($cart['cart_title_extra'])){
								$table_content .= '
								<tr class="add_remark_box">
									<td style="text-align:center; vertical-align:top; '.$default_style1_show.' '.$default_td_break.'">&nbsp;</td>
									<td style="color:#a54706; font-style: italic; vertical-align:top; '.$default_style2_show.' '.$default_td_break.'">'.nl2br(dataFilter($cart['cart_title_extra'])).'</td>
									<td style="vertical-align:top; '.$default_style2_show.' '.$default_td_break.'">'.nl2br(dataFilter($cart['cart_description_extra'])).'</td>
									<td style="vertical-align:top; '.$default_style2_show.' '.$default_td_break.' text-align:center;">'.nl2br(dataFilter($cart['cart_quantity_extra'])).'</td>
									'.($setting == '1' ? '<td style="vertical-align:top; '.$default_style2_show.' '.$default_td_break.' text-align:center;">'.nl2br(dataFilter($cart['cart_serially_numbered_extra'])).'</td>' : '').'
									<td style="vertical-align:top; '.$default_style2_show.' '.$default_td_break.' text-align:right;">'.nl2br(dataFilter($cart['cart_price_extra'])).'</td>
									<td style="vertical-align:top; '.$default_style2_show.' '.$default_td_break.' text-align:right;">'.nl2br(dataFilter($cart['cart_sub_total_extra'])).'</td>
								</tr>' ;
							}
						}
					}
				}
			}
			
			$current_cart_total = $array_total['cart_total'] ;
			$current_cart_total = ($current_cart_total != '' ? $current_cart_total : '0') ;
			if ( numberFormat($check_grand_total) != numberFormat($current_cart_total) ){
				$check_boolean_grand_total = false ;
			}
			
			if ($quotation_rate_type == 'myr'){
				// count gst charge
				$check_gst_charge = numberFormat(checkZeroValue($check_grand_total * GSTCHARGE / 100)) ;
				$check_grand_total += $check_gst_charge ;
				if ( numberFormat(checkZeroValue($check_gst_charge)) != numberFormat(checkZeroValue($array_total['cart_gst_charge'])) ){
					$check_boolean_gst_charge = false ;
				}
			}
			
			// check if edit or not
			if ($boolean_edit){
				
				// total amount
				$table_content .= '
				<tr class="add_before_remark">
					<td colspan="'.($count_main_tr - 1).'"><a href="#" class="btn btn-purple add_item_remark">Add Item</a></td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td colspan="'.($count_main_tr - 1).'" class="v_align_middle text_right"><b>TOTAL PRICE</b></td>
					<td><input type="text" name="cart_total" class="form-control width_100_percent shipping_total" value="'.$array_total['cart_total'].'"></td>
				</tr>
				<tr class="service_charge_invoice_tr" id="add_charge_invoice_tr">
					<td colspan="'.($count_main_tr - 1).'" class="v_align_middle text_right"></td>
					<td><a href="#" id="add_charge_invoice">
						Add Charge</a>
						<span class="display_none invoice_current_td">'.($count_main_tr - 1).'</td>
					</td>
				</tr>' ;
				
				if (arrayCheck($array_service)){
					foreach($array_service as $key => $value){
						$table_content .= '
						<tr class="service_charge_invoice_tr service_charge_invoice_append">
							<td colspan="'.($count_main_tr - 1).'" class="v_align_middle text_right">
								<button class="btn btn-purple charge_invoice_remove" id="charge_invoice_remove">Remove</button>
								<input type="text" name="service_name['.$key.']" class="form-control width_100_percent float_right" style="width:150px;" value="'.dataFilter(strtoupper($value['name'])).'">
							</td>
							<td>
								<input type="hidden" name="service_charge_id[]" class="service_charge_id" value="'.$key.'">
								<input type="hidden" name="service_charge_my['.$key.']" class="form-control width_100_percent" value="'.$value['charge_my'].'">
								<input type="text" name="service_charge['.$key.']" class="form-control width_100_percent shipping_live_change" value="'.$value['charge'].'">
							</td>
						</tr>' ;
						$check_grand_total += $value['charge'] ;
					}
				}
				$table_content .= '
				<tr>
					<td colspan="'.($count_main_tr - 1).'" class="v_align_middle text_right"><b>GST CHARGE</b></td>
					<td><input type="text" name="cart_gst_charge" class="form-control width_100_percent shipping_gst_charge" value="'.$array_total['cart_gst_charge'].'"></td>
				</tr>
				<tr>
					<td colspan="'.($count_main_tr - 1).'" class="v_align_middle text_right"><b>GRAND TOTAL</b></td>
					<td>
						<input type="text" name="cart_grand_total" class="form-control width_100_percent shipping_total_grand" value="'.$array_total['cart_grand_total'].'">
						<input type="hidden" name="default_cart_grand_total" class="form-control width_100_percent" value="'.$array_total['cart_grand_total'].'">
					</td>
				</tr>
				<tr>
					<td colspan="'.($count_main_tr).'">
						<input type="text" name="cart_grand_total_wording" class="form-control width_100_percent cart_grand_total_wording" value="'.$text_price_wording.'">
						<input type="hidden" name="cart_service_box" value="'.base64_encode(serialize($array_service)).'">
						<input type="hidden" name="cart_total_box" value="'.base64_encode(serialize($array_total)).'">
					</td>
				</tr>' ;
			}else{
				// total amount
				$table_content .= '
				<tr>
					<td colspan="'.($count_main_tr - 3).'" style="font-style: italic; text-transform:uppercase; border-top:1px solid #000; border-bottom:1px solid #000; padding:5px; vertical-align:top;"><b>'.$text_price_wording.'</b></td>
					<td colspan="2" style="border-top:1px solid #000; border-left:1px solid #000; text-align:right; padding:5px; font-weight:bold;">TOTAL : </td>
					<td style="border-top:1px solid #000; border-left:1px solid #000; text-align:right; padding:5px;">'.numberFormatDigit($array_total['cart_total'], 2).'</td>
				</tr>' ;
				if (arrayCheck($array_service)){
					foreach($array_service as $key => $value){
						$table_content .= '
						<tr>
							<td colspan="'.($count_main_tr - 3).'" style="border-left:3px solid #fff;">&nbsp;</td>
							<td colspan="2" style="border-bottom:0; text-align:right; padding:5px; font-weight:bold;">'.strtoupper($value['name']).' : </td>
							<td style="border-top:1px solid #000; border-left:1px solid #000; text-align:right; padding:5px;">'.numberFormatDigit($value['charge'], 2).'</td>
						</tr>' ;
						$check_grand_total += $value['charge'] ;
					}
				}
				if ($array_key != 'gst'){
					$table_content .= '
					<tr>
						<td colspan="'.($count_main_tr - 3).'" style="border-left:3px solid #fff;">&nbsp;</td>
						<td colspan="2" style="text-align:right; padding:5px; font-weight:bold;">GST @ '.GSTCHARGE.'% : </td>
						<td style="border-top:1px solid #000; border-left:1px solid #000; text-align:right; padding:5px;">'.numberFormatDigit(checkZeroValue($array_total['cart_gst_charge']), 2).'</td>
					</tr>' ;
				}else{
					$without_gst_td = 'border-top:1px solid #000;' ;
				}
				$table_content .= '
				<tr>
					<td colspan="'.($count_main_tr - 3).'" style="border-left:3px solid #fff; border-bottom:3px solid #fff; '.$without_gst_td.' border-top:0;">&nbsp;</td>
					<td colspan="2" style="border-bottom:3px solid #fff; text-align:right; padding:5px; font-weight:bold;">GRAND TOTAL : </td>
					<td style="text-align:right; border-top:1px solid #000; border-left:1px solid #000; padding:5px;"><b>'.numberFormatDigit($array_total['cart_grand_total'], 2).'</b></td>
				</tr>' ;
			}
			
			if ( numberFormat($check_grand_total) != numberFormat($array_total['cart_grand_total']) ){
				$check_boolean_grand_total = false ;
			}
			
		$table_content .= '
		</table>' ;
	}
	// keep all result in array
	$array['content'] = $table_content ;
	$array['company_name'] = $company_name ;
	$array['container_no'] = $container_no ;
	$array['seal_no'] = $seal_no ;
	
	$array['boolean_sub_total'] = $check_boolean_sub_total ;
	$array['boolean_grand_total'] = $check_boolean_grand_total ;
	$array['boolean_gst_charge'] = $check_boolean_gst_charge ;
	
	// return result
	return $array ;
}

// count box
function countNumberOfBox($value){
	// basic information
	$array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z') ;
	// set value in for lop
	$continue = '' ;
	if ($value > 0){
		for ($a = 0; $a < $value; $a++){
			$continue .= ($a == 0 ? '' : ' + ') . $array[$a] ;
		}
	}
	return $continue ;
}

// show container volume
function showContainerVolume($mysqli, $prefixPost, $prefixContainerVolume, $head_id){
	// total container volume
	$mysqli_volume = $mysqli->query("SELECT post_id, post_title FROM $prefixPost
									WHERE post_title != '' AND post_type = 'container-volume' AND post_categories = 'container-volume' AND post_trash = '0' ORDER BY post_order") ;
	// default variable
	$default_volume = '' ;
	// check if container exsits
	if ($mysqli_volume->num_rows > 0){
		// loop all container
		while ($row_volume = $mysqli_volume->fetch_array(MYSQLI_ASSOC)){
			// check total volume
			$mysqli_volume_quantity = $mysqli->query("SELECT * FROM $prefixContainerVolume
													WHERE container_volume_head_id = '".$head_id."' AND container_volume_post_id = '".$row_volume['post_id']."' AND container_volume_trash = '0'") ;
			// total quantity
			$total = $mysqli_volume_quantity->num_rows ;
			if ($total > 0){
				// show content
				$default_volume .= 
				$total.' X '.dataFilter($row_volume['post_title']).", " ;
			}
		}
	}
	// remove last comma
	return removeLastComma($default_volume) ;
}

// get so number
function soNumber($type){
	global $mysqli, $prefixCombineHead, $prefixQuotation ;
	// check type
	switch($type){
		case 'order-combine' :
			// query for order combine
			$mysqli_quotation = $mysqli->query("SELECT combine_head_date FROM $prefixCombineHead
												WHERE DATE(combine_head_date) = '".date('Y-m-d')."'") ;
		break ;
		case 'quotation' :
		case 'order-draft' :
		case 'order' :
			// query for order
			$mysqli_quotation = $mysqli->query("SELECT quotation_date FROM $prefixQuotation
												WHERE quotation_type = '".$type."' AND DATE(quotation_date) = '".date('Y-m-d')."'") ;
		break ;	
	}
	// default variable
	$so = $mysqli_quotation->num_rows ;
	$date = date('Ymd') ;
	// check if query exsits
	$so = ($so > 0 ? $so : 1) ;
	// set boolean = true
	$boolean_so = true ;
	// get so number
	while($boolean_so){
		// temp so
		$so_temp = $date . strPad(5, $so) ;
		// check type
		switch($type){
			case 'draft-order-combine' :
				$so_temp = 'DL'.$so_temp;
				// query for order combine
				$mysqli_quotation = $mysqli->query("SELECT * FROM $prefixCombineHead
													WHERE combine_head_so = '".$so_temp."' AND DATE(combine_head_date) = '".date('Y-m-d')."' LIMIT 1") ;
			break ;
			case 'order-combine' :
				// query for order combine
				$mysqli_quotation = $mysqli->query("SELECT * FROM $prefixCombineHead
													WHERE combine_head_so = '".$so_temp."' AND DATE(combine_head_date) = '".date('Y-m-d')."' LIMIT 1") ;
			break ;
			case 'quotation' :
			case 'order-draft' :
			case 'order' :
				// query for order
				$mysqli_quotation = $mysqli->query("SELECT * FROM $prefixQuotation
													WHERE quotation_so = '".$so_temp."' AND quotation_type = '".$type."' AND DATE(quotation_date) = '".date('Y-m-d')."' LIMIT 1") ;
			break ;
		}
		// check if query exsits
		if ($mysqli_quotation->num_rows == 0){
			// set boolean = false
			$boolean_so = false ;
		}else{
			$so++ ;
		}
	}
	return $so_temp ;
}

// get so number
function soNumberMethod2($type, $query1, $table1, $field_type, $query2, $rate_type, $packing_type = ''){
	global $mysqli, $prefixCombineHead, $prefixQuotation, $prefixRemove ;
	
	$rate_type = ($rate_type == 'myr' ? 'myr' : ($rate_type == 'eur' ? 'eur' : 'usd')) ;
	$query_rate_type = '' ;
	
	// check type
	switch($type){
		case 'combine_head' :
		case 'combine_head2' :
			// query for combine head
			$inv_pl_query = "combine_head_inv" ;
			if ($table1 == 'combine_head_pl' || $table1 == 'combine_head_pl2'){
				$inv_pl_query = "combine_head_pl" ;
			}
			if ($table1 == 'combine_head_do' || $table1 == 'combine_head_do2'){
				$inv_pl_query = "combine_head_do" ;
			}
			// check rate type search pl_query
			if ($inv_pl_query == 'combine_head_do'){
				$query_rate_type = " AND combine_head_rate_type = '".$rate_type."'" ;
			}
			$query_default = "combine_head_type = 'order' ".$query_rate_type." AND ".$inv_pl_query." != '0' AND YEAR(combine_head_updated) = '".date('Y')."' AND MONTH(combine_head_updated) = '".date('n')."' AND combine_head_status = 'packing' AND (combine_head_trash = '0')" ;
			$mysqli_quotation = $mysqli->query("SELECT * FROM $prefixCombineHead
												WHERE ".$query_default." ".$query1) ;
		break ;
		case 'quotation' :
		case 'order-draft' :
		case 'order' :
		case 'order-po' :
			// query for order
			$mysqli_quotation = $mysqli->query("SELECT quotation_date FROM $prefixQuotation
												WHERE quotation_type = '".$type."' AND quotation_trash = '0' ".$query1) ;
		break ;	
	}
	
	// default variable
	$so = $mysqli_quotation->num_rows ;									
	$so = ($so > 0 ? $so : 1) ;
	if ($packing_type == 'draft-packing' || $packing_type == 'draft-delivery'){
		$date = date('y-md-') ;
	}else{
		$date = date('ym') ;
	}
	
	// set boolean = true
	$boolean_so = true ;
	$boolean_so_type = false ;
	// set boolean so type
	switch($type){
		case 'combine_head2' :
			$boolean_so_type = true ;
		break ;
	}
	// get so number
	while($boolean_so){
		// temp so
		$so_temp = ($boolean_so_type ? $date.strPad(2, $so) : $so) ;
		// check type
		switch($type){
			case 'combine_head' :
			case 'combine_head2' :
				// query for combine head
				$mysqli_quotation = $mysqli->query("SELECT * FROM $prefixCombineHead
													WHERE ".$table1." = '".$so_temp."' AND ".$query_default." ".$query1." LIMIT 1") ;
			break ;
			case 'quotation' :
			case 'order-draft' :
			case 'order' :
			case 'order-po' :
				// query for order
				$mysqli_quotation = $mysqli->query("SELECT * FROM $prefixQuotation
													WHERE ".$table1." = '".$so_temp."' AND quotation_type = '".$type."' AND quotation_trash = '0' ".$query1." LIMIT 1") ;
			break ;	
		}
		// check if query exsits
		if ($mysqli_quotation->num_rows == 0){
			// set boolean = false
			$boolean_so = false ;
		}else{
			$so++ ;
		}
	}
	// check pending remove | $table1
	// query for order remove
	$mysqli_remove = $mysqli->query("SELECT remove_id, remove_number FROM $prefixRemove
									 WHERE remove_from = '".$type."' AND remove_type = '".$field_type."' AND remove_trash = '0' ".$query2." ORDER BY remove_number ASC LIMIT 1") ;
	// check query if exists
	if ($mysqli_remove->num_rows > 0){
		// set query as array
		$row_remove = $mysqli_remove->fetch_array(MYSQLI_ASSOC) ;
		// reset result
		$so_temp = $row_remove['remove_number'] ;
		// remove this id
		$mysqli->query("UPDATE $prefixRemove SET
						remove_trash = '1'
						WHERE remove_id = '".$row_remove['remove_id']."'") ;
	}
	// return resut
	return $so_temp ;
}

// get so number
function soNumberMethod3($type, $field_type, $order_id){
	global $mysqli, $prefixQuotation ;
	
	// check if po exists before
	$boolean_generate = true ;
	$so2_manual = 0 ;
	$so_type = 'new' ;
	
	$mysqli_po = $mysqli->query("SELECT quotation_id, quotation_so2, quotation_so2_manual, quotation_so2_temp, quotation_total_quotation FROM $prefixQuotation
								WHERE quotation_type = 'order-po' AND quotation_po_id = '".$order_id."' AND quotation_trash = '1' ORDER BY quotation_id DESC") ;
	$total_po = $mysqli_po->num_rows ;
	
	if ($total_po > 0){
		$count = 0 ;

		while($row_po = $mysqli_po->fetch_array(MYSQLI_ASSOC)){
			
			if ($count == 0){	
				$so = $row_po['quotation_total_quotation'] ;
				$so_temp = $row_po['quotation_so2'] ;
				$so2_manual = $row_po['quotation_so2_manual'] ;
				$so2_temp = $row_po['quotation_so2_temp'] ;
				
				$explode_so2_temp = explode('[:temp]', $so2_temp) ;
				$so3 = str_replace('PO', '', $explode_so2_temp[0]) ;
				
				$so_type = 'edit' ;
				
				$boolean_generate = false ;
			}
			
			// delete previous order
			$mysqli->query("DELETE FROM $prefixQuotation
							WHERE quotation_id = '".$row_po['quotation_id']."'") ;
			
			$count++ ;
			
		}
	}
	
	// generate new so number for po
	if ($boolean_generate){
		// check type
		switch($type){
			case 'order-po' :
				// query for order
				$query_default = "quotation_type = '".$type."' AND YEAR(quotation_date) = '".date('Y')."' AND MONTH(quotation_date) = '".date('n')."'" ;
				$mysqli_quotation = $mysqli->query("SELECT quotation_date FROM $prefixQuotation
													WHERE ".$query_default) ;
			break ;	
		}
		// default variable
		// echo $query_default ;
		$so = $mysqli_quotation->num_rows ;
		$date = date('ym') ;
		// check if query exsits
		$so = ($so > 0 ? $so : 1) ;
		// set boolean = true
		$boolean_so = true ;
		// get so number
		while($boolean_so){
			// temp so
			$so_temp = $so3 = $date . '-' . strPad(3, $so) ;
			// check type
			switch($type){
				case 'order-po' :
					// query for order
					$mysqli_quotation = $mysqli->query("SELECT * FROM $prefixQuotation
														WHERE ".$query_default." AND quotation_so3 = '".$so_temp."' LIMIT 1") ;
				break ;
			}
			// check if query exsits
			if ($mysqli_quotation->num_rows == 0){
				// set boolean = false			
				$boolean_so = false ;
			}else{
				$so++ ;
			}
		}
	}
	
	// keep result in array
	$array['so'] = $so ;
	$array['so_temp'] = $so_temp ;
	$array['so2_manual'] = $so2_manual ;
	$array['so2_temp'] = $so2_temp ;
	$array['so3'] = $so3 ;
	$array['so_type'] = $so_type ;
	// return result
	return $array ;
}

// product breadcrumbs
function productBreadcrumbs($page_id){
	global $mysqli, $prefixPost, $rate_type ;
	// start loop breadcrumbs
	$mysqli_breadcrumbs = $mysqli->query("SELECT post_id, post_title, post_parent FROM $prefixPost 
											WHERE post_id = '".$page_id."' AND post_trash = '0' LIMIT 1") ;
	$row_breadcrumbs = $mysqli_breadcrumbs->fetch_array(MYSQLI_ASSOC) ;
	// page title
	$title = dataFilter($row_breadcrumbs['post_title']) ;
	// keep title in breadcrumbs
	$breadcrumbs = $title ;
	// post parent
	$post_parent = $row_breadcrumbs['post_parent'] ;
	while ($post_parent != 0){
		$mysqli_breadcrumbs = $mysqli->query("SELECT post_id, post_title, post_parent FROM $prefixPost 
												WHERE post_id = '".$post_parent."' AND post_trash = '0' LIMIT 1") ;
		$row_breadcrumbs = $mysqli_breadcrumbs->fetch_array(MYSQLI_ASSOC) ;
		// keep title in breadcrumbs
		// page title
		$title = dataFilter($row_breadcrumbs['post_title']) ;
		$breadcrumbs = '<a href="order-product.php?rate_type='.$rate_type.'&id='.$row_breadcrumbs['post_id'].'">'.$title.'</a> / '.$breadcrumbs ;
		// post parent
		$post_parent = $row_breadcrumbs['post_parent'] ;
	}
	$breadcrumbs = '<a href="order-product.php?rate_type='.$rate_type.'">Main Category</a>'.($breadcrumbs != '' ? ' / '.$breadcrumbs : '') ;
	return $breadcrumbs;
}

// get my current ip
function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'unknown';
    return $ipaddress;
}

function userAgent($ua){
    $iphone = strstr(strtolower($ua), 'mobile'); //Search for 'mobile' in user-agent (iPhone have that)
    $android = strstr(strtolower($ua), 'android'); //Search for 'android' in user-agent
    $windowsPhone = strstr(strtolower($ua), 'phone'); //Search for 'phone' in user-agent (Windows Phone uses that)
     
    function androidTablet($ua){ //Find out if it is a tablet
        if(strstr(strtolower($ua), 'android') ){//Search for android in user-agent
            if(!strstr(strtolower($ua), 'mobile')){ //If there is no ''mobile' in user-agent (Android have that on their phones, but not tablets)
                return true;
            }
        }
    }
    $androidTablet = androidTablet($ua); //Do androidTablet function
    $ipad = strstr(strtolower($ua), 'ipad'); //Search for iPad in user-agent
   $kindle = strstr(strtolower($ua), 'kindle'); //Search for iPad in user-agent
     
    if($androidTablet || $ipad || $kindle){ //If it's a tablet (iPad / Android / Kindly)
        return 'tablet';
    }
    elseif($iphone || $android || $windowsPhone){ //If it's a phone and NOT a tablet
        return 'mobile';
    }
    else{ //If it's not a mobile device
        return 'desktop';
    }    
}

//****************************************************************
//**************************************************************** zip all jpeg file
//****************************************************************
/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
	//if the zip file already exists and overwrite is false, return false
	if(file_exists($destination) && !$overwrite) { return false; }
	//vars
	$valid_files = array();
	//if files were passed in...
	if(is_array($files)) {
		//cycle through each file
		foreach($files as $file) {
			//make sure the file exists
			if(file_exists($file)) {
				$valid_files[] = $file;
			}
		}
	}
	//if we have good files...
	if(count($valid_files)) {
		//create the archive
		$zip = new ZipArchive();
		if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
			return false;
		}
		//add the files
		foreach($valid_files as $file) {
			$zip->addFile($file,$file);
		}
		//debug
		//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
		
		//close the zip -- done!
		$zip->close();
		
		//check to make sure the file exists
		return file_exists($destination);
	}
	else
	{
		return false;
	}
}

// get combine quotation id
function combineQuotation($mysqli, $prefixCombineSub, $prefixQuotation, $prefixRate, $prefixCompany, $prefixUser, $head_id, $all_order_item){

	// query for sub
	$mysqli_sub = $mysqli->query("SELECT quotation_id, quotation_so, quotation_so2, quotation_so2_manual, quotation_rate_id, quotation_total_quotation, quotation_total_company, quotation_total, quotation_po_id, rate_currency, company_account_code, company_code, company_number, user_name FROM ".$prefixCombineSub." a
									LEFT JOIN ".$prefixQuotation." b ON (a.combine_sub_quotation_id = b.quotation_id)
									LEFT JOIN ".$prefixCompany." c ON (b.quotation_company_id = c.company_id)
									LEFT JOIN ".$prefixRate." d ON (b.quotation_rate_id = d.rate_id)
									LEFT JOIN ".$prefixUser." e ON (b.quotation_user_id = e.user_id )
									WHERE a.combine_sub_head_id = '".$head_id."' AND a.combine_sub_trash = '0' AND b.quotation_trash = '0'") ;
	// defaut variable
	$order_list = $order_list_po = $order_rate = $incarge_person = '' ;
	$count = $total_pi = $total_po = $count_selected = 0 ;
	// check if query exsits
	if ($mysqli_sub->num_rows > 0){
		
		// check invoice item
		$arrray_item = array() ;
		if (arrayCheck($all_order_item)){
			foreach($all_order_item as $key => $value){
				$arrray_item[$value['cart_quotation_id']][] = $value ;	
			}
			array_values($arrray_item) ;
		}	
		
		// loop all query
		while ($row_sub = $mysqli_sub->fetch_array(MYSQLI_ASSOC)){
			// decode data and keep in variable
			$quotation_total_pi = unserialize(base64_decode($row_sub['quotation_total'])) ;
			$total_pi += $quotation_total_pi['cart_grand_total'] ;
			$count++ ;
			// query for po
			$mysqli_po = $mysqli->query("SELECT quotation_so2, quotation_total, quotation_so2_manual, quotation_product FROM ".$prefixQuotation."
										 WHERE quotation_po_id = '".$row_sub['quotation_id']."' AND quotation_type = 'order-po' AND quotation_trash = '0' LIMIT 1") ;
			// count selected
			$count_selected++ ;
			// invoice number
			$invoice_manual = $row_sub['quotation_so2_manual'] ;
			$invoice = $row_sub['quotation_so'] ;
			$invoice2 = ($invoice_manual == 1 ? '' : 'P').$row_sub['quotation_so2'] ;
			// get company details
			$company_number = dataFilterDash($row_sub['company_number']) ;
			$company_account_code = dataFilterDash($row_sub['company_account_code']) ;
			$company_code = dataFilterDash($row_sub['company_code']) ;
			// keep order so
			$order_list .= $invoice2 . ($mysqli_sub->num_rows != $count_selected ? ', ' : '') ;
			$order_rate .= dataFilter($row_sub['rate_currency']) . ($mysqli_sub->num_rows != $count_selected ? ', ' : '') ;
			$incarge_person .= ucwords(strtolower($row_sub['user_name'])) . ($mysqli_sub->num_rows != $count_selected ? ', ' : '') ;
			// check if query exists
			if ($mysqli_po->num_rows > 0){
				// set query as array
				$row_po = $mysqli_po->fetch_array(MYSQLI_ASSOC) ;
				$array_po = $arrray_item[$row_sub['quotation_id']] ;
				// decode data and keep in variable
				// $quotation_total_po = unserialize(base64_decode($row_po['quotation_total'])) ;
				$quotation_product = unserialize(base64_decode($row_po['quotation_product'])) ;
				// check array po
				if (arrayCheck($array_po)){
					foreach( $array_po as $key_po => $value_po ){
						
						foreach( $quotation_product as $key_item => $value_item ){
						
							if ( $value_po['cart_id'] == $value_item['cart_id'] ){
								$price_my = ( $value_item['cart_price_my'] ) ;
							}
							
						}
						
						$total_po += ($price_my * $value_po['cart_quantity']) ;
					}
				}
				// $total_po += $quotation_total_po['cart_grand_total_my'] ;
				// keep order so
				$order_list_po .= ($row_po['quotation_so2_manual'] == 1 ? '' : 'PO').dataFilterDash($row_po['quotation_so2']) . ', ' ;
			}
		}
		$order_list_po = ($order_list_po != '' ? substr($order_list_po, 0, -2) : '') ;
	}
	// set result as array
	$array['total_order'] = $count ;
	$array['pi'] = $order_list ;
	$array['pi_total'] = $total_pi ;
	$array['po'] = dataFilterDash($order_list_po) ;
	$array['po_total'] = $total_po ;
	$array['incharge'] = $incarge_person ;
	$array['rate_currency'] = $order_rate ;
	$array['company_number'] = $company_number ;
	$array['company_account_code'] = $company_account_code ;
	$array['company_code'] = $company_code ;
	// return result
	return $array ;
}

// start inv number
function startInvoiceNumber($number){
	$startFrom = '53361' ;
	$number = $number + $startFrom ;
	return 'IV' . $number ;	
}

// set 0 infront of number
function strPad($zero, $value){
	return str_pad($value, $zero, '0', STR_PAD_LEFT) ;	
}

// reset key from 0 to 1
function resetKeyArray($array, $array_service, $container, $seal, $page_mode){
	global $rate_type ;
	
	$default_array = $return_array = array() ;
	$total_number_of_carton = $total_m3_single = $total_m3 = $total_kgs_gw_single = $total_kgs_gw = $total_kgs_nw = $total =  0 ;
	foreach($array as $key => $value){
		// keep value in array
		$default_array[$key]['item'] = array_values($value['item']) ;
		$default_array[$key]['sub_quantity'] = $value['sub_quantity'] ;
		$default_array[$key]['sub_number_of_carton'] = $value['sub_number_of_carton'] ;
		$default_array[$key]['sub_m3_single'] = $value['sub_m3_single'] ;
		$default_array[$key]['sub_m3'] = $value['sub_m3'] ;
		$default_array[$key]['sub_kgs_gw_single'] = $value['sub_kgs_gw_single'] ;
		$default_array[$key]['sub_kgs_gw'] = $value['sub_kgs_gw'] ;
		$default_array[$key]['sub_kgs_nw'] = $value['sub_kgs_nw'] ;
		$default_array[$key]['sub_total'] = $value['sub_total'] ;
		$default_array[$key]['sub_total_wording'] = $value['sub_total_wording'] ;
		$default_array[$key]['container'] = $container[$key] ;
		$default_array[$key]['seal'] = $seal[$key] ;
		// couting
		$total_number_of_carton += $value['sub_number_of_carton'] ;
		$total_m3_single += $value['sub_m3_single'] ;
		$total_m3 += $value['sub_m3'] ;
		$total_kgs_gw_single += $value['sub_kgs_gw_single'] ;
		$total_kgs_gw += $value['sub_kgs_gw'] ;
		$total_kgs_nw += $value['sub_kgs_nw'] ;
		$total += $value['sub_total'] ;
	}
	
	// check services
	$total_grand = $total ;
	if (arrayCheck($array_service)){
		foreach($array_service as $key => $value){
			$total_grand += $value['service_charge'] ;	
		}
	}
	
	// keep all in new array
	$return_array['all_container'] = $default_array ;
	if ($page_mode == 'draft-container'){
		$return_array['title'] = 'DRAFT LOADING LIST' ;
	}else{
		$return_array['title'] = 'LOADING LIST' ;
	}
	
	$return_array['header'] = "&lt;div style=\&quot;font-size:28px\&quot;&gt;&lt;strong&gt;S&amp;amp;E ENTERPRISE SDN. BHD. &lt;/strong&gt;&lt;span style=\&quot;font-size:13px\&quot;&gt;&lt;em&gt;(809181-W)&lt;/em&gt;&lt;/span&gt;&lt;/div&gt;&lt;table border=\&quot;0\&quot; cellpadding=\&quot;0\&quot; cellspacing=\&quot;0\&quot; style=\&quot;font-size:16px; width:100%\&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td colspan=\&quot;2\&quot;&gt;PLO 65, No: K16, Kawasan Perindustrian Tanjung Agas, 84000 Muar Johor, West Malaysia&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=\&quot;width:250px\&quot;&gt;Tel: 606 952 9995&lt;/td&gt;&lt;td&gt;Fax: 606 953 3860&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Website: &lt;a href=\&quot;https://www.seowexport.com.my\&quot; style=\&quot;text-decoration:none; color:#000;\&quot;&gt;www.seowexport.com.my&lt;/a&gt;&lt;/td&gt;&lt;td&gt;E-Mail: &lt;a href=\&quot;mailto: sne@seowexport.com.my\&quot; style=\&quot;text-decoration:none; color:#000;\&quot;&gt;sne@seowexport.com.my&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;" ;
	$return_array['total_number_of_carton'] = $total_number_of_carton ;
	$return_array['total_m3_single'] = $total_m3_single ;
	$return_array['total_m3'] = $total_m3 ;
	$return_array['total_kgs_gw_single'] = $total_kgs_gw_single ;
	$return_array['total_kgs_gw'] = $total_kgs_gw ;
	$return_array['total_kgs_nw'] = $total_kgs_nw ;
	$return_array['total'] = $total ;
	$return_array['total_grand'] = $total_grand ;
	$return_array['total_wording'] = 'SAY TOTAL IN '.strtoupper($rate_type).' ' . strtoupper(numtowords($total_grand, ($rate_type == 'myr' ? 'RINGGIT' : ($rate_type == 'eur' ? 'EURO' : 'DOLLAR')))) . '.' ;
	$return_array['service'] = $array_service ;
	// return result
	return $return_array ;
}

function emailVerifcationCode($mysqli, $prefixUser, $company, $from, $fullname, $rand){
	if ($rand != '' && strlen($rand) == 6){
		// query for user
		$mysqli_user = $mysqli->query("SELECT * FROM $prefixUser
										WHERE user_permission = 'admin' AND user_trash = '0'") ;
		if ($mysqli_user->num_rows > 0){
			// loop all user
			while ($row_user = $mysqli_user->fetch_array(MYSQLI_ASSOC)){
				// email to owner | sbs admin
				$to = dataFilter($row_user['user_email']) ;
				$title = SYSTEM . ' verification code for ' . $fullname ;
				// show content
				$body = SYSTEM . ' verification code for ' . $fullname . ' is ' . $rand ;
				//put your email address here
				$header = 'From: '.$company.' <'.$from.'>'.PHP_EOL.
					'MIME-Version: 1.0'.PHP_EOL.
					'Content-type: text/html; charset=utf-8'.PHP_EOL.
					'Content-Transfer-Encoding: 8bit'.PHP_EOL.
					'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL ; // send to owner
				mail($to, $title, $body, $header); // send to user	
			}
		}
	}
	return $boolean ;
}

//
function emailFormat($company, $from, $to, $title, $body){
	//put your email address here
	$header = 'From: '.$company.' <'.$from.'>'.PHP_EOL.
		'MIME-Version: 1.0'.PHP_EOL.
		'Content-type: text/html; charset=utf-8'.PHP_EOL.
		'Content-Transfer-Encoding: 8bit'.PHP_EOL.
		'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL ; // send to owner
	if (mail($to, $title, $body, $header)){
		return true ;
	}
	return false ;
}

// get company number
function getCompanyNumber($mysqli, $prefixCompany, $company_id){
	// query for company
	$mysqli_company = $mysqli->query("SELECT company_number FROM $prefixCompany
									  WHERE company_id = '".$company_id."' LIMIT 1") ;
	// check if company exsit
	$company_number = '' ;
	if ($mysqli_company->num_rows > 0){
		$row_company = $mysqli_company->fetch_array(MYSQLI_ASSOC) ;	
		$company_number = $row_company['company_number'] ;
	}
	return $company_number ;
}

// auto generate quotation number
function generateQuotationNumber($total, $company_number, $total_company){
	return strPad(3, $total) . '-' . dataFilter($company_number) . '-' . strPad(3, $total_company) ;
}

// continue array with text with comma
function continueTextWithComma($array){
	$wording = '' ;
	if (count($array) > 0 && is_array($array)){
		foreach($array as $key => $value){
			$wording .= ($key == 0 ? '' : ', ') . $value ;
		}
	}
	return $wording ;
}

// check is numberic
function checkNumberic($value){
	$value = ($value > 0 ? $value : 0) ;
	return $value ;
}

// change rate from usd to myr
function rateUsdMyr($value, $rate){
	$value = ($value * $rate) ;
	return $value ;
}

// return forecast
function forecastAmount($mysqli, $prefixForecast, $head_id, $po_total){
	$mysqli_forecast = $mysqli->query("SELECT * FROM $prefixForecast
										WHERE forecast_head_id = '".$head_id."' AND forecast_trash = '0' ORDER BY forecast_id DESC") ;
	$container_volume = '' ;
	$payment_tt_lc = $payment_charges = $payment_check_balance = $payment_received = $ofao_amount = $fc_amount = $ext_rate = $sbs_fwd = $received_myr = $gross_profit = 0 ;
	// check if forecast exsits
	if ($mysqli_forecast->num_rows > 0){
		while ($row_forecast = $mysqli_forecast->fetch_array(MYSQLI_ASSOC)){
			// check type
			switch($row_forecast['forecast_type']){
				case 'pyt' :
					$payment_tt_lc = $row_forecast['forecast_amount'] ;
					$payment_charges = $row_forecast['forecast_charges'] ;
					$payment_check_balance = ($payment_tt_lc) ;
					$payment_received = ($payment_tt_lc - $payment_charges) ;
				break ;
				case 'ofao' :
					$ofao_amount = $row_forecast['forecast_amount'] ;
				break ;
				case 'fc' :
					$fc_amount = $row_forecast['forecast_amount'] ;
				break ;
				case 'ext' :
					$ext_rate = $row_forecast['forecast_amount'] ;
				break ;
				case 'ctvl' :
					$container_volume = $row_forecast['forecast_remark'] ;
				break ;
				case 'sbfw' :
					$sbs_fwd = $row_forecast['forecast_amount'] ;
				break ;
			}
		}
	}
	$ofao_amount = rateUsdMyr($ofao_amount, $ext_rate) ;
	$received_myr = rateUsdMyr($payment_received, $ext_rate) ;
	$gross_profit = ($received_myr - $po_total - $ofao_amount - $fc_amount + $sbs_fwd) ;
	// keep result in array
	$array['payment_tt_lc'] = numberFormatComma($payment_tt_lc) ;
	$array['payment_charges'] = numberFormatComma($payment_charges) ;
	$array['payment_check_balance'] = numberFormat($payment_check_balance) ;
	$array['payment_received'] = numberFormatComma($payment_received) ;
	$array['payment_received2'] = numberFormat($payment_received) ;
	$array['ofao_amount'] = numberFormatComma($ofao_amount) ;
	$array['fc_amount'] = numberFormatComma($fc_amount) ;
	$array['container_volume'] = $container_volume ;
	$array['sbs_fwd'] = numberFormatComma($sbs_fwd) ;
	$array['ext_rate'] = $ext_rate ;
	$array['received_myr'] = numberFormatComma($received_myr) ;
	$array['gross_profit'] = numberFormatComma($gross_profit) ;
	// return result as array
	return $array ;
}

// key selected invoice in all invoice
function recordAllTaxInvoice($selected_tax_invoice, $array_key, $array_update_invoice){
	$array = array() ;
	$total = 0 ;
	if (count($selected_tax_invoice) > 0 && is_array($selected_tax_invoice)){
		foreach($selected_tax_invoice as $key => $value){
			if ($key == $array_key){
				$array[$key] = $array_update_invoice ;
				$total += $array_update_invoice['total']['cart_grand_total'] ;
			}else{
				$array[$key] = $value ;
				$total += $value['total']['cart_grand_total'] ;
			}
		}
	}
	// keep result in array
	$return_array['invoice'] = $array ;
	$return_array['total'] = $total ;
	return $return_array ;
}

// check if zero value
function checkZeroValue($value){
	return ($value > 0 ? $value : 0) ;
}


// json_encode array
function jsonEncodeDecode($type, $array){
	
	if ($type == 'encode'){
		return json_encode($array) ;
	}else{
		return json_decode($array, true) ;
	}
	
}

// add array with set max array
function arrayInsertMax($array, $insert, $max){

	array_unshift($array, $insert) ;
	array_splice($array, $max) ;
	
	return $array ;
}

// check for vessel type
function vesselNameType($value){
	$value = dataFilter($value) ;
	return ($value != '' ? $value : 'FEEDER VESSEL') ;
}

// othe vessel name title check
function shippingNameType($value, $key, $rate_type = 'usd'){
	$array = array('bn' => 'BOOKING NO',
				   'mv' => 'MOTHER VESSEL',
				   'pol' => 'PORT OF LOADING',
				   'pod' => 'PORT OF DESTINATION',
				   'cv' => ($rate_type == 'myr' ? 'LORRY VOLUME' : 'CONTAINER VOLUME'),
				   'ci' => ($rate_type == 'myr' ? 'LORRY TRUCK IN' : 'CONTAINER IN'),
				   'co' => ($rate_type == 'myr' ? 'LORRY TRUCK OUT' : 'CONTAINER OUT'),
				   'lli' => 'LAST LOADING ITEM',
				   'cn' => 'CONTAINER NO',
				   'sn' => 'SEAL NO') ;
	return ($value != '' ? $value : $array[$key]) ;
}	

// list all country in array
function allCountry($type, $selected){
	$content = '' ;
	
	$countries = array("AFGHANISTAN", "ALBANIA", "ALGERIA", "AMERICAN SAMOA", "ANDORRA", "ANGOLA", "ANGUILLA", "ANTARCTICA", "ANTIGUA AND BARBUDA", "ARGENTINA", "ARMENIA", "ARUBA", "AUSTRALIA", "AUSTRIA", "AZERBAIJAN", "BAHAMAS", "BAHRAIN", "BANGLADESH", "BARBADOS", "BELARUS", "BELGIUM", "BELIZE", "BENIN", "BERMUDA", "BHUTAN", "BOLIVIA", "BOSNIA AND HERZEGOWINA", "BOTSWANA", "BOUVET ISLAND", "BRAZIL", "BRITISH INDIAN OCEAN TERRITORY", "BRUNEI DARUSSALAM", "BULGARIA", "BURKINA FASO", "BURUNDI", "CAMBODIA", "CAMEROON", "CANADA", "CAPE VERDE", "CAYMAN ISLANDS", "CENTRAL AFRICAN REPUBLIC", "CHAD", "CHILE", "CHINA", "CHRISTMAS ISLAND", "COCOS (KEELING) ISLANDS", "COLOMBIA", "COMOROS", "CONGO", "CONGO, THE DEMOCRATIC REPUBLIC OF THE", "COOK ISLANDS", "COSTA RICA", "COTE D'IVOIRE", "CROATIA (HRVATSKA)", "CUBA", "CYPRUS", "CZECH REPUBLIC", "DENMARK", "DJIBOUTI", "DOMINICA", "DOMINICAN REPUBLIC", "EAST TIMOR", "ECUADOR", "EGYPT", "EL SALVADOR", "EQUATORIAL GUINEA", "ERITREA", "ESTONIA", "ETHIOPIA", "FALKLAND ISLANDS (MALVINAS)", "FAROE ISLANDS", "FIJI", "FINLAND", "FRANCE", "FRANCE METROPOLITAN", "FRENCH GUIANA", "FRENCH POLYNESIA", "FRENCH SOUTHERN TERRITORIES", "GABON", "GAMBIA", "GEORGIA", "GERMANY", "GHANA", "GIBRALTAR", "GREECE", "GREENLAND", "GRENADA", "GUADELOUPE", "GUAM", "GUATEMALA", "GUINEA", "GUINEA-BISSAU", "GUYANA", "HAITI", "HEARD AND MC DONALD ISLANDS", "HOLY SEE (VATICAN CITY STATE)", "HONDURAS", "HONG KONG", "HUNGARY", "ICELAND", "INDIA", "INDONESIA", "IRAN (ISLAMIC REPUBLIC OF)", "IRAQ", "IRELAND", "ISRAEL", "ITALY", "JAMAICA", "JAPAN", "JORDAN", "KAZAKHSTAN", "KENYA", "KIRIBATI", "KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF", "KOREA, REPUBLIC OF", "KUWAIT", "KYRGYZSTAN", "LAO, PEOPLE'S DEMOCRATIC REPUBLIC", "LATVIA", "LEBANON", "LESOTHO", "LIBERIA", "LIBYAN ARAB JAMAHIRIYA", "LIECHTENSTEIN", "LITHUANIA", "LUXEMBOURG", "MACAU", "MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF", "MADAGASCAR", "MALAWI", "MALAYSIA", "MALDIVES", "MALI", "MALTA", "MARSHALL ISLANDS", "MARTINIQUE", "MAURITANIA", "MAURITIUS", "MAYOTTE", "MEXICO", "MICRONESIA, FEDERATED STATES OF", "MOLDOVA, REPUBLIC OF", "MONACO", "MONGOLIA", "MONTSERRAT", "MOROCCO", "MOZAMBIQUE", "MYANMAR", "NAMIBIA", "NAURU", "NEPAL", "NETHERLANDS", "NETHERLANDS ANTILLES", "NEW CALEDONIA", "NEW ZEALAND", "NICARAGUA", "NIGER", "NIGERIA", "NIUE", "NORFOLK ISLAND", "NORTHERN MARIANA ISLANDS", "NORWAY", "OMAN", "PAKISTAN", "PALAU", "PANAMA", "PAPUA NEW GUINEA", "PARAGUAY", "PERU", "PHILIPPINES", "PITCAIRN", "POLAND", "PORTUGAL", "PUERTO RICO", "QATAR", "REUNION", "ROMANIA", "RUSSIAN FEDERATION", "RWANDA", "SAINT KITTS AND NEVIS", "SAINT LUCIA", "SAINT VINCENT AND THE GRENADINES", "SAMOA", "SAN MARINO", "SAO TOME AND PRINCIPE", "SAUDI ARABIA", "SENEGAL", "SEYCHELLES", "SIERRA LEONE", "SINGAPORE", "SLOVAKIA (SLOVAK REPUBLIC)", "SLOVENIA", "SOLOMON ISLANDS", "SOMALIA", "SOUTH AFRICA", "SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS", "SPAIN", "SRI LANKA", "ST. HELENA", "ST. PIERRE AND MIQUELON", "SUDAN", "SURINAME", "SVALBARD AND JAN MAYEN ISLANDS", "SWAZILAND", "SWEDEN", "SWITZERLAND", "SYRIAN ARAB REPUBLIC", "TAIWAN, PROVINCE OF CHINA", "TAJIKISTAN", "TANZANIA, UNITED REPUBLIC OF", "THAILAND", "TOGO", "TOKELAU", "TONGA", "TRINIDAD AND TOBAGO", "TUNISIA", "TURKEY", "TURKMENISTAN", "TURKS AND CAICOS ISLANDS", "TUVALU", "UGANDA", "UKRAINE", "UNITED ARAB EMIRATES", "UNITED KINGDOM", "UNITED STATES", "UNITED STATES MINOR OUTLYING ISLANDS", "URUGUAY", "UZBEKISTAN", "VANUATU", "VENEZUELA", "VIETNAM", "VIRGIN ISLANDS (BRITISH)", "VIRGIN ISLANDS (U.S.)", "WALLIS AND FUTUNA ISLANDS", "WESTERN SAHARA", "YEMEN", "YUGOSLAVIA", "ZAMBIA", "ZIMBABWE");
	
	switch($type){
		case 'select' :
			$content = '
			<select name="country" id="call_country" class="form-control ui-search-input">
				<option value="">Country</option>' ;
				foreach($countries as $key => $value){
					$content .= '
					<option value="'.$value.'" '.($value == $selected ? 'selected' : '').'>'.$value.'</option>' ;
				}
			$content .= '
			</select>' ;
		break ;
		case 'list' :
			$array['list'] = $countries ;
		break ;
	}
	
	$array['content'] = $content ;
	return $array ;
}


// export to csv
function exportToCsv($filename, $array_columns, $array_content){
	// output headers so that the file is downloaded rather than displayed
	header('Content-Type: text/csv; charset=utf-8') ;
	header('Content-Disposition: attachment; filename='.$filename.'.csv') ;

	// create a file pointer connected to the output stream
	$output = fopen('php://output', 'w') ;
	
	// output the column headings
	fputcsv($output, $array_columns) ;
	
	// fetch the data
	if (arrayCheck($array_content)){
	
		// loop over the rows, outputting them
		foreach ($array_content as $key => $value){
			fputcsv($output, $value) ;	
		}
	}
	
}

// list all user
function listAllUser($permission, $search_query){
	global $mysqli, $prefixUser ;
	
	$result = false ;
	$array_list_email = array() ;
	
	if ($permission != ''){
		$search_query .= " AND user_permission = '".$permission."'" ;
	}
	
	// page query
	$mysqli_query = "SELECT * FROM $prefixUser
					 WHERE user_trash = '0'".$search_query ;
	$mysqli_page = $mysqli->query($mysqli_query." ORDER BY user_id") ;
	
	if ($mysqli_page->num_rows > 0){
		$result = true ;
		while ($row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC)){
			$array_list_email[] = $row_page['user_email'] ;
		}
	}
	
	$array['list_email'] = $array_list_email ;
	$array['result'] = $result ;
	return $array ;
}	

// list all user
function listAllEmployment($select_worker, $search_query, $select_box_name, $selected_id, $required){
	global $mysqli, $prefixEmployment;
	
	$result = false ;
	$select_box = '' ;
	
	if ($select_worker != ''){
		$search_query .= " AND employment_type = '".$select_worker."'" ;	
	}
	
	// page query
	$mysqli_query = "SELECT * FROM $prefixEmployment
					 WHERE employment_trash = '0'".$search_query ;
	$mysqli_page = $mysqli->query($mysqli_query." ORDER BY employment_id") ;
	
	if ($mysqli_page->num_rows > 0){
		$result = true ;
		while ($row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC)){
			$select_box .= '
			<option value="'.$row_page['employment_id'].'" '.($row_page['employment_id'] == $selected_id ? 'selected' : '').'>'.dataFilter($row_page['employment_name']).'</option>' ;
		}
	}
	
	$select_box = '
	<select name="'.$select_box_name.'" class="form-control ui-search-input" '.$required.'>
		<option value="">Select Employment</option>
		'.$select_box.'
	</select>' ;
	
	$array['select'] = $select_box ;
	$array['result'] = $result ;
	return $array ;
}	

// return gst charge
function getGstCharge(){
	global $mysqli, $prefixPost ;
	
	$gst_charge = 0 ;
	
	$mysqli_page = $mysqli->query("SELECT * FROM $prefixPost
								   WHERE post_type = 'page-gst-charge' AND post_categories = 'page-gst-charge' AND post_trash = '0' LIMIT 1") ;
	if ($mysqli_page->num_rows > 0){
		$row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
		$gst_charge = $row_page['post_price'] ;
	}
	
	return $gst_charge ;
}

// get customer id reference
function IDReference($number, $search_query){
	global $mysqli, $prefixCustomer ;
	
	$boolean_check = true ;
	$count = 0 ;
	
	while ($boolean_check){
		$mysqli_customer = $mysqli->query("SELECT * FROM $prefixCustomer
										   WHERE customer_id_reference = '".$number."' AND customer_trash = '0' LIMIT 1") ;
		if ($mysqli_customer->num_rows > 0){
			$number++ ;
			$count++ ;
		}else{
			$boolean_check = false ;
		}	
	}
	
	$array['count'] = $count ;
	$array['number'] = $number ;
	return $array ; 

}

// return customer value
function getCustomer($select_customer_id, $select_company_id){
	global $mysqli, $prefixCustomer, $prefixCompany ;
	$result = false ;
	
	$mysqli_query = "SELECT * FROM $prefixCustomer a 
					 LEFT JOIN $prefixCompany b ON (a.customer_company_id = b.company_id)
					 WHERE a.customer_id = '".$select_customer_id."' AND (a.customer_trash = '0' || a.customer_trash = '9') AND (b.company_trash = '0' || b.company_trash = '9') " ;
	$mysqli_customer = $mysqli->query($mysqli_query." LIMIT 1") ;
	if ($mysqli_customer->num_rows > 0){
		
		$row_customer = $mysqli_customer->fetch_array(MYSQLI_ASSOC) ;

		$array['content'] = $row_customer ;
		$result = true ;
	}
	
	$array['result'] = $result ;
	return $array ;
	
}

// export file
function exportFileExcel($records) {
	$heading = false ;
	if(!empty($records))
	  foreach($records as $row) {
		if(!$heading) {
		  // display field/column names as a first row
		  echo implode("\t", array_keys($row)) . "\n" ;
		  $heading = true;
		}
		echo implode("\t", array_values($row)) . "\n" ;
	  }
	exit;
}

// update new po price
function updateNewPoPrice($product, $service, $total){
	global $mysqli, $prefixQuotation ;
	
	$product = $new_product = unserialize(base64_decode($product)) ;
	$service = unserialize(base64_decode($service)) ;
	$total = unserialize(base64_decode($total)) ;
	$total_my_unit = $total_my_sub_total = 0 ;
	
	if ( arrayCheck($product) ){
		foreach ( $product as $key => $value ){
			$price = getOriPrice($value['cart_id']) ;
			$sub_total = ($price * $value['cart_quantity']) ;
			$new_product[$key]['cart_price_my'] = $price ;
			$new_product[$key]['cart_sub_total_my'] = $sub_total ;
			$total_my_unit += $price ;
			$total_my_sub_total += $sub_total ;
		}
	}
	
	$total['cart_total_my'] = $total_my_sub_total ;
	$total['cart_grand_total_my'] = $total_my_sub_total ;
	
	$array['product'] = $new_product ;
	$array['service'] = $service ;
	$array['total'] = $total ;
	return $array ;
	
}

// get ori price
function getOriPrice($cart_id){
	global $mysqli, $prefixQuotation, $prefixPost ;
	
	$price = 0 ;
	
	$mysqli_product = $mysqli->query("SELECT * FROM $prefixPost
									  WHERE post_id = '".$cart_id."' AND post_type = 'product' LIMIT 1") ;
	if ( $mysqli_product->num_rows > 0 ){
		$row_product = $mysqli_product->fetch_array(MYSQLI_ASSOC) ;
		$price = $row_product['post_price'] ;
	}
	
	return $price ;
}

// get ori price
function getProduct($cart_id){
	global $mysqli, $prefixPost ;

	$array = array() ;
	$result = false ;
	
	$mysqli_product = $mysqli->query("SELECT post_file FROM $prefixPost
									  WHERE post_id = '".$cart_id."' AND post_type = 'product' LIMIT 1") ;
	if ( $mysqli_product->num_rows > 0 ){
		$row_product = $mysqli_product->fetch_array(MYSQLI_ASSOC) ;
		$array = $row_product ;
		$result = true ;
	}
	
	$array['result'] = $result ;
	return $array ;
}

// combine shipping item to one
function combineShippingItemToOne($array){
	
	$result = false ;
	$all_container = $array['all_container'] ;
	$array_content = $array ;
	$array_content['all_container'] = array() ;
	
	$count_container = 0 ;
	$total_number_of_carton = $total_m3_single = $total_m3 = $total_kgs_gw_single = $total_kgs_gw = $total_kgs_nw = 0 ;
	
	// loop all container
	if ( arrayCheck($all_container) ){
		foreach ( $all_container as $key_container => $value_container ){
			
			$array_temp = array() ;
			
			// loop all item
			if ( arrayCheck($value_container['item']) ){
				foreach ( $value_container['item'] as $key_item => $value_item ){

					// check again the temp array
					$boolean_exists = false ;
					$temp_key = '' ;
					
					if ( arrayCheck($array_temp) ){
						foreach ( $array_temp as $key_temp => $value_temp ){
							
							// check if array exist
							if ( !$boolean_exists && $value_temp['cart_id'] == $value_item['cart_id'] ){
								$boolean_exists = true ;
								$temp_key = $key_temp ;
							}
							
						}
					}
					
					// check boolean exists or not
					if ( $boolean_exists ){
						
						$temp = $array_temp[$temp_key] ;
						$number_of_box = ( $temp['cart_number_of_carton'] / $temp['cart_quantity'] ) ;
						$quantity = ( $value_item['cart_quantity'] + $temp['cart_quantity'] ) ;
						$number_of_carton = ( $number_of_box * $quantity ) ;
						$sub_m3 = ( $temp['cart_m3'] * $quantity ) ;
						$sub_kg = ( $temp['cart_kg'] * $quantity ) ;
						$sub_kg_nw = ( $sub_kg - $number_of_carton ) ;
						
						// reset temp array
						$array_temp[$temp_key]['cart_quantity'] = ( $quantity ) ;
						$array_temp[$temp_key]['cart_number_of_carton'] = ( $number_of_carton ) ;
						$array_temp[$temp_key]['cart_serially_numbered'] = ( '1 TO '.$number_of_carton ) ;
						$array_temp[$temp_key]['cart_sub_m3'] = ( $sub_m3 ) ;
						$array_temp[$temp_key]['cart_sub_kg'] = ( $sub_kg ) ;
						$array_temp[$temp_key]['cart_sub_kg_nw'] = ( $sub_kg_nw ) ;
						
					}else{
						$array_temp[] = $value_item ;
					}
					
				}
			}
			
			// reset to count total
			$sub_number_of_carton = $sub_m3_single = $sub_m3 = $sub_kgs_gw_single = $sub_kgs_gw = $sub_kgs_nw = 0 ;
			if ( arrayCheck($array_temp) ){
				foreach ( $array_temp as $key_temp => $value_temp ){
					$sub_number_of_carton += ($value_temp['cart_number_of_carton']) ;
					$sub_m3_single += ($value_temp['cart_sub_m3_single']) ;
					$sub_m3 += ($value_temp['cart_sub_m3']) ;
					$sub_kgs_gw_single += ($value_temp['cart_sub_kgs_gw_single']) ;
					$sub_kgs_gw += ($value_temp['cart_sub_kg']) ;
					$sub_kgs_nw += ($value_temp['cart_sub_kg_nw']) ;
				}
			}
			
			$array_content['all_container'][$count_container] = $value_container ;
			$array_content['all_container'][$count_container]['item'] = $array_temp ;
			$array_content['all_container'][$count_container]['sub_number_of_carton'] = $sub_number_of_carton ;
			$array_content['all_container'][$count_container]['sub_m3_single'] = $sub_m3_single ;
			$array_content['all_container'][$count_container]['sub_m3'] = $sub_m3 ;
			$array_content['all_container'][$count_container]['sub_kgs_gw_single'] = $sub_kgs_gw_single ;
			$array_content['all_container'][$count_container]['sub_kgs_gw'] = $sub_kgs_gw ;
			$array_content['all_container'][$count_container]['sub_kgs_nw'] = $sub_kgs_nw ;
			
			$total_number_of_carton += $sub_number_of_carton ;
			$total_m3_single += $sub_m3_single ;
			$total_m3 += $sub_m3 ;
			$total_kgs_gw_single += $sub_kgs_gw_single ;
			$total_kgs_gw += $sub_kgs_gw ;
			$total_kgs_nw += $sub_kgs_nw ;
			
			$count_container++ ;
			
		}
		
		$array_content['total_number_of_carton'] = $total_number_of_carton ;
		$array_content['total_m3_single'] = $total_m3_single ;
		$array_content['total_m3'] = $total_m3 ;
		$array_content['total_kgs_gw_single'] = $total_kgs_gw_single ;
		$array_content['total_kgs_gw'] = $total_kgs_gw ;
		$array_content['total_kgs_nw'] = $total_kgs_nw ;
		
	}
	
	return $array_content ;
	
}

function getCompanyID(){
	global $mysqli, $prefixCustomer, $prefixCompany ;
	$get_company_id = $mysqli->query( "SELECT b.company_id FROM $prefixCustomer a
									   LEFT JOIN $prefixCompany b ON ( a.customer_company_id = b.company_id )
									   WHERE customer_company_id = '".$_SESSION['system_customer_id']."' LIMIT 1" ) ;
	$company_id = 0 ;
	if ( $get_company_id->num_rows > 0 ){
		$row_company_id	= $get_company_id->fetch_assoc() ;
		$company_id = $row_company_id['company_id'] ;
	}	
	return $company_id ;
}

function updateOrderCompany( $company, $customer, $other_customer ){
	global $mysqli, $prefixCustomer, $prefixCompany, $prefixQuotation, $today_date ;
	
	// query for company
	$mysqli_company = $mysqli->query("SELECT company_id FROM $prefixCompany
									  WHERE company_name = '".$company."' LIMIT 1") ;
	if ( $mysqli_company->num_rows > 0 ){
		
		$row_company = $mysqli_company->fetch_assoc() ;
		$company_id = $row_company['company_id'] ;
		
		// query for customer
		$mysqli_customer = $mysqli->query("SELECT customer_id FROM $prefixCustomer
										   WHERE customer_company_id = '".$company_id."' AND customer_name = '".$customer."' LIMIT 1") ;
		if ( $mysqli_customer->num_rows > 0 ){
			
			$row_customer = $mysqli_customer->fetch_assoc() ;
			$customer_id = $row_customer['customer_id'] ;
			
		}else{
			// insert into customer table
			$mysqli->query("INSERT INTO $prefixCustomer
							(customer_company_id, customer_name, customer_date, customer_trash) VALUES
							('".$company_id."', '".$other_customer."', '".$today_date."', '0')") ;
			$customer_id = $mysqli->insert_id ;	
		}
			
	}
	
	$array['company'] = $company_id ;
	$array['customer'] = $customer_id ;
	return $array ;
}

// check same key
function checkArrayKey($array_old, $array_new){
	// default wrong
	$foo = false;
	if( arrayCheck($array_old) && arrayCheck($array_new) ){
		if( !(arrayCheck( array_diff_key($array_old, $array_new) )) ){
			$foo = true; // correct when it is same key
		}
	}
	
	return $foo;
}
?>