<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 17-7-3
 * Time: 下午4:49
 * To change this template use File | Settings | File Templates.
 */
class Service_BHAZATGApiProcess
{
    private $hosts = 'http://api.faryaa.com/order/create';
    private $accountNo = 'A225';
    private $token = 'ab7f4f90befd435e90e0214e81568495';
    private $pieces = "";
    private $order_code = "";

    public function __construct($order_code)
    {
        $this->order_code = $order_code;
        //$this->so_code = $so_code;
        //$this->pieces = $pieces;
    }

    public function submitData() {
        $requestArr = array(
            'user_code'=>$this->accountNo,
            'token'=>$this->token,
        );
        $orderRecAddress = Service_OrderAddressBook::getByWhere(array('order_code'=>$this->order_code,'oab_type'=>0));
        $country = Service_Country::getByField($orderRecAddress['oab_country_id'],"country_id","*");
        $orderProduct = Service_OrderProduct::getByCondition(array('order_code'=>$this->order_code),"*");
        $total_declare_value = 0;
        $items = array();
        $totalWeight = 0;
        foreach($orderProduct as $op){
            $product = Service_Product::getByField($op['product_id'],'product_id',"*");
            $it = array(
                'sku'=>$product['product_sku'],
                'quantity'=>$op['op_quantity'],
                'declare_name'=>$op['product_title_en'],
                'declare_value'=>$op['op_purpose_declared_value'],
            );
            $total_declare_value+=$op['op_purpose_declared_value']*$op['op_quantity'];
            $totalWeight+=$product['product_weight']*$op['op_quantity'];
            $items[] = $it;
        }
        //$pie = $this->pieces;
        $orderData = array(
            'reference_order_number'=>$this->order_code,
            'ship_channel_code'=>'AZAT',
            //'tracking_number'=>'SYBCN00568956',
            'total_declare_value'=>$total_declare_value,
            'total_weight'=>$totalWeight,
            'length'=>10,
            'width'=>10,
            'height'=>10,
            'recipient_name'=>$orderRecAddress['oab_firstname']." ".$orderRecAddress['oab_lastname'],
            'recipient_company'=>$orderRecAddress['oab_company'],
            'recipient_address1'=>$orderRecAddress['oab_street_address1'],
            'recipient_address2'=>$orderRecAddress['oab_street_address2'],
            'recipient_country'=>$country['country_code'],
            'recipient_state'=>$orderRecAddress['oab_state'],
            'recipient_city'=>$orderRecAddress['oab_city'],
            'recipient_zip'=>$orderRecAddress['oab_postcode'],
            'recipient_phone'=>$orderRecAddress['oab_phone'],
            'recipient_email'=>$orderRecAddress['oab_email'],
            'items'=>$items,
        );
        $requestArr['orders'][] = $orderData;
        $jsonStr = json_encode($requestArr);
        $url = $this->hosts;
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, 'request_data='.$jsonStr); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $response = curl_exec($curl);
        //echo $response;
        //$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        //$result = array($httpCode, $response);
        //return $result;
        //var_dump($result);
        //var_dump(json_decode($response));
        $result = Common_Common::objectToArray(json_decode($response));
        //var_dump($result);
        return $result['data'];
    }

    public static function getOrderInfo($orderCode){
        $url = "http://api.faryaa.com/order/getOrderInfo";
        $orderRow = Service_Orders::getByField($orderCode,"order_code","*");
        $requestArr = array(
            'user_code'=>'A225',
            'token'=>'ab7f4f90befd435e90e0214e81568495',
            'order_number'=>$orderRow['agent_mailno'],
            'reference_order_number'=>$orderRow['order_code'],
        );
        $jsonStr = json_encode($requestArr);
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, 'request_data='.$jsonStr); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $response = curl_exec($curl);
        echo $response;
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $result = array($httpCode, $response);
        //var_dump($result);
        //var_dump(objectToArray(json_decode($response)));
        $result = Common_Common::objectToArray(json_decode($response));
        return $result;
        //exit;
    }
}
