HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/breadsecret.com/productionIC.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include '/var/www/html/breadsecret.com/wp-load.php';
// set timezone
date_default_timezone_set("Asia/Hong_Kong");
global $wpdb, $current_site, $sitepress;
?>
<style>
#pageloader
{
  background: rgba( 255, 255, 255, 0.9 );  
  height: 100%;
  position: fixed;
  width: 100%;
  z-index: 9999;
  /*top:80px;*/
}

#pageloader img
{
    width: 100px;
    height: 100px;
    position: absolute;
    left: 45%;
    top: 40%;
}
</style>
<?php
//check key for autenication
if(!isset($_GET['key']) || $_GET['key']!="22785700"){
	echo "You are not allowed to enter this page";
	exit();
}

//get delivery date
if(isset($_GET['delivery_date']) && $_GET['delivery_date']!=""){
	$date = $_GET['delivery_date'];
}else{
	$date = date("Y-m-d");
}

// test function for simulating order qty
function get_order_qty($sku, $date){ 

	return get_production_qty($sku, $date);
/*
	global $wpdb;
	$results = $wpdb->get_results("SELECT qty FROM test WHERE sku = '".$sku."' LIMIT 1 ");
	if(isset($results)){
		return $results[0]->qty;
	} else {
		return intval("0");	
	}
*/
}

// 取得同組內所有SKU執行公式後之產量總和 
function get_calculated_group_order_qty($arr_sku, $qty, $date, $type=''){	

	$ttl_qty = ceil($qty);	
	
	foreach($arr_sku as $sku){
		$salve_qty = get_order_qty($sku, $date);			
		$ttl_qty += ceil(get_formula_result_for_single_product($sku, $salve_qty, 'bypass', $type));		
	}
	
	return $ttl_qty;
}

// 檢查公式是否符合格式要求
function check_formula($formula){
		
	$group = false;
	$inherit_sku = "";
	
	if(strpos($formula, "$")===false){
		return 'invalid formula';
	}else{		
		$arr_segment = explode("$",$formula);	
		foreach($arr_segment as $idx=>$segment){
			if($idx==0){
				continue;
			}else{
				$operator = substr($segment,0,1);
				switch($operator){
					case "+":										
					case "-":
					case "*":
					case "/":
						$number = substr($segment,1);
						if(!is_numeric($number)){							
							return 'invalid number '.$number;								
						}
						break;	
					case "u":						
					case "d":
						break;	
					case "g":
						$group = true;
						$str_sku = substr($segment,1);	
						$arr_sku = explode(",",$str_sku);
						$sku_check = true;
						foreach($arr_sku as $sku_value){
							if(get_sku_init_info($sku_value)==false){
								return 'invalid sku '.$sku_value;										
							}
						}
						break;
					case "v":
						$extra_sku = substr($segment,1);
						if(get_sku_init_info($extra_sku)==false){
							return 'invalid sku '.$extra_sku;										
						}else{
							$inherit_sku = $extra_sku;
						}								
						break;
					
					default:							
						return 'invalid operator '.$operator;
						break;						
				}				
				
			}
			
		} // end for each
		
		if($group==true && $inherit_sku == ""){
			return "missing \$v operator";	
		}
		
		return ;
	}
}

// 按符號來取得參數
function get_formula_parameter($formula, $sign){	
	$segmentPosition = strpos($formula, $sign);
	$segment = substr($formula, $segmentPosition);
	$arr_segment = explode("$", $segment);
	$value = substr($arr_segment[1],1);	
	return $value;
}

// 檢查是否屬於合併計算的產品
function check_group($formula){
	if(strpos($formula, "\$g")!==false){
		return "group";	
	}else{		
		return "single";	
	}
}

// 取得除自己外, 其他在同組的SKU
function get_grouped_sku($formula){	
	$str_sku = get_formula_parameter($formula, "\$g");
	$arr_sku = explode(",",$str_sku);				
	return $arr_sku;
}

// 取得哪些SKU可以被分配剩餘數
function get_grouped_sku_can_be_assigned($formula, $sku, $qty, $date){		
	$arr_sku_can_be_assigned = array();	

	if($qty!=0){
		array_push($arr_sku_can_be_assigned, $sku);	
	}	
	
	foreach(get_grouped_sku($formula) as $grouped_sku){
		if(get_order_qty($grouped_sku, $date)!=0){
			array_push($arr_sku_can_be_assigned, $grouped_sku);	
		}
	}
					
	return $arr_sku_can_be_assigned;
}

// 取得所有同組的SKU訂單數之總和
function get_group_order_qty($arr_sku, $qty, $date){	
	$ttl_qty = ceil($qty);
	foreach($arr_sku as $sku){
		$ttl_qty += ceil(get_order_qty($sku, $date));
	}	
	return $ttl_qty;
}

// 檢查同組內的所有SKU是否有相同的焗爐容量
function check_group_oven_qty($arr_sku, $oven_qty){	
	foreach($arr_sku as $sku){
		$skuObj = get_sku_init_info($sku);
		if($skuObj->oven_qty != $oven_qty){
			return false;
		}
	}
	return true;	
}


// 檢查是屬於哪一類型的訂單 (全團購/全街客/混合)
function check_customer_type($sku, $arr_sku, $date){
	return "individualCustomer";	
	
	$arr_type = array();
	if(check_stock_init($sku, $date)==1){
		array_push($arr_type, 1);
	} else {
		array_push($arr_type, 0);
	}
	
	foreach($arr_sku as $group_sku){
		if(check_stock_init($sku, $date)==1){
			array_push($arr_type, 1);		
		} else {
			array_push($arr_type, 0);
		}		
	}
	
	if(array_sum($arr_type)==0){
		return "groupCustomer";	
	} elseif(array_sum($arr_type)==count($arr_type)){
		return "individualCustomer";	
	} else {
		return "mixedCustomer";	
	}
}




// 檢查訂單數是否已排滿一個爐
// true = proceed to calculation
// false = do not proceed to calculation

function check_oven_qty($sku, $formula, $order_qty, $packing_qty, $oven_qty, $date){	
	
	$type = check_group($formula);
	$order_qty = ceil($order_qty);	
	$order_qty_pieces = $order_qty * $packing_qty;
	
	if($type=="single"){
		if($oven_qty==0){
			return true;
		}else{
			if($order_qty_pieces%$oven_qty!=0){
				return true;
			} else { 
				return false;		
			}
		}		
	} else {
		$arr_sku = get_grouped_sku($formula);
		
		if(check_group_oven_qty($arr_sku, $oven_qty)){ //同組內的所有SKU有相同的焗爐容量
			if($oven_qty==0){
				return true;
			}elseif(get_group_order_qty($arr_sku, $order_qty, $date)*$packing_qty%$oven_qty==0){
				return false;		
			} else {
				return true;	
			}
		} else { // 同組內的所有SKU有不同的焗爐容量, 按比例來計算
			if($oven_qty==0){
				return true;		
			}else{
				$total = $order_qty_pieces/$oven_qty;
				foreach($arr_sku as $group_sku){
					$skuObj = get_sku_init_info($group_sku);
					$total += get_order_qty($group_sku,$date)*$packing_qty / $skuObj->oven_qty;	
				}
				
				if(ctype_digit(strval($total))){ //如果除得盡, 即代表剛好已排滿一個爐
					return false;	
				} else {
					return true;
				}				
			}		
		}		
	}
}

// 取得執行公式後之數量 ($min 如果是'bypass', 則不需要理會min. qty)
function get_formula_result_for_single_product($sku, $qty, $min='', $zero=''){
	
	$skuObj = get_sku_init_info($sku);
	$min_qty = $skuObj->min_production_qty;
	$oven_qty = $skuObj->oven_qty;
	$formula = $skuObj->custom_formula;
	$packing_qty = $skuObj->unit_qty;
	
	$arr_segment = explode("$",$formula);
	$result = ceil($qty);
	
	if($zero=='bypass'){
		if($result==0){
			return intval("0");	
		}
	}
	
	foreach($arr_segment as $idx=>$segment){
		if($idx==0){
			continue;
		}else{
			$operator = substr($segment,0,1);
			
			switch($operator){
				case "+":
					$number = substr($segment,1);
					if(!is_numeric($number)){							
						return 'invalid number '.$number;								
					}else{
						$result = $result + $number;
					}	
					break;						
				case "-":
					$number = substr($segment,1);
					if(!is_numeric($number)){							
						return 'invalid number '.$number;								
					}else{
						$result = $result - $number;
					}
					break;	
				case "*":
					$number = substr($segment,1);
					if(!is_numeric($number)){							
						return 'invalid number '.$number;								

					}else{
						$result = (ceil($result)%$number === 0) ? ceil($result) : round(($result+$number/2)/$number)*$number;
					}
					break;							
				case "/":
					$number = substr($segment,1);
					if(!is_numeric($number)){							
						return 'invalid number '.$number;								
					}else{
						$result = $result / $number;
					}
					break;	
				case "u":							
					$result = ceil($result);
					break;						
				case "d":
					$result = floor($result);
					break;
				case "g":			
				case "v":				
					break;					
				default:							
					return 'invalid operator '.$operator;
					break;						
			}
		}	
	}
	
	if($min=='bypass'){
		return $result;
	} else {
		return $result*$packing_qty<$min_qty?ceil($min_qty/$packing_qty):$result; 
	}
}


// main function
function get_calculated_qty($sku, $qty, $date){
	
	$skuObj = get_sku_init_info($sku);
	$min_qty = $skuObj->min_production_qty;
	$oven_qty = $skuObj->oven_qty;
	$formula = $skuObj->custom_formula;
	$packing_qty = $skuObj->unit_qty;
	$roundup_qty = ceil($qty); // 先roundup為整數
	$order_qty_pieces = $roundup_qty*$packing_qty;
	
	if($formula==""){ // 如果沒有設定公式, 只檢查最少訂單量
		return $order_qty_pieces<$min_qty?ceil($min_qty/$packing_qty):$roundup_qty; 	
	}else{
		$formula_check = check_formula($formula);
		if($formula_check!=""){ // 如果公式有問題, 則顯示錯誤
			return $formula_check;
		}else{
			$type = check_group($formula); // 取得是否組合計算的SKU
			$arr_sku = get_grouped_sku($formula); //取得同組內其他SKU
			$customer_type = check_customer_type($sku, $arr_sku, $date);
			
			if($type=="single"){ // 如果是單一計算產品								
				
				if($roundup_qty==0){ // 如果不用生產, 就咩都唔做
					return intval("0");	
				}
											
				if(check_oven_qty($sku, $formula, $roundup_qty, $packing_qty, $oven_qty, $date)){ // 如果未滿爐就行formula
					return get_formula_result_for_single_product($sku, $roundup_qty, '', 'bypass');	
				} else {
					return $order_qty_pieces<$min_qty?ceil($min_qty/$packing_qty):$roundup_qty;  // 檢查最少訂單量
				}
				
			} else { //group product			
				$group_order_qty = get_group_order_qty($arr_sku, $roundup_qty, $date);  // 取得所有同組的SKU訂單數之總和
				$group_order_qty_pieces = $group_order_qty*$packing_qty;
				
				$calculated_qty = get_formula_result_for_single_product($sku, $roundup_qty, 'bypass', 'bypass'); // 取得執行公式後之數量 ($min 如果是'bypass', 則不需要理會min. qty)
				$calculated_qty_pieces = $calculated_qty*$packing_qty;
				
				$calculated_group_order_qty = get_calculated_group_order_qty($arr_sku, $calculated_qty, $date, 'bypass'); // 取得同組內所有SKU執行公式後之產量總和 
				$calculated_group_order_qty_pieces = $calculated_group_order_qty*$packing_qty;
				
				if($group_order_qty==0){ // 所有同組的SKU訂單數之總和是0, 就咩都唔做
					return intval("0");	
				} else {
					
					if(check_oven_qty($sku, $formula, $roundup_qty, $packing_qty, $oven_qty, $date)){ // 睇下本身合拼數是否已排滿一個爐
						
						//檢查產品客人類別 //groupCustomer / individualCustomer / mixedCustomer
						if($customer_type=="groupCustomer"){
							if($roundup_qty==0){ // 如果不用生產, 就咩都唔做
								return intval("0");	
							}							
							
							if($group_order_qty_pieces<$min_qty){ // 如果同組所有SKU訂單數少於最少訂單數
								
								
								$remaining_qty_pieces = $min_qty - $group_order_qty_pieces; 
								$remaining_qty = floor($remaining_qty_pieces / $packing_qty);								
								$arr_sku_being_assigned = get_grouped_sku_can_be_assigned($formula, $sku, $roundup_qty, $date); //取得哪些SKU可以被分配剩Min.Qty餘數
								$sku_being_assigned_count = count($arr_sku_being_assigned);
								
								if($sku_being_assigned_count==1){ // 如果只得一個SKU可以被分配Min.Qty剩餘數, 全部剩餘數就分配給此SKU
									return $roundup_qty + $remaining_qty;
								} else {								
									$remainder_pieces = $remaining_qty_pieces%$sku_being_assigned_count; // 取得分配Min.Qty餘數後之最終餘數
									$remainder = $remainder_pieces/$packing_qty;
									$quotient_pieces = intval($remaining_qty_pieces/$sku_being_assigned_count);	// 取得每個SKU(不包括QTY為0的SKU)平均可分得多少餘數															
									$extra =  $quotient_pieces/$packing_qty;								
	
									if($remainder_pieces==0){ // 如果Min.Qty餘數可平均分給所有SKU (不包括QTY為0的SKU)								
										return $roundup_qty + $extra;
									} else {																	
										if($sku==get_formula_parameter($formula, "\$v")){  // 如果Min.Qty餘數不可平均分給所有SKU (不包括QTY為0的SKU), 把最終餘數全加到$v的SKU內
											return $roundup_qty + $extra + $remainder;
										} else {
											return $roundup_qty + $extra;
										}									
									}
								}
								
							} else { // 同組所有SKU訂單數總和符合最少訂單數要求						
								
								if(check_group_oven_qty($arr_sku, $oven_qty)) { // 同組內SKU有相同的焗爐容量									
									$plate_count_before_calculation = $group_order_qty_pieces/$oven_qty; // 計算組內所有SKU訂單數量所佔的焗爐
									$plate_count_after_calculation = $calculated_group_order_qty_pieces/$oven_qty; //計算組內所有SKU訂單數量執行公式後所佔的焗爐位置
									$full_plate_after_calculation = ctype_digit(strval($plate_count_after_calculation)); // 檢查組內所有SKU訂單數量執行公式後所佔的焗爐位置是否整數
									
									if($full_plate_after_calculation){ // 如果組內所有SKU訂單數量執行公式後所佔的焗爐位置是整數 (即剛好用盡所有位置)
										return $calculated_qty;
									} else {								
										
										if(intval($plate_count_after_calculation) - intval($plate_count_before_calculation) >=1){ // 如果組內所有SKU訂單數量執行公式後需要另開一爐	
											$remaining_qty_pieces = $oven_qty*intval($plate_count_after_calculation) - $group_order_qty_pieces;											
											$additional_qty_pieces = floor($remaining_qty_pieces/(count($arr_sku)+1));
											$final_remaining_qty_pieces = $remaining_qty_pieces%(count($arr_sku)+1);
																						
											if($sku==get_formula_parameter($formula, "\$v")){  // 如果Min.Qty餘數不可平均分給所有SKU (不包括QTY為0的SKU), 把最終餘數全加到$v的SKU內
												
												return $roundup_qty + $additional_qty_pieces/$packing_qty + $final_remaining_qty_pieces/$packing_qty;
											} else {
												return $roundup_qty + $additional_qty_pieces/$packing_qty;
											}												
										} else {
											return $calculated_qty;
										}
									}
								} else { // 同組內SKU有不同的焗爐容量							
																	
									$group_sku_weighting = $order_qty_pieces/$oven_qty;	// 計算此SKU訂單數量需要佔多少焗爐位置						
									$group_sku_weighting_after_formula = $calculated_qty_pieces/$oven_qty;	// 計算此SKU訂單數量執行公式後需要佔多少焗爐位置									
									
									foreach($arr_sku as $grouped_sku){ // 計算組內其他SKU訂單數量需要佔多少焗爐位置							
										$groupedSkuObj = get_sku_init_info($grouped_sku);
										$group_sku_production_qty = get_order_qty($grouped_sku, $date);		
										$group_sku_weighting += $group_sku_production_qty*$packing_qty/$groupedSkuObj->oven_qty;								
										$group_sku_weighting_after_formula += get_formula_result_for_single_product($grouped_sku, $group_sku_production_qty, 'bypass', 'bypass')/$groupedSkuObj->oven_qty;
									}	
																
									$calculated_group_sku_weighting_integer = intval($group_sku_weighting); 
									$calculated_group_sku_weighting_after_formula_integer = intval($group_sku_weighting_after_formula);
									
									// 如果組內所有SKU訂單數量執行公式後需要另開一爐 或 	
									if($calculated_group_sku_weighting_after_formula_integer - $calculated_group_sku_weighting_integer >= 1 || ctype_digit(strval($group_sku_weighting))){
										if($sku==get_formula_parameter($formula, "\$v")){									
											$remaining_qty_can_be_added = (1-($group_sku_weighting-floor($group_sku_weighting)))*$oven_qty;										
											
											return $roundup_qty + ($remaining_qty_can_be_added/$packing_qty);
											
											//return $roundup_qty + floor($remaining_qty_can_be_added/$packing_qty);
											
											
											
										} else {
											return $roundup_qty;
										}
									} else { // 如果組內所有SKU訂單數量執行公式後不需要另開一爐
										return get_formula_result_for_single_product($sku, $roundup_qty, 'bypass', 'bypass');
									}								
								}
								
							}
						} elseif($customer_type=="individualCustomer") {

							$calculated_qty = get_formula_result_for_single_product($sku, $roundup_qty, 'bypass', ''); // 取得執行公式後之數量 ($min 如果是'bypass', 則不需要理會min. qty)
							$calculated_qty_pieces = $calculated_qty*$packing_qty;								
							$calculated_group_order_qty = get_calculated_group_order_qty($arr_sku, $calculated_qty, $date, ''); // 取得同組內所有SKU執行公式後之產量總和 
							$calculated_group_order_qty_pieces = $calculated_group_order_qty*$packing_qty;
															
							if($group_order_qty_pieces<$min_qty){ // 如果同組所有SKU訂單數少於最少訂單數
								
								if($calculated_group_order_qty_pieces<$min_qty){
									
									if($sku==get_formula_parameter($formula, "\$v")){  
										$remaining_qty_pieces = $min_qty - $calculated_group_order_qty_pieces;
										return get_formula_result_for_single_product($sku, $roundup_qty, 'bypass', '') + floor($remaining_qty_pieces / $packing_qty);
									} else {
										return get_formula_result_for_single_product($sku, $roundup_qty, 'bypass', '');
									}
										
								} else {																		
									return get_formula_result_for_single_product($sku, $roundup_qty, 'bypass', '');									
								}
							
							} else { // 同組所有SKU訂單數總和符合最少訂單數要求
								
								if(check_group_oven_qty($arr_sku, $oven_qty)) { // 同組內SKU有相同的焗爐容量
									
									//return "1!!1111";
									$plate_count_before_calculation = $group_order_qty_pieces/$oven_qty; // 計算組內所有SKU訂單數量所佔的焗爐位置
									$plate_count_after_calculation = $calculated_group_order_qty_pieces/$oven_qty; //計算組內所有SKU訂單數量執行公式後所佔的焗爐位置
									$full_plate_after_calculation = ctype_digit(strval($plate_count_after_calculation)); // 檢查組內所有SKU訂單數量執行公式後所佔的焗爐位置是否整數
									
									if($full_plate_after_calculation){ // 如果組內所有SKU訂單數量執行公式後所佔的焗爐位置是整數 (即剛好用盡所有位置)
										return $calculated_qty;
									} else {								
										if(intval($plate_count_after_calculation) - intval($plate_count_before_calculation) >=1){ // 如果組內所有SKU訂單數量執行公式後需要另開一爐	
											if($sku==get_formula_parameter($formula, "\$v")){  // 如果Min.Qty餘數不可平均分給所有SKU (包括QTY為0的SKU), 把最終餘數全加到$v的SKU內
												$remaining_qty_pieces = $oven_qty*intval($plate_count_after_calculation) - $group_order_qty_pieces;
												return $roundup_qty + floor($remaining_qty_pieces/$packing_qty);						
												
											} else {
												return $roundup_qty;
											}												
										} else {
											/*
											$excess_qty_pieces = $oven_qty - ($calculated_group_order_qty_pieces % $oven_qty);
											$additional_qty_pieces = floor($excess_qty_pieces/(count($arr_sku)+1));
											$additional_qty = $additional_qty_pieces/$packing_qty;
											if($sku==get_formula_parameter($formula, "\$v")){  // 如果Min.Qty餘數不可平均分給所有SKU (包括QTY為0的SKU), 把最終餘數全加到$v																		
												return $calculated_qty + $additional_qty + ($excess_qty_pieces%(count($arr_sku)+1)/$packing_qty);												
											} else {
												return $calculated_qty + $additional_qty;
											}
											*/
											return $calculated_qty;
										}
									}
									
									
								} else { // 同組內SKU有不同的焗爐容量								
									
									/****************************************/
									$group_sku_weighting = $order_qty_pieces/$oven_qty;	// 計算此SKU訂單數量需要佔多少焗爐位置						
									$group_sku_weighting_after_formula = $calculated_qty_pieces/$oven_qty;	// 計算此SKU訂單數量執行公式後需要佔多少焗爐位置									
									
									foreach($arr_sku as $grouped_sku){ // 計算組內其他SKU訂單數量需要佔多少焗爐位置							
										$groupedSkuObj = get_sku_init_info($grouped_sku);
										$group_sku_production_qty = get_order_qty($grouped_sku, $date);		
										$group_sku_weighting += $group_sku_production_qty*$packing_qty/$groupedSkuObj->oven_qty;								
										$group_sku_weighting_after_formula += get_formula_result_for_single_product($grouped_sku, $group_sku_production_qty, 'bypass', '')/$groupedSkuObj->oven_qty;
									}	
																
									$calculated_group_sku_weighting_integer = intval($group_sku_weighting); 
									$calculated_group_sku_weighting_after_formula_integer = intval($group_sku_weighting_after_formula);
									
									
									
									
									// 如果組內所有SKU訂單數量執行公式後需要另開一爐 或 	
									
									if($calculated_group_sku_weighting_after_formula_integer - $calculated_group_sku_weighting_integer >= 1 || ctype_digit(strval($group_sku_weighting))){
										
										if($sku==get_formula_parameter($formula, "\$v")){	
																															
											$remaining_qty_can_be_added = (1-($group_sku_weighting-floor($group_sku_weighting)))*$oven_qty;
											return $roundup_qty + ($remaining_qty_can_be_added/$packing_qty);
										} else {
											return $roundup_qty;
										}
									} else { // 如果組內所有SKU訂單數量執行公式後不需要另開一爐
										
										return get_formula_result_for_single_product($sku, $roundup_qty, 'bypass', '');
									}
									
								}
							}
							
						} else {
							return "未做mixedCustomerCase";
						}
						
					} else {
						return $roundup_qty;
					}	
							
				}
				
			}
			
		}
		
	}
	
}

echo "<div id='pageloader'>";
   echo "<img src='https://www.breadsecret.com/custom/image/page-loader.gif' alt='processing...' />";
echo "</div>";

//legend table
echo "<table style='border-collapse: collapse;display:none' border='1' cellpadding='3' cellspacing='3' id='legend_table'>";
	echo "<thead>";
		echo "<tr>";
			echo "<th>";
				echo "Symbol";
			echo "</th>";			
			echo "<th>";
				echo "Meaning";			
			echo "</th>";						
			echo "<th>";
				echo "Example";			
			echo "</th>";				
		echo "</tr>";
	echo "</thead>";
	echo "<tbody>";
		echo "<tr>";
			echo "<td>$+[n]</td><td>Add n to the quantity value</td><td>$+1</td>";				
		echo "</tr>";
		echo "<tr>";
			echo "<td>$-[n]</td><td>Subtract n from the quantity value</td><td>$-1</td>";				
		echo "</tr>";
		echo "<tr>";
			echo "<td>$*[n]</td><td>Round the quantity value up to the nearest n multiples</td><td>$*4</td>";				
		echo "</tr>";
		echo "<tr>";
			echo "<td>$/[n]</td><td>Divide the quantity value by n</td><td>$/4</td>";				
		echo "</tr>";	
		echo "<tr>";
			echo "<td>\$u</td><td>Round the quantity value up to the nearest integer</td><td>\$u</td>";				
		echo "</tr>";	
		echo "<tr>";
			echo "<td>\$d</td><td>Round the quantity value down to the nearest integer</td><td>\$d</td>";				
		echo "</tr>";		
		echo "<tr>";
			echo "<td>\$g[SKU1]</td><td>Use together with \$v[SKU2]<br>If the sum of quantity values of current SKU and [SKU1] is smaller than the min. production quantity value,<br>then add the excess quanitity to [SKU2] to meet the min. production quantity requirement
			</td><td>\$gBD-WNL-460\$vBD-RBL-460</td>";				
		echo "</tr>";	
		echo "<tr style='background-color:lavender'>";
			echo "<td>Color Legend</td><td colspan='2'>SKU in group is only opened for group buyer</td>";		
		echo "</tr>";	
		echo "<tr style='background-color:lightyellow'>";
			echo "<td>Color Legend</td><td colspan='2'>SKU in group is only opened for individual customer</td>";
		echo "</tr>";	
										
	echo "</tbody>";		
echo "</table>";

// main query
// Bagle
$result = $wpdb->get_results("SELECT * FROM stock_init WHERE sku in ('BD-BGL-200','BD-SPB-200','BD-CJB-280') ORDER by custom_formula DESC, if(end_date='0000-00-00',0,1), end_date desc LIMIT 100");	
// test
//$result = $wpdb->get_results("SELECT * FROM stock_init WHERE sku in ('BD-BRL-460','BD-FPB-300','BD-WLR-300') ORDER by custom_formula DESC, if(end_date='0000-00-00',0,1), end_date desc LIMIT 100");	
// 方包
//$result = $wpdb->get_results("SELECT * FROM stock_init WHERE sku in ('BD-RBL-460','BD-WNL-460') ORDER by custom_formula DESC, if(end_date='0000-00-00',0,1), end_date desc LIMIT 100");	
// ALL
//$result = $wpdb->get_results("SELECT * FROM stock_init ORDER by custom_formula DESC, if(end_date='0000-00-00',0,1), end_date desc LIMIT 100");	

echo "<br>";
echo "Case C (組內所有SKU都有開放給街客購買) <br>";
// content table
echo "<table style='border-collapse: collapse;' border='1' cellpadding='5' cellspacing='5'>";
	echo "<tr>";
		echo "<td><input type='button' class='legend' value='顯示/隱藏說明'></td>";
		echo "<td>送貨日期: </td>";				
		echo "<td><input type='date' name='delivery_date' id='delivery_date' value='".$date."'></td>";		
		echo "<td colspan='9' align='right'><input class='calculate' type='button' value='重新運算'> </td>";		
		//echo "<td>送貨日期: </td>";				
		//echo "<td><input type='date' name='delivery_date' id='delivery_date' value='".$date."'></td>";
		//echo "<td><input class='calculate' type='button' value='重新運算'></td>";
		//echo "<td><input type='button' class='legend' value='顯示/隱藏說明'></td>";
	echo "</tr>";	
	echo "<tr>";
		echo "<td>SKU</td>";	
		echo "<td>產品名稱</td>";		
		echo "<td>最少生產量</td>";	
		echo "<td>焗爐容量</td>";				
		echo "<td>運算公式</td>";
		echo "<td>包裝數量</td>";	
		echo "<td colspan='2'>訂單數量</td>";
		echo "<td colspan='2'>建議生產量</td>";	
						
	echo "</tr>";
	
	foreach($result as $skuObj){			
		$style="";
		$customerType = check_customer_type($skuObj->sku, get_grouped_sku($skuObj->custom_formula), $date);
		if($customerType=='groupCustomer'){
		//if(strpos(strtoupper($skuObj->description), "BAGEL")!==false){
			$style="background-color:lavender";
		} elseif($customerType=='individualCustomer') {
			$style="background-color:lightyellow";
		} else {
			$style="background-color:#eee";	
		}
		
		echo "<tr style='".$style."'>";
			
			$order_qty = get_order_qty($skuObj->sku, $date);
			$order_qty_per_pieces = $order_qty * $skuObj->unit_qty;
			//$actual_production_qty = get_calculated_qty($skuObj->sku, get_order_qty($skuObj->sku, $date), $date);
			$actual_production_qty = get_calculated_qty($skuObj->sku, $order_qty, $date);
			$actual_production_qty_per_pieces = $actual_production_qty * $skuObj->unit_qty;
			
			echo "<td class='sku'>".$skuObj->sku."</td>";	
			echo "<td>".$skuObj->description."</td>";	
			echo "<td><input class='min_production_qty' type='number' value='".$skuObj->min_production_qty."' style='width:80px'> ".$skuObj->display_unit."</td>";	
			echo "<td><input class='oven_qty' type='number' value='".$skuObj->oven_qty."' style='width:80px'> ".$skuObj->display_unit."</td>";																	
			echo "<td><input class='formula' type='text' value='".$skuObj->custom_formula."' style='width:400px'></td>";				
			echo "<td align='center'><input class='packing_qty' type='number' value='".$skuObj->unit_qty."' style='width:80px'>".$skuObj->display_unit."</td>";
			//echo "<td align='center'><input class='qty_with_packing_unit' type='number' value='".$order_qty."' style='width:80px'> ".$skuObj->packing_unit."</td>";				
			echo "<td align='center'>".$order_qty." ".$skuObj->packing_unit."</td>";				
			echo "<td class='qty_with_display_unit' align='center'><span style='color:blue'>".$order_qty_per_pieces."</span> ".$skuObj->display_unit."</td>";							
			echo "<td class='result_with_packing_unit' align='center' style='background-color:wheat'><span style='color:blue'>".$actual_production_qty."</span> ".$skuObj->packing_unit."</td>";			
			echo "<td class='result_with_display_unit' align='center' style='background-color:thistle'><span style='color:blue'>".$actual_production_qty_per_pieces."</span> ".$skuObj->display_unit."</td>";									
		echo "</tr>";		
	}
echo "<table>";

?>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>

/*
	$(".formula").on('paste', function (e) {
		return false;
	});
	
	$(".min_production_qty").on('paste', function (e) {
		return false;
	});	
*/	
	$(".formula").on('keyup blur', function (e) {
		sku = $(this).parent().parent().find('.sku').text();
		var dataString = 'section=update_formula&sku='+encodeURIComponent(sku)+'&formula='+encodeURIComponent($(this).val());		
		
		$.ajax({
			type: "POST",
			url: "ajax_test.php",
			data: dataString,
			cache: false,
			success: function(data){
				setTimeout(keyPressReload(), 500 );
			}
		});				
		
	});
	
	$(".min_production_qty").on('keyup blur', function (e) {
		sku = $(this).parent().parent().find('.sku').text();
		var dataString = 'section=update_min_production_qty&sku='+encodeURIComponent(sku)+'&min_production_qty='+encodeURIComponent($(this).val());		
				
		$.ajax({
			type: "POST",
			url: "ajax_test.php",
			data: dataString,
			cache: false,
			success: function(data){
				setTimeout(keyPressReload(), 500 );
			}
		});				
		
	});	
	
	$(".oven_qty").on('keyup blur', function (e) {
		sku = $(this).parent().parent().find('.sku').text();
		var dataString = 'section=update_oven_qty&sku='+encodeURIComponent(sku)+'&oven_qty='+encodeURIComponent($(this).val());		
				
		$.ajax({
			type: "POST",
			url: "ajax_test.php",
			data: dataString,
			cache: false,
			success: function(data){
				setTimeout(keyPressReload(), 200 );
			}
		});				
	});		
	
	$(".packing_qty").on('keyup blur', function (e) {
		sku = $(this).parent().parent().find('.sku').text();
		var dataString = 'section=update_packing_qty&sku='+encodeURIComponent(sku)+'&packing_qty='+encodeURIComponent($(this).val());		
				
		$.ajax({
			type: "POST",
			url: "ajax_test.php",
			data: dataString,
			cache: false,
			success: function(data){
				setTimeout(keyPressReload(), 200 );
			}
		});				
	});		
	
	$(".qty_with_packing_unit").on('keyup blur', function (e) {
		sku = $(this).parent().parent().find('.sku').text();
		var dataString = 'section=update_production_qty&sku='+encodeURIComponent(sku)+'&production_qty='+encodeURIComponent($(this).val());		
				
		$.ajax({
			type: "POST",
			url: "ajax_test.php",
			data: dataString,
			cache: false,
			success: function(data){
				setTimeout(keyPressReload(), 200 );
			}
		});				
	});		
	
	
	function reload(){
		$("#pageloader").fadeIn();
		window.location = "?key=<?php echo $_GET['key'];?>&delivery_date="+$("#delivery_date").val();	
		//window.location = "?key=<?php echo $_GET['key'];?>";
	}
	
	$(".calculate").click(function(e){
		reload();
	});
	
	$("#delivery_date").change(function(e){
		reload();	
	});	

	
	function keyPressReload(){	
		$(".formula").on('keypress',function(e) {
			if(e.which == 13) {
				reload();	
			}
		});	
		
		$(".min_production_qty").on('keypress',function(e) {
			if(e.which == 13) {
				reload();	
			}
		});		
		
		$(".oven_qty").on('keypress',function(e) {
			if(e.which == 13) {
				reload();	
			}
		});		
	}
		
		
	$("#pageloader").fadeOut();
	
	$(".legend").click(function(e){
		$("#legend_table").toggle();
	});	
	
</script>