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/production.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;

//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");
}


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 first
	if($formula==""){
		return $qty*$packing_qty<$min_qty?ceil($min_qty/$packing_qty):$qty; 
	}else{		
		$formula_check = check_formula($formula);
		if($formula_check!=""){
			return $formula_check;
		}else{
			
			$type = check_group($formula);
			
			if($type=="single"){ //checked no problem		
				$production_qty = $roundup_qty*$packing_qty;
				
				if($production_qty==0){
					return intval("0");	
				}
								
				if(check_oven_qty($production_qty,$oven_qty)){					
					return get_formula_result_for_single_product($sku, $qty);	
				} else {
					return $production_qty<$min_qty?ceil($min_qty/$packing_qty):$production_qty/$packing_qty; 
				}
			}elseif($type=="group"){
				/*
				echo $sku;
				echo "<br>";	
						
				echo $group_order_qty = get_group_order_qty($formula, $qty, $date); 
				echo "<br>";
				
				echo $roundup_qty;
				echo "<br>";				
				
				echo $calculated_qty = get_formula_result_for_single_product($sku, $roundup_qty, 'bypass');
				echo "<br>";
				
				echo $calculated_group_order_qty = get_calculated_group_order_qty($formula, $calculated_qty, $date); 
				echo "<br>";
				*/


				$group_order_qty = get_group_order_qty($formula, $qty, $date); 
				$roundup_qty;
				$calculated_qty = get_formula_result_for_single_product($sku, $roundup_qty, 'bypass');
				$calculated_group_order_qty = get_calculated_group_order_qty($formula, $calculated_qty, $date); 
				
				$arr_grouped_sku = get_grouped_sku($formula);
				
				if(strpos($formula, "\$p")!==false){		
					$zero_qty = false;
					if(empty($roundup_qty)){
						$zero_qty = true;
					}
					
					if(empty($min_qty)){			
						if(empty($oven_qty)){
							return $calculated_qty;
						} else {													
							
							$group_sku_weighting = $roundup_qty/$oven_qty;							
							$group_sku_weighting_after_formula = $calculated_qty/$oven_qty;							
							
							foreach($arr_grouped_sku as $grouped_sku){								
								$groupedSkuObj = get_sku_init_info($grouped_sku);
								$group_sku_production_qty = get_production_qty($grouped_sku, $date);		
								if(empty($group_sku_production_qty)){
									$zero_qty = true;
								}
														
								$group_sku_weighting += $group_sku_production_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);
									
							if($calculated_group_sku_weighting_after_formula_integer-$calculated_group_sku_weighting_integer>0 || ctype_digit(strval($group_sku_weighting))){
								return $roundup_qty;
							} else {
								return get_formula_result_for_single_product($sku, $roundup_qty, 'bypass');
							}
						}
					} else {						
						// has min qty;	
					}
				} else {
				
					if($group_order_qty==0){ //if sum of order qty of all grouped productis 0
						return intval("0");	
					}else{
						
						if(check_oven_qty($group_order_qty, $oven_qty)){						
							$production_qty = get_formula_result_for_single_product($sku, $roundup_qty);		
							if($min_qty!=0){
								if($min_qty>$calculated_group_order_qty){
									if(get_extra_sku($formula)==$sku){		
										//return ceil($min_qty/$packing_qty)." - ".$calculated_group_order_qty." + ".get_formula_result_for_single_product($sku, $roundup_qty, 'bypass');
										return (ceil($min_qty/$packing_qty) - $calculated_group_order_qty) + get_formula_result_for_single_product($sku, $roundup_qty, 'bypass'); //checked;
									} else {									
										return get_formula_result_for_single_product($sku, $roundup_qty, 'bypass'); //checked;
									}
								} else {
									return get_formula_result_for_single_product($sku, $roundup_qty, 'bypass'); // checked;
								}
							} else {
								return get_formula_result_for_single_product($sku, $roundup_qty, 'bypass'); //checked;
							}
							
						} else {	
							
							if($min_qty!=0){
								if($min_qty>$group_order_qty){
									//$production_qty = get_formula_result_for_single_product($sku, $roundup_qty);
									if(get_extra_sku($formula)==$sku){
										return ($min_qty - $group_order_qty) + $roundup_qty; //checked;
									} else {
										return $roundup_qty; //checked;
									}
								} else {
									return $roundup_qty; //checked;
								}
							} else {
								return $roundup_qty; //checked;
							}
							
							//return $roundup_qty*$packing_qty<$min_qty?ceil($min_qty/$packing_qty):$roundup_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":
					case "p":
						$group = true;
						$str_sku = substr($segment,1);	
						$arr_sku = explode(",",$str_sku);
						$sku_check = true;
						foreach($arr_sku as $sku_value){
							if(check_sku($sku_value)==false){
								return 'invalid sku '.$sku_value;										
							}
						}
						break;
					case "v":
						$extra_sku = substr($segment,1);
						if(check_sku($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_result_for_single_product($sku, $qty, $min=''){
	
	$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 = $qty;
	//$arr_sku = array();
	//$extra_sku = "";
	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 "p":				
				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; 
	}
}

function check_sku($sku_value){
	return get_sku_init_info($sku_value);							
}

function check_group($formula){
	if(strpos($formula, "\$g")!==false){
		return "group";	
	}elseif(strpos($formula, "\$p")!==false){
		return "group";	
	}else{		
		return "single";	
	}
}

function get_grouped_sku($formula){
	$segmentPosition = strpos($formula, "\$g");
	if($segmentPosition===false){
		$segmentPosition = strpos($formula, "\$p");
	}	
	$segment = substr($formula, $segmentPosition);
	$arr_segment = explode("$", $segment);
	$str_sku = substr($arr_segment[1],1);	
	$arr_sku = explode(",",$str_sku);		
	
	return $arr_sku;
}


function check_oven_qty($production_qty, $oven_qty){
	// true = proceed to calculation
	// false = do not proceed to calculation
	if($oven_qty==0){
		return true;	
	}else{
		if($production_qty%$oven_qty!=0){
			return true;	
		} else { 
			return false;		
		}
	}
}

function get_group_order_qty($formula, $qty, $date){	
	$ttl_qty = ceil($qty);
	
	/*
	$segmentPosition = strpos($formula, "\$g");
	if($segmentPosition===false){
		$segmentPosition = strpos($formula, "\$p");
	}
	
	$segment = substr($formula, $segmentPosition);
	$arr_segment = explode("$", $segment);
	$str_sku = substr($arr_segment[1],1);	
	$arr_sku = explode(",",$str_sku);	
	*/
	
	$arr_sku = get_grouped_sku($formula);
	
	foreach($arr_sku as $sku){
		$ttl_qty += ceil(get_production_qty($sku, $date));
	}
	
	return $ttl_qty;
}


function get_calculated_group_order_qty($formula, $qty, $date){	

	$ttl_qty = ceil($qty);	
	
	/*	
	$segmentPosition = strpos($formula, "\$g");
	if($segmentPosition===false){
		$segmentPosition = strpos($formula, "\$p");
	}
		
	$segment = substr($formula, $segmentPosition);
	$arr_segment = explode("$", $segment);
	$str_sku = substr($arr_segment[1],1);	
	$arr_sku = explode(",",$str_sku);	
	*/
	
	$arr_sku = get_grouped_sku($formula);
	
	foreach($arr_sku as $sku){
		$salve_qty = get_production_qty($sku, $date);			
		$ttl_qty += ceil(get_formula_result_for_single_product($sku, $salve_qty, 'bypass'));		
		
	}
	
	return $ttl_qty;
	
}


function get_extra_sku($formula){	
	$segmentPosition = strpos($formula, "\$v");
	$segment = substr($formula, $segmentPosition);
	$arr_segment = explode("$", $segment);
	$extra_sku = substr($arr_segment[1],1);	
	return $extra_sku;
}

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;
}
?>
<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
echo "<div id='pageloader'>";
   echo "<img src='https://www.breadsecret.com/custom/image/page-loader.gif' alt='processing...' />";
echo "</div>";
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>";
			echo "<td>\$p[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 "</tbody>";		
echo "</table>";


//$result = $wpdb->get_results("SELECT * FROM stock_init WHERE sku = 'BD-JPL-425' 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-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 ORDER by custom_formula DESC, if(end_date='0000-00-00',0,1), end_date desc LIMIT 100");	
echo "<br>";
echo "<table style='border-collapse: collapse;' border='1' cellpadding='5' cellspacing='5'>";
	echo "<tr>";
		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="";
		if(strpos(strtoupper($skuObj->description), "BAGEL")!==false){
			$style="background-color:#eee";
		}
		echo "<tr style='".$style."'>";
			
			$order_qty = get_production_qty($skuObj->sku, $date);
			$order_qty_per_pieces = get_production_qty($skuObj->sku, $date) * $skuObj->unit_qty;
			$actual_production_qty = get_calculated_qty($skuObj->sku, get_production_qty($skuObj->sku, $date), $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'>".$skuObj->unit_qty." ".$skuObj->display_unit."</td>";
			echo "<td class='qty_with_packing_unit' align='center'><span style='color:blue'>".$order_qty."</span> ".$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'><span style='color:blue'>".$actual_production_qty."</span> ".$skuObj->packing_unit."</td>";			
			echo "<td class='result_with_display_unit' align='center'><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 );
			}
		});				
	});		
	
	function reload(){
		$("#pageloader").fadeIn();
		window.location = "?key=<?php echo $_GET['key'];?>&delivery_date="+$("#delivery_date").val();	
	}
	
	$(".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>