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

if($_POST['section']=="get_custom_currency_list"){
	$arr_result = array();
	$arr_result['data'] = array();

	$currency_list = $wpdb->get_results("SELECT * FROM `custom_currency` WHERE status = 1 ORDER BY id");

	$i = 1;
	foreach($currency_list as $currencyObj){		
		array_push($arr_result['data'], 
			array(
				$i,
				"<span style='display:none'>".$currencyObj->currency_code."</span><input type='text' id='".$currencyObj->id."-currency_code' name='".$currencyObj->id."-currency_code' value='".$currencyObj->currency_code."' callout='".$currencyObj->currency_code."' class='text currency_edit'>",
				"<span style='display:none'>".$currencyObj->currency_rate."</span><input type='number' id='".$currencyObj->id."-currency_rate' name='".$currencyObj->id."-currency_rate' value='".$currencyObj->currency_rate."' callout='".$currencyObj->currency_rate."' class='text currency_edit' min='0'>",
				"<i class='text-danger fas fa-3x fa-minus-square remove_currency_btn' style='cursor:pointer' id='remove_currency-".$currencyObj->id."'></i>")
		);
		$i++;
	}
	
	$arr_result['recordsTotal'] = count($arr_result['data']);
	$arr_result['recordsFiltered'] = count($arr_result['data']);
	
	echo json_encode($arr_result);
}

if($_POST['section']=="get_custom_supplier_list"){
	$arr_result = array();
	$arr_result['data'] = array();

	$supplier_list = $wpdb->get_results("SELECT * FROM `custom_supplier` WHERE status = 1 ORDER BY id");
	$i = 1;
	foreach($supplier_list as $supplierObj){		
		array_push($arr_result['data'], 
			array(
				$i,
				"<span style='display:none'>".$supplierObj->supplier_name."</span><input type='text' id='".$supplierObj->id."-supplier_name' name='".$supplierObj->id."-supplier_name' value='".$supplierObj->supplier_name."' callout='".$supplierObj->supplier_name."' class='text supplier_edit'>",				
				"<i class='text-danger fas fa-3x fa-minus-square remove_supplier_btn' style='cursor:pointer' id='remove_supplier-".$supplierObj->id."'></i>")
		);
		$i++;
	}
	
	$arr_result['recordsTotal'] = count($arr_result['data']);
	$arr_result['recordsFiltered'] = count($arr_result['data']);
	
	echo json_encode($arr_result);
}

if($_POST['section']=="get_paper_cost_list"){
	$arr_result = array();
	$arr_result['data'] = array();

	$paper_cost_list = $wpdb->get_results("SELECT * FROM `custom_lookup_paper_cost` ORDER BY id");
	$i = 1;
	foreach($paper_cost_list as $costObj){		
		array_push($arr_result['data'], 
			array(
				$i,
				"<span style='display:none'>".$costObj->attribute_name."</span><input type='text' id='".$costObj->id."-paper_cost-attribute_name' name='".$costObj->id."-paper_cost-attribute_name' value='".$costObj->attribute_name."' callout='".$costObj->attribute_name."' class='text paper_cost_edit'>",
				"<input type='text' id='".$costObj->id."-paper_cost-base_cost' name='".$costObj->id."-paper_cost-base_cost' value='".$costObj->base_cost."' callout='".$costObj->base_cost."' class='text paper_cost_edit'>",
				"<input type='text' id='".$costObj->id."-paper_cost-additional_cost_per_sheet' name='".$costObj->id."-paper_cost-additional_cost_per_sheet' value='".$costObj->additional_cost_per_sheet."' callout='".$costObj->additional_cost_per_sheet."' class='text paper_cost_edit'>",	
				"<i class='text-danger fas fa-2x fa-minus-square remove_paper_cost_btn' style='cursor:pointer' id='remove_paper_cost-".$costObj->id."'></i>")
		);
		$i++;
	}
	
	$arr_result['recordsTotal'] = count($arr_result['data']);
	$arr_result['recordsFiltered'] = count($arr_result['data']);
	
	echo json_encode($arr_result);
}


if($_POST['section']=="get_extra_cost_list"){
	$arr_result = array();
	$arr_result['data'] = array();

	$extra_cost_list = $wpdb->get_results("SELECT * FROM `custom_lookup_extra_cost` ORDER BY id");
	$i = 1;
	foreach($extra_cost_list as $costObj){		
		array_push($arr_result['data'], 
			array(
				$i,
				"<span style='display:none'>".$costObj->attribute_name."</span><input type='text' id='".$costObj->id."-extra_cost-attribute_name' name='".$costObj->id."-extra_cost-attribute_name' value='".$costObj->attribute_name."' callout='".$costObj->attribute_name."' class='text extra_cost_edit'>",
				"<input type='text' id='".$costObj->id."-extra_cost-option_value' name='".$costObj->id."-extra_cost-option_value' value='".$costObj->option_value."' callout='".$costObj->option_value."' class='text extra_cost_edit'>",
				"<input type='text' id='".$costObj->id."-extra_cost-cost' name='".$costObj->id."-extra_cost-cost' value='".$costObj->cost."' callout='".$costObj->cost."' class='text extra_cost_edit'>",					
				"<i class='text-danger fas fa-2x fa-minus-square remove_extra_cost_btn' style='cursor:pointer' id='remove_extra_cost-".$costObj->id."'></i>")
		);
		$i++;
	}
	
	$arr_result['recordsTotal'] = count($arr_result['data']);
	$arr_result['recordsFiltered'] = count($arr_result['data']);
	
	echo json_encode($arr_result);
}

/*
By Samiel on 2021-08-02
function to adjust order meta (poduct cost/shipping cost/awb no)
*/
if($_POST['section']=="edit_postmeta"){
	$arr_result = array();
	
	if($_POST['post_id']!="" && $_POST['field_name']!="") {
	 	update_post_meta( $_POST['post_id'], $_POST['field_name'], sanitize_text_field( $_POST['nvalue']) );
		$arr_result['id'] = $_POST['post_id']."-".$_POST['field_name'];
		$arr_result['callout'] = $_POST['nvalue'];	
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}


/*
By Samiel on 2021-08-23
function to update shipping cost 
*/

if($_POST['section']=="edit_shippingcost"){

	$arr_result = array();
	$arr_result['id'] = $_POST['key'];
	if(isset($_POST['key']) && $_POST['key']!=""){
		
		$key = explode("--",$_POST['key']);
		$field = $key[0];
		$order_id = $key[1];
		$track_no = $key[2];
		
		$hash = get_post_meta($order_id, '_wc_shipment_tracking_items');
		
		$ship_data = $hash[0];
						
		$updated_ship_data = array();
		foreach ($ship_data as $line){
			
			if($line['tracking_number']==$track_no){			
				$line['shipping_cost'] = $_POST['nvalue'];				
			}
			
			array_push($updated_ship_data, $line);
			
		}
		
		update_post_meta($order_id, '_wc_shipment_tracking_items', $updated_ship_data);
		$arr_result['callout'] = $_POST['nvalue'];	
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	

	echo json_encode($arr_result);
}

if($_POST['section']=="edit_supplier"){
	$arr_result = array();	
	
	if($_POST['supplier_id']!="" && $_POST['field_name']!="") {
		$wpdb->update('custom_supplier', array( $_POST['field_name'] => $_POST['nvalue']),array('id'=>$_POST['supplier_id']));
		$arr_result['id'] = $_POST['supplier_id']."-".$_POST['field_name'];
		$arr_result['callout'] = $_POST['nvalue'];	
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="edit_currency"){
	$arr_result = array();
	
	if($_POST['currency_id']!="" && $_POST['field_name']!="") {
		$wpdb->update('custom_currency', array( $_POST['field_name'] => $_POST['nvalue']),array('id'=>$_POST['currency_id']));
		$arr_result['id'] = $_POST['currency_id']."-".$_POST['field_name'];
		$arr_result['callout'] = $_POST['nvalue'];	
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="add_supplier"){
	$arr_result = array();
	
	if($_POST['value']!="") {
		$wpdb->insert('custom_supplier', array(
			'supplier_name' => $_POST['value']
		));
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="add_currency"){
	$arr_result = array();
	
	if($_POST['code']!="" && $_POST['value']!="") {
		$wpdb->insert('custom_currency', array(
			'currency_code' => $_POST['code'],
			'currency_rate' => $_POST['value']
		));
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="remove_supplier"){
	$arr_result = array();
	
	if($_POST['supplier_id']!="") {
		$wpdb->delete("custom_supplier", array( 'id' => $_POST['supplier_id'] ) );
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="remove_currency"){
	$arr_result = array();
	
	if($_POST['currency_id']!="") {
		$wpdb->delete("custom_currency", array( 'id' => $_POST['currency_id'] ) );
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}


if($_POST['section']=="add_paper_cost"){
	$arr_result = array();
	
	if($_POST['attribute_name']!="") {
		$wpdb->insert('custom_lookup_paper_cost', array(
			'attribute_name' => $_POST['attribute_name'],
			'base_cost' => $_POST['base_cost'],
			'additional_cost_per_sheet' => $_POST['additional_cost_per_sheet']
		));
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="add_extra_cost"){
	$arr_result = array();
	
	if($_POST['attribute_name']!="") {
		$wpdb->insert('custom_lookup_extra_cost', array(
			'attribute_name' => $_POST['attribute_name'],
			'option_value' => $_POST['option_value'],
			'cost' => $_POST['cost']
		));
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="remove_paper_cost"){
	$arr_result = array();
	
	if($_POST['id']!="") {
		$wpdb->delete("custom_lookup_paper_cost", array( 'id' => $_POST['id'] ) );
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="remove_extra_cost"){
	$arr_result = array();
	
	if($_POST['id']!="") {
		$wpdb->delete("custom_lookup_extra_cost", array( 'id' => $_POST['id'] ) );
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="edit_paper_cost"){
	$arr_result = array();	
	
	if($_POST['id']!="" && $_POST['field_name']!="") {
		$wpdb->update('custom_lookup_paper_cost', array( $_POST['field_name'] => $_POST['nvalue']),array('id'=>$_POST['id']));
		$arr_result['id'] = $_POST['id']."-paper_cost-".$_POST['field_name'];
		$arr_result['callout'] = $_POST['nvalue'];	
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="edit_extra_cost"){
	$arr_result = array();	
	
	if($_POST['id']!="" && $_POST['field_name']!="") {
		$wpdb->update('custom_lookup_extra_cost', array( $_POST['field_name'] => $_POST['nvalue']),array('id'=>$_POST['id']));
		$arr_result['id'] = $_POST['id']."-extra_cost-".$_POST['field_name'];
		$arr_result['callout'] = $_POST['nvalue'];	
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="get_vendor_detail"){
	$arr_result = array();	
	
	if(isset($_POST['vendor_name']) && $_POST['vendor_name']!="") {
		
		$vendor_info = $wpdb->get_results("SELECT * FROM `custom_vendor` WHERE name = '".$_POST['vendor_name']."' Limit 1");
		
		if(count($vendor_info)>0){
			$arr_result['condition']='success';
			$arr_result['content']=$vendor_info[0]->contact_detail;	
		} else {
			$arr_result['condition']='fail';
		}
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="get_order_item_data"){
	$arr_result = array();	
	
	$select_box = "
	<select class='form-control item_type' name='item_type[]'>
      <option>Product Cost</option>
      <option>Shipping Cost</option>
      <option>Other Cost</option>
    </select>";
	
	$check_box = "<div class='form-check'><input class='form-check-input checkbox-1x item_checkbox'type='checkbox' name='item_checkbox[]' checked><input type='hidden' name='checkbox_value[]' value='1'></div>";	
	
	if(isset($_POST['order_id']) && $_POST['order_id']!="") {
		$seq = $_POST['sequence'];
		$order = wc_get_order($_POST['order_id']);

		if($order===false){
			$arr_result['condition']='fail';
			$arr_result['content']='invalid order#';	
		} else {
			$arr_result['condition']='success';
			$arr_result['content']="";
			
			foreach( $order->get_items() as $item ){
				$item_name = $item->get_name();	
				$item_id = $item->get_ID();
				$qty = $item->get_quantity();
				$unit_price = get_product_cost($item_id);
				$total = $qty*$unit_price;
				
				$arr_result['content'].="<tr>";
					//$arr_result['content'].="<td align='center'>".$check_box."</td>";					
					$arr_result['content'].="<td><input type='number' class='form-control item_sequence' name='item_sequence[]' value='".$seq."' step='1'></td>";
					$arr_result['content'].="<td>".$select_box."</td>";
					//$arr_result['content'].="<td><input type='text' class='form-control item_name' name='item_name[]' value='".$item_name."'></td>";	
					$arr_result['content'].="<td><textarea class='form-control item_name' name='item_name[]' rows='1'>".get_formatted_item_content($_POST['order_id'], $item_id)."</textarea></td>";
					$arr_result['content'].="<td><input type='number' class='form-control item_qty' name='item_qty[]' value='".$qty."' step='1' readonly></td>";
					$arr_result['content'].="<td><input type='number' class='form-control item_unit_price' name='item_unit_price[]' value='".$unit_price."' step='Any'></td>";
					$arr_result['content'].="<td><input type='number' class='form-control item_total' name='item_total[]' value='".$total."' step='Any' readonly></td>";
					$arr_result['content'].="<td><i class='fas fa-2x fa-times-square text-danger remove_line_btn' aria-hidden='true' title='Remove Line'></i></td>";					
				$arr_result['content'].="</tr>";
				$seq++;
			}			
		}		
	} else {
		$arr_result['condition']='fail';
		$arr_result['content']='invalid order#';
	}	
	echo json_encode($arr_result);
}


if($_POST['section']=="generate_po_no"){
	$arr_result = array();	
		
	if(isset($_POST['order_id']) && $_POST['order_id']!="") {
		$order = wc_get_order($_POST['order_id']);
		if($order!==false){
			$new_no = "PO#".$_POST['order_id'];
			/*
			$key = "PO#".$_POST['order_id']."-";
			
			$sql = "SELECT * FROM custom_po_master WHERE po_number LIKE '".$key."%' order by po_number DESC LIMIT 1";		
			$result = $wpdb->get_results($sql);
			
			if(count($result)>0){
				foreach($result as $record) {	
					$max_po = $record->po_number;
					$arr_max_no = explode("-",$max_po);
					$new_no = $arr_max_no[0]."-".($arr_max_no[1]+1);
				}
			} else {	
				$new_no = $key."1";	
			}			
			$new_po = $key;
			*/
			$arr_result['condition']='success';
			$arr_result['number']=$new_no;
			
			$fullname = $order->get_formatted_shipping_full_name()==""?"":"".$order->get_formatted_shipping_full_name();
			$company = $order->get_shipping_company()==""?"":"\r\n".$order->get_shipping_company();
			$address1 = $order->get_shipping_address_1()==""?"":"\r\n".$order->get_shipping_address_1();
			$address2 = $order->get_shipping_address_2()==""?"":"\r\n".$order->get_shipping_address_2();
			$city = $order->get_shipping_city()==""?"":"\r\n".$order->get_shipping_city();
			$state = $order->get_shipping_state()==""?"":"\r\n".$order->get_shipping_state();
			$country = $order->get_shipping_country()==""?"":"\r\n".$order->get_shipping_country();
			$arr_result['ship_address']=$fullname.$company.$address1.$address2.$city.$state.$country;
		}
		
	} else {
		$arr_result['condition']='fail';
		$arr_result['message']='fail to generate po#';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="cancel_po"){
	$arr_result = array();	
	
	if(isset($_POST['id']) && $_POST['id']!="") {
		$wpdb->update('custom_po_master', array( "status" => 0),array('id'=>$_POST['id']));		
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}
?>