File: /var/www/html/breadsecret.com_bak20260330/custom/bak/ajax_data.php.bak20211217a
<?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;
/*
By Samiel on 2021-03-03
function to get new order information in every ? seconds
critiria:
(1) order date = today
(2) delivery date = today
(3) message not yet read
*/
if($_POST['section']=="get_today_new_order"){
//get today date (remember to set timezone to HK)
$today = date('Y-m-d');
$arr_result = array();
//order status to check...
$post_status = implode("','", array('wc-pending', 'wc-processing', 'wc-completed'));
//find order matched critria (1)
$result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status IN ('{$post_status}') AND post_date LIKE '".$today."%'");
//loop all found orders
foreach ($result as $order_obj ){
$order = wc_get_order($order_obj->ID);
//get order delivery date from order meta table
$delivery_date = $order->get_meta('delivery_date', true);
// if delivery date is today
if($delivery_date == $today ){
//check if the order is already notified
$master_read_record = $wpdb->get_results("SELECT * FROM `notification_master_read` WHERE order_id = '".$order_obj->ID."' LIMIT 1");
if(count($master_read_record)==0){
//check if the order is notified by specific user
$user_read_record = $wpdb->get_results("SELECT * FROM `notification_user_read` WHERE order_id = '".$order_obj->ID."' AND user_id = '".$_POST['user_id']."' LIMIT 1");
if(count($user_read_record)==0){
$order = wc_get_order( $order_obj->ID );
$arr_ord = array();
$arr_ord['order_id'] = $order_obj->ID;
$arr_ord['order_number'] = $order->get_order_number();
$arr_ord['product'] = array();
//loop all line item in order
foreach ( $order->get_items() as $item ) {
//get line item product info.
$prod = $item->get_product();
$sku = $prod->get_sku();
// get variation name
$variation_id = $item->get_variation_id();
$variation = new WC_Product_Variation($variation_id);
$variationName = implode(" / ", $variation->get_variation_attributes());
$product = wc_get_product(wc_get_product_id_by_sku($sku));
if($variation_id!=0){
$prod_name = $product->get_name()." - ".$variationName;
}else{
$prod_name = $product->get_name();
}
$arr_item = array();
$arr_item['name'] = $prod_name;
$arr_item['qty'] = $item->get_quantity();
$arr_ord['product'][]= $arr_item;
}
array_push($arr_result,$arr_ord);
}
}
}
}
//return result to ajax call in json format
echo json_encode($arr_result);
}
/*
By Samiel on 2021-03-03 // last edit : 2021-05-11
function to let system know an order notification is already read and do not display again
*/
if($_POST['section']=="remove_notification"){
$json = str_replace("\\","",$_POST['order_id']);
foreach(json_decode($json) as $ord_id){
$wpdb->replace('notification_master_read', array(
'order_id' => $ord_id,
'status' => 1
)
);
}
}
/*
By Samiel on 2021-03-03 // last edit : 2021-05-11
function to let system know an order notification is already read by specific user
*/
if($_POST['section']=="read_notification"){
$json = str_replace("\\","",$_POST['order_id']);
foreach(json_decode($json) as $ord_id){
$wpdb->replace('notification_user_read', array(
'order_id' => $ord_id,
'user_id' => $_POST['user_id'],
'status' => 1
)
);
}
}
/*
By Samiel on 2021-03-11
function to adjust sku stock in sku stock page
*/
if($_POST['section']=="adjust_sku_stock"){
$arr_result = array();
if($wpdb->update('sku_stock', array( $_POST['sku'] => $_POST['nqty']),array('stock_date'=>$_POST['stock_date']))){
$arr_result['condition']='success';
$arr_result['callout'] = $_POST['nqty'];
$arr_result['id'] = $_POST['sku']."__".$_POST['stock_date'];
$product = wc_get_product(wc_get_product_id_by_sku($_POST['sku']));
$sql_statement = "UPDATE sku_stock SET `".$_POST['sku']."` = '".$_POST['nqty']."' WHERE stock_date = '".$_POST['stock_date']."' LIMIT 1";
write_log($_POST['stock_date'], "Adjust Stock", "Back End", "0", $_POST['sku'], esc_sql($product->get_name()), $_POST['oqty'], $_POST['nqty'], $_POST['user_id'], get_client_ip(), $sql_statement);
}else{
$arr_result['condition']='fail';
}
echo json_encode($arr_result);
}
/*
By Samiel on 2021-04-07
function to mark an order is completed in order summary page
*/
if($_POST['section']=="mark_completed"){
$arr_result = array();
$arr_result['id'] = $_POST['ord_id'];
if($_POST['ord_id']!=""){
$order = wc_get_order($_POST['ord_id']);
$arr_result['number'] = $order->get_order_number();
$orderDetail = new WC_Order( $_POST['ord_id'] );
$orderDetail->update_status("wc-completed", 'Completed', TRUE);
if($_POST['payment']!=""){
if(metadata_exists('post', $_POST['ord_id'], 'Payment')) {
update_post_meta($_POST['ord_id'], 'Payment', $_POST['payment']);
wc_create_order_note($_POST['ord_id'], "Payment: ".sanitize_text_field( $_POST['payment'] ), false, true);
} else {
add_post_meta($_POST['ord_id'], 'Payment', $_POST['payment'], TRUE);
wc_create_order_note($_POST['ord_id'], "Payment: ".sanitize_text_field( $_POST['payment'] ), false, true);
}
}
$arr_result['condition'] = "success";
}else{
$arr_result['condition'] = "fail";
}
echo json_encode($arr_result);
}
/*
By Samiel on 2021-05-07
function to check payment and load a customer payment method list
*/
if($_POST['section']=="check_payment"){
$arr_result = array();
$arr_result['id'] = $_POST['ord_id'];
if($_POST['ord_id']!=""){
$order = new WC_Order( $_POST['ord_id'] );
$arr_result['payment'] = $order->get_payment_method_title();
$arr_result['number'] = $order->get_order_number();
$button = "<br><p class='woocommerce'>";
$button .= "<button style='font-size:10px; float:right;line-height: 25px !important;height: 25px !important;margin-top: -22px;padding: 0px 10px 0px 10px;' class='button btn_ok' id='btn_ok_".$_POST['ord_id']."'>Confirm<button>";
//$button .= "<button style='float:right;line-height: 30px !important;height: 30px !important;' class='button btn_cancel' id='btn_cancel_".$_POST['ord_id']."'>CANCEL<button>";
$button .= "</p>";
$content = "<br>";
if($arr_result['payment']!="Credit Card (Stripe)" && $arr_result['payment']!="AlipayHK" && $arr_result['payment']!="WeChat Pay"){
$custom_payment_method_list = get_customer_payment_method_list();
if(count($custom_payment_method_list) > 0 ){
$content .= "Paid by: <select name='customer_payment_method' id='select_".$_POST['ord_id']."'>";
foreach($custom_payment_method_list as $custom_payment_method){
$selected_custom_payment_method = get_post_meta($_POST['ord_id'], 'Payment', true);
$content .= "<option value='".$custom_payment_method."' ";
if($custom_payment_method==$selected_custom_payment_method)
$content .= "selected";
$content .= ">".$custom_payment_method."</option>";
}
$content .= "</select>";
}
}
$arr_result['content'] = $content.$button;
$arr_result['condition'] = "success";
}else{
$arr_result['condition'] = "fail";
}
echo json_encode($arr_result);
}
/*
By Samiel on 2021-08-16
function to update stock status label of each product in STORE page
*/
if($_POST['section']=="update_stock_notice"){
$arr_result = array();
$arr_result['condition'] = "success";
$arr_result['content'] = array();
$date = $_POST['date'];
$lang = $_POST['lang'];
$today = date("Y-m-d");
if($today == $date) {
$isToday = true;
} else {
$isToday = false;
}
$data = json_decode(str_replace("\\", "", $_POST['data']));
//$arr_result['data'] = $data;
foreach($data as $item){
$arr_key = explode("_",$item);
$product_id = $arr_key[0];
$sku = $arr_key[2];
$sku_result = array();
$notice = "";
if(in_array($date, $arr_holiday) || date('N',strtotime($date))==7){ // check if today is in holiday list
if ($lang == 'chi') {
$sku_result['notice'] = "<p class='b50442'>是日店休</p>";
}else{
$sku_result['notice'] = "<p class='b50442'>It's holiday today</p>";
}
}else{
$stock_sku = get_master_sku($sku);
$unit_qty = get_master_sku_unit_qty($sku);
$result = $wpdb->get_results("SELECT `".$stock_sku."` FROM sku_stock WHERE stock_date = '".$date."' LIMIT 1");
$init_info = get_sku_init_info($stock_sku);
$end_date = $init_info->end_date;
$array = (array) $result[0];
$limit = floor($array[$stock_sku]/$unit_qty);
if(check_stock_init($stock_sku, $date)==1){ // within selling period
if(count($result)>0){
if(date("Y-m-d") == $date && date("H")>=16){
$sku_result['cartContent'] = "none";
if ($lang == 'chi') {
$sku_result['notice'] = "<p class='status-outofstock'>".($isToday?'今天已':'')."售罄</p>";
}else{
$sku_result['notice'] = "<p class='status-outofstock'>Sold Out ".($isToday?'Today':'')."</p>";
}
}elseif($limit<=0){
$sku_result['cartContent'] = "none";
if($end_date==$date){ // check if last date of selling
if ($lang == 'chi') {
$sku_result['notice'] = "<p class='status-outofstock'>".($isToday?'今天已':'')."售罄</p>";
}else{
$sku_result['notice'] = "<p class='status-outofstock'>Sold Out ".($isToday?'Today':'')."</p>";
}
}else{
if ($lang == 'chi') {
$sku_result['notice'] = "<p class='status-outofstock'>".($isToday?'今天已':'')."售罄</p>";
}else{
$sku_result['notice'] = "<p class='status-outofstock'>Sold Out ".($isToday?'Today':'')."</p>";
}
}
}elseif($limit<=3){
$sku_result['cartContent'] = "block";
if ($lang == 'chi') {
$sku_result['notice'] = "<p class='status-littlestock'>餘".$limit."份</p>";
}else{
$sku_result['notice'] = "<p class='status-littlestock'>".$limit." Left</p>";
}
}else{
$sku_result['cartContent'] = "block";
if ($lang == 'chi') {
$sku_result['notice'] = "<p class='status-instock'>有貨</p>";
}else{
$sku_result['notice'] = "<p class='status-instock'>In Stock</p>";
}
}
}
} elseif(check_stock_init($stock_sku, $date)==0) {
$sku_result['cartContent'] = "none";
if ($lang == 'chi') {
$sku_result['notice'] = "<p class='status-ended'>限定期間已售完</p>";
}else{
$sku_result['notice'] = "<p class='status-ended'>Sales Ended</p>";
}
} elseif(check_stock_init($stock_sku, $date)==2) {
$sku_result['cartContent'] = "none";
if ($lang == 'chi') {
$sku_result['notice'] = "<p class='status-coming'>即將推出</p>";
}else{
$sku_result['notice'] = "<p class='status-coming'>Coming Soon</p>";
}
}
}
$sku_result['id'] = $item;
$sku_result['cartContentID'] = "cart_".$product_id;
array_push($arr_result['content'], $sku_result);
} // end for each item
echo json_encode($arr_result);
}
/*
By Samiel on 2021-08-16
function to set session variable
*/
if($_POST['section']=="set_session_variable"){
if(isset($_POST['field']) && $_POST['field']!=""){
if(isset($_POST['value']) && $_POST['value']!=""){
$_SESSION[$_POST['field']] = $_POST['value'];
}
}
}
/*
By Samiel on 2021-08-24
function for wholeseller page setting
*/
if($_POST['section']=="edit_sku_wholesaler_field"){
$arr_result = array();
if($_POST['period']!="" && $_POST['sku']!="" && $_POST['field_name']!=""){
if($_POST['ovalue']!=$_POST['nvalue']){
$arr_result['id'] = $_POST['field_name']."_".$_POST['period']."_".$_POST['sku'];
switch($_POST['field_name']){
case "factor":
$field = "price_factor";
break;
case "lowerlimit":
$field = "lower_limit";
break;
case "upperlimit":
$field = "upper_limit";
break;
case "visibility":
$field = "visibility";
break;
default:
$field = "";
break;
}
if($wpdb->update('sku_wholesaler', array( $field => $_POST['nvalue']),array('sku_id'=>$_POST['sku'], 'period_id'=>$_POST['period']))){
$arr_result['condition'] = 'success';
$arr_result['callout'] = $_POST['nvalue'];
} else {
if($wpdb->insert('sku_wholesaler', array('period_id' => $_POST['period'], 'sku_id' => $_POST['sku'], $field => $_POST['nvalue']))){
$arr_result['condition'] = 'success';
$arr_result['callout'] = $_POST['nvalue'];
} else {
$arr_result['condition']='fail';
}
}
} else {
$arr_result['condition']='fail';
}
} else {
$arr_result['condition']='fail';
}
echo json_encode($arr_result);
}
if($_POST['section']=="update_sku_wholesaler_period"){
$arr_result = array();
if($_POST['period']!=""){
if($_POST['field']!=""){
if($_POST['value']!=""){
if($wpdb->update('sku_wholesaler_period', array( $_POST['field'] => $_POST['value']),array('id'=>$_POST['period']))){
$arr_result['condition'] = 'success';
} else {
$arr_result['condition'] = 'fail';
}
} else {
$arr_result['condition']='fail';
}
} else {
$arr_result['condition']='fail';
}
} else {
$arr_result['condition']='fail';
}
echo json_encode($arr_result);
}
if($_POST['section']=="remove_sku_wholesaler_period"){
$arr_result = array();
if($_POST['id']!=""){
if($wpdb->update('sku_wholesaler_period', array( 'status' => 0),array('id'=>$_POST['id']))){
$arr_result['condition'] = 'success';
} else {
$arr_result['condition'] = 'fail';
}
} else {
$arr_result['condition']='fail';
}
echo json_encode($arr_result);
}
if($_POST['section']=="add_sku_wholesaler_period"){
$arr_result = array();
if($wpdb->insert('sku_wholesaler_period', array( 'status' => 1))){
$arr_result['condition'] = 'success';
$arr_result['id'] = $wpdb->insert_id;
} else {
$arr_result['condition'] = 'fail';
}
echo json_encode($arr_result);
}
if($_POST['section']=="sku_init"){
$holiday_list = get_holiday_list();
//print_r($_POST);
$arr_result = array();
if($_POST['action']=="Add"){
if(trim($_POST['sku'])=="") {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Missing SKU";
} elseif($_POST['start_date']=="") {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Missing Start Date";
} elseif($_POST['limits']=="") {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Missing Limit";
} elseif(trim($_POST['production_multiple'])=="" || $_POST['production_multiple']==0) {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Missing Production Multiple";
} else {
$product_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", trim($_POST['sku'])) );
if ( $product_check ) {
$result = $wpdb->get_results("SELECT * FROM stock_init WHERE sku = '".trim($_POST['sku'])."' LIMIT 1");
if(count($result)>0){
$arr_result['condition'] = "fail";
$arr_result['message'] = "SKU already exist";
} else {
$block_date_from_holiday = "0";
$arr_block_date_from_holiday = array_map('trim', explode(',', $_POST['block_date_from_holiday']));
if(trim($_POST['block_date_from_holiday'])!=""){
$block_date_from_holiday .= ",".implode(",",$arr_block_date_from_holiday);
}
if($wpdb->insert('stock_init',
array(
'sku' => $_POST['sku'],
'description' => $_POST['description'],
'start_date' => $_POST['start_date'] ,
'end_date' => $_POST['end_date'],
'block_date_from_holiday' => $block_date_from_holiday,
'block_dayofweek' => implode(",",$_POST['block_dayofweek']),
'limits' => $_POST['limits'],
'production_multiple' => $_POST['production_multiple'],
'remark' => $_POST['remark']
))
){
if($wpdb->insert('sku_wholesaler_sku',
array(
'sku' => $_POST['sku'],
'remark' => $_POST['description']
))
){
if($wpdb->query("ALTER TABLE sku_stock ADD `".$_POST['sku']."` FLOAT NOT NULL DEFAULT '0' COMMENT '".$_POST['description']."'")){
$update_sql = "SELECT * FROM sku_stock WHERE stock_date >= '".$_POST['start_date']."' ";
if($_POST['end_date']!=""){
$update_sql .= "AND stock_date <= '".$_POST['end_date']."' ";
}
$results = $wpdb->get_results($update_sql);
foreach($results as $stockObj){
$date = $stockObj->stock_date;
$stock_dayofweek = date('N', strtotime($date));
$is_blocked = false;
$arr_block_dates = array_map("trim", explode(",", $block_date_from_holiday));
// check block_date_from_holiday
foreach($holiday_list as $holiday) {
foreach($arr_block_dates as $buffer){
if($date == date("Y-m-d",strtotime($buffer." days",strtotime($holiday)))){
$is_blocked = true;
}
}
}
if(!$is_blocked){ // check block date
$arr_block_dayofweeks = $_POST['block_dayofweek'];
if(!in_array($stock_dayofweek, $arr_block_dayofweeks)) { // check block dayofweek
$wpdb->query("UPDATE sku_stock SET `".$_POST['sku']."` = ".$_POST['limits']." WHERE id = '".$stockObj->id."'");
}
}
}
$arr_result['condition'] = "success";
$arr_result['message'] = "Initialization Success";
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Initialization Fail in sku_stock Table";
}
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Initialization Fail in Wholesaler Table";
}
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Initialization Fail in stock_init Table";
}
}
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Invalid SKU";
}
}
} elseif($_POST['action']=="Edit"){
if($_POST['id']==""){
$arr_result['condition'] = "fail";
$arr_result['message'] = "Missing ID";
} elseif(trim($_POST['sku'])=="") {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Missing SKU";
} elseif($_POST['start_date']=="") {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Missing Start Date";
} elseif($_POST['limits']=="") {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Missing Limit";
} elseif(trim($_POST['production_multiple'])=="" || $_POST['production_multiple']==0) {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Missing Production Multiple";
} else {
$product_check = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", trim($_POST['sku'])) );
if ( $product_check ) {
$result = $wpdb->get_results("SELECT * FROM stock_init WHERE sku = '".trim($_POST['sku'])."' AND id != '".$_POST['id']."' LIMIT 1");
if(count($result)>0){
$arr_result['condition'] = "fail";
$arr_result['message'] = "SKU already exist";
} else {
$block_date_from_holiday = "0";
$arr_block_date_from_holiday = array_map('trim', explode(',', $_POST['block_date_from_holiday']));
if(trim($_POST['block_date_from_holiday'])!=""){
$block_date_from_holiday .= ",".implode(",",$arr_block_date_from_holiday);
}
if(is_bool($wpdb->update('stock_init',
array(
'sku' => $_POST['sku'],
'description' => $_POST['description'],
'start_date' => $_POST['start_date'] ,
'end_date' => $_POST['end_date'],
'block_date_from_holiday' => $block_date_from_holiday,
'block_dayofweek' => implode(",",$_POST['block_dayofweek']),
'limits' => $_POST['limits'],
'production_multiple' => $_POST['production_multiple'],
'remark' => $_POST['remark']
),
array('id'=>$_POST['id']))!=1)
){
if(is_bool($wpdb->update('sku_wholesaler_sku',
array(
'sku' => $_POST['sku'],
'remark' => $_POST['description']
),array('sku' => $_POST['o_sku']))!=1)
){
if(is_bool($wpdb->query("ALTER TABLE sku_stock CHANGE `".$_POST['o_sku']."` `".$_POST['sku']."` FLOAT NOT NULL DEFAULT '0' COMMENT '".$_POST['description']."'"))){
if($_POST['o_start_date'] > $_POST['start_date']){
for($i = strtotime($_POST['start_date']); $i < strtotime($_POST['o_start_date']); $i+=86400){
$date = date('Y-m-d', $i);
$stock_dayofweek = date('N', strtotime($date));
$is_blocked = false;
$arr_block_dates = array_map("trim", explode(",", $block_date_from_holiday));
// check block_date_from_holiday
foreach($holiday_list as $holiday) {
foreach($arr_block_dates as $buffer){
if($date == date("Y-m-d",strtotime($buffer." days",strtotime($holiday)))){
$is_blocked = true;
}
}
}
if(!$is_blocked){ // check block date
$arr_block_dayofweeks = $_POST['block_dayofweek'];
if(!in_array($stock_dayofweek, $arr_block_dayofweeks)) { // check block dayofweek
$wpdb->query("UPDATE sku_stock SET `".$_POST['sku']."` = ".$_POST['limits']." WHERE stock_date = '".date('Y-m-d', $i)."'");
}
}
}
}
if($_POST['o_start_date'] < $_POST['start_date']){
for($j = strtotime($_POST['o_start_date']); $j < strtotime($_POST['start_date']); $j+=86400){
$wpdb->query("UPDATE sku_stock SET `".$_POST['sku']."` = 0 WHERE stock_date = '".date('Y-m-d', $j)."'");
}
}
if($_POST['o_end_date']!=""){
$o_end_date = $_POST['o_end_date'];
} else {
$o_end_date = date('Y-m-d', strtotime("+15 day"));
}
if($_POST['end_date']!=""){
$end_date = $_POST['end_date'];
} else {
$end_date = date('Y-m-d', strtotime("+15 day"));
}
if($o_end_date > $end_date){
for($k = strtotime($o_end_date); $k > strtotime($end_date); $k-=86400){
$wpdb->query("UPDATE sku_stock SET `".$_POST['sku']."` = 0 WHERE stock_date = '".date('Y-m-d', $k)."'");
}
}
if($o_end_date < $end_date){
for($l = strtotime($end_date); $l > strtotime($o_end_date); $l-=86400){
$date = date('Y-m-d', $l);
$stock_dayofweek = date('N', strtotime($date));
$is_blocked = false;
$arr_block_dates = array_map("trim", explode(",", $block_date_from_holiday));
// check block_date_from_holiday
foreach($holiday_list as $holiday) {
foreach($arr_block_dates as $buffer){
if($date == date("Y-m-d",strtotime($buffer." days",strtotime($holiday)))){
$is_blocked = true;
}
}
}
if(!$is_blocked){ // check block date
$arr_block_dayofweeks = $_POST['block_dayofweek'];
if(!in_array($stock_dayofweek, $arr_block_dayofweeks)) { // check block dayofweek
$wpdb->query("UPDATE sku_stock SET `".$_POST['sku']."` = ".$_POST['limits']." WHERE stock_date = '".date('Y-m-d', $l)."'");
}
}
}
}
$arr_result['condition'] = "success";
$arr_result['message'] = "Update Success";
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Update fail in sku_stock table";
}
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Update fail in wholesaler table";
}
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Update Fail";
}
}
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Invalid SKU";
}
}
} elseif($_POST['action']=="Remove"){
if($_POST['id']==""){
$arr_result['condition'] = "fail";
$arr_result['message'] = "Missing ID";
} elseif($_POST['sku']=="") {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Missing SKU";
} else {
$wpdb->delete("stock_init", array( 'id' => $_POST['id'] ));
$wpdb->delete("sku_wholesaler_sku", array( 'sku' => $_POST['sku'] ));
$wpdb->query("ALTER TABLE `sku_stock` DROP COLUMN `".$_POST['sku']."`");
$arr_result['condition'] = "success";
$arr_result['message'] = $_POST['sku']." is removed from Sku Stock Initialization table";
}
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Unknown Action";
}
echo json_encode($arr_result);
}
if($_POST['section']=="add_reservation"){
$number = strtoupper(substr(md5(rand()), 0, 6));
if($_POST['hash']=="" || !isset($_POST['hash'])){
$arr_result['condition'] = "fail";
$arr_result['message'] = "Invalid hash value";
} else {
if(get_user_id_from_hash($_POST['hash'])){
$user_id = get_user_id_from_hash($_POST['hash']);
$userObj = get_userdata($user_id);
if($wpdb->insert('reservation_record',
array(
'reservation_number' => $number,
'delivery_date' => $_POST['delivery_date'],
'user_id' => $user_id,
'user_name' => $userObj->display_name,
'user_ip' => getClientIpAddr()
))
){
$reservation_id = $wpdb->insert_id;
$ttl_amount = 0;
foreach($_POST['qty'] as $idx=>$value){
if($value!="" && $value!=0){
$product = wc_get_product($_POST['productID'][$idx]);
$trid = $sitepress->get_element_trid($product->get_id(), 'post_product');
$translations = $sitepress->get_element_translations($trid, 'post_product');
foreach( $translations as $lang=>$translation){
if($translation->language_code == "zh-hant")
$zh_post_id = $translation->element_id;
}
$item_amount = $_POST['priceValue'][$idx]*$value;
$trid = $sitepress->get_element_trid($_POST['attributeID'][$idx], 'tax_pa_cut');
$variation_name = "";
$variation_name_zh = "";
$translations = $sitepress->get_element_translations($trid, 'tax_pa_cut');
foreach( $translations as $lang=>$translation){
if($translation->language_code == "zh-hant"){
$variation_name_zh = $translation->name;
}
if($translation->language_code == "en"){
$variation_name = $translation->name;
}
}
if($wpdb->insert('reservation_detail',
array(
'reservation_id' => $reservation_id,
'sku' => $product->get_sku(),
'product_id' => $_POST['productID'][$idx],
'product_name' => $product->get_name(),
'product_name_zh' => get_the_title($zh_post_id),
'variation_id' => $_POST['variationID'][$idx],
'variation_name' => $variation_name,
'variation_name_zh' => $variation_name_zh,
'qty' => $value,
'unit_price' => $_POST['priceValue'][$idx],
'item_amount' => $item_amount
)
)){
$ttl_amount += $item_amount;
}
}
}
if($wpdb->update('reservation_record', array('total_amount' => $ttl_amount),array('reservation_number' => $number))){
$arr_result['condition'] = "success";
$arr_result['message'] = "Your reservation is received.<br>Reservation No.: ".$number."<br>Submission Time: ".date("Y-m-d H:i:s");
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Database error";
}
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Database error";
}
} else {
$arr_result['condition'] = "fail";
$arr_result['message'] = "Invalid hash value";
}
}
echo json_encode($arr_result);
/*
$arr_result = array();
if($_POST['id']!=""){
if($wpdb->update('sku_wholesaler_period', array( 'status' => 0),array('id'=>$_POST['id']))){
$arr_result['condition'] = 'success';
} else {
$arr_result['condition'] = 'fail';
}
} else {
$arr_result['condition']='fail';
}
echo json_encode($arr_result);
*/
}
?>