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/www.winghung.com/demo_v4/ajax_data.php
<?php
//include wp libriries and set timezone to HK
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('../wp-load.php'); 
session_start();
date_default_timezone_set("Asia/Hong_Kong"); 
global $wpdb, $sitepress;


if($_POST['section']=="calculate_price"){	
	
	$paper = $wpdb->get_results("SELECT * FROM `custom_material_paper` WHERE id = '".$_POST['paper']."' LIMIT 1");
	$color = $wpdb->get_results("SELECT * FROM `custom_printing_color` WHERE id = '".$_POST['color']."' LIMIT 1");
	
	$custom_value = $wpdb->get_results("SELECT * FROM `custom_value` WHERE name = 'product_width_buffer' LIMIT 1");
	$buffer_width = $custom_value[0]->value;

	$custom_value = $wpdb->get_results("SELECT * FROM `custom_value` WHERE name = 'paper_wastage' LIMIT 1");
	$paper_wastage = $custom_value[0]->value;	
		
	$arr_result = array('qty'=>$_POST['qty'], 'paper'=>$paper[0]->name, 'color'=>$color[0]->name, 'buffer'=>$buffer_width);
	
	$productResult = $wpdb->get_results("SELECT * FROM `custom_quotation_product` WHERE status = '1'");
	foreach($productResult as $product){
		$arr_product = array();
		$arr_product['selector_id'] = 'price_'.$product->id;
		$arr_product['product_id'] = $product->id;
		$arr_product['product_name'] = $product->name;
		
		$arr_product['product_buffered_length_cm'] = $product->folded_length_cm + 2*$buffer_width;
		$arr_product['product_buffered_width_cm'] = $product->folded_width_cm + 2*$buffer_width;

		$paper_length = $paper[0]->length_cm;
		$paper_width = $paper[0]->width_cm;

		$arr_product['paper_length'] = $paper_length;
		$arr_product['paper_width'] = $paper_width;

		// type set 1
		$count_length_1 = floor($paper_length/$arr_product['product_buffered_length_cm']);
		$count_width_1 = floor($paper_width/$arr_product['product_buffered_width_cm']);		

		//$arr_product['count_length_1'] = $count_length_1;
		//$arr_product['count_width_1'] = $count_width_1;

		$typesetting_1 = $count_length_1 * $count_width_1;
		//$arr_product['typesetting_1'] = $typesetting_1;

		// type set 2
		$count_length_2 = floor($paper_width/$arr_product['product_buffered_length_cm']);
		$count_width_2 = floor($paper_length/$arr_product['product_buffered_width_cm']);		

		//$arr_product['count_length_2'] = $count_length_2;
		//$arr_product['count_width_2'] = $count_width_2;

		$typesetting_2 = $count_length_2 * $count_width_2;
		//$arr_product['typesetting_2'] = $typesetting_2;

		$product_per_sheet = max($typesetting_1,$typesetting_2);

		$sheet_used = ceil($_POST['qty']*(1+$paper_wastage) / $product_per_sheet);
		
		$arr_product['sheet_used'] = $sheet_used;
		$arr_product['paper_cost'] = $sheet_used*$paper[0]->material_cost_per_sheet;
		$arr_product['print_cost'] = $sheet_used*$paper[0]->print_cost_per_page*$color[0]->print_page;
		$arr_product['price'] = number_format(round(($arr_product['paper_cost']+$arr_product['print_cost']) / 0.3, 0),2,".",",");
		$arr_result['content'][] = $arr_product;



	}

	$arr_result['condition'] = 'success';

	echo json_encode($arr_result);

}



if($_POST['section']=="calculate_price_by_type"){	
	
	$paper = $wpdb->get_results("SELECT * FROM `custom_material_paper` WHERE id = '".$_POST['paper']."' LIMIT 1");
	$color = $wpdb->get_results("SELECT * FROM `custom_printing_color` WHERE id = '".$_POST['color']."' LIMIT 1");
	$product = $wpdb->get_results("SELECT * FROM `custom_quotation_product` WHERE id = '".$_POST['type']."' LIMIT 1");

	$custom_value = $wpdb->get_results("SELECT * FROM `custom_value` WHERE name = 'product_width_buffer' LIMIT 1");
	$buffer_width = $custom_value[0]->value;

	$custom_value = $wpdb->get_results("SELECT * FROM `custom_value` WHERE name = 'paper_wastage' LIMIT 1");
	$paper_wastage = $custom_value[0]->value;	
		
	$arr_result = array(
		'qty'=>$_POST['qty'], 
		'name'=>$product[0]->name, 
		'paper'=>$paper[0]->name,
		'paper_length'=>$paper[0]->length_cm,
		'paper_width'=>$paper[0]->width_cm,
		'color'=>$color[0]->name, 
		'buffer_width'=>$buffer_width,
		'type'=>$_POST['type'],
		'product_buffered_length_cm'=>$product[0]->folded_length_cm + 2*$buffer_width,
		'product_buffered_width_cm'=>$product[0]->folded_width_cm + 2*$buffer_width,
	);

	// type set 1
	$count_length_1 = floor($arr_result['paper_length']/$arr_result['product_buffered_length_cm']);
	$count_width_1 = floor($arr_result['paper_width']/$arr_result['product_buffered_width_cm']);		
	$typesetting_1 = $count_length_1 * $count_width_1;

	// type set 2
	$count_length_2 = floor($arr_result['paper_width']/$arr_result['product_buffered_length_cm']);
	$count_width_2 = floor($arr_result['paper_length']/$arr_result['product_buffered_width_cm']);
	$typesetting_2 = $count_length_2 * $count_width_2;

	$product_per_sheet = max($typesetting_1,$typesetting_2);
	
	$sheet_used = number_format(($_POST['qty']*(1+$paper_wastage) / $product_per_sheet),2,'.','');	
	//$sheet_used = $_POST['qty']*(1+$paper_wastage) / $product_per_sheet;	
	$sheet_used = ceil($sheet_used);
	
	$arr_result['sheet_used'] = $sheet_used;
	
	$arr_result['paper_cost'] = $sheet_used*$paper[0]->material_cost_per_sheet;
	$arr_result['print_cost'] = $sheet_used*$paper[0]->print_cost_per_page*$color[0]->print_page;

	$arr_result['price'] = number_format(round(($arr_result['paper_cost']+$arr_result['print_cost']) / 0.3, 0),2,".","");
	$arr_result['unit_price'] = number_format($arr_result['price']/$_POST['qty'],2,'.','');	

	$arr_result['display_price'] = number_format(round(($arr_result['paper_cost']+$arr_result['print_cost']) / 0.3, 0),2,".",",");
	$arr_result['display_unit_price'] = number_format($arr_result['price']/$_POST['qty'],2,'.',',');	

	$arr_result['condition'] = 'success';
	
	echo json_encode($arr_result);
}

if($_POST['section']=="calculate_price_by_product_type"){	
	
	$paper = $wpdb->get_results("SELECT * FROM `custom_material_paper` WHERE id = '".$_POST['paper']."' LIMIT 1");
	$color = $wpdb->get_results("SELECT * FROM `custom_printing_color` WHERE id = '".$_POST['color']."' LIMIT 1");
	
	$product = $wpdb->get_results("SELECT * FROM `custom_quotation_product` WHERE id = '".$_POST['type']."' LIMIT 1");

	$custom_value = $wpdb->get_results("SELECT * FROM `custom_value` WHERE name = 'product_width_buffer' LIMIT 1");
	$buffer_width = $custom_value[0]->value;

	$custom_value = $wpdb->get_results("SELECT * FROM `custom_value` WHERE name = 'paper_wastage' LIMIT 1");
	$paper_wastage = $custom_value[0]->value;
		
	$length = $_POST['length'];
	$width = $_POST['width'];
	$height = $_POST['height'];

	if($_POST['type']==1){
		$product_buffered_length_cm = $length + 2*($width) + 2*($buffer_width);
		$product_buffered_width_cm = 2*($width + $height)+ 2*($buffer_width);
	} elseif($_POST['type']==2){
		$product_buffered_length_cm = $length + 2*($buffer_width);
		$product_buffered_width_cm = 2*($width) + $height + 2*($buffer_width);
	} elseif($_POST['type']==3){
		$product_buffered_length_cm = $length + 2*($buffer_width);
		$product_buffered_width_cm = $width + 2*($buffer_width);
	}
		
	$arr_result = array(
		'qty'=>$_POST['qty'], 
		'name'=>$product[0]->name, 
		'paper'=>$paper[0]->name,
		'paper_length'=>$paper[0]->length_cm,
		'paper_width'=>$paper[0]->width_cm,
		'color'=>$color[0]->name, 
		'buffer_width'=>$buffer_width,
		'type'=>$_POST['type'],
		'product_buffered_length_cm'=>$product_buffered_length_cm,
		'product_buffered_width_cm'=>$product_buffered_width_cm,
	);

	// type set 1
	$count_length_1 = floor($arr_result['paper_length']/$arr_result['product_buffered_length_cm']);
	$count_width_1 = floor($arr_result['paper_width']/$arr_result['product_buffered_width_cm']);		
	$typesetting_1 = $count_length_1 * $count_width_1;

	// type set 2
	$count_length_2 = floor($arr_result['paper_width']/$arr_result['product_buffered_length_cm']);
	$count_width_2 = floor($arr_result['paper_length']/$arr_result['product_buffered_width_cm']);
	$typesetting_2 = $count_length_2 * $count_width_2;

	$product_per_sheet = max($typesetting_1,$typesetting_2);

	if(empty($product_per_sheet)){
		$arr_result['condition'] = 'fail';
	} else {	
		$sheet_used = number_format(($_POST['qty']*(1+$paper_wastage) / $product_per_sheet),2,'.','');	
		//$sheet_used = $_POST['qty']*(1+$paper_wastage) / $product_per_sheet;	
		$sheet_used = ceil($sheet_used);
		
		$arr_result['sheet_used'] = $sheet_used;
		
		$arr_result['paper_cost'] = $sheet_used*$paper[0]->material_cost_per_sheet;
		$arr_result['print_cost'] = $sheet_used*$paper[0]->print_cost_per_page*$color[0]->print_page;

		$arr_result['price'] = number_format(round(($arr_result['paper_cost']+$arr_result['print_cost']) / 0.3, 0),2,".","");
		$arr_result['unit_price'] = number_format($arr_result['price']/$_POST['qty'],2,'.','');	

		$arr_result['display_price'] = number_format(round(($arr_result['paper_cost']+$arr_result['print_cost']) / 0.3, 0),2,".",",");
		$arr_result['display_unit_price'] = number_format($arr_result['price']/$_POST['qty'],2,'.',',');	

		$arr_result['condition'] = 'success';
	}
	echo json_encode($arr_result);
}
?>