Address.php 8.9 KB
Newer Older
R
init  
root 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
/**
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */
namespace fecshop\services\customer;
use Yii;
use yii\base\InvalidValueException;
use yii\base\InvalidConfigException;
use fec\helpers\CSession;
use fec\helpers\CUrl;
15
use fecshop\models\mysqldb\customer\Address as MyAddress;
R
update  
root 已提交
16
use fecshop\services\Service;
R
init  
root 已提交
17 18 19 20 21
/**
 * Address  child services
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
R
update  
root 已提交
22
class Address extends Service
R
init  
root 已提交
23
{
24 25
	protected  $currentCountry;
	protected  $currentState;
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
	
	protected function actionGetPrimaryKey(){
		
		return 'address_id';
	}
	/**
	 * @property $primaryKey | Int
	 * @return Object(MyCoupon)
	 * 通过id找到cupon的对象
	 */
	protected function actionGetByPrimaryKey($primaryKey){
		$one = MyAddress::findOne($primaryKey);
		$primaryKey = $this->getPrimaryKey();
		if($one[$primaryKey]){
			return $one;
		}else{
			return new MyAddress;
		}
	}
	
R
root 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
	protected function actionGetAddressByIdAndCustomerId($address_id,$customer_id){
		$primaryKey = $this->getPrimaryKey();
		$one = MyAddress::findOne([
			$primaryKey 	=> $address_id,
			'customer_id' 	=> $customer_id,
		]);
		
		if($one[$primaryKey]){
			return $one;
		}else{
			return false;
		}
	}
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	
	
	/**
	 * @property $filter|Array
	 * @return Array;
	 * 通过过滤条件,得到coupon的集合。
	 * example filter:
	 * [
	 * 		'numPerPage' 	=> 20,  	
	 * 		'pageNum'		=> 1,
	 * 		'orderBy'	=> ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
			'where'			=> [
				['>','price',1],
				['<=','price',10]
	 * 			['sku' => 'uk10001'],
	 * 		],
	 * 	'asArray' => true,
	 * ]
	 */
	protected function actionColl($filter=''){
		$query = MyAddress::find();
		$query = Yii::$service->helper->ar->getCollByFilter($query,$filter);
		$coll  = $query->all();
		if(!empty($coll)){
			foreach($coll as $k => $one){
				$coll[$k] = $one;
			}
		}
		//var_dump($one);
		return [
			'coll' => $coll,
			'count'=> $query->count(),
		];
	}
	
94 95 96 97
	protected function actionCurrentAddress(){
		
		
	}
98
	
99 100 101 102
	/**
	 * 
	 *
	 */
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	protected function actionCurrentAddressList(){
		$arr = [];
		if(!Yii::$app->user->isGuest){
			$identity = Yii::$app->user->identity;
			$customer_id = $identity['id'];
			if($customer_id ){
				$filter = [
					'numPerPage' 	=> 30,  	
					'pageNum'		=> 1,
					'orderBy'		=> ['updated_at' => SORT_DESC, ],
					'where'			=> [
						['customer_id' => $customer_id],
					],
					'asArray' => true,
				];
				$coll = $this->coll($filter);
119
				$ii = 0;
120 121 122 123 124 125 126 127 128
				if(is_array($coll['coll']) && !empty($coll['coll'])){
					foreach($coll['coll'] as $one){
						$address_id = $one['address_id'];
						$first_name = $one['first_name'];
						$last_name = $one['last_name'];
						$email = $one['email'];
						$telephone = $one['telephone'];
						$street1 = $one['street1'];
						$street2 = $one['street2'];
129
						$is_default = $one['is_default'];
130
						$city = $one['city'];
131 132 133
						
						//$state = Yii::$service->helper->country->getStateByContryCode($one['country'],$one['state']);
						$state = $one['state'];
134 135 136 137
						$zip = $one['zip'];
						$country = Yii::$service->helper->country->getCountryNameByKey($one['country']);
						$str = $first_name.' '.$last_name.' '.$email.' '.
								$street1.' '.$street2.' '.$city.' '.$state.' '.$country.' '.
138
								$zip.' '.$telephone;
139 140 141 142 143 144 145 146 147 148 149 150 151 152
						if($is_default == 1){
							$ii = 1;
						}
						$arr[$address_id] = [
							'address' => $str,
							'is_default'=>$is_default,
						];
					}
					if(!$ii){
						# 如果没有默认的地址,则取第一个当默认
						foreach($arr as $k=>$v){
							$arr[$k]['is_default'] = 1;
							break;
						}
153 154 155 156 157 158 159 160 161
					}
				}
			}
		}
		return $arr;
	}
	
	/**
	 * @property $one|Array , save one data .
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	 * @return  Int  保存coupon成功后,返回保存的id。
	 * example $one = [
		'first_name' => '',
		'last_name' => '',
		'email' => '',
		'company' => '',
		'telephone' => '',
		'fax' => '',
		'street1' => '',
		'street2' => '',
		'city' => '',
		'state' => '',
		'zip' => '',
		'country' => '',
		'customer_id' => '',
		'is_default' => '',
		
	 ];
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	 */
	protected function actionSave($one){
		$time = time();
		$primaryKey = $this->getPrimaryKey();
		$primaryVal = isset($one[$primaryKey]) ? $one[$primaryKey] : '';
		if($primaryVal){
			$model = MyAddress::findOne($primaryVal);
			if(!$model){
				Yii::$service->helper->errors->add('address '.$this->getPrimaryKey().' is not exist');
				return;
			}
		}else{
			
			$model = new MyAddress;
			$model->created_at = time();
			if(isset(Yii::$app->user)){
				$user = Yii::$app->user;
				if(isset($user->identity)){
					$identity = $user->identity;
					$person_id = $identity['id'];
					//$model->created_person = $person_id;
				}
			}
		}
		$model->updated_at = time();
		$saveStatus = Yii::$service->helper->ar->save($model,$one);
		
		if(!$primaryVal){
			$primaryVal = Yii::$app->db->getLastInsertID();
		}
		if($one['is_default'] == 1){
			$customer_id = $one['customer_id'];
			MyAddress::updateAll(
				['is_default'=>2],  # $attributes
				'customer_id = '.$customer_id.' and  '.$primaryKey.' != ' .$primaryVal      # $condition
				//[':customer_id' => $customer_id]
			);
		}
		return $primaryVal;
	}
	
	
	/**
	 * @property $ids | Int or Array   
	 * @return boolean
	 * 如果传入的是id数组,则删除多个
	 * 如果传入的是Int,则删除一个coupon
	 * 
	 */
	protected function actionRemove($ids,$customer_id=''){
		if(!$ids){
			Yii::$service->helper->errors->add('remove id is empty');
			return false;
		}
		if(is_array($ids) && !empty($ids)){
				foreach($ids as $id){
					$model = MyAddress::findOne($id);
					if(isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()]) ){
						if($customer_id){
							if($model['customer_id'] == $customer_id){
240 241
								
								$this->removeCartAddress($model['customer_id'],$id);
242 243 244 245 246
								$model->delete();
							}else{
								Yii::$service->helper->errors->add("remove address is not current customer address");
							}
						}else{
247
							$this->removeCartAddress($model['customer_id'],$id);
248
							$model->delete();
249
							
250 251 252 253 254 255 256 257 258 259 260 261 262
						}
						
					}else{
						Yii::$service->helper->errors->add("address Remove Errors:ID $id is not exist.");
						return false;
					}
				}
		}else{
			$id = $ids;
			$model = MyAddress::findOne($id);
			if(isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()]) ){
				if($customer_id){
					if($model['customer_id'] == $customer_id){
263
						$this->removeCartAddress($model['customer_id'],$id);
264 265 266 267 268 269
						$model->delete();
					}else{
						Yii::$service->helper->errors->add("remove address is not current customer address");
				
					}
				}else{
270
					$this->removeCartAddress($model['customer_id'],$id);
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
					$model->delete();
				}
						
			}else{
				Yii::$service->helper->errors->add("Address Remove Errors:ID:$id is not exist.");
				return false;
			}
		}
		# 查看是否有默认地址?如果该用户存在记录,但是没有默认地址,
		# 则查找用户是否存在非默认地址,如果存在,则取一个设置为默认地址
		if($customer_id){
			$addressOne = MyAddress::find()->asArray()
						->where(['customer_id' => $customer_id,'is_default' => 1])
						->one();
			if(!$addressOne['address_id']){
				$assOne = MyAddress::find()
						->where(['customer_id' => $customer_id])
						->one();
				if($assOne['address_id']){
					$assOne->is_default = 1;
					$assOne->updated_at = time();
					$assOne->save();
				}
			}
			
		}
		return true;
	}
	
300 301 302 303 304 305 306 307 308 309 310
	# 删除购物车中的address部分。
	protected function removeCartAddress($customer_id,$address_id){
		$cart = Yii::$service->cart->quote->getCartByCustomerId($customer_id);
		if(isset($cart['customer_address_id']) &&  !empty($cart['customer_address_id'])){
			if($cart['customer_address_id'] == $address_id){
				$cart->customer_address_id = '';
				$cart->save();
			}
		}
	}
	
311 312 313 314 315
	/**
	 * @property $customer_id | int 用户的id
	 * @return Array Or ''
	 * 得到customer的默认地址。
	 */
316
	/*
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	protected function actionGetDefaultAddress($customer_id = ''){
		if(!$customer_id){
			$identity = Yii::$app->user->identity;
			$customer_id = $identity['id'];
		}
		if($customer_id ){
			$addressOne = MyAddress::find()->asArray()
							->where(['customer_id' => $customer_id,'is_default' => 1])
							->one();
			if($addressOne['address_id']){
				return $addressOne;
			}else{
				$assOne = MyAddress::find()->asArray()
							->where(['customer_id' => $customer_id])
							->one();
				if($assOne['address_id']){
					return $assOne;
				}
			}
		}
	}
338
	*/
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
R
init  
root 已提交
359 360
	
}