RefundapplyController.php 13.1 KB
<?php

class Home_RefundapplyController extends QLib_Controller_Home_Action
{
	
	public function init()
	{
		$this->QLayoutTitle()->headTitle('退货申请');
		$this->view->uid = $this->uid = $this->helpIsLogin();
	}
	
	public function indexAction()
	{
	
	}
	
	/**
	 * 去换货页面
	 */
	public function barterAction()
	{
		$this->QLayoutScript()
		->offsetSetFile(30, $this->_js('user_main'))
		->offsetSetFile(31, $this->_js('global'))
		->offsetSetFile(32, $this->_js('formValidator'))
		->offsetSetFile(33, $this->_js('formValidatorRegex'))
		->offsetSetFile(34, $this->_js('refundapply'));
		
		$this->QLayoutLink()->appendStylesheet($this->_css('formValidator'));
		
		$productId = intval($this->helpGquery('product_id'));
		$productId < 1 ? $this->helpJsRedirect('产品的ID不能为空!') : TRUE;
		
		$order_code = $this->helpGquery('order_code');
		$order_code < 1 ? $this->helpJsRedirect('订单编码不能为空!') : TRUE;
		
		$this->view->order = $order = QModels_Orders_Client::getOrderByCode($order_code, $this->uid);
		empty($order) ? $this->helpJsRedirect('很抱歉!您要换货的订单不存在') : TRUE;
		($order['exchange'] == false) ? $this->helpJsRedirect('退换货商品在订单完成15天内才可以提交换货申请!') : TRUE;
		
		$goods_id = intval($this->helpGquery('goods_id'));
		$goods_id < 1 ? $this->helpJsRedirect('商品的ID不能为空!') : TRUE;
		
		$num = intval($this->helpGquery('num'));
		$num < 1 ? $this->helpJsRedirect('您的操作有误') : TRUE;
		$this->view->num = $num;
		$this->view->good = $good = QModels_Orders_Goods_Client::getOneGoodsById($this->uid, $goods_id, $order['id']);

		$this->view->product = $product = QModels_Product_Client::getProductFullInfo($productId);
		
		$this->view->storage = $storage = QModels_Product_Client::getStorage($productId);
		
		empty($storage['goods']) || $storage['storage'] == 0 ? $this->helpJsRedirect('没有库存,您可以进行退货或者联系客服') : TRUE;
		$this->view->size = $size = QINProduct_Models_Size_Client::getAllSizes();
	}
	
	/**
	 * 换货申请
	 */
	public function exchangeAction()
	{
		$orderCode = $this->helpGpost('orderCode');
		empty($orderCode) ? $this->helpJsRedirect('您没有选着换的商品') : TRUE;
		
		$goods = $this->helpGpost('goods');
		empty($goods) ? $this->helpJsRedirect('您没有选着换的商品') : TRUE;
		
		$size = $this->helpGpost('size');
		empty($size) ? $this->helpJsRedirect('您没有选中商品的尺码') : TRUE;
		
		$num = $this->helpGpost('num');
		empty($num) ? $this->helpJsRedirect('您没有选中商品数量') : TRUE;
		
		$oldnum = intval($this->helpGpost('oldnum'));
		$oldnum < 1 ? $this->helpJsRedirect('您的操作有误') : TRUE;
		
		$goods_id = intval($this->helpGpost('goods_id'));
		$goods_id < 1 ? $this->helpJsRedirect('您的选着要换的商品有误') : TRUE;
		
		$erp_sku_id = intval($this->helpGpost('erp_sku_id'));
		$erp_sku_id < 1 ? $this->helpJsRedirect('您的选着要换的商品有误') : TRUE;
		
		$orderId = intval($this->helpGpost('orderId'));
		$orderId < 1 ? $this->helpJsRedirect('订单ID不能为空!') : TRUE;
		
		$last_price = intval($this->helpGpost('last_price'));
		$last_price < 1 ? $this->helpJsRedirect('您的操作有误') : TRUE;
		
		$size_id = intval($this->helpGpost('size_id'));
		$size_id < 1 ? $this->helpJsRedirect('您的操作有误') : TRUE;
		
		$remark = $this->helpGpost('question');
		empty($remark) ? $this->helpJsRedirect("问题描述不能为空!") : TRUE;
		
		$sales_prices = $this->helpGpost('sales_prices');
		
		$last_price = $this->helpGpost('last_price');
		
		
		//判断商品数量
		$countnum = 0;
		foreach ($goods as $k=>$v)
		{
			$countnum += $num[$v];
		}
		$countnum > $oldnum ? $this->helpJsRedirect('您要换的商品数量不够,请重新输入!') : TRUE;
		
		//创建数组
		$exchangeList = array();
		foreach ($goods as $k => $v)
		{
			$size[$v] < 1 ? $this->helpJsRedirect('您没有选中商品的尺码!') : TRUE;
			$erp_sku = QModels_Product_Storage_Client::getOneStorage($v, $size[$v]);
			$exchangeList[$k]['erp_sku_id'] = $erp_sku['erp_sku_id'];
			$exchangeList[$k]['num'] = $num[$v];
		}
		//获得订单编码
		$Order = QModels_Orders_OrdersNum_Client::getOrderCode();
		$newOrderCode = $Order['order_code'];
		
		//地址信息
		$addrInfo = $this->GetParamers();

		//调用接口
		$erpOrder = QErp_Order_Factory::factory();		
		$result = $erpOrder->exchangeGoods($orderCode, $erp_sku_id, $num, $sales_prices, $last_price, $exchangeList, $newOrderCode, $addrInfo, $remark);
		if(QErp_Config::$connectionFailCode != $result['code']){
			$result['code'] != 200 ? $this->helpJsRedirect('很抱歉!您的商品不能换货,请与客服联系。电话:400-889-9646!') : TRUE;
		}
		
		//退商品
		$goods_info = QModels_Orders_Goods_Client::getOneGoodsById($this->uid, $goods_id, $orderId);
		//组织要退商品的数组
		$good = array(
			array(
				'order_goods_id' => $goods_info['id'], 
				'erp_sku_id' => $goods_info['erp_sku_id'], 
				'num' => $countnum
			)
		);
		//退商品
		$status = QModels_Orders_Returned_Client::setReturnedGoods($orderId, $good);
		$oldorder = QModels_Orders_Client::getOne($orderId);
		//组织订单信息
		$parameter = array(
			'order_code' => $result['orderCode'], 
			'order_type' => 3, 
			'shipping_type_id' => $oldorder['shipping_type_id'], 
			'user_name' => $addrInfo['addressee_name'], 
			'mobile' => $addrInfo['mobile'], 
			'phone' => $addrInfo['phone'],
			'area_code' => $addrInfo['area_code'], 
			'zip_code' => $addrInfo['zip_code'], 
			'amount' => '0.00', 
			'uid' => $this->uid, 
			'is_invoice' => $oldorder['is_invoice'], 
			'invoices_type' => $oldorder['invoices_type'], 
			'invoices_payable' => $oldorder['invoices_payable'], 
			'yoho_coin_num' => $oldorder['yoho_coin_num'], 
			'payment_type' => $oldorder['payment_type'], 
			'payment_status' => $oldorder['payment_status'],
			'remark' => $oldorder['remark'], 
			'shipping_cost' => $oldorder['shipping_cost'], 
			'express_id' => $oldorder['express_id'], 
			'receipt_time' => $oldorder['receipt_time'],
			'is_print_price' => $oldorder['is_print_price'],
			'yoho_give_coin' => 0
		);
		//组织订单商品信息
		$newgoods = array();
		foreach ($goods as $k => $v)
		{
			$goodinfo = QModels_Goods_Client::getOneById($v);
			$erp_sku = QModels_Product_Storage_Client::getOneStorage($v, $size[$v]);
			$size_info = QModels_Product_Size_Client::getOneSize($size[$v]);
			$newgoods[$k]['size_name'] = $size_info['size_name'];
			$product_price = QModels_Product_Price_Client::getPrice($goodinfo['product_id']);
			$product = QModels_Product_Client::getOneProduct($goodinfo['product_id']);
			$newgoods[$k]['erp_sku_id'] = $erp_sku_id;
			$newgoods[$k]['num'] = $num[$v];
			$newgoods[$k]['size_id'] = $size_info['id'];
			$newgoods[$k]['goods_price'] = $product_price['market_price'];
			$newgoods[$k]['goods_amount'] = $product_price['sales_price'];
			$newgoods[$k]['goods_id'] = $goodinfo['id'];
			$newgoods[$k]['brand_id'] = $product['brand_id'];
			$newgoods[$k]['product_id'] = $goodinfo['product_id'];
			$newgoods[$k]['goods_type'] = 1;
			$newgoods[$k]['color_id'] = $goodinfo['color_id'];
			$newgoods[$k]['color_name'] = $goodinfo['color_name'];
		}
		$newOrderId = QModels_Orders_Client::setOrder($parameter, $newgoods);
		$newOrderId < 1 ? $this->helpJsRedirect('您创建新的订单有误,请与客服联系。电话:400-889-9646!') : TRUE;
		
		
		//添加换商品组织换商品的数据
		$exchangeGoods = array();
		foreach ($goods as $k => $v)
		{
			$exchangeGoods[$k]['order_goods_id'] = $v;
			$erp_sku = QModels_Product_Storage_Client::getOneStorage($v, $size[$v]);
			$exchangeGoods[$k]['erp_sku_id'] = $erp_sku['erp_sku_id'];
			$exchangeGoods[$k]['num'] = $v['num'];
		}
		$status = QModels_Orders_Returned_Client::setReturnedGoods($orderId, $exchangeGoods, 2,$newOrderId);
		$status == TRUE ? TRUE : $this->helpJsRedirect('添加换货商品有误,请与客服联系。电话:400-889-9646!');
		
		//更新换货状态
		$status = QModels_Orders_Client::updateExchangeStatus($orderId, $this->uid, 1);
		$status == TRUE ? $this->helpRefresh('/home/orders','您的换货申请成功!') : $this->helpJsRedirect('修改订单状态失败!');
	}
	
	/**
	 * 得到地址信息
	 */
	public function GetParamers()
	{
		$parameter['addressee_name'] = QLib_Utils_String::String()->pregReplaceHtml($this->helpGpost('addressee_name'));
		empty($parameter['addressee_name']) ? $this->helpJsRedirect('收货人姓名不能为空') : TRUE;
		
		$parameter['address'] = QLib_Utils_String::String()->pregReplaceHtml($this->helpGpost('address'));
		
		empty($parameter['address']) ? $this->helpJsRedirect('收货地址不能为空') : TRUE;
		
		$parameter['area_code'] = $this->helpGpost('area_code');
		
		$parameter['zip_code'] = QLib_Utils_String::String()->pregReplaceHtml($this->helpGpost('zip_code'));
		empty($parameter['zip_code']) ? $this->helpJsRedirect('邮编不能为空') : TRUE;
		
		$parameter['mobile'] = QLib_Utils_String::String()->pregReplaceHtml($this->helpGpost('mobile'));
		$parameter['phone'] = QLib_Utils_String::String()->pregReplaceHtml($this->helpGpost('phone'));
		(empty($parameter['mobile']) && empty($parameter['phone'])) ? $this->helpJsRedirect('固话和手机号至少填一项') : TRUE;
	
		return $parameter;
	}
	
	/**
	 * 退货
	 * Enter description here ...
	 */
	public function refundAction()
	{
		$this->QLayoutScript()->offsetSetFile(30, $this->_js('refundapply'))->appendFile($this->_js('global'));
		
		$order_code = intval($this->helpGquery('order_code'));
		
		$order_code < 1 ? $this->helpJsRedirect('您的操作有误') : TRUE;
		
		$this->view->order = $order = QModels_Orders_Client::getOrderByCode($order_code, $this->uid);
		empty($order) ? $this->helpJsRedirect('很抱歉!您要退货的订单不存在') : TRUE;
		
		$order['refund'] == false ? $this->helpJsRedirect('退换货商品在订单完成七天内才可以提交退货申请!') : TRUE;
		
		$this->view->promotion = $promotion = QModels_Orders_Promotion_Client::getAllPromotionByOrder($order['id']);
		
		$markey = 0;
		foreach ($promotion as $k => $v){
			$promotions = QModels_Promotion_Commodity_Client::getOneById($v['promotion_id']);
			if ($promotions['favorable_type_id'] != 6){
				$markey += 1;
				break;
			}
		}
		$good = array();
		if ($markey < 1){
			$good = QModels_Orders_Goods_Client::getOrderGoods($order['id'], $order_code);
		}
		$this->view->good = $good;
	}
	
	/**
	 * 提交退货申请
	 * Enter description here ...
	 */
	public function refundgoodsAction(){
		
		$oldgood = $this->helpGpost('good');
		
		$goods_ids = $this->helpGpost('goods_ids');
		empty($goods_ids) && empty($oldgood) ? $this->helpJsRedirect('请选择要退货的商品') : TRUE;
			
		$last_prices = $this->helpGpost('last_price');
		empty($last_prices) && empty($oldgood) ? $this->helpJsRedirect('请选择要退货的商品') : TRUE;
		
		$order_code = $this->helpGpost('order_code');
		
		$nums = $this->helpGpost('num');
		empty($nums) && empty($oldgood) ? $this->helpJsRedirect('请选择要退货的商品') : TRUE;
		
		$this->view->order = $order = QModels_Orders_Client::getOrderByCode($order_code, $this->uid);
		empty($order) ? $this->helpJsRedirect('很抱歉!该订单不存在,无法进行退货。') : TRUE;
		if (!empty($oldgood)){
			$good = QModels_Orders_Goods_Client::getOrderGoods($order['id'], $order_code);	
		}else{
			$good =array();
			foreach ($goods_ids as $k => $v){
				$good[$k] = QModels_Orders_Goods_Client::getOneGoodsById($this->uid, $v, $order['id']);
			}
		} 
		
		$refundList =array();
		foreach ($good as $k => $v){
			$product_price = QModels_Product_Price_Client::getPrice($v['product_id']);
			$refundList[$k]['sales_price'] = $product_price['sales_price'];
			
			!empty($oldgood) ? $last_price = $v['goods_amount'] : $last_price = $last_prices[$v['goods_id']];
			$refundList[$k]['last_price'] = $last_price;
			
			$refundList[$k]['erp_sku_id'] = $v['erp_sku_id'];
			!empty($oldgood) ? $num = $v['num'] : $num = $nums[$v['goods_id']];
			$refundList[$k]['num'] = $num;
		}

		//调用接口
		$erpOrder = QErp_Order_Factory::factory();
		$result = $erpOrder->refundGoods($order['order_code'], $refundList);
		
		(QErp_Config::$connectionFailCode == $result['code']) ? $this->helpJsRedirect('很抱歉!您的退货申请失败。请与客服联系。电话:400-889-9646.') : true; 
		$result['code'] != 200 ? $this->helpJsRedirect('很抱歉!您的订单为促销商品,请与客服联系。电话:400-889-9646!') : TRUE;
		
		//添加退货商品
		$exchangeGoods = array();
		foreach ($good as $k=>$v)
		{
			$exchangeGoods[$k]['order_goods_id'] = $v['goods_id'];
			$erp_sku = QModels_Product_Storage_Client::getOneStorage($v['goods_id'], $v['size_id']);
			$exchangeGoods[$k]['erp_sku_id'] = $v['erp_sku_id'];
			!empty($oldgood) ? $num = $v['num'] : $num = $nums[$v['goods_id']];
			$exchangeGoods[$k]['num'] = $num;
		}
		
		$status = QModels_Orders_Returned_Client::setReturnedGoods($order['id'], $exchangeGoods);
		$status == TRUE ? TRUE : $this->helpJsRedirect('您的退货申请失败!');
		
		$status = QModels_Orders_Client::updateRefundStatus($order['id'], $this->uid, 1);
		$status == TRUE ? $this->helpRefresh('/home/orders','您的退货申请成功!') : $this->helpJsRedirect('您的退货申请失败!');
	}
}