File: /var/www/html/amberconcept/upload_awb_excel.php.bak20220414
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//include wp libriries and set timezone to HK
require_once('wp-load.php');
require_once('PHPExcel.php');
date_default_timezone_set("Asia/Hong_Kong");
global $wpdb;
$path = "/var/www/html/ossfs/amber/awb_excel/";
if ($_FILES["excelFile"]["name"] <> ""){
$filename=time()."_".$_FILES["excelFile"]["name"];
$filepath = $path.$filename;
if(move_uploaded_file($_FILES["excelFile"]["tmp_name"], $path.$filename)){
$arr_result['condition'] = 'success';
$arr_result['message'] = 'upload success';
if(file_exists($filepath)){
$arr_data = array(array());
$xlsObj = PHPExcel_IOFactory::load($filepath);
$wsObj = $xlsObj->getActiveSheet();
$maxColIdx = $wsObj->getHighestColumn();
$maxColNo = PHPExcel_Cell::columnIndexFromString($maxColIdx);
$maxCol = $maxColNo-1;
foreach($wsObj->getRowIterator() as $row) {
$xlsRow = $row->getRowIndex();
if(trim($wsObj->getCellByColumnAndRow(0, $xlsRow)->getFormattedValue())!=""){
for($col=0;$col<=$maxCol;$col++){
$arr_data[$xlsRow][] = trim($wsObj->getCellByColumnAndRow($col, $xlsRow)->getFormattedValue());
}
}
}
array_shift($arr_data); // remove first empty row
if(count($arr_data)>0){
$trackNoIdx = array_search("快递货运编号", $arr_data[0]);
$orderNoIdx = array_search("相册内容", $arr_data[0]);
$delDateIdx = array_search("安排时间", $arr_data[0]);
if($trackNoIdx!=false){
if($orderNoIdx!==false){
if($delDateIdx!==false){
for($dataRow=1; $dataRow<count($arr_data); $dataRow++){
$trackNo = $arr_data[$dataRow][$trackNoIdx];
$orderNo = $arr_data[$dataRow][$orderNoIdx];
$delDate = $arr_data[$dataRow][$delDateIdx];
$delDate = date("Y-m-d H:i:s", strtotime($delDate));
$orderID = trim(preg_replace('/[^0-9]/', '', $orderNo));
$order = wc_get_order($orderID);
$exist = false;
$shipping_hash = get_post_meta($orderID, '_wc_shipment_tracking_items', true ); // get plugin value;
if($shipping_hash!=""){
foreach($shipping_hash as $shipping_idx=>$shipping_arr){ // check trackNo is existing in plugin value
if(trim($shipping_arr['tracking_number'])==$trackNo){
$exits = true;
}
}
}else{
$shipping_hash = array();
}
if($exist){ // if exist, do nothing
;
} else {
$array_ship_line = array(
"tracking_provider" => "sf-express",
"custom_tracking_provider" => "",
"custom_tracking_link" => "",
"tracking_number" => $trackNo,
"tracking_product_code" => "",
"date_shipped" => strtotime($delDate),
"products_list" => "",
"status_shipped" => "1",
"tracking_id" => md5(time()),
);
array_push($shipping_hash, $array_ship_line);
$shipping_hash = array_map("unserialize", array_unique(array_map("serialize", $shipping_hash)));
update_post_meta($orderID, '_wc_shipment_tracking_items', $shipping_hash);
}
}
if(in_array($order->get_status(), array("processing","on-hold","pending"))){
$order->update_status('completed');
}
$arr_result['condition'] = 'success';
$arr_result['message'] = 'import success';
}else{
$arr_result['condition'] = 'fail';
$arr_result['message'] = 'missing 相册内容 column';
}
} else {
$arr_result['condition'] = 'fail';
$arr_result['message'] = 'missing 安排时间 column';
}
} else {
$arr_result['condition'] = 'fail';
$arr_result['message'] = 'missing 快递货运编号 column';
}
} else {
$arr_result['condition'] = 'fail';
$arr_result['message'] = 'file content is empty';
}
} else {
$arr_result['condition'] = 'fail';
$arr_result['message'] = 'uploaded file not found';
}
} else {
$arr_result['condition'] = 'fail';
$arr_result['message'] = 'unable to upload file';
}
}
echo json_encode($arr_result);
?>