<?php
class Service_CustomerAddressProcess
{
	private function validator($row){
		if($row['cab_type']=='1'){
			return $this->validatorReceive($row);	
		}elseif($row['cab_type']=='2'){
			return $this->validatorSend($row);
		}else{
			$error[] = "传递参数错误";
			return $error;
		}
	}
	/**
	 * @author william-fan
	 * @todo 用于检测输入收件人
	 */
	private function validatorReceive($row){
		//print_r($row);
		$error = array();
		if($row['cab_email']==''){
			$error[] = '邮箱不能为空';
		}else if(!preg_match("/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/",$row['cab_email'])){
		
			$error[] = '邮箱格式不对';
		}		
		/* else{
			//判断邮箱的重复性
			$checkEmail = Service_Customer::getByField($row['customer_email'], 'customer_email');
			if(!empty($checkEmail)){
				$error[] = '该邮箱已经被注册';
			}
		} */
		
		if($row['cab_type']==''){
			$error[] = '类型必填';
		}elseif($row['cab_type']!='1' && $row['cab_type']!='2'){
			$error[] = '类型必填';
		}
		if($row['cab_type']!='1' && $row['cab_type']!='2'){
			
		}
		if($row['cab_code']==''){
			$error[] = '客户参考代码必填';
		}
		if($row['cab_lastname']==''){
			$error[] = '客户姓名必填';
		}
		if($row['cab_country_id']=='253'){
			if($row['cab_state']==''||$row['cab_state']=='0'){
				$error[] = '州必填';
			}
		}
		if($row['cab_company']==''){
			$error[] = '公司名必填';
		}
		if($row['cab_country_id']==''){
			$error[] = '国家必填';
		}
		if($row['cab_cell_phone']==''){
			$error[] = '电话必填';
		}
		if($row['cab_street_address1']==''){
			$error[] = '地址1必填';
		} 
        
		return $error;
	}
	/**
	 * @author william-fan
	 * @todo 用于检测输入收件人
	 */
	private function validatorSend($row){
		//print_r($row);
		$error = array();
		if($row['cab_email']==''){
			$error[] = '邮箱不能为空';
		}else if(!preg_match("/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/",$row['cab_email'])){
	
			$error[] = '邮箱格式不对';
		}
		/* else{
		 //判断邮箱的重复性
		$checkEmail = Service_Customer::getByField($row['customer_email'], 'customer_email');
		if(!empty($checkEmail)){
		$error[] = '该邮箱已经被注册';
		}
		} */
	
		if($row['cab_type']==''){
			$error[] = '类型必填';
		}elseif($row['cab_type']!='1' && $row['cab_type']!='2'){
			$error[] = '类型必填';
		}
		if($row['cab_type']!='1' && $row['cab_type']!='2'){
				
		}
		if($row['cab_code']==''){
			$error[] = '客户参考代码必填';
		}
		if($row['cab_lastname']==''){
			$error[] = '客户姓名必填';
		}
		if($row['cab_country_id']=='253'){
			if($row['cab_state']==''||$row['cab_state']=='0'){
				$error[] = '州必填';
			}
		}
	    if($row['cab_company']==''){
			$error[] = '公司名必填';
		}
        if(strlen($row['cab_company'])>30){
            $error[] = '公司名长度不能大于30字符';
        }
		if($row['cab_country_id']==''){
			$error[] = '国家必填';
		}
		if($row['cab_cell_phone']==''){
			$error[] = '电话必填';
		}
		if($row['cab_street_address1']==''){
			$error[] = '地址1必填';
		}        
		if($row['cab_city']==''){
			$error[] = '城市必填';
		}
		if($row['cab_state']==''){
			$error[] = '省份必填';
		}         
        
        if(preg_match("/[\x7f-\xff]/",$row['cab_lastname'])) {
			$error[] = '客户姓名不能包含中文';
        }
        if(preg_match("/[\x7f-\xff]/",$row['cab_company'])) {
			$error[] = '公司名不能包含中文';
        }
        if(preg_match("/[\x7f-\xff]/",$row['cab_state'])) {
			$error[] = '省份不能包含中文';
        }
        if(preg_match("/[\x7f-\xff]/",$row['cab_city'])) {
			$error[] = '城市不能包含中文';
        }
        if(preg_match("/[\x7f-\xff]/",$row['cab_street_address1'])) {
			$error[] = '地址1不能包含中文';
        }
       
		return $error;
	}
	/**
	 * @author william-fan
	 * @todo 用于更新客户信息
	 */
	public function updateCustomerTransaction($data,$customerId){
		$result = array("ask"=>0,"message"=>"更改信息失败",'cab_id'=>'','error'=>array());
		$db = Common_Common::getAdapter();
		$db->beginTransaction();
		try{
				$cab_id	= $data['cab_id'];
				$customerAddress = Service_CustomerAddressBook::getByField($cab_id);
				if(empty($customerAddress)){
					$db->rollback();
					$result['message'] = '客户地址无效';
					return $result;
				}
				if($customerAddress['customer_id']!=$customerId){
					$result['message'] = '无权操作';
					$db->rollback();
					return $result;
				}
				$error = $this->validator($data);
				$conditionCustomerAddressBook = array(
						'cab_code'=>$data['cab_code'],
						'customer_id'=>$customerId,
				);
				$infos=Service_CustomerAddressBook::getByCondition($conditionCustomerAddressBook,'*');
				if(!empty($infos) && $infos[0]['cab_code']!=$data['cab_code']){
					$error[] = "客户参考代码必须唯一";
				}
				if(!empty($error)){
					$result['error'] = $error;
					$db->rollback();
					return $result;
				}
				$addressBook['cab_type']			= $data['cab_type'];
				$addressBook['cab_code']			= $data['cab_code'];
				$addressBook['cab_firstname']		= $data['cab_firstname'];
				$addressBook['cab_lastname']		= $data['cab_lastname'];
				$addressBook['cab_company']			= $data['cab_company'];
				$addressBook['cab_country_id']		= $data['cab_country_id'];
				//$addressBook['cab_phone']			= $data['cab_phone'];
				$addressBook['cab_cell_phone']		= $data['cab_cell_phone'];
				$addressBook['cab_fax']				= $data['cab_fax'];
				$addressBook['cab_state']			= $data['cab_state'];
				$addressBook['cab_city']			= $data['cab_city'];
				$addressBook['cab_postcode']		= $data['cab_postcode'];
				
				$addressBook['cab_street_address1']	= $data['cab_street_address1'];
				$addressBook['cab_email']			= $data['cab_email'];
				/* echo $cab_id;
				print_r($addressBook);
				exit; */
                /*
				if($data['cab_type']=='2'){
					//发件人信息
					$stateinfo = Service_Region::getByField($data['cab_state']);
					$cityinfo = Service_Region::getByField($data['cab_city']);
					$addressBook['cab_country_id'] 		= '49';
					$addressBook['cab_state']			= !empty($stateinfo)?$stateinfo['region_name']:'';
					$addressBook['cab_city']			= !empty($cityinfo)?$cityinfo['region_name']:'';
					$addressBook['cab_state_id']		= $data['cab_state'];
					$addressBook['cab_city_id']			= $data['cab_city'];
				}*/
				Service_CustomerAddressBook::update($addressBook, $cab_id);
				
				$db->commit();
				//$db->rollback();
				$result['ask']='1';
				$result['message']='更新资料成功';
				$result['cab_id'] = $cab_id;
				return $result;
			}catch(Exception $e){
				$db->rollback();
				$result = array("ask"=>0,"message"=>$e->getMessage(),'errorCode'=>$e->getCode());
			}
		return $result;
	}
	/**
	 * @author william-fan
	 * @todo 用于添加客户地址信息
	 */
	public function creataCustomerTransaction($data,$customerId){
		$result = array("ask"=>0,"message"=>"添加信息失败",'error'=>array(),'cab_id'=>'');
		$db = Common_Common::getAdapter();
		$db->beginTransaction();
		try{
			$customer = Service_Customer::getByField($customerId);
			if(empty($customer)){
				$db->rollback();
				$result['message'] = '客户无效';
				return $result;
			}
			if($customer['customer_status']!='2'){
				$result['message'] = '该客户暂不可用';
				$db->rollback();
				return $result;
			}
			$error = $this->validator($data);
			$conditionCustomerAddressBook = array(
				'cab_code'=>$data['cab_code'],
				'customer_id'=>$customerId,
			);
			$count=Service_CustomerAddressBook::getByCondition($conditionCustomerAddressBook,'count(*)');
			if($count>0){
				$error[] = '客户参考代码必须唯一';
			}
			if(!empty($error)){
				$result['error'] = $error;
				$db->rollback();
				return $result;
			}
			$addressBook['customer_code']		= $customer['customer_code'];
			$addressBook['customer_id']			= $customerId;
			$addressBook['cab_type']			= $data['cab_type'];
			$addressBook['cab_code']			= $data['cab_code'];
			$addressBook['cab_firstname']		= $data['cab_firstname'];
			$addressBook['cab_lastname']		= $data['cab_lastname'];
			$addressBook['cab_company']			= $data['cab_company'];
			$addressBook['cab_country_id']		= $data['cab_country_id'];
			//$addressBook['cab_phone']			= $data['cab_phone'];
			$addressBook['cab_cell_phone']		= $data['cab_cell_phone'];
			$addressBook['cab_fax']				= $data['cab_fax'];
			$addressBook['cab_state']			= $data['cab_state'];
			$addressBook['cab_city']			= $data['cab_city'];
			$addressBook['cab_postcode']		= $data['cab_postcode'];
			$addressBook['cab_street_address1']	= $data['cab_street_address1'];
			$addressBook['cab_email']			= $data['cab_email'];
			/*
            if($data['cab_type']=='2'){
				//发件人信息
				$stateinfo = Service_Region::getByField($data['cab_state']);
				$cityinfo = Service_Region::getByField($data['cab_city']);
				$addressBook['cab_country_id'] 		= '49';
				$addressBook['cab_state']			= !empty($stateinfo)?$stateinfo['region_name']:'';
				$addressBook['cab_city']			= !empty($cityinfo)?$cityinfo['region_name']:'';
				$addressBook['cab_state_id']		= $data['cab_state'];
				$addressBook['cab_city_id']			= $data['cab_city'];
			}*/
			/* echo $cab_id;
			 print_r($addressBook);
			exit; */
			$cab_id=Service_CustomerAddressBook::add($addressBook);
			$result['cab_id'] = $cab_id;
			$db->commit();
			//$db->rollback();
			$result['ask']='1';
			$result['message']='添加资料成功';
			return $result;
		}catch(Exception $e){
			$db->rollback();
			$result = array("ask"=>0,"message"=>$e->getMessage(),'errorCode'=>$e->getCode());
		}
		return $result;
	}
}