File: /var/www/html/www.winghung.com/demo_v7/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_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_product` WHERE id = '".$_POST['id']."' 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(in_array($_POST['id'],[1,2])){
$product_buffered_length_cm = $length + 2*($width) + 2*($buffer_width);
$product_buffered_width_cm = 2*($width + $height)+ 2*($buffer_width);
} elseif(in_array($_POST['id'],[5,6,7])){
$product_buffered_length_cm = $length + 2*($buffer_width);
$product_buffered_width_cm = 2*($width) + $height + 2*($buffer_width);
} elseif(in_array($_POST['id'],[9])){
$product_buffered_length_cm = $length + 2*($buffer_width);
$product_buffered_width_cm = $width + 2*($buffer_width);
} elseif(in_array($_POST['id'],[10])){
$product_buffered_length_cm = 2*($length) + 2*($buffer_width);
$product_buffered_width_cm = $width + 2*($buffer_width);
} elseif(in_array($_POST['id'],[13,14])){ // canvas
$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,
'id'=>$_POST['id'],
'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);
}
?>