<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-11-26
 * Time: 下午5:32
 * To change this template use File | Settings | File Templates.
 */
require_once 'config.php';
set_time_limit(0);
$flag = APPLICATION_PATH . '/../data/log/getTrackingInfo.lock';

if (@file_exists($flag)) {
    echo "Another process is running.\r\n";
    echo "{$flag}\r\n";
    exit();
}

$handler = fopen($flag, 'wb');
fclose($handler);

echo '[', date('Y-m-d H:i:s'), '] Starting.', "\r\n";
//查找是环球资源创建的客户，跟踪信息取英文
$registerCondition = array(
    'regis_customer_code'=>Service_CustomerProcess::$HuanQiu
);
$hqCustomer = array();
//注册客户不是很多暂时不用分页
$registerCustomer = Service_CustomerRegister::getByCondition($registerCondition,'*');
if(!empty($registerCondition)){
    foreach($registerCustomer as $k=>$v){
        $hqCustomer[] = $v['customer_code'];
    }
}

$smCodeArr = array(
    'HKDHL'=>'DHL',
    'WWDHLP'=>'DHL',
    'WWDHLU'=>'DHL',
    'BH-HKDHL'=>'DHL',
    'HKUPS-KW'=>'UPS',
    'SZY-EMS'=>'EMS',
    'WG-EMS'=>'EMS',
    'SZY-ETK'=>'EMS',
    'BH-DGMG'=>'DHL_GLB',
    'CJ-DGMG'=>'DHL_GLB',
    'BH-SGG'=>'IXJPYZ',
    'SZY-EUB'=>'GJEYB',
    'WG-EUB'=>'GJEYB',
    'WG-EUB-EBAY'=>'GJEYB',
    'PNLG'=>'NL',
    'HKPG'=>'XGYZ',
    'BH-DPD'=>'IOZYZ',
    'AUP'=>'IADLYYZ',
    'BH-USPS'=>'USPS',


);
$EBusinessID = '1255385';
$AppKey = "3921afe4-1123-4bc3-bd7c-5b553bb68eb8";
$validateUrl = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
$timespace = strtotime("-2 months",strtotime(date("Y-m-d")));
foreach($smCodeArr as $key=>$company){
    $condition = array(
        'order_status'=>'11',
        'sm_code'=>$key,
        'add_time_start'=>date("Y-m-d H:i:s",$timespace),
    );
    $total = Service_Orders::getByCondition($condition,'count(*)');
    $pageSize = 20;
    $totalPage = ceil($total/$pageSize);
    for($page=1;$page<=$totalPage;$page++){
        $ordersList = Service_Orders::getByCondition($condition,"*",$pageSize,$page,'order_id asc');
        foreach($ordersList as $orderRow){
            if($orderRow['customer_code']=="E0207"||$orderRow['customer_code']=="E0110") continue;
            $shipOrder = Service_ShipOrder::getByField($orderRow['order_code'],"order_code","*");
            if(empty($shipOrder)) continue;
            if( in_array($orderRow['customer_code'],$hqCustomer) ){
                //环球的客户是dhl的，变成英文的
                if($company=='DHL'){
                    $company = 'DHL_EN';
                }
            }else{
                if($company == 'DHL_EN'){
                    $company = 'DHL';
                }
            }
            $items.="<Item><No>".$shipOrder['tracking_number']."</No><Bk></Bk></Item>";
            $orderCodeArr[] =  $orderRow['order_code'];
            $orderCodeStr.= $orderRow['order_code'].",";
            $requestData = "<Content><Code>".$company."</Code><Items><Item><No>".$shipOrder['tracking_number']."</No><Bk></Bk></Item></Items></Content>";
            $datasign = urlencode(base64_encode(md5($requestData.$AppKey)));
            $params = array(
                'EBusinessID' => $EBusinessID,
                'RequestType' => '1005',
                'RequestData' => $requestData ,
                'DataType' => '1',
                'DataSign'=>$datasign,
            );
            //var_dump($params);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $validateUrl);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $response = curl_exec($ch);
            echo $response;
            $xlm = simplexml_load_string($response);
            $result = Common_Common::objectToArray($xlm);
            if($result['Success']=="true"){
                echo $orderRow['order_code']." $requestData Traces order Success!\n";
            }else{
                echo $orderCodeStr."Traces order failure:".$result['Reason']."\n";
            }
        }
    }
}
echo '[', date('Y-m-d H:i:s'), '] End run', "\r\n";
@unlink($flag);