<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 14-8-20
 * Time: 下午5:49
 * To change this template use File | Settings | File Templates.
 */
class Service_UktlWebService
{
    private $_wsdlurl = "http://oneworldexpress.co.uk/remote/main/smartsystemintegration.php?wsdl";
    private $_AccountCode = "CSCM";
    private $_UserName = "CSCM";
    private $_Password = "CSCM.com";
    public function createUktlOrder($orderCode){
        $orderRow = Service_Orders::getByField($orderCode,'order_code',"*");
        $orderReceiveAddress = Service_OrderAddressBook::getByCondition(array('order_code'=>$orderCode,'oab_type'=>'0'),"*");
        $receiver = $orderReceiveAddress[0];
        $reCountry = Service_Country::getByField($receiver['oab_country_id'],"country_id","*");
        $orderProduct = Service_OrderProduct::getByCondition(array('order_code'=>$orderCode),"*");
        $weight = 0;
        $description = "";
        $totalValue = 0;
        foreach($orderProduct as $key=>$op){
            $product = Service_Product::getByField($op['product_id'],"product_id","*");
            $weight +=$product['product_weight']*$op['op_quantity'];
            $totalValue+=$op['op_purpose_declared_value'];
            if($op['product_title_en']!=""){
                $description.=$op['product_title_en']." ";
            }else{
                $description.=$product['product_title_en']." ";
            }

        }
        $address2 = "";
        $address3 = "";
        $lenkey = "";
        if(strlen($receiver['oab_street_address2'])>30){
            $address2Arr = explode(" ",$receiver['oab_street_address2']);
            foreach($address2Arr as $k=>$adr){
                $address2.=$adr." ";
                if(strlen($address2)-1<=30){
                    $lenkey=$k;
                }else{
                    $address2 = "";
                    break;
                }
            }
            for($i=0;$i<count($address2Arr);$i++){
                if($i<=$lenkey){
                    $address2.=$address2Arr[$i]." ";
                }else{
                    $address3.=$address2Arr[$i]." ";
                }
            }
            $address2 = trim($address2);
            $address3 = trim($address3);
        }else{
            $address2 = $receiver['oab_street_address2'];
        }
        $serviceCode = "";
        if($weight>0&&$weight<=3){
            $serviceCode = "3HPA";
        }
        if($weight>3&&$weight<17){
            $serviceCode = "3H";
        }
        $totalValue = Service_Currency::converByCode('USD','GBP',$totalValue);
        $paramContent = $orderCode."||".$receiver['oab_company']."||".$receiver['oab_firstname']."||".$receiver['oab_street_address1']."||".$address2."||".$address3."||".$receiver['oab_city']."||".$reCountry['country_code']."||".$receiver['oab_postcode']."||".$receiver['oab_phone']."||1||".$weight."||".$description."||".$totalValue."||GBP||CSCM||".$orderCode."||".$serviceCode."||".$this->_AccountCode."||".$this->_UserName."||".$this->_Password."||||||||||||||||||||||".$orderRow['remark']."||||||";
        //echo $paramContent."<br>";
        $client = new SoapClient($this->_wsdlurl);
        $return = $client->getLabels($paramContent);
        $result = explode("||",$return);
        return $result;
    }
}
