RefundController.php 9.78 KB
<?php
class Home_RefundController extends QLib_Controller_Home_Action
{
	public function init()
	{
		$this->QLayoutTitle()->headTitle('退货申请');
		$this->_redirector = $this->_helper->getHelper('Redirector');
		$this->QLayoutScript()->offsetSetFile(60, $this->_js('jqueryui'))->offsetSetFile(61, $this->_js('global'))->offsetSetFile(62,$this->_js('user_main'))->offsetSetFile(63,$this->_js('currency'));
		$this->QLayoutLink()->offsetSetStylesheet(25, $this->_css('common'))->offsetSetStylesheet(21, $this->_css('myyoho'))->offsetSetStylesheet(22, $this->_css('jqueryuicss'))->offsetSetStylesheet(30, $this->_css('style_css'));
	}
	
	public function indexAction()
	{
		$this->QLayoutScript()->offsetSetFile(90, $this->_js('jquery.uploadify.min'))->offsetSetFile(91, $this->_js('QFSUpload'))->offsetSetFile(92, $this->_js('refund'));
		$order_code = $this->helpGquery('order_code');
		empty($order_code) ? $this->helpJsRedirect('参数错误') : true;
		$this->view->order = $order = QINOrders_Models_Client::getByOrdercode($this->uid, $order_code);
		empty($order) ? $this->helpJsRedirect('很抱歉没有该订单') : true;
		$order['status'] != 6 ? $this->helpJsRedirect('很抱歉!') : true;
		$order_id = $order['id'];
		$goodsList = QINOrders_Models_Goods_Client::getOrderGoodsById($order_id);
		$this->view->couponInfo = QINOrders_Models_Coupons_Client::getRelationByOrderId($order_id);
		
		$total_amount = $order['shipping_cost'];
		foreach ($goodsList as $k => $v)
		{
			$noVaildNum = QINOrders_Models_Apply_Refund_Goods_Client::getRefundNum($this->uid, $order_code, $v['erp_sku_id'], $v['goods_price']);
			$goodsList[$k]['noVaildNum'] = $noVaildNum;
			$goodsList[$k]['vaildNum'] = $goodsList[$k]['num'] - $noVaildNum > 0 ? $goodsList[$k]['num'] - $noVaildNum : 0;
			$productInfo = QINProduct_Models_Product_Client::getByProductId($v['product_id']);
			$skcInfo = QINProduct_Models_Goods_Client::getById($v['goods_id'], $v['product_id']);
			$productInfo['default_pic'] = QINLib_Images::get($skcInfo['color_image'], 'goodsimg', '60x60', 2);
			$productInfo['url'] = QINLib_Utils::getProductUrl($productInfo['id'], $skcInfo['id'], $productInfo['cn_alphabet']);
			$goodsList[$k]['product'] = $productInfo;
			$goodsList[$k]['product_skc'] = $skcInfo['product_skc'];
			if($goodsList[$k]['product']['max_sort_id'] == 6) {
				$this->view->hasShoes = 1;
			}
			$total_amount += $goodsList[$k]['num'] * $goodsList[$k]['goods_price'];
		}
		$this->view->total_amount = $total_amount;
		$this->view->goodsList = $goodsList;
	}
	
	/**
	 * 退货申请明细
	 */
	public function detailAction() {
		$id = $this->helpGparam('id');
		$this->view->refundData = QINOrders_Models_Apply_Refund_Client::getById($this->uid, $id);
		if(empty($this->view->refundData)) {
			$this->helpJsRedirect('没有找到该申请单');
		}
		$this->view->sourceOrder = QINOrders_Models_Orders_Client::getOrderByCode($this->view->refundData['source_order_code']);
		$refundGoodsList = QINOrders_Models_Apply_Refund_Goods_Client::getByRequestId($this->uid, $id);
		foreach($refundGoodsList as $k => $v) {
			$productInfo = QINProduct_Models_Product_Client::getBySkn($v['product_skn']);
			$refundGoodsList[$k]['product_name'] = $productInfo['product_name'];
		}
		$this->view->refundGoodsList = $refundGoodsList;
		
	}
	
	
	public function saveAction()
	{
		//$session = $this->helpSession('yohobuy_refunds')->__get('refund_' . $this->uid);
		//empty($session) ? $this->helpGo('http://www.yohobuy.com/home/orders?t=' . microtime(true), '申请正在处理中,请不要重复提交') : true;
		$this->QLayoutLink()->appendStylesheet($this->_css('buy'))->appendStylesheet($this->_css('style_css'))->appendStylesheet($this->_css('common_style'));
		$order_code = $this->helpGpost('order_code');
		$order_id = intval($this->helpGpost('order_id'));
		$goodsList = $this->helpGpost('goods');
		$object_type = $this->helpGpost('object_type');
		
		(empty($order_code) || $order_id < 1 || empty($goodsList)) ? $this->helpJsRedirect('非法提交') : true;
		
		$mc = QINLib_Utils::Mc();
		$status = $mc->add('order_code_' . $order_code, 1, 15);
		($status == false) ? $this->helpRefresh('/home/returns?t=' . microtime(true), '非法提交') : true;
		
		
		$orderInfo = QINOrders_Models_Orders_Client::getOrderByCode($order_code);
		empty($orderInfo) ? $this->helpJsRedirect('没有找到该订单') : true;
		//判断是否过期
		if((time()-$orderInfo['update_time']) > (QConfigs_Order_Config::$orderRefundLimitTime * 86400)){
			$this->helpJsRedirect('您的订单已经超过了退货有效期');
		}
		$return_amount_mode = $this->helpGpost('return_amount_mode');
		$remark = $this->helpGpost('remark');
		$payee_name = $this->helpGpost('payee_name', '');
		$area_code = $this->helpGpost('area_code', '');
		$bank_name= $this->helpGpost('bank_name', '');
		$bank_card = $this->helpGpost('bank_card', '');
		$alipay_account = $this->helpGpost('alipay_account', '');
		$alipay_name = $this->helpGpost('alipay_name', '');
		$areaData = QINPassport_Models_Area_Client::getAreaById($area_code);
		$refundOrder = array(
				'uid' => $this->uid,
				'init_order_code'=>$order_code,
				'source_order_code' => $order_code,
				'change_purchase_id' => 0,
				'return_amount_mode' => $return_amount_mode,
				'return_mode' => 10,  //寄回退货
				'return_shipping_cost' => 0,
				'return_amount' => 0,
				'is_return_coupon' => 'N',
				'return_yoho_coin' => 0,
				'remark' => $remark, 
				'payee_name' => $payee_name,
				'area_code' => $area_code,
				'province' => isset($areaData['province']) ? $areaData['province'] : '',
				'city' => isset($areaData['city']) ?$areaData['city']:'' ,
				'county' => isset($areaData['county'] ) ? $areaData['county'] : '',
				'bank_name' => $bank_name, 
				'bank_card' => $bank_card,
				'alipay_account' => $alipay_account, 
				'alipay_name' => $alipay_name, 
				'express_company' => '',
				'express_number' => '',
			    'reject' => 0,   //1为驳回
			    'erp_refund_id' => 0, 
				'status' => 0
		);
			
		$refundGoodsList = array();
		
		foreach ($goodsList as $k => $v)
		{
			if(empty($v['returned_reason']) || $v['returned_reason'] == 'all') {
				continue;
			}	
			if (($v['returned_reason'] == 4 || $v['returned_reason'] == 6 || $v['returned_reason'] == 8)){
				empty($v['evidence_images']) ? $this->helpRefresh('/home/refund?order_code='.$order_code.'&t=' . microtime(true),'请上传问题商品图片或联系客服') : true;
				if (count($v['evidence_images']) >= 5){
					$this->helpRefresh('/home/refund?order_code='.$order_code.'&t=' . microtime(true),'您最多只能上传四张图片');
				}
			}
			$refundNum = QINOrders_Models_Apply_Refund_Goods_Client::getRefundNum($this->uid, $order_code, $v['product_sku'], $v['last_price']);
			if($refundNum >= $v['num']) {
				$this->helpRefresh('/home/refund?order_code='.$order_code.'&t=' . microtime(true),'您申请的退货商品中存在不能进行换货的商品');
			}
			$refundData = array();
			$refundData['order_code'] = $order_code;
			$refundData['product_skn'] = $v['product_skn'];
			$refundData['product_skc'] = $v['product_skc'];
			$refundData['product_sku'] = $v['product_sku'];
			$refundData['goods_type'] = $v['goods_type'];
			$refundData['last_price'] = $v['last_price'];   //成交价
			$refundData['returned_reason'] = $v['returned_reason'];
			$refundData['remark'] = empty($v['remark']) ? '' : $v['remark'] ;
			$refundData['evidence_images'] = empty($v['evidence_images']) ? array() : $v['evidence_images'] ;
			$refundGoodsList[] = $refundData;
		}
		if(empty($refundGoodsList)) {
			$this->helpJsRedirect('请选择要退货的商品');
		}
		
		$erpClient = QErp_Order_Factory::factory();
		$erpResult = $erpClient->refundGoods($refundOrder, $refundGoodsList);
		if($erpResult['code'] == 200)
		{
			try {
				//本地保存
				$refundOrder['erp_refund_id'] = $erpResult['data']['returned_id'];
				$refundOrder['status'] = $erpResult['data']['returned_status'];
				$this->view->apply_id = $apply_id = QINOrders_Models_Apply_Refund_Client::setRefund($order_code, $this->uid, $refundOrder);
				$this->view->is_pass = $erpResult['data']['returned_status'];  //10为直接审核通过
				if(empty($apply_id)) {
					//创建失败
					throw  new Exception('创建本地退货单失败');
				}
				foreach ($refundGoodsList as $g) {
					$apply_goods_id = QINOrders_Models_Apply_Refund_Goods_Client::setRefundGoods($this->uid, $apply_id, $g['order_code'], $g['product_skn'], $g['product_skc'], 
						$g['product_sku'], $g['goods_type'], $g['last_price'], $g['returned_reason'], $g['remark'], $refundOrder['status']);
					if(!empty($g['evidence_images'])) {
						foreach($g['evidence_images'] as $img) {
							QINOrders_Models_Apply_Images_Client::setImages($apply_id, $apply_goods_id, 1, $img);
						}
					}
				}
			}catch(Exception $e) {
				$this->helpJsRedirect($e->getMessage());
			}
		}
		else
		{
			$this->helpSession('yohobuy_refunds')->__unset('refund_'. $this->uid);
			$this->helpJsRedirect($erpResult['message']);
		}
		
	}
	
	/**
	 * 更新快递信息
	 */
	public function setexpressAction() {
		$id = $this->helpGparam('id');
		$express_company = $this->helpGparam('express_company');
		$express_number = QLib_Utils_String::String()->replaceDangerCode($this->helpGparam('express_number'));
		$refundOrder = QINOrders_Models_Apply_Refund_Client::getById($this->uid, $id);
		if(empty($refundOrder)) {
			$this->helpJsonResult(412, "没有找到该退货申请");
		}
		$erpClient = QErp_Order_Factory::factory();
		$erpRet = $erpClient->setRefundExpressData($refundOrder['erp_refund_id'], $express_company, $express_number);
		if($erpRet['code'] != 200) {
			$this->helpJsonResult(500, $erpRet['message']);
		}
		$ret = QINOrders_Models_Apply_Refund_Client::updateExpress($this->uid, $id, $express_company, $express_number);
		if(empty($ret)) {
			$this->helpJsonResult(413, "设置快递单号失败");
		}else {
			$this->helpJsonResult(200);
		}
	}
}