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/upload_awb_excel.php.bak20220420
<?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 {
									
									if(substr($trackNo, 0, 2)=="SF"){									
										$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()),
										);
									} else {
										$arr_tackNo = explode("-",$trackNo);										
										$array_ship_line = array(									
											"tracking_provider" => $arr_tackNo[0],
											"custom_tracking_provider" => "",
											"custom_tracking_link" => "",
											"tracking_number" => $arr_tackNo[1],
											"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);
?>