<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 14-5-21
 * Time: 下午4:50
 * To change this template use File | Settings | File Templates.
 */
class Service_SflogisticsService extends Common_Service
{
    private $param = array();

    private $PostMethod = "POST";

    private $head = 'SZBHDZ';
    private $checkword = 'lp3Kv3QVwnbRWblK';
    private $DeleteMethod = "DELETE";
    private $postHeader = array("Content-type: text/xml");

    private $validateUrl = "http://218.17.248.244:11080/bsp-oisp/sfexpressService";

    //private $orderUrl = "http://218.17.248.244:11080/bsp-oisp/expressService";


    public static function getOrderContent($logistics_code){
        $orderRow = Service_LogisticsOrder::getByField($logistics_code,'logistics_code',"*");
        $orderReceiveAddress = Service_LogisticsOrderAddress::getByCondition(array('logistics_code'=>$orderCode,'loa_type'=>'0'),"*");
        $receiver = $orderReceiveAddress[0];

        $reCountry = Service_Country::getByField($receiver['loa_country'],"country_id","*");
        $logisticsOrderHs = Service_LogisticsOrderHs::getByCondition(array('logistics_code'=>$logistics_code),"*");
        $customer=Service_Customer::getByField($orderRow['customer_code'],'customer_code');
        $customerAddr=Service_CustomerAddressBook::getByField($orderRow['customer_code'],'customer_code');
        $Country = Service_Country::getByField($customerAddr['cab_country_id'],"country_id");
        $customerAddress = $customerAddr['cab_street_address1']." ".$customerAddr['cab_street_address2'];
        //快件类型
        if ($orderRow['sm_code']=="SF-RUPT") {
            $express_type=9;
        }else if($orderRow['sm_code']=="SF-RUPR"){
            $express_type=10;
        }else{
            $express_type=29;
        }
        $city='';
        //城市代码
        switch ($reCountry['country_code']) {
            case 'RU':
                $city='MOW';
                break;
            case 'LT':
                $city='VNO';
                break;
            case 'LV':
                $city='RIX';
                break;
            case 'EE':
                $city='TLL';
                break;
            case 'SE':
                $city='ARN';
                break;
            case 'NO':
                $city='OSL';
                break;
            case 'FI':
                $city='HEL';
                break;
            case 'BY':
                $city='MSQ';
                break;
            case 'UA':
                $city='KBP';
                break;
            case 'PL':
                $city='WAW';
                break;
            default:
                break;
        }
        $cargo='';
        $totalValue=0;
        $withBatty=0;
        foreach($logisticsOrderHs as $opk=>$opv){
            $pu_code=Service_ProductUom::getByField($opv['loh_unit'],'pu_code');
            $cargo.="<Cargo name='".$opv['loh_hs_name']."' count='".$opv['loh_quantity']."' unit='".$pu_code['pu_name']."' weight='' amount='".$opv['loh_total_price']."' currency='USD' source_area='CN'></Cargo>";
            $totalValue+=$opv['loh_total_price'];
        }

        $xmlData = "<Request lang='zh-CN' service='OrderService'>
        <Head>SZBHDZ</Head>
        <Body>
        <Order orderid='".$orderRow['logistics_code']."' express_type='".$express_type."' j_company='".$customerAddr['cab_company']."' j_contact='".$customerAddr['cab_lastname'].$customerAddr['cab_firstname']."' j_tel='".$customerAddr['cab_phone']."' j_mobile='".$customerAddr['cab_cell_phone']."' j_address='".$customerAddress."' d_company='".$receiver['loa_company']."' d_contact='".$receiver['loa_name']."' d_tel='".$receiver['loa_phone']."' d_mobile='".$receiver['loa_phone']."' d_address='".$receiver['loa_address']."' parcel_quantity='1' pay_menthod='1' j_province='".$customerAddr['cab_state']."' j_city='".$customerAddr['cab_city']."' d_province='".$receiver['loa_province']."' d_city='".$receiver['loa_city']."' declared_value='".$totalValue."' declared_value_currency='USD' custid='7552965620' j_country='".$Country['country_code']."' j_county='".$customerAddr['cab_suburb']."' j_shippercode='755' j_post_code='".$customerAddr['cab_postcode']."' d_country='".$reCountry['country_code']."' d_county='' d_deliverycode='".$city."' d_post_code='".$receiver['loa_zipcode']."' cargo_total_weight='".$orderRow['lo_weight']."' remark='' >".
            $cargo.
            "</Order>
        </Body>
        </Request>";  
        //$md5=base64_encode(substr(md5($xmlData."lp3Kv3QVwnbRWblK"),0,16)); // 16位MD5加密       
        //$md5=base64_encode(md5($xmlData."lp3Kv3QVwnbRWblK"));
        //$xml=array('xml'=>$xmlData,'verifyCode'=>$md5);
        //$xml=array('xml'=>$xmlData);
        return $xmlData;
    }

    public function validate($xml){
        $param=array('xml'=>$xml,'verifyCode'=>base64_encode(md5($xml.$this->checkword,true)));
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->validateUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $this->head);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($ch);
        if(curl_errno($ch)){
            print_r(curl_error($ch));
        }
        $obj = simplexml_load_string($response);
        $result = Common_Common::objectToArray($obj);
        if(empty($result)){
            echo $response."</br>";
        }
        curl_close ($ch);
        return $result['Head']=='OK'?$result['Body']['OrderResponse']:$result['ERROR'];
    }

}
