<?php
/* @todo：用于验证订单数据
 * @author:gilbert
 * @date : 2017-06-23
 * 这里的验证都是从Service_OrderProcess->validateInput()里整理出来的
 */
class Service_OrderDataValidate {

    protected $_error = array();
    protected $_customer;
    protected $_order;
    protected $_city;
    protected $_country;
    protected $_parcel;
    protected $_customerIsRight = false;
    protected $_modeTypeIsRight = false;
    protected $_warehouseIsRight = false;
    protected $_notSelfSmCodeIsRight = false;//非自有运输方式 1：表示不是自有 0：表示自有
    protected $_smCodeIsRight = false;
    protected $_countryIsRight = false;
    protected $_orderTypeIsRight = false;
    protected $_cityIsRight = false;

    public function __construct() {

    }
    protected function addError( $error ) {
        $this->_error[] = $error;
    }
    public function getError() {
        return $this->_error;
    }
    public function getParcel() {
        return $this->_parcel;
    }
    public static function trimData( $data ) {
        if( is_array( $data ) || is_object( $data ) ) {
            foreach( $data as $key => $value ) {
                if( is_scalar( $value ) && !is_bool( $value ) ) {
                    $data[ $key ] = trim( $value );
                } else if( is_array( $value ) || is_object( $value ) ) {
                    $data[ $key ] = self::trimData( $value );
                }
            }
        } else if( is_scalar( $data ) && !is_bool( $data ) ) {
            $data = trim( $data );
        }
        return $data;
    }
    protected function setOrder( $order ) {
        $this->_order = $order;
        if( empty( $this->_order ) )
            $this->addError( '订单数据为空' );
    }
    public function getOrder() {
        return $this->_order;
    }
    //判断数据是否为空
    public function isEmpty( $data ) {
        return  is_null( $data ) || ( is_string( $data ) && $data === '' ) || ( is_array( $data ) && empty( $data ) ) ? true :false;
    }
    /* 注意，里面有调用到一些变量，请勿打打乱方法的调用
     *
     *
     */
    public function validate( $order ) {
        $order = self::trimData($order);
        $this->setOrder( $order );
        if( !empty( $this->_error ) ) {
            return false;
        }
        $this->validateCustomer();
        $this->validateModeType();
        $this->validateWarehouse();
        $this->validateCountry();
        $this->validateIsSelfSmCode();
        $this->validateOrderType();
        $this->validateSmCode();
        $this->validateReferenceNumber();
        $this->validateFba();
        $this->validateFbaTax();
        $this->validatePod();
        $this->validateDeliveryAddress();
        $this->validateRefTrackingNumber();
        $this->validateFirstName();
        $this->validateCompany();
        $this->validateState();
        $this->validateCity();
        $this->validatePostcode();
        $this->validateAddress1();
        $this->validateAddress2();
        $this->validatePhone();
        $this->validateEmail();
        $this->validateCurrency();
        $this->validateTotalDealPrice();
        $this->validateInsurance();
        $this->validatePackage();
        $this->validateProduct();

        return empty( $this->_error ) ? true :false;
    }
    protected function validateCustomer() {
        if( $this->isEmpty( @$this->_order['customer_id'] ) ) {
            $this->addError( '缺少用户代码' );
        } else {
            //可以改成查缓存
            $customer = Service_Customer::getByField( $this->_order['customer_id'], 'customer_id' );
            if( !$customer ) {
                $this->addError( '用户代码存在' );
            } else {
                $this->_customer = $customer;
                $this->_customerIsRight = true;
            }
        }
    }
    //验证订单模式
    protected function validateModeType() {
        if( $this->isEmpty( @$this->_order['order_mode_type'] ) ) {
            $this->addError( '订单模式不能为空' );
        } else if ( $this->_order['order_mode_type'] != '0' && $this->_order['order_mode_type'] != '1' ) {
            $this->addError( Ec_Lang::getInstance()->getTranslate('OrderModeOnlyBeCollectingModeOrDeliveryMode') );
        } else {
            $this->_modeTypeIsRight = true;
        }
    }
    //验证仓库
    protected function validateWarehouse() {
        if ( $this->isEmpty( @$this->_order['warehouse_id'] )  ) {
            $this->addError( Ec_Lang::getInstance()->getTranslate('MustSelectWarehouse') );
        } else {
            $warehouse = Common_DataCache::getWarehouse( 0, $this->_order['warehouse_id'] );
            if( empty( $warehouse ) ) {
                $this->_warehouseIsRight = false;
                $this->addError( '仓库不存在' );
            } else {
                $this->_warehouseIsRight = true;
            }
        }
    }
    //验证国家
    protected function validateCountry() {
        if ( $this->isEmpty( @$this->_order['oab_country_id'] )  ) {
            $this->addError( '国家不能为空' );
        } else {
            $country = Common_DataCache::getCountry( false, $this->_order['oab_country_id'] );
            if( empty( $country ) ) {
                $this->_countryIsRight = false;
                $this->addError( '国家不存在' );
            } else {
                $this->_countryIsRight = true;
                $this->_country = $country;
            }
        }
    }
    //验证是否是自有渠道, 数据库里已经change_order字段改用于【是否是自有渠道】
    protected function validateIsSelfSmCode() {
        if ( $this->isEmpty( @$this->_order['change_order'] ) ) {
            if( $this->_modeTypeIsRight ) {
                if( $this->_order['order_mode_type'] == 1 ) {
                    $this->addError( '是否自有渠道不能为空' );
                } else {
                    //备货模式没有用自用渠道
                    $this->_order['change_order'] = 1;//非自有渠道
                    $this->_notSelfSmCodeIsRight = true;
                }
            }
        } else {
            if ( $this->_order['change_order'] != 0 && $this->_order['change_order'] != 1 ) {
                $this->addError( '是否自有渠道只能填0或1' );
            } else {
                $this->_notSelfSmCodeIsRight = true;
            }
        }
    }
    //验证订单类型
    protected function validateOrderType() {
        if( $this->isEmpty( @$this->_order['order_type'] ) ) {
            $this->addError( '订单类型不能为空' );
        } else if( !in_array( $this->_order['order_type'], array( 0, 1, 2, 3, 4 ) ) ) {
            $this->addError( '订单类型不正确' );
        } else {
            $this->_orderTypeIsRight = true;
        }
    }
    //验证运输方式
    protected function validateSmCode() {
        //非自有渠道
        if ( $this->isEmpty( @$this->_order['sm_code'] )  ) {
            $this->addError( '运输方式不能为空' );

        } else {
            //是否自有渠道的验证通过了才进去,并且订单类型不是3（self pickup）
            if( $this->_countryIsRight && $this->_orderTypeIsRight && $this->_notSelfSmCodeIsRight && $this->_order['order_type'] != 3 && $this->_order['change_order'] != 0 ) {
                $shippingMethods = Common_DataCache::getShippingMethod();
                $smCodes         = array();
                foreach ($shippingMethods as $sm) {
                    $smCodes[] = $sm['sm_code'];
                }
                if (!in_array( $this->_order['sm_code'], $smCodes )) {
                    $this->addError( Ec_Lang::getInstance()->getTranslate('ShippingMethodDoNotExist') ); //'运输方式不存在';
                    return;
                }
                $con = array(
                    'sm_code'    => $this->_order['sm_code'],
                    'country_id' => $this->_order['oab_country_id'],
                );

                $countryMapRows = Service_SmCountryMap::getByCondition($con);

                if (empty($countryMapRows)) {
                    $this->addError( 'country -->' . $this->_country['country_code'] . Ec_Lang::getInstance()->getTranslate('DoesNotSupportTheShippingMethod') . '-->' . $this->_order['sm_code'] );
                } else {
                    $this->_smCodeIsRight = true;
                }
            } else if( $this->_order['order_type'] == 3 ) {//自提
                $this->_smCodeIsRight = true;
            }
            //验证CNDHL
            if( $this->_smCodeIsRight ) {
                if( $this->_order['sm_code'] == "CNDHL" ){
                    $ftpMessageType = Service_FtpMessageType::getByField( $this->_customer['customer_code'].'-CNDHL', 'fmt_code' );
                    if( empty( $ftpMessageType ) )
                        $this->addError( '运输方式CNDHL不可用' );
                }
            }
        }
    }
    //
    protected function validateReferenceNumber() {
        if ( $this->isEmpty( @$this->_order['reference_no'] )  ) {

        } else {
            $condition = array(
                'reference_no'=> $this->_order['reference_no'],
                'order_status_arr'=>array(1,2,3,4,5,6,7,8,9,10,11,12,13,14),
            );
            $order = Service_Orders::getByCondition($condition);
            if( @$this->_order['action'] == 'add' && $order){
                $this->addError( '系统已存在交易订单号' );
            } else if( @$this->_order['action']=='update') {
                if( $order ){
                    foreach($order as $value ){
                        if( $value['order_code'] != $this->_order['ordersCode']){
                            $this->addError( '系统已存在交易订单号' );
                            break;
                        }
                    }
                }
            }
        }
    }
    //验证FBA
    protected function validateFba() {
        if( $this->isEmpty( @$this->_order['is_fba'] ) ) {
             if(in_array(@$this->_order['sm_code'],array('HKDHL','BH-HKDHL','4PX-HKDHL','DHL-TH'))){
                 $this->addError( '当运输方式为HKDHL、BH-HKDHL、4PX-HKDHL、DHL-TH时，是否FBA必填' ) ;
             }
        } else {
            if ( $this->_order['is_fba'] !=  0 &&  $this->_order['is_fba'] != 1 )
                $this->addError( 'FBA填写错误' ) ;
            if(@$this->_order['sm_code']=="BH-HKDHL"&&$this->_order['is_fba'] ==  1){
                $this->addError( '当运输方式为BH-HKDHL时，FBA只能选择否' ) ;
            }
        }
    }
    //验证FBA TAX
    protected function validateFbaTax() {
        if( $this->isEmpty( @$this->_order['is_fba_tax'] ) ) {
         //noting to do
        } else {
            if ( $this->_order['is_fba_tax'] != 0 && $this->_order['is_fba_tax'] != 1 ){
                $this->addError( '目的地关税填写错误' ) ;
            }
            if(@$this->_order['sm_code']=="BH-HKDHL"&&$this->_order['is_fba_tax'] ==  1){
                $this->addError( '当运输方式为BH-HKDHL时，目的地关税只能为到付' ) ;
            }
        }
    }
    //验证POD
    protected function validatePod() {
        if( $this->isEmpty( @$this->_order['is_pod'] ) ) {
            switch( @$this->_order['sm_code'] ) {
                case '4PX-LYT_GH':
                    if( @$this->_order['oab_country_id'] == 253 ){
                        $this->addError( 'POD 必填' );
                    }
            }
        } else {
            if ( $this->_order['is_pod'] != 0 && $this->_order['is_pod'] != 1 ) {
                $this->addError( 'POD数据填写错误' ) ;
            }
        }
    }
    //
    protected function validateDeliveryAddress() {
        if( $this->isEmpty( @$this->_order['delivery_address'] ) ) {
            if( $this->_notSelfSmCodeIsRight && $this->_order['change_order'] == 0 && $this->_modeTypeIsRight && $this->_order['order_mode_type'] == 1) {
                $this->addError( '派送地址必填' );
            }
        } else {
            //派送地址多长并没有限定？

        }
    }
    //验证跟踪号
    protected function validateRefTrackingNumber() {
        if( $this->isEmpty( @$this->_order['ref_tracking_number'] ) ) {
            //假如是集货模式  如果选了 使用自有渠道 是的情况  服务商单号就必填
            if( ( $this->_modeTypeIsRight && $this->_notSelfSmCodeIsRight && $this->_order['order_mode_type'] == 1 && $this->_order['change_order'] == 0 ) || ( $this->_smCodeIsRight && $this->_order['sm_code'] == 'WG-EUB-EBAY' ) ) {
                $this->addError( '服务商单号必填' );
            }

        } else {
            //验证同一个运输方式的服务商单号的唯一性
            if( $this->_modeTypeIsRight && $this->_order['order_mode_type'] == 1 ) {
                $condition = array(
                    //'sm_code'=>$row['sm_code'],
                    'ref_tracking_number'        => $this->_order['ref_tracking_number'],
                    'except_order_status_delete' => true,
                );
                $numberRefTrack  = Service_Orders::getByCondition( $condition );
                $serviceTracking = Service_ServiceTrackingNumber::getByField( $this->_order['ref_tracking_number'], 'stn_code');

                if ( isset( $this->_order['order_code'] ) ) {
                    // 编辑模式
                    if ( !empty( $numberRefTrack ) ) {
                        foreach ($numberRefTrack as $item) {
                            if ( trim( $item['order_code']) !== trim( $this->_order['order_code'] ) ) {
                                $this->addError( '服务商单号已经存在' );
                                break;
                            }
                        }
                    } else if (!empty( $serviceTracking ) && trim( $serviceTracking['order_code'] ) !== $this->_order['order_code']) {
                        $this->addError( '服务商单号已经存在' );
                    }
                } else {
                    // 新增模式
                    if ( !empty( $numberRefTrack ) || ( !empty($serviceTracking) && 0 !== (int) $serviceTracking['stn_status'] ) ) {
                        $this->addError( '服务商单号已经存在' );
                    }
                }
            }
        }
    }
    //
    protected function validateFirstName() {
        if( $this->isEmpty( @$this->_order['oab_firstname'] ) ) {
            $this->addError( '收件人姓名必填' );
        } else {
            switch( @$this->_order['sm_code'] ) {
                case 'BH-DPD':
                case 'UTK':
                    if( mb_strlen( $this->_order['oab_firstname'] ) > 35 ) {
                        $this->addError( '收件人姓名最长只能是35个字符' );
                    }
                    break;
                case 'WWDHLU':
                case 'WWDHLP':
                case 'HKDHL':
                case 'CNDHL':
                case 'BH-HKDHL':
                    if( mb_strlen( $this->_order['oab_firstname'] ) > 35 ) {
                        $this->addError( '收件人姓名最长只能是35个字符' );
                    }
                    //HKDHL不接受&，否则&及其之后的内容会被清掉
                    $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';

                    if ( !preg_match( $pattern, $this->_order['oab_firstname'] ) ) {
                        $this->addError( '收件人姓名含有非英语字符或&字符' );
                    }
                    break;
                case 'UKTL':
                    if( mb_strlen( $this->_order['oab_firstname'] ) > 30 ) {
                        $this->addError( '收件人姓名最长只能是30个字符' );
                    }
                    break;
                case "TWPG":
                case 'TWPP':
                    if ( mb_strlen( $this->_order['oab_firstname'] ) > 15) {
                        $this->addError( '收件人姓名最长只能是15个字符' );
                    }
                    break;
				case 'DHL-TH':
                    $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                    
                    if ( !preg_match( $pattern, $this->_order['oab_firstname'] ) ) {
                        $this->addError( '收件人姓名含有非英语字符或&字符' );
                    }
                    break;
                case 'BH-BEAT':
                case 'BH-NLAT':
                case 'BH-AZAT-G':
                    if( mb_strlen( $this->_order['oab_firstname'] ) > 50 ) {
                        $this->addError( '收件人姓名最长只能是50个字符' );
                    }
                    break;
                case 'BH-SFEP':
                    //oab_country_id
                    if($this->_order['oab_country_id']==253){
                        if( mb_strlen( $this->_order['oab_firstname'] ) > 30 ) {
                            $this->addError( '收件人姓名最长只能是30个字符' );
                        }
                    }elseif($this->_order['oab_country_id']==252||$this->_order['oab_country_id']==13){
                        if( mb_strlen( $this->_order['oab_firstname'] ) > 35 ) {
                            $this->addError( '收件人姓名最长只能是35个字符' );
                        }
                    }else{
                        if( mb_strlen( $this->_order['oab_firstname'] ) > 60 ) {
                            $this->addError( '收件人姓名最长只能是60个字符' );
                        }
                    }
                    break;
                default:
                    if( mb_strlen( $this->_order['oab_firstname'] ) > 64 ) {
                        $this->addError( '收件人姓名最长只能是64个字符' );
                    }
            }
        }
    }
    //
    protected function validateCompany() {
        if( $this->isEmpty( @$this->_order['oab_company'] ) ) {

        } else {
            switch( @$this->_order['sm_code'] ) {
                case 'WWDHLU':
                case 'WWDHLP':
                case 'HKDHL':
                case 'BH-HKDHL':
                case 'CNDHL':
                    if ( mb_strlen( $this->_order['oab_company']) > 35) {
                        $this->addError( 'WWDHLU,WWDHLP,HKDHL,CNDHL,BH-HKDHL公司名必须小于等于35个字符' );
                    }
                    //HKDHL不接受&，否则&及其之后的内容会被清掉
                    $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                    if ( !preg_match( $pattern, $this->_order['oab_company'] ) ) {
                        $this->addError( '收件人公司含有非英语字符或&字符' );
                    }
                    break;
                case 'DHL-TH':
                    $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                    if ( !preg_match( $pattern, $this->_order['oab_company'] ) ) {
                        $this->addError( '收件人公司含有非英语字符或&字符' );
                    }
                    break;
                case 'UKTL':
                    if( mb_strlen( $this->_order['oab_company'] ) > 30 ){
                        $this->addError( '公司名最长30个字符' );
                    }
                    break;
                case 'BH-SFEP':
                    if($this->_order['oab_country_id'] == 252){
                        if( mb_strlen( $this->_order['oab_company'] ) > 35 ){
                            $this->addError( '公司名最长35个字符' );
                        }
                    }elseif(in_array($this->_order['oab_country_id'],array(13,35,109,110,121,125,144,172,184,191,222,236,260,252,253))){
                        if( mb_strlen( $this->_order['oab_company'] ) > 30 ){
                            $this->addError( '公司名最长30个字符' );
                        }
                    }elseif(in_array($this->_order['oab_country_id'],array(39,75,81,89,131,137,182,192,201,229,250))){
                        if( mb_strlen( $this->_order['oab_company'] ) > 60 ){
                            $this->addError( '公司名最长60个字符' );
                        }
                    }else{
                        if( mb_strlen( $this->_order['oab_company'] ) > 50 ){
                            $this->addError( '公司名最长50个字符' );
                        }
                    }
                    break;
                default:
                    if( mb_strlen( $this->_order['oab_company'] ) > 128 ){
                        $this->addError( '公司名最长128个字符' );
                    }
                    break;

            }
        }
    }
    //
    protected function validateState() {
        if( $this->isEmpty( @$this->_order['oab_state'] ) ) {
            $needState = array( '4PX-RU_POST-LINK_RMAIL', 'CJ-DGMG', 'CJ-DGMP', 'HKPG', 'HKPP','SZY-EUB','WG-EUB','BH-USPS','BH-AZAT-G','BH-BEAT','BH-NLAT');

            if( @$this->_order['oab_country_id'] == 253 ) {
                $this->addError( '州/区域必填' );
            } else if( $this->_smCodeIsRight && in_array( $this->_order['sm_code'], $needState ) ) {
                $this->addError( '州/区域必填' );
            }else if(in_array($this->_order['oab_country_id'],array(13,39,75,81,137,182,192,201,229,250))&&$this->_order['sm_code']=="BH-SFEP"){
                $this->addError( '州/区域必填' );
            }
        } else {
            switch( @$this->_order['sm_code'] ) {
                case 'BH-USPS':
                    if( @$this->_order['oab_country_id'] == 253 ) {
                        $validatorState = array("Alabama", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");
                        //州限制不区分大小写
                        $validatorStateInArray = false;
                        foreach ($validatorState as $value ) {
                            if ( strtolower( $value ) == strtolower( $this->_order['oab_state'] ) ) {
                                $validatorStateInArray = true;
                                break;
                            }
                        }
                        if ( !$validatorStateInArray ) {
                            $this->addError( '州/区域不正确' );
                        }
                    }
                    break;
                case 'BH-SFEP':
                    if( @$this->_order['oab_country_id'] == 253 ) {
                        $validatorState = array("Alabama", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");
                        //州限制不区分大小写
                        $validatorStateInArray = false;
                        foreach ($validatorState as $value ) {
                            if ( strtolower( $value ) == strtolower( $this->_order['oab_state'] ) ) {
                                $validatorStateInArray = true;
                                break;
                            }
                        }
                        if ( !$validatorStateInArray ) {
                            $this->addError( '州/区域不正确' );
                        }
                    }
                    if(@$this->_order['oab_country_id'] == 13){
                        if( mb_strlen( $this->_order['oab_state'] ) > 80 ){
                            $this->addError( '州/区域最长80字符' );
                        }
                    }elseif(@$this->_order['oab_country_id'] == 253){
                        if( mb_strlen( $this->_order['oab_state'] ) > 30 ){
                            $this->addError( '州/区域最长30字符' );
                        }
                    }else{
                        if( mb_strlen( $this->_order['oab_state'] ) > 60 ){
                            $this->addError( '州/区域最长60字符' );
                        }
                    }
                    break;
                case 'WWDHLU':
                case 'WWDHLP':
                case 'HKDHL':
                case 'BH-HKDHL':
                case 'CNDHL':
                case 'DHL-TH':
                    // HKDHL不接受&，否则&及其之后的内容会被清掉
                    $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                    if ( !preg_match($pattern, $this->_order['oab_state']) ) {
                        $this->addError( '收件人州/区域含有非英语字符或&字符' );
                    }
                    break;
                case 'BH-BEAT':
                case 'BH-NLAT':
                case 'BH-AZAT-G':
                    if( mb_strlen( $this->_order['oab_state'] ) > 50 ){
                        $this->addError( '州/区域最长50字符' );
                    }
                    break;
			    case 'DGM-US':
					$filePath = APPLICATION_PATH . '/../data/file/USA-state-validity.xlsx';
                    $fileData = Common_Upload::readEXCEL($filePath);
                    $res      = 0;
                    foreach ($fileData as $k => $v) {
                        if ($k == 0) {
                            continue;
                        }
                        $str = strpos(strtolower($this->_order['oab_state']), strtolower($v[1]));
                        if (is_numeric($str)) {
                            $res = 1;
                            break;
                        }
                    }
                    if (!$res) { 
                        $this->addError( "当运输方式为DGM-US，收件人州/区域所填项'{$this->_order['oab_state']}'无效，请填写美国有效州/区域");
                    }
                default:
                    if( mb_strlen( $this->_order['oab_state'] ) > 60 ){
                        $this->addError( '州/区域最长60字符' );
                    }
            }
        }
    }
    //
    protected function validateCity() {
        if( $this->isEmpty( @$this->_order['oab_city'] ) ) {
            $needCity = array('WWDHLU','WWDHLP','HKDHL','BH-HKDHL','CNDHL','4PX-HKDHL','4PX-RU_POST-LINK_RMAIL','CJ-DGMG', 'DGM-US','CJ-DGMP','HKPG', 'DHL-TH', 'HKPP','UKTL','BH-AZAT-G','DHL-BH03',"BH-SFEP",'BH-BEAT','BH-NLAT');
            if ( in_array( @$this->_order['sm_code'], $needCity ) ) {
                $this->addError( '城市必填' );
            }
        } else {
            switch( @$this->_order['sm_code'] ) {
                case 'WWDHLU':
                case 'WWDHLP':
                case 'HKDHL':
                case 'BH-HKDHL':
                case 'CNDHL':
                case '4PX-HKDHL':
                case 'DHL-TH':
                case 'DGM-US':
                case 'DHL-BH03':
                    if( $this->_order['sm_code'] == 'DGM-US' ) {
                        $city = Service_City::getByCondition(array('country_id' => $this->_order['oab_country_id'],'city_name' => $this->_order['oab_city'],'sm_code' => $this->_order['sm_code']));
                    } else {
                        $city = Service_City::getByCondition(array('country_id' => $this->_order['oab_country_id'],'city_name' => $this->_order['oab_city'],'sm_code' => 0 ));
                    }
                    if( $city ) {
                        $this->_city = $city[0];
                        $this->_cityIsRight = true;
                        if( in_array( $this->_order['sm_code'], array( 'HKDHL', 'CNDHL', 'BH-HKDHL','DHL-TH','DHL-BH03','WWDHLU','WWDHLP') ) ) {
                            //HKDHL不接受&，否则&及其之后的内容会被清掉
                            $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                            // 收件人城市
                            if ( !preg_match( $pattern, $this->_order['oab_city'] ) ) {
                                $this->addError( '收件人城市含有非英语字符或&字符' );
                            }
                        }
                    } else {
                        $this->addError( '请填写正确的收件人城市' );
                    }
                    break;
                case 'UKTL':
                    if( mb_strlen( $this->_order['oab_city'] ) > 20 ) {
                        $this->addError( '收件人城市最长20字符' );
                    }
                    break;
                case 'BH-DPD':
                    if ( mb_strlen( $this->_order['oab_city'] ) > 30 ) {
                        $this->addError( '收件人城市最长30字符' );
                    }
                    break;
                case 'BH-NLAT':
                case 'BH-BEAT':
                case 'BH-AZAT-G':
                    if ( mb_strlen( $this->_order['oab_city'] ) > 50 ) {
                        $this->addError( '收件人城市最长50字符' );
                    }
                    break;
                case "BH-SFEP":
                    if($this->_order['oab_country_id']==253){
                        if ( mb_strlen( $this->_order['oab_city'] ) > 30 ) {
                            $this->addError( '收件人城市最长30字符' );
                        }
                    }elseif($this->_order['oab_country_id']==252){
                        if ( mb_strlen( $this->_order['oab_city'] ) > 35 ) {
                            $this->addError( '收件人城市最长35字符' );
                        }
                    }elseif($this->_order['oab_country_id']==13){
                        if ( mb_strlen( $this->_order['oab_city'] ) > 80 ) {
                            $this->addError( '收件人城市最长80字符' );
                        }
                    }elseif(in_array($this->_order['oab_country_id'],array(33,35,39,75,81,109,110,121,125,131,137,144,172,182,184,191,192,201,222,229,236,250,260))){
                        if ( mb_strlen( $this->_order['oab_city'] ) > 100 ) {
                            $this->addError( '收件人城市最长100字符' );
                        }
                    }else{
                        if ( mb_strlen( $this->_order['oab_city'] ) > 50 ) {
                            $this->addError( '收件人城市最长50字符' );
                        }
                    }
                    break;
                default:
                    $this->_cityIsRight = true;//其他运输方式不用验证
                    if( mb_strlen( $this->_order['oab_city'] ) > 60 ) {
                        $this->addError( '收件人城市最长60字符' );
                    }
            }

        }
    }
    //
    protected function validatePostcode() {
        $allCodesOfCity = 0;
        if( $this->_cityIsRight && $this->_city ) {
            $allCodesOfCity = Service_Postcode::getByCondition(array('city_id' => $this->_city['city_id']), "count(*)");
        }
        $needPostcode = array( 'CJ-DGMG', 'CJ-DGMP', 'HKPG', 'HKPP','UKTL','TWPG','TWPP','BH-AZAT-G','BH-BEAT','BH-NLAT');
        $mayNeedPostCode = array(  'HKDHL', 'BH-HKDHL', 'CNDHL', '4PX-HKDHL', 'DGM-US','DHL-TH','DHL-BH03','WWDHLU','WWDHLP' );

        if( $this->isEmpty( @$this->_order['oab_postcode'] ) ) {
            if ( $this->_smCodeIsRight ) {
                if( in_array( $this->_order['sm_code'], $needPostcode ) ) {
                    $this->addError( '收件人邮编必填' );
                } else if( in_array( $this->_order['sm_code'], $mayNeedPostCode ) && $allCodesOfCity > 0) {
                    $this->addError( '收件人邮编必填' );
                }
            }
            if(in_array($this->_order['oab_country_id'],array(13,252,253))&&$this->_order['sm_code']=="BH-SFEP"){
                $this->addError( '收件人邮编必填' );
            }
        } else {
            if( $this->_cityIsRight && $this->_smCodeIsRight && in_array( $this->_order['sm_code'], $mayNeedPostCode ) ) {
                //非环球资源才验证邮编
                if( Service_CustomerProcess::$HuanQiu != Service_CustomerProcess::getOwner( $this->_customer['customer_code'] ) ){
                    //某些城市不需要邮编
                    if ( $allCodesOfCity ) {
                        $tempPostCode = $this->_order['oab_postcode'];
                        if( $this->_order['sm_code'] === 'BH-HKDHL'){
                            switch( @$this->_order['oab_country_id'] ) {
                                case 41:
                                    //加拿大 A9A9A9 手动补1位
                                    if( preg_match('/^[A-Z]{1}[0-9]{1}[A-Z]{1} ?[0-9]{1}[A-Z]{1}[0-9]{1}$/', $this->_order['oab_postcode'])){
                                        $tempPostCode = str_replace( ' ', '', $this->_order['oab_postcode'] );
                                        $tempPostCode=  substr( $tempPostCode, 0, -1);
                                    }else {
                                        $this->addError( '邮编格式为城市邮编+一位数字补码' );
                                    }
                                    break;
                                case 98:
                                    //海峡群岛 在目前的验证邮编基础上，必须加上一位数字和2位大写字母补码
                                    if( preg_match('/^[A-Z0-9]{6,7}$/', $this->_order['oab_postcode'])){
                                        $tempPostCode=  substr( $this->_order['oab_postcode'], 0, -3);

                                        //邮编从后往前，第3位前面加上空格，例如邮编GY10BN，保存时需存为：GY1 0BN
                                        $cnt = mb_strlen( $this->_order['oab_postcode'] );
                                        $part1 = mb_substr( $this->_order['oab_postcode'], 0, $cnt - 3 );
                                        $part2 = mb_substr( $this->_order['oab_postcode'], $cnt - 3 );
                                        $this->_order['oab_postcode'] = $part1.' '.$part2;

                                    } else if( preg_match('/^[A-Z0-9]{3,4} [A-Z0-9]{3}$/', $this->_order['oab_postcode']) ) {
                                        $tempPostCode = str_replace( ' ', '', $this->_order['oab_postcode'] );
                                        $tempPostCode=  substr( $tempPostCode, 0, -3);
                                    } else {
                                        $this->addError( '邮编格式为6位的字母和数字' );
                                    }
                                    break;
                                case 119:
                                    //海峡群岛 泽西岛（AA9 9AA，手动补3位）根西岛（AA9 9AA手动补三位）
                                    if( preg_match('/^[A-Z]{2}[0-9]{2}[A-Z]{2}$/',$this->_order['oab_postcode']) ){
                                        $tempPostCode=  substr( $this->_order['oab_postcode'], 0, -3);

                                        //邮编从后往前，第3位前面加上空格，例如邮编S0N0A1，保存时需存为：S0N 0A1
                                        $cnt = mb_strlen( $this->_order['oab_postcode'] );
                                        $part1 = mb_substr( $this->_order['oab_postcode'], 0, $cnt - 3 );
                                        $part2 = mb_substr( $this->_order['oab_postcode'], $cnt - 3 );
                                        $this->_order['oab_postcode'] = $part1.' '.$part2;

                                    } else if( preg_match('/^[A-Z]{2}[0-9]{1} [0-9]{1}[A-Z]{2}$/',$this->_order['oab_postcode']) ) {
                                        $tempPostCode = str_replace( ' ', '', $this->_order['oab_postcode'] );
                                        $tempPostCode=  substr( $tempPostCode, 0, -3);
                                    } else{
                                        $this->addError( '邮编格式为城市邮编+一位数字和两位大写字母补码' );
                                    }
                                    break;
                                case 195:
                                    //葡萄牙
                                    if( preg_match('/^[0-9]{7}$/',$this->_order['oab_postcode']) ){
                                        $tempPostCode=  $this->_order['oab_postcode'];

                                        //邮编从后往前，第3位前面加上-，了例如邮编3456789，保存时需存为：3456-789
                                        $cnt = mb_strlen( $this->_order['oab_postcode'] );
                                        $part1 = mb_substr( $this->_order['oab_postcode'], 0, $cnt - 3 );
                                        $part2 = mb_substr( $this->_order['oab_postcode'], $cnt - 3 );
                                        $this->_order['oab_postcode'] = $part1.'-'.$part2;

                                    } else if( preg_match('/^[0-9]{4}\-[0-9]{3}$/',$this->_order['oab_postcode']) ) {
                                        $tempPostCode = str_replace( '-', '', $this->_order['oab_postcode'] );

                                    } else{
                                        $this->addError( '邮编格式为6位数字' );
                                    }
                                    break;
                                case 252:
                                    //英国补两位后两位是字母
                                    if( preg_match('/[A-Z0-9]{3,5}[A-Z]{2}$/',$this->_order['oab_postcode'])){
                                        $tempPostCode=  substr( $tempPostCode, 0, -2);

                                        //邮编从后往前，第3位前面加上空格，例如邮编：AB100YT，保存时需存为：AB10 0YT
                                        $cnt = mb_strlen( $this->_order['oab_postcode'] );
                                        $part1 = mb_substr( $this->_order['oab_postcode'], 0, $cnt - 3 );
                                        $part2 = mb_substr( $this->_order['oab_postcode'], $cnt - 3 );
                                        $this->_order['oab_postcode'] = $part1.' '.$part2;

                                    } else if( preg_match('/[A-Z0-9]{2,4} [A-Z0-9]{3}$/',$this->_order['oab_postcode']) ) {
                                        $tempPostCode = str_replace( ' ', '', $this->_order['oab_postcode'] );
                                        $tempPostCode=  substr( $tempPostCode, 0, -2);

                                    } else{
                                        $this->addError( '邮编格式为城市邮编+两位大写字母补码' );
                                    }
                                    break;
                            }
                        }
                        if($this->_order['sm_code'] === 'WWDHLU'||$this->_order['sm_code'] === 'WWDHLP'){
                            $tempPostCode = $this->_order['oab_postcode'];
                            switch( @$this->_order['oab_country_id'] ){
                                case 41://加拿大
                                    if(preg_match('/^[A-Z]{1}[0-9]{1}[A-Z]{1}[ ]{1}[0-9]{1}[A-Z]{1}$/', $this->_order['oab_postcode'])){
                                        $tempPostCode = str_replace( ' ', '', $this->_order['oab_postcode'] );
                                    }else {
                                        $this->addError( '正确邮编格式为字母+数字+字母+空格+数字+字母' );
                                    }
                                    break;
                                case 118://日本
                                    if(preg_match('/^[0-9]{3}[-]{1}[0-9]{4}$/', $this->_order['oab_postcode'])){
                                        $tempPostCode = str_replace( '-', '', $this->_order['oab_postcode'] );
                                    }else {
                                        $this->addError( '正确邮编格式为3位+横杆+4位' );
                                    }
                                    break;
                                case 192://波兰
                                    if(preg_match('/^[0-9]{2}[-]{1}[0-9]{3}$/', $this->_order['oab_postcode'])){
                                        $tempPostCode = str_replace( '-', '', $this->_order['oab_postcode'] );
                                    }else {
                                        $this->addError( '正确邮编格式为2位+横杆+3位' );
                                    }
                                    break;
                                case 195://葡萄牙
                                    if(preg_match('/^[0-9]{4}[-]{1}[0-9]{3}$/', $this->_order['oab_postcode'])){
                                        $tempPostCode = str_replace( '-', '', $this->_order['oab_postcode'] );
                                    }else {
                                        $this->addError( '正确邮编格式为4位+横杆+3位' );
                                    }
                                    break;
                                case 65://捷克
                                case 216://斯洛伐克
                                case 229://瑞典
                                    if(preg_match('/^[0-9]{3}[ ]{1}[0-9]{2}$/', $this->_order['oab_postcode'])){
                                        $tempPostCode = str_replace( ' ', '', $this->_order['oab_postcode'] );
                                    }else {
                                        $this->addError( '正确邮编格式为3位+空格+2位' );
                                    }
                                    break;
                            }
                        }
                        $postcodes = Service_Postcode::getPostcodes( $this->_city['city_id'], $tempPostCode);
                        if (empty($postcodes)) {
                            $this->addError( '请填写正确的收件人邮编' );
                        } else {
                            if (!in_array($tempPostCode, $postcodes)) {
                                $this->addError( '请填写正确的收件人邮编' );
                            }
                        }
                    }
                }
            } else {
                switch( @$this->_order['sm_code'] ) {
                    case 'SZY-EUB':
                    case 'WG-EUB':
                        switch ( @$this->_order['oab_country_id'] ) {
                            case 13://澳大利亚
                                if (!preg_match("/^[0-9]{4}+$/", $this->_order['oab_postcode'])) {
                                    $this->addError( '邮编格式不正确;正确格式:4位数字' );
                                }
                                break;
                            case 41:
                                if (!preg_match("/^[A-Za-z]{1}[0-9]{1}[A-Za-z]{1}(\s){0,1}[0-9]{1}[A-Za-z]{1}[0-9]{1}+$/", $this->_order['oab_postcode'])) {
                                    $this->addError( '邮编格式不正确;正确格式:字母+数字+字母+空格+数字+字母+数字或字母+数字+字母+数字+字母+数字' );
                                }
                                break;
                            case 82:
                            case 250://法国、乌克兰
                                if (!preg_match("/^[0-9]{5}+$/", $this->_order['oab_postcode'])) {
                                    $this->addError( '邮编格式不正确;正确格式:5位数字' );
                                }
                                break;
                            case 201:
                                if (!preg_match("/^[0-9]{6}+$/", $this->_order['oab_postcode'])) {
                                    $this->addError( '邮编格式不正确;正确格式:6位数字' );
                                }
                                break;
                            case 208:
                                if (!preg_match("/^(\d{5}|\d{7}|(\d{5}\-\d{4}))$/", $this->_order['oab_postcode'])) {
                                    $this->addError( '邮编格式不正确;正确格式:5位数字+短横线+4位数字 或 5位数字 或 7位数字' );
                                }
                                break;
                            case 252://英国
                                if (!preg_match("/^[0-9A-Za-z]{2,4}(\s)[0-9A-Za-z]{3}+$/", $this->_order['oab_postcode'])) {
                                    $this->addError( '邮编格式不正确;正确格式:2-4位数字或字母+空格+3位数字或字母' );
                                }
                                break;
                            case 253://美国
                                if (!preg_match("/^[0-9]{5}(\-[0-9]{4}){0,1}+$/", $this->_order['oab_postcode'])) {
                                    $this->addError( '邮编格式不正确;正确格式:5位数字+短横线+4位数字或者5位数字' );
                                }
                                break;
                        }
                        break;
                    case 'SZY-ETK':
                        switch ( @$this->_order['oab_country_id'] ) {
                            case 33:
                                if (!preg_match("/^[0-9]{5}(\-[0-9]{3})+$/", $this->_order['oab_postcode'])) {
                                    $this->addError( '邮编格式不正确;正确格式:5位数字+短横线+3位数字' );
                                }
                                break;
                            case 39:
                                if ( !preg_match("/^[0-9]{6}+$/", $this->_order['oab_postcode'])) {
                                    $this->addError( '邮编格式不正确;正确格式:6位数字' );
                                }
                                break;
                            case 201:
                                if ( !preg_match("/^[0-9]{6}+$/", $this->_order['oab_postcode']) ) {
                                    $this->addError( '邮编格式不正确;正确格式:6位数字' );
                                }
                                break;
                        }
                        break;
                    case 'BH-USPS':
                        if (!preg_match("/^[0-9]{5}(\-[0-9]{4}){0,1}+$/", $this->_order['oab_postcode'])) {
                            $this->addError( '邮编格式不正确;正确格式:5位数字+短横线+4位数字或者5位数字' );
                        }
                        break;
                    case 'BH-DPD':
                        if ( mb_strlen( $this->_order['oab_postcode'] ) > 9 ) {
                            $this->addError( '邮编必填且长度不能超过9个字符' );
                        } else if( $this->_countryIsRight ) {
                            $result = Service_OrderProcess::validatePostcode( $this->_country['country_code'], $this->_order['oab_postcode'] );
                            if (!$result) {
                                $this->addError( '当前运输方式暂时不支持该国家此邮编的派送' );
                            }
                        }
                        break;
                    case "TWPG":
                    case 'TWPP':
                        if ( mb_strlen( $this->_order['oab_postcode']) > 10) {
                            $this->addError( '邮编长度不能超过10个字符' );
                        }
                        break;
                    case 'BH-CORREOS':
                        $pattern = '/^(5[12]|07|3[58])/';
                        if (preg_match($pattern, $this->_order['oab_postcode'])) {
                            $this->addError( '该邮编为偏远地区' );
                        }
                        break;
                    case 'BH-19EURDPDDE':
                        $pattern = '/^(18565|83(209|256)|258(45|49|59|63|69)|259(38|46|80|92|96|99)|264(65|74|86)|265(48|71|79)|26757|2749[89])$/';
                        if (preg_match($pattern, $this->_order['oab_postcode'])) {
                            $this->addError( '该邮编为偏远地区' );
                        }
                        break;
                    case 'BH-DAC':
                        $pattern = '/^(3012[1-6]|04027|3013[23]|30135|3014[12]|5703[0-4,6-9]|58012|8007[1,3-7,9]|910(17|23)|98055|0[7-9][\d]{3}|8[7-9][\d]{3}|9[\d]{4})$/';
                        if (preg_match($pattern, $this->_order['oab_postcode'])) {
                            $this->addError( '该邮编为偏远地区' );
                        }
                        break;
                    case 'BH-SL018':
                        $pattern = '/^9(7[1-8]|8[4,6-8])/';
                        if (preg_match($pattern, $this->_order['oab_postcode'])) {
                            $this->addError( '该邮编为偏远地区' );
                        }
                        break;
                    case 'WWDHLP':
                    case 'WWDHLU':
                    case 'HKDHL':
                    case 'CNDHL':
                    case 'BH-HKDHL':
                        //HKDHL不接受&，否则&及其之后的内容会被清掉
                        $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                        if ( !preg_match( $pattern, $this->_order['oab_postcode'] ) ) {
                            $this->addError( '收件人邮编含有非英语字符或&字符' );
                        }
                        break;
                    case "BH-SFEP":
                        $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#&$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                        if($this->_order['oab_country_id']==253){
                            if (!(preg_match("/^[0-9-]+$/", $this->_order['oab_postcode']))) {
                                $this->addError( '收件人邮编只能是阿拉伯数字和横杆' );
                            }
                        }elseif($this->_order['oab_country_id']==252){
                            if(mb_strlen( $this->_order['oab_postcode'] ) > 15){
                                $this->addError( '邮编最长15字符' );
                            }elseif(!(preg_match("/^[0-9A-Za-z\s]+$/", $this->_order['oab_postcode']))){
                                $this->addError( '收件人邮编只能是英文字符和数字' );
                            }
                        }elseif($this->_order['oab_country_id']==13){
                            if(mb_strlen( $this->_order['oab_postcode'] ) > 4){
                                $this->addError( '邮编最长4字符' );
                            }elseif(!(preg_match("/^[0-9]+$/", $this->_order['oab_postcode']))){
                                $this->addError( '收件人邮编只能是数字' );
                            }
                        }elseif($this->_order['oab_country_id']==89&&$this->_order['oab_postcode']!=""){
                            if(mb_strlen( $this->_order['oab_postcode'] ) > 15){
                                $this->addError( '邮编最长15字符' );
                            }elseif(!(preg_match($pattern, $this->_order['oab_postcode']))){
                                $this->addError( '收件人邮编只能是英文,英文符号和数字' );
                            }
                        }elseif(in_array($this->_order['oab_country_id'],array(39,75,81,131,137,182,192,201,229,250,5,109,110,121,125,144,172,184,191,222,236,260))&&$this->_order['oab_postcode']!=""){
                            if(mb_strlen( $this->_order['oab_postcode'] ) > 60){
                                $this->addError( '邮编最长60字符' );
                            }elseif(!(preg_match($pattern, $this->_order['oab_postcode']))){
                                $this->addError( '收件人邮编只能是英文字符和数字' );
                            }
                        }else{
                            if(mb_strlen( $this->_order['oab_postcode'] ) > 15){
                                $this->addError( '邮编最长15字符' );
                            }elseif(!(preg_match($pattern, $this->_order['oab_postcode']))){
                                $this->addError( '收件人邮编只能是英文字符和数字' );
                            }
                        }
                        break;
                    case 'BH-NLAT':
                    case 'BH-BEAT':
                    case 'BH-AZAT-G':
                        if( mb_strlen( $this->_order['oab_postcode'] ) > 20 ) {
                            $this->addError( '邮编最长20字符' );
                        }
                    default:
                        if( mb_strlen( $this->_order['oab_postcode'] ) > 60 ) {
                            $this->addError( '邮编最长60字符' );
                        }
                }
            }
        }
    }
    //
    protected function validateAddress1() {
        if( $this->isEmpty( @$this->_order['oab_street_address1'] ) ) {
            $this->addError( '收件人地址1必填' );
        } else {
            switch( @$this->_order['sm_code'] ) {
                case 'CJ-DGMP':
                case 'CJ-DGMG':
                    if( $this->_orderTypeIsRight && $this->_order['order_type'] == '0') {
                        $search = array(" ","　","\n","\r","\t");
                        $this->_order['oab_street_address1'] = str_replace( $search,"", $this->_order['oab_street_address1']);
                        if( strstr( strtoupper( $this->_order['oab_street_address1'] ), 'PACKSTATION' ) ){
                            $this->addError( '当运输方式为'.$this->_order['sm_code'].'时，地址1内不能包含‘PACKSTATION’' );
                        }
                    }
                    break;
                case 'HKPG':
                    if( mb_strlen( $this->_order['oab_street_address1'] ) > 60 ) {
                        $this->addError( '地址1不能大于60个字符' );
                    }
                    break;
                case 'WWDHLP':
                case 'WWDHLU':
                case 'HKDHL':
                case 'CNDHL':
                case 'BH-HKDHL':
                    if( mb_strlen( $this->_order['oab_street_address1'] ) > 35 ) {
                        $this->addError( '地址1不能大于35个字符' );
                    }
                    //HKDHL不接受&，否则&及其之后的内容会被清掉
                    $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                    if ( !preg_match( $pattern, $this->_order['oab_street_address1'] ) ) {
                        $this->addError( '收件人地址1含有非英语字符或&字符' );
                    }
                    break;
                case 'DHL-TH':
                    $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                    if ( !preg_match( $pattern, $this->_order['oab_street_address1'] ) ) {
                        $this->addError( '收件人地址1含有非英语字符或&字符' );
                    }
                    break;
                case 'UKTL':
                    if( mb_strlen( $this->_order['oab_street_address1'] ) > 30 ) {
                        $this->addError( '地址1不能大于30个字符' );
                    }
                    if( strpos( $this->_order['oab_street_address1'], ",") !== false) {
                        $this->addError( '地址1不能有逗号' );
                    }
                    break;
                case "SZY-EUB":
                case "WG-EUB":
                    if ( strpos( $this->_order['oab_street_address1'], "&") !== false) {
                        $this->addError( '地址1不能有特殊字符&' );
                    }
                    break;
                case 'BH-USPS':
                    if ( strpos( $this->_order['oab_street_address1'], "&") !== false) {
                        $this->addError( '地址1不能有特殊字符&' );
                    }
                    if (( mb_strlen( $this->_order['oab_street_address1'] ) + mb_strlen( @$this->_order['oab_street_address2'] ) ) > 49) {
                        $this->addError( '收件人地址1+地址2长度不能超过50' );
                    }
                    break;
                case 'BH-DPD':
                    if ( mb_strlen( $this->_order['oab_street_address1'] ) + mb_strlen( @$this->_order['oab_street_address2'] ) > 70) {
                        $this->addError( '收件人地址1+地址2不得超过70个字符' );
                    }
                    break;
                case "TWPG":
                case 'TWPP':
                    if (( mb_strlen( $this->_order['oab_street_address1']) + mb_strlen( @$this->_order['oab_street_address2'] ) ) > 100) {
                        $this->addError( '收件人地址1+地址2长度不能超过100' );
                    }
                    break;
                case '4PX-LYT_GH':
                    if ( @$this->_order['oab_country_id'] == 89) {
                        if (( mb_strlen( $this->_order['oab_street_address1'] ) + mb_strlen( $this->_order['oab_street_address2'] ) ) > 35) {
                            $this->addError( '收件人地址1+地址2不能超35个字符' );
                        }
                        $address = $this->_order['oab_street_address1']." ".$this->_order['oab_street_address2'];
                        $addressArr = explode(" ",trim($address));
                        if( count($addressArr) >= 2) {
                            $regAddress = isset( $addressArr[2] ) ?$addressArr[1].$addressArr[2] : $addressArr[1];
                            if(!preg_match("/[0-9]{1,}/", $regAddress)){
                                $this->addError( '渠道4PX-LYT_GH到德国地址的第二或第三个单词必须有数字' );
                            }
                        }else{
                            if(!preg_match("/[0-9]{1,}/", $addressArr[0])){
                                $this->addError( '渠道4PX-LYT_GH到德国地址必须有数字' );
                            }
                        }
                        if ('1' == preg_match('/(P.O.BOX)/i', $this->_order['oab_street_address1']) || '1' == preg_match('/(P.O.BOX)/i', @$this->_order['oab_street_address2'] ) ){
                            $this->addError( '不提供邮政信箱 (P.O.BOX) 地址派送服务，请重新提交' );
                        }
                    }
                    break;
                case '4PX-LYT_GH':
                    if( $this->_order['oab_country_id'] == 252 ){//英国
                        if ('1' == preg_match('/(P.O.BOX)|(BFPO)/i', $this->_order['oab_street_address1']) || '1' == preg_match('/(P.O.BOX)|(BFPO)/i', @$this->_order['oab_street_address2'] ) ){
                            $this->addError( '不提供邮政信箱 (P.O.BOX) 及英军信箱 (BFPO) 地址派送服务，请重新提交' );
                        }
                    }
                    if (( mb_strlen( $this->_order['oab_street_address1'] ) + mb_strlen( @$this->_order['oab_street_address2'])) >= 120) {
                        $this->addError( '收件人地址1+地址2长度不能超过120个字符' );
                    }
                    break;
                case '4PX-EU_PACKET':
                case '4PX-RU_POST-LINK_RMAIL':
                case '4PX-HKDHL':
                case '4PX-Singapo_SPack_IMAIR':
                case '4PX-SG_ordinary_mail+':
                case '4PX-OZXBTH':
                case 'LYT-SMTGH-X':
                    if (( mb_strlen( $this->_order['oab_street_address1'] ) + mb_strlen( @$this->_order['oab_street_address2'])) >= 120) {
                        $this->addError( '收件人地址1+地址2长度不能超过120个字符' );
                    }
                    break;
                case "BH-SFEP":
                    if (in_array($this->_order['oab_country_id'],array(89,39,75,81,131,137,182,192,201,229,250))){
                        if (( mb_strlen( $this->_order['oab_street_address1'] ) + mb_strlen( @$this->_order['oab_street_address2'])) > 120) {
                            $this->addError( '收件人地址1+地址2长度不能超过120个字符' );
                        }
                    }elseif(in_array($this->_order['oab_country_id'],array(35,109,110,121,125,144,172,184,191,222,236,260))){
                        if (( mb_strlen( $this->_order['oab_street_address1'] ) + mb_strlen( @$this->_order['oab_street_address2'])) > 70) {
                            $this->addError( '收件人地址1+地址2长度不能超过70个字符' );
                        }
                    }elseif($this->_order['oab_country_id']==253){
                        if (( mb_strlen( $this->_order['oab_street_address1'] ) + mb_strlen( @$this->_order['oab_street_address2'])) > 30) {
                            $this->addError( '收件人地址1+地址2长度不能超过30个字符' );
                        }
                    }elseif($this->_order['oab_country_id']==252){
                        if (( mb_strlen( $this->_order['oab_street_address1'] ) + mb_strlen( @$this->_order['oab_street_address2'])) > 105) {
                            $this->addError( '收件人地址1+地址2长度不能超过105个字符' );
                        }
                    }else{
                        if (( mb_strlen( $this->_order['oab_street_address1'] ) + mb_strlen( @$this->_order['oab_street_address2'])) > 200) {
                            $this->addError( '收件人地址1+地址2长度不能超过200个字符' );
                        }
                    }
                    break;
            }
        }
    }
    //
    protected function validateAddress2() {
        if( $this->isEmpty( @$this->_order['oab_street_address2'] ) ) {

        } else {
            switch( @$this->_order['sm_code'] ) {
                case 'CJ-DGMP':
                case 'CJ-DGMG':
                    if( $this->_orderTypeIsRight && $this->_order['order_type'] == '0') {
                        $search = array(" ","　","\n","\r","\t");
                        $this->_order['oab_street_address2'] = str_replace( $search,"", $this->_order['oab_street_address2']);
                        if( strstr( strtoupper( $this->_order['oab_street_address2'] ), 'PACKSTATION' ) ){
                            $this->addError( '当运输方式为'.$this->_order['sm_code'].'时，地址2内不能包含‘PACKSTATION’' );
                        }
                    }
                    break;
                case 'HKPG':
                case 'UKTL':
                    if( mb_strlen( $this->_order['oab_street_address2'] ) > 60 ) {
                        $this->addError( '地址2不能大于60个字符' );
                    }
                    if( strpos( $this->_order['oab_street_address2'], ",") !== false) {
                        $this->addError( '地址1不能有逗号' );
                    }
                    break;
                case 'WWDHLP':
                case 'WWDHLU':
                case 'HKDHL':
                case 'CNDHL':
                    if( mb_strlen( $this->_order['oab_street_address2'] ) > 70 ) {
                        $this->addError( '地址2不能大于70个字符' );
                    }
                    //HKDHL不接受&，否则&及其之后的内容会被清掉
                    $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                    if ( !preg_match( $pattern, $this->_order['oab_street_address2'] ) ) {
                        $this->addError( '收件人地址1含有非英语字符或&字符' );
                    }
                    break;
                case 'BH-HKDHL':
                    if( mb_strlen( $this->_order['oab_street_address2'] ) > 70 ) {
                        $this->addError( '地址2不能大于60个字符' );
                    }
                    //HKDHL不接受&，否则&及其之后的内容会被清掉
                    $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                    if ( !preg_match( $pattern, $this->_order['oab_street_address2'] ) ) {
                        $this->addError( '收件人地址1含有非英语字符或&字符' );
                    }
                    break;
                case "SZY-EUB":
                case "WG-EUB":
                    if ( strpos( $this->_order['oab_street_address2'], "&" ) !== false) {
                        $this->addError( '地址2不能有特殊字符&' );
                    }
                    break;
                case 'BH-USPS':
                    if ( strpos( $this->_order['oab_street_address2'], "&") !== false) {
                        $this->addError( '地址2不能有特殊字符&' );
                    }
                    break;
                case "DHL-TH":
                    if ( mb_strlen( $this->_order['oab_street_address2'] ) > 35) {
                        $this->addError( '地址2不能大于70个字符' );
                    }
                    $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                    if ( !preg_match( $pattern, $this->_order['oab_street_address2'] ) ) {
                        $this->addError( '收件人地址1含有非英语字符或&字符' );
                    }
                    break;
            }
        }
    }
    protected function validatePhone() {
        if( $this->isEmpty( @$this->_order['oab_phone'] ) ) {
            $this->addError( '收件人电话必须填写' );
        } else {
            switch( @$this->_order['sm_code'] ) {
                case "TWPG":
                case 'TWPP':
                    if ( mb_strlen( $this->_order['oab_phone'] ) > 20 ) {
                        $this->addError( '收件人电话不能超过20个字符' );
                    }
                    break;
                case 'SF-RUPR':
                case 'SF-RUPT':
                    if ( !(preg_match("/^[0-9-+\s]{5,}$/", $this->_order['oab_phone']))) {
                        $this->addError( '收件人电话必须为数字或横杠或者加号或者空格并且至少5位数' );
                    }
                    break;
                case 'WWDHLP':
                case 'WWDHLU':
                case 'HKDHL':
                case 'CNDHL':
                case 'BH-HKDHL':
                    //HKDHL不接受&，否则&及其之后的内容会被清掉
                    $pattern = '/^[a-zA-Z0-9' . preg_quote('~`!@#$%^*()_-+={[}]|\:;"\'<,>.?/ ', '/') . ']+$/';
                    if ( !preg_match( $pattern, $this->_order['oab_phone'] ) ) {
                        $this->addError( '收件人电话含有非英语字符或&字符' );
                    }
                    break;
                case 'BH-BEAT':
                case 'BH-NLAT':
                case 'BH-AZAT-G':
                    if( strlen( $this->_order['oab_phone']) > 20 ){
                        $this->addError( '收件人电话最长只能是20个字符' );
                    }
                    break;
                case 'BH-SFEP':
                    if(strlen( $this->_order['oab_phone']) > 12&&$this->_order['oab_country_id']==252){//英国
                        $this->addError( '收件人电话最长只能是12个字符' );
                    }elseif(in_array($this->_order['oab_country_id'],array(39,75,81,131,137,182,192,201,229,250))&&strlen( $this->_order['oab_phone']) > 50){
                        //'俄罗斯','立陶宛','拉脱维亚','爱沙尼亚','芬兰','挪威','瑞典','白俄罗斯','乌克兰','波兰'
                        $this->addError( '收件人电话最长只能是50个字符' );
                    }elseif(strlen( $this->_order['oab_phone']) > 20){
                        $this->addError( '收件人电话最长只能是20个字符' );
                    }
                    break;
                default:
                    if( strlen( $this->_order['oab_phone']) > 25 ){
                        $this->addError( '收件人电话最长只能是25个字符' );
                    }
                    if ( !(preg_match("/^[0-9-+\/()\s]+$/", $this->_order['oab_phone']))) {
                        $this->addError( '收件人电话必须为数字或横杠或斜杠或者加号.' );
                    }
                    break;
            }
        }
    }
    //
    protected function validateEmail() {
        if( $this->isEmpty( @$this->_order['oab_email'] ) ) {
            //无处理
        } else {
            if ( preg_match("/^[a-zA-Z0-9_\.-]+\@([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/i", $this->_order['oab_email']) === false ) {
                $this->addError( Ec_Lang::getInstance()->getTranslate('EmaiFormatError') ); //"Emai格式错误";
            }
            if(in_array($this->_order['oab_country_id'],array(39,75,81,131,137,182,192,201,229,250))&&strlen( $this->_order['oab_email']) > 15&&$this->_order['sm_code']=="BH-SFEP"){
                $this->addError( '收件人电子邮件最长只能是15个字符' );
            }elseif(strlen( $this->_order['oab_email']) > 50&&$this->_order['sm_code']=="BH-SFEP"){
                $this->addError( '收件人电子邮件最长只能是50个字符' );
            }
        }
    }
    //
    protected function validateCurrency() {
        if( $this->isEmpty( @$this->_order['currency_code'] ) ) {
            //无处理
        } else {
            $currency = Service_Currency::getByField( $this->_order['currency_code'], 'currency_code', "*" );
            if (!$currency) {
                $this->addError( '非法的交易币种.' ); //"成交价格";
            }
        }
    }
    //验证成交总价
    protected function validateTotalDealPrice() {
        if( $this->isEmpty( @$this->_order['charge'] ) ) {
            //无处理
        } else {
            if ( !is_numeric( $this->_order['charge']) || $this->_order['charge'] <= 0 ) {
                $this->addError( '成交总价必须是大于0的数值.' ); //"成交价格";
            }
        }
    }
    //验证保险
    protected function validateInsurance() {
        if( $this->isEmpty( @$this->_order['insurance'] ) ) {
            //无处理
        } else {
            //检查保险
            $buyInsuranceShipMethod = Service_ShippingMethod::getBuyInsuranceShipMethod();
            if( $this->_smCodeIsRight && $this->_order['insurance'] == 1 ) {
                if( $this->isEmpty( @$this->_order['currency_code'] ) ) {
                    $this->addError( '币种必填' );
                }
                if( !in_array( $this->_order['sm_code'],$buyInsuranceShipMethod) ) {
                    $this->addError( '运输方式'.$this->_order['sm_code'].'未支持购买保险' );
                }
                if( $this->isEmpty( $this->_order['charge'] ) ){
                    $this->addError( '成交总价必填' );
                }
                if( $this->isEmpty( $this->_order['insurance_code'] ) ) {
                    $this->addError( '保险项目不能为空' );
                } else {
                    //检查保险项目是否存在
                    $tmpItem = Service_OtherFee::getByField( $this->_order['insurance_code'], 'fee_code' );
                    if( empty($tmpItem) )
                        $this->addError( '保险项目不存在' );
                    else if($tmpItem['status'] != 1)
                        $this->addError( '保险不可用' );
                }

                if( $this->isEmpty( $this->_order['insurance_rate'] ) || $this->_order['insurance_rate'] < 1 || $this->_order['insurance_rate'] > 1.1) {
                    $this->addError( '投保金额率大于等于1，且小于等于1.1，有效小数位：2位' );
                }
            }
        }
    }
    //验证包裹信息
    protected function validatePackage() {
        if( $this->isEmpty( @$this->_order['piecesDetail'] ) ) {
            if( in_array( $this->_customer['customer_code'], array('E0693') ) && in_array( @$this->_order['sm_code'], array('HKDHL','BH-HKDHL')) ) {
                $this->addError( '缺少piecesDetail数据' );
            }
        } else {
            //检查产品重量尺寸 环球客户
            if( in_array( $this->_customer['customer_code'], array('E0693') ) && in_array( @$this->_order['sm_code'], array('HKDHL','BH-HKDHL')) ) {
                if( !is_array( $this->_order['piecesDetail'] ) ) {
                    $this->addError( 'piecesDetail数据异常' );
                } else {
                    $flag = true;
                    foreach( $this->_order['piecesDetail'] as $piece ) {
                        if( $this->isEmpty( @$piece['serialNumber'] ) ) {
                            $flag = false;
                            break;
                        }
                        if( !isset( $piece['weight'] ) || !is_numeric( $piece['weight'] ) || $piece['weight'] <= 0 ) {
                            $flag = false;
                            break;
                        }
                        if( !isset( $piece['length'] ) || !is_numeric( $piece['length'] ) || $piece['length'] <= 0 ) {
                            $flag = false;
                            break;
                        }
                        if( !isset( $piece['width'] ) || !is_numeric( $piece['width'] ) || $piece['width'] <= 0 ) {
                            $flag = false;
                            break;
                        }
                        if( !isset( $piece['height'] ) || !is_numeric( $piece['height'] ) || $piece['height']  <= 0 ) {
                            $flag = false;
                            break;
                        }
                    }
                    if( !$flag ) {
                        $this->addError( 'piecesDetail数据异常' );
                    }
                }
            }
        }
    }
    //验证产品
    protected function validateProduct() {
        if( $this->isEmpty( @$this->_order['order_product'] ) ) {
            $this->addError( Ec_Lang::getInstance()->getTranslate('ProductsMusBeSelected') );

        } else {
            if ( !is_array( $this->_order['order_product'] ) ) {
                $this->addError( '产品数据格式不正确' );
                return;
            }
            $parcelArr = array(
                'parcel_declared_value' => '',
                'parcel_contents'       => '',
                'parcel_quantity'       => '',
            );
            $total_purpose_declare = 0;
            $productHscode         = array();
            $withBatteryQuantity   = 0;//统计带电数量
            $others = array();
            if( $this->_order['sm_code'] == 'DHL-TH') {
                $others['state'] = @$this->_order['oab_state'];
                $others['city'] = @$this->_order['oab_city'];
            }
            foreach ( $this->_order['order_product'] as $v ) {
                $product_id  = $v['product_id'];
                $qty         = trim($v['qty']);
                $haveDeclare = false;

                $product = Service_Product::getByField($product_id, 'product_id');
                if (empty($product)) {
                    //忽略不存在的产品
                    $this->addError( "找不到ID为{$product_id}的产品." );
                    continue;
                }

                if( $product['product_status'] != "1" ){
                    $this->addError( $product['product_sku'].'未备案' );
                    continue;
                }

                $product_title_en = $v['product_title_en'];
                if (trim($product_title_en) == '') {
                    $this->addError( '英文名称不能为空' );
                } else {
                    /*if (in_array($orderRow['sm_code'],array('4PX-EU_PACKET','4PX-HKDHL','4PX-LYT_GH','4PX-RU_POST-LINK_RMAIL','4PX-SG_ordinary_mail+','4PX-Singapo_SPack_IMAIR')) && !preg_match("/^[0-9a-zA-Z\- ]+$/", trim($product_title_en))){
                        $err[] = "英文名称[".$product_title_en."] 只能包含英文字母、数字、空格和 - ";
                    }*/
                }

                /*if(in_array($product['hs_code'],array('8504409999','9023009000','8414809090','9018909010'))){
                $err[] = "海关编码是8504409999、9023009000、8414809090、9018909010的产品不能下单";
                }*/
                //限制运输带电产品的渠道
                if( $product['with_battery'] == 1 ) {
                    $allow = Service_OrderProcess::allowTakeBattery( @$this->_order['sm_code'], @$this->_order['oab_country_id'], $product['battery_type'], $others );
                    if( !$allow['allow'] ) {
                        $this->addError( '产品sku['. $product['product_sku'] .']:'.$allow['msg'] );
                    }
                    $withBatteryQuantity += ( is_numeric( $qty ) ? $qty : 0 );
                }

                if (!in_array($product['hs_code'], $productHscode)) {
                    $productHscode[] = $product['hs_code'];
                }

                $hs_code = Service_Hscodes::getByField($product['hs_code'], 'hs_code');
                if ($hs_code['hs_code_status'] == '-1') {
                    $this->addError( '产品sku:' . $product['product_sku'] . '海关编码'.$product['hs_code'].'已废止' );
                }

                if (isset($v['declare_price']) && $v['declare_price'] != "") {
                    $declare_price = trim($v['declare_price']);
                    $haveDeclare   = true;
                } else {
                    $declare_price = $product['product_declared_value'];
                }

                $purpose_declared_value = $v['op_declared_value'];

                if (!(preg_match("/^[0-9]+$/", $qty) && is_numeric($qty) && $qty > 0)) {
                    $this->addError( '产品sku'.$product['product_sku'].'数量必须大于0的整数-->['.$product['product_sku'].']' );
                    //continue;
                }
                if ($haveDeclare) {
                    if (!is_numeric($declare_price) || $declare_price <= 0) {
                        $this->addError( '产品sku'.$product['product_sku'].'申报单价必须大于0的数字-->['.$product['product_sku'].']' );
                    } /*elseif($declare_price<($product['product_declared_value']*0.8)){
                    $err[] = "产品sku{$product['product_sku']}申报单价必须不能低于产品的备案价值的80%";
                    }elseif($declare_price>$product['product_declared_value']*1.2){
                    $err[] = "产品sku{$product['product_sku']}申报单价不能高与产品备案价值的120%";
                    }*/
                }
                if (empty($purpose_declared_value) || !(is_numeric($purpose_declared_value) && $purpose_declared_value > 0)) {
                    $this->addError( '产品sku'.$product['product_sku'].'目的海关申报单价大于0的数字-->['.$product['product_sku'].']' );
                }
                if ($product['currency_code'] != "") {
                    $currency = Service_Currency::getByField($product['currency_code'], 'currency_code', "*");
                } else {
                    $currency = Service_Currency::getByField('RMB', 'currency_code', "*");
                }
                $parcelArr['parcel_declared_value'] += $declare_price * $qty * $currency['currency_rate'];
                $parcelArr['parcel_contents'] .= $product['product_title'] . "[" . $product['product_sku'] . "]x" . $qty . "<br/>";
                $parcelArr['parcel_quantity'] += $qty;
                $total_purpose_declare += $purpose_declared_value * $qty;

                switch( @$this->_order['sm_code'] ) {
                    case 'WWDHLP':
                    case 'WWDHLU':
                    case 'HKDHL':
                    case 'SZY-EUB':
                    case 'WG-EUB':
                    case 'CJ-DGMG':
                    case 'CJ-DGMP':
                    case 'CNDHL':
                        if ( empty( $product_title_en ) ) {
                            $this->addError( '产品sku'.$product['product_sku'].'英文品名必填-->['.$product['product_sku'].']' );
                        }
                        break;
                    case 'WG-EUB-EBAY':
                        if ( $this->isEmpty( @$v['ebay_item_id'] ) ) {
                            $this->addError( 'WG-EUB-EBAY产品sku['.$product['product_sku'].']ebay物品号必填' );
                        }
                        break;
                    case 'BH-USPS':
                    case 'BH-DPD':
                    case 'SF-RUPT':
                    case 'SF-RUPR':
                    case 'PNLN':
                    case 'PNLG':
                        if (  $product['product_title_en'] == '' && $product_title_en === '') {
                            $this->addError( $product['product_sku'].'英文品名不能为空' );

                        } else if( $product['product_title_en'] !== '' && $product_title_en === '') {
                            $keyData = array('顺丰国际电商专递'=>array('BH-DPD'),'顺丰国际小包挂号'=>array('PNLG','SF-RUPR'),'顺丰国际小包挂号（美国）'=>array('BH-USPS'),'顺丰国际小包平邮'=>array('PNLN','SF-RUPT'));
                            $keyMinGan = '';
                            foreach ($keyData as $key => $value) {
                                if(in_array( $this->_order['sm_code'], $value)){
                                    $keyMinGan = $key;
                                    break;
                                }
                            }
                            $filePath = APPLICATION_PATH . '/../data/file/mingan.xls';
                            $fileData = Common_Upload::readEXCEL($filePath);
                            $res      = 0;
                            if($keyMinGan){
                                foreach ($fileData as $k => $v) {
                                    if ($k == 0) {
                                        continue;
                                    }
                                    if($v[0]==$keyMinGan){
                                        $str = strpos(strtolower($v['product_title_en']), strtolower($v[1]));
                                        if (is_numeric($str)) {
                                            $res = 1;
                                            break;
                                        }
                                    }
                                }
                                if ($res) {
                                    $this->addError( '英文申报品名根据服务商要求，申报品名包含敏感词'.$v[1].'禁止运输，请选择其他运输方式！' );
                                }
                            }
                        }
                        break;
                    case "BH-HKDHL":
                        if ( empty( $product_title_en ) ) {
                            $this->addError(  '产品sku'.$product['product_sku'].'英文品名必填-->['.$product['product_sku'].']' );
                        }
                        if( $product['with_battery'] == "1" && $this->_countryIsRight ){
                            $filePath = APPLICATION_PATH . '/../data/file/BH-HKDHL-limit-country.xls';
                            $fileData = Common_Upload::readEXCEL($filePath);
                            $res      = 0;
                            foreach ($fileData as $k => $v) {
                                if ($k == 0) {
                                    continue;
                                }
                                $str = strpos(strtolower( $this->_country['country_name'] ), strtolower($v[0]));
                                if (is_numeric($str)) {
                                    $res = 1;
                                    break;
                                }
                            }
                            if ($res) {
                                $this->addError( '渠道【'. $this->_order['sm_code'] .'】国家【'.$this->_country['country_name'].'】限制运输带电产品['.$product['product_sku'].']' );
                            }else{
                                //允许带电的国家，也只能发内置电池产品
                                if($product['battery_type'] !=2){
                                    $this->addError( '渠道【'. $this->_order['sm_code'] .'】国家【'.$this->_country['country_name'].'】只能发内置电池产品['.$product['product_sku'].']' );
                                }
                            }
                        }
                        break;
                    case 'BH-NLPOST-TH':
                        if(mb_strlen($product_title_en) > 17){
                            $this->addError('渠道【'. $this->_order['sm_code'].',产品英文品名长度不能超过17个字符');
                        }
                        break;
                    case "DHL-BH03": 
                        if($product['with_battery'] == "1" && $this->_countryIsRight){
                            $filePath = APPLICATION_PATH . '/../data/file/DHL-BH-limit-country.xlsx';
                            $fileData = @Common_Upload::readEXCEL($filePath);
                            $res      = 0;
                            foreach ($fileData as $k => $v) {
                                if ($k == 0) {
                                    continue;
                                }
                                $str = strpos(strtolower( $this->_country['country_name'] ), strtolower($v[2]));
                                if (is_numeric($str)) {
                                    $res = 1;
                                    break;
                                }
                            }
                            if ($res) {
                                //土耳其（只能寄往Istanbul, Ankara, Izmir, Denizli, Adana, Kayseri, Gaziantep, Antalya, Bursa，其它城市不接受
                                //挪威的货件，不接受任何电池货物中转的城市有：Jan Mayen 、Svalbard
                                $allowWithBatteryCityByTR = array('Istanbul', 'Ankara', 'Izmir', 'Denizli', 'Adana', 'Kayseri', 'Gaziantep', 'Antalya', 'Bursa');
                                $notAllowWithBatteryCityByNO = array('Jan Mayen' ,'Svalbard');
                                if($this->_country['country_code'] == 'TR' && in_array($this->_order['oab_city'], $allowWithBatteryCityByTR)){
                                    //nothing to do
                                }elseif($this->_country['country_code'] == 'NO' && !in_array($this->_order['oab_city'], $notAllowWithBatteryCityByNO)){
                                    //nothing to do
                                }else{
                                    $this->addError( '渠道【'. $this->_order['sm_code'] .'】国家【'.$this->_country['country_name'].'】限制运输带电产品['.$product['product_sku'].']' );
                                }
                            }else{
                                //允许带电的国家，也只能发内置电池产品
                                if($product['battery_type'] !=2){
                                    $this->addError( '渠道【'. $this->_order['sm_code'] .'】国家【'.$this->_country['country_name'].'】只能发内置电池产品['.$product['product_sku'].']' );
                                }
                            }
                            
                        }
                        break;
                    case '4PX-HKDHL':
                        if( $product['with_battery'] == "1" && $this->_countryIsRight ){
                            $filePath = APPLICATION_PATH . '/../data/file/4PX-HKDHL-limit-country.xls';
                            $fileData = Common_Upload::readEXCEL($filePath);
                            $res      = 0;
                            foreach ($fileData as $k => $v) {
                                if ($k == 0) {
                                    continue;
                                }
                                $str = strpos(strtolower( $this->_country['country_name']), strtolower($v[0]));
                                if (is_numeric($str)) {
                                    $res = 1;
                                    break;
                                }
                            }
                            if ( $res ) {
                                $this->addError( '渠道【'.$this->_order['sm_code'].'】国家【'.$this->_country['country_name'].'】限制运输带电产品['. $product['product_sku'] .']' );
                            }
                        }
                        break;
                    case '4PX-OZXBTH':
                    case '4PX-LYT_GH':
                    case '4PX-EU_PACKET':
                    case '4PX-RU_POST-LINK_RMAIL':
                    case '4PX-Singapo_SPack_IMAIR':
                    case '4PX-SG_ordinary_mail+':
                    case 'LYT-SMTGH-X':
                        if (strpos(strtolower( $product['product_title_en']), 'gift') !== false || strpos(strtolower( $product['product_title_en']), 'sample') !== false){
                            $this->addError( '申报品名不能为GIFT，SAMPLE等笼统的品名' );
                        }
                        break;
                }
            }
            switch( @$this->_order['sm_code'] ) {
                case 'HKPG':
                    $purpose_totalDeclareHK = Service_Currency::converByCode("USD", "HKD", $total_purpose_declare);
                    if ($purpose_totalDeclareHK > 3000) {
                        $this->addError( 'HKPG总目的海关申报价值大于3000HKD');
                    }
                    break;
                case 'SZY-EUB':
                case 'WG-EUB':
                    if ( $total_purpose_declare > 30 && @$this->_order['oab_country_id'] == "82") {
                        $this->addError( 'SZY-EUB、WG-EUB到法国的目的海关申报单价不能超过30美元' );
                    }
                    break;
                case 'PNLN':
                    $purpose_totalDeclareEUR = Service_Currency::converByCode("USD", "EUR", $total_purpose_declare);
                    if ($purpose_totalDeclareEUR > 22) {
                        $this->addError( 'PNLN总目的海关申报价值不能超过22欧元' );
                    }
                    $re = Service_OrderProcess::validateMingan( $this->_order['order_product'], $this->_order['sm_code']);
                    if (!empty($re)) {
                        foreach ($re as $key => $s) {
                            $this->addError( '英文申报品名根据服务商要求，申报品名包含敏感词['.$s.']禁止运输，请选择其他运输方式！' );
                        }

                    }
                    break;
                case 'HKDHL':
                case 'BH-HKDHL':
                case '4PX-HKDHL':
                    if( @$this->_order['is_fba'] == 1 && @$this->_order['is_fba_tax'] == 0 ){
                        $this->addError( '当运输方式为HKDHL、BH-HKDHL、4PX-HKDHL、DHL-TH时，若其是否FBA为是，则目的地关税自动对应为预付' );
                    }
                    break;
                case 'ALBP-P':
                case 'BH-BPP':
                    $country = array('14','22','36','64','65','66','75','81','82','89','92','107','113','115','131','137','138','147','168','192','195','199','216','217','221','229','252');
                    if( in_array( @$this->_order['oab_country_id'], $country ) ) {
                        $purpose_totalDeclareEUR = Service_Currency::converByCode("USD", "EUR", $total_purpose_declare);
                        if ($purpose_totalDeclareEUR > 22) {
                            $this->addError( '总目的海关申报价值大于22EUR' );
                        }
                    }
                    break;
                case 'BH-3HPA':
                case 'BH-3H':
                case 'BH-UKECSLTYD':
                case 'BH-UKECSLTRM':
                case 'BH-CORREOS':
                case 'BH-19EURDPDDE':
                case 'BH-DAC':
                case 'BH-SL018':
                case 'BH-IESLTANPOST':
                    if( $total_purpose_declare> 165 ){
                        $this->addError(  '渠道['.$this->_order['sm_code'].']货物申报价值不能大于165美金。');
                    }
                    break;
                case 'ALBP-EU':
                case 'BH-BPEU':
                    if ( $total_purpose_declare > 22 ) {
                        $this->addError( '总目的海关申报价值大于22USD');
                    }
                    break;
                case "SF-RUPR":
                case "PNLG":
                case "SF-RUPT":
                case 'BH-USPS':
                case 'BH-DPD':
                    $re = Service_OrderProcess::validateMingan( $this->_order['order_product'], $this->_order['sm_code']);
                    if ( !empty( $re ) ) {
                        foreach ($re as $key => $s) {
                            $this->addError( '英文申报品名根据服务商要求，申报品名包含敏感词['.$s.']禁止运输，请选择其他运输方式！' );
                        }

                    }
                    break;
                case 'DHL-TH':
                    if( $withBatteryQuantity > 2 ){
                        $this->addError( '当运输方式为DHL-TH时，带电产品总数不能超过2');
                    }
                    if( @$this->_order['is_fba'] == 1 && @$this->_order['is_fba_tax'] == 0 ){
                        $this->addError( '当运输方式为HKDHL、BH-HKDHL、4PX-HKDHL、DHL-TH时，若其是否FBA为是，则目的地关税自动对应为预付' );
                    }
                    break;
                case 'CJ-DGMG':
                case 'CJ-DGMP':
				case 'DGM-US':
                    if( $withBatteryQuantity > 2 ){
                        $this->addError( '当运输方式为:CJ-DGMG/CJ-DGMP/DGM-US,订单内的带电产品数量不能超过2个' );
                    }
                    if(($total_purpose_declare > 800) && @$this->_order['sm_code'] == 'DGM-US'){
                        $this->addError('当运输方式为：DGM-US，订单总目的海关申报价不允许大于800USD的');
                    }
                    break;
				case 'BH-MMP':
				case 'BH-MMP1':
				    if( $withBatteryQuantity > 2 ){
				        $this->addError( '当运输方式为:BH-MMP,订单内的带电产品数量不能超过2个' );
				    }
				    break;
                case 'BH-SFEP':
                    if ( $total_purpose_declare >= 19&&$this->_order['oab_country_id']==252&&$this->_order['oab_email']=="") {
                        $this->addError( '申报大于等于19美金收件人电子邮件必填' );
                    }
                    break;
            }
            $totalHsCode = count( $productHscode );
            if( $this->_customerIsRight && $this->_customer['customer_platform'] == 'FX' && $totalHsCode > 50) {
                $this->addError( '海关系统不接收超过50个海关编码的订单' );
            } else if( ( !$this->_customerIsRight || ($this->_customerIsRight && $this->_customer['customer_platform'] != 'FX') ) &&  $totalHsCode > 20 ) {
                $this->addError( '海关系统不接收超过20个海关编码的订单');
            }
            $this->_parcel = $parcelArr;
        }
    }
}