922 行
38 KiB
PHP
可执行文件
922 行
38 KiB
PHP
可执行文件
<?php
|
||
|
||
namespace Api\Controller;
|
||
|
||
use Common\Business\PayCoupon;
|
||
use Common\Business\Plug;
|
||
use Common\Business\Sell;
|
||
use Common\Business\SerialNumberGoods;
|
||
use Common\Model\RetailBillOrderModel;
|
||
use Common\Model\RetailBillReasonModel;
|
||
use Common\Model\RetailSellModel;
|
||
use LittleGift\Business\Order;
|
||
use Think\Controller;
|
||
use Common\Lib\Maiyatian;
|
||
use Common\Model\RetailMaiyatianOrderModel;
|
||
|
||
//[1.8.72]
|
||
|
||
/**
|
||
* Class CustomerController
|
||
* @package Api\Controller
|
||
* 商家处理售后订单的公共方法
|
||
*/
|
||
class CustomerController extends Controller
|
||
{
|
||
/**
|
||
* 订单详情
|
||
* bill_id 订单id
|
||
*/
|
||
public function order_info($parameter)
|
||
{
|
||
$bill_id = $parameter['bill_id'];
|
||
$SellModel = new RetailSellModel();
|
||
$BillReasonModel = new RetailBillReasonModel();
|
||
$fields = array(
|
||
'id',
|
||
"vip_discount",
|
||
"goods_id",
|
||
"goods_name",
|
||
"num",
|
||
"price",
|
||
"vip_price",
|
||
"original_price",
|
||
"plus_vip_price",
|
||
"all_money",
|
||
"attribute_state",
|
||
"sku_json_info",
|
||
"pricing_method",
|
||
"vip_discount",
|
||
"goods_img_id",
|
||
"integral_marketing",
|
||
"mem_integral",
|
||
"integral_dis_money",
|
||
"company",
|
||
'serial_number_state',
|
||
'sell_id',
|
||
);
|
||
$order_info = M("retail_bill_order as b")
|
||
->join("ls_retail_bill_online as o on b.id=o.bill_id")
|
||
->join("ls_retail_bill_discount as d on b.id=d.bill_id")
|
||
->where(array("b.id" => $bill_id))
|
||
->field("b.*,o.status,o.delivery_fee,o.user_name,o.user_phone,o.user_address,d.coupon_money,d.vip_dis_money,d.integral_dis_money,d.pay_coupon_fee")
|
||
->find();
|
||
$order_info['notes'] = '';
|
||
if ($order_info['refund_status'] == 1 && $order_info['order_type'] == 2 && $order_info['status'] == 1) {
|
||
if ($order_info['order_type'] == 2) {
|
||
$order_info['notes'] = '退款申请(客户还未收到商品)';
|
||
}
|
||
} else if ($order_info['refund_status'] == 1) {
|
||
if ($order_info['order_type'] == 2) {
|
||
$order_info['notes'] = '退款申请(客户已收到商品)';
|
||
} else {
|
||
$order_info['notes'] = '换货申请(客户已收到商品)';
|
||
}
|
||
}
|
||
$order_info['pay_coupon_fee'] = floatval($order_info['pay_coupon_fee']);
|
||
$reason = $BillReasonModel->getFind(array('bill_id' => $bill_id));
|
||
$reason['img_path'] = array();
|
||
if (!empty($reason['goods_img_id'])) {
|
||
$reason['img_path'] = array();
|
||
$reason_img = explode(",", $reason['goods_img_id']);
|
||
foreach ($reason_img as $value) {
|
||
$reason['img_path'][] = D('ImageGallery')->getImagePath($value);
|
||
}
|
||
}
|
||
$reason['apply_reason'] = $reason['apply_reason'] ? $reason['apply_reason'] : "";
|
||
$sellList = $SellModel->getSelect(array("bill_id" => $bill_id), $fields);
|
||
if (!empty($sellList)) {
|
||
$SerialClass = new SerialNumberGoods();
|
||
foreach ($sellList as &$v) {
|
||
if ($v['integral_marketing'] == 2) {
|
||
$v['price'] = floatval($v['price']) . '+' . $v['mem_integral'] . '积分';
|
||
$v['all_money'] = round($v['all_money'] + $v['integral_dis_money'], 2);
|
||
} else {
|
||
$v['price'] = floatval($v['price']);
|
||
}
|
||
$v['original_price'] = floatval($v['original_price']);
|
||
$v['vip_price'] = floatval($v['vip_price']);
|
||
$v['plus_vip_price'] = floatval($v['plus_vip_price']);
|
||
$v['all_money'] = floatval($v['all_money']);
|
||
$v['num'] = floatval($v['num']);
|
||
if ($v['attribute_state'] == 2) {
|
||
$sku_info = json_decode($v["sku_json_info"], true);
|
||
$v['sku_name'] = $sku_info['name'];
|
||
$v['sku_id'] = $sku_info['id'];
|
||
} else {
|
||
$v['sku_name'] = '';
|
||
$v['sku_id'] = 0;
|
||
}
|
||
$v['image'] = D('ImageGallery')->getImgPath(['id' => $v['goods_img_id']]);
|
||
//串号信息
|
||
if ($v['serial_number_state'] == 1) {
|
||
$sell_id = $SellModel->getFindField([
|
||
'sell_id' => $v['sell_id']
|
||
], 'id', true);
|
||
array_push($sell_id, $v['sell_id']);
|
||
$result = $SerialClass->sellSerialList([
|
||
'id' => ['in', $sell_id],
|
||
'type' => 2,
|
||
'files' => 'id,main_serial_number,auxiliary_serial_number_one,auxiliary_serial_number_two'
|
||
]);
|
||
$v['refund_serial_list'] = $result['data']['list'];
|
||
$result = $SerialClass->sellSerialList([
|
||
'id' => $v['id'],
|
||
'files' => 'id,main_serial_number,auxiliary_serial_number_one,auxiliary_serial_number_two'
|
||
]);
|
||
$v['retired_serial_list'] = $result['data']['list'] ? $result['data']['list'] : [];
|
||
}
|
||
}
|
||
}
|
||
$res['status'] = 1;
|
||
$res['order_info'] = $order_info;
|
||
$res['reason'] = $reason;
|
||
$res['sell_list'] = $sellList;
|
||
return $res;
|
||
}
|
||
|
||
|
||
/**
|
||
* 验证订单是否符合条件
|
||
* order_no 订单号
|
||
* refund_status 1申请售后服务 4待商家收货
|
||
*/
|
||
protected function VerificationOrder($parameter)
|
||
{
|
||
$merchant_id = $parameter['merchant_id'];
|
||
$store_id = $parameter['store_id'];
|
||
$order_no = $parameter['order_no'];
|
||
$refund_status = $parameter['refund_status'];
|
||
if (empty($order_no) || empty($merchant_id) || empty($store_id) || empty($refund_status)) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '参数缺失,请刷新重试';
|
||
return $res;
|
||
}
|
||
$RetailBillOrderModel = D("RetailBillOrder");
|
||
$where = array(
|
||
'merchant_id' => $merchant_id,
|
||
'store_id' => $store_id,
|
||
'order_no' => $order_no,
|
||
'order_type' => array('in', '2,3'),
|
||
'type' => 1,
|
||
'refund_status' => $refund_status
|
||
);
|
||
$bill_order = $RetailBillOrderModel->getFind($where);
|
||
if (empty($bill_order)) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '该订单状态已改变,请刷新重试';
|
||
return $res;
|
||
}
|
||
$SellModel = new RetailSellModel();
|
||
$sell_list = $SellModel->getSelect(array('bill_id' => $bill_order['id']), "id,serial_number_state");
|
||
if (empty($sell_list)) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '未找到该订单的商品信息';
|
||
return $res;
|
||
}
|
||
$res = array(
|
||
'status' => 1,
|
||
'msg' => '验证成功',
|
||
'bill_order' => $bill_order
|
||
);
|
||
return $res;
|
||
}
|
||
|
||
|
||
/**
|
||
* 验证审核
|
||
*/
|
||
protected function examine_state($parameter)
|
||
{
|
||
if (!in_array($parameter['type'], array(1, 2))) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '参数缺失';
|
||
return $res;
|
||
}
|
||
if ($parameter['type'] == 2 && (empty($parameter['reason']) || empty($parameter['reason_id']))) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '请输入驳回原因';
|
||
return $res;
|
||
}
|
||
$res = array(
|
||
'status' => 1,
|
||
'msg' => '验证成功',
|
||
);
|
||
return $res;
|
||
}
|
||
|
||
|
||
/**
|
||
* 审核操作
|
||
* @param $parameter
|
||
* [
|
||
* platform_type 商户平台类型 1默认系统 2微礼平台
|
||
* merchant_id
|
||
* store_id
|
||
* $order_no 订单号
|
||
* refund_status 待审核订单
|
||
* type 1同意申请 2拒绝申请
|
||
* goods_list 商品信息
|
||
* ]
|
||
* @return array|mixed
|
||
*/
|
||
public function examineOperation($parameter)
|
||
{
|
||
$examine_state = $this->examine_state($parameter);
|
||
if ($examine_state['status'] == 0) {
|
||
return $examine_state;
|
||
}
|
||
$info = $this->VerificationOrder($parameter);
|
||
if ($info['status'] == 0) {
|
||
return $info;
|
||
}
|
||
$bill_order = $info['bill_order'];
|
||
M()->startTrans();
|
||
$RetailBillOrderModel = new RetailBillOrderModel();
|
||
if ($parameter['type'] == 1) {
|
||
//同意
|
||
if (!empty($parameter['goods_list']) && $parameter['deliver_state'] == 0) {
|
||
//串号绑定
|
||
$SellClass = new Sell();
|
||
$goodsResult = $SellClass->confirmSellGoods([
|
||
'merchant_id' => $parameter['merchant_id'],
|
||
'store_id' => $parameter['store_id'],
|
||
'type' => 2,
|
||
'goods_list' => $parameter['goods_list']
|
||
]);
|
||
if ($goodsResult['status'] != 1) {
|
||
M()->rollback();
|
||
$goodsResult['msg'] = $goodsResult['message'];
|
||
return $goodsResult;
|
||
}
|
||
}
|
||
$parameter['bill_order'] = $bill_order;
|
||
$agree = $this->examine_agree($parameter);
|
||
if ($agree['status'] == 0) {
|
||
M()->rollback();
|
||
return $agree;
|
||
}
|
||
$data = $agree['data'];
|
||
$maiyatianOrder = (new RetailMaiyatianOrderModel)->where(['order_id' => $bill_order['refund_no']])->find();
|
||
if ($maiyatianOrder) {
|
||
$maiyatian = new Maiyatian($bill_order['store_id']);
|
||
$res['orderCanceled'] = $maiyatian->orderCanceled([
|
||
'order_id' => $bill_order['refund_no'],
|
||
'reason' => '取消订单',
|
||
'reason_code' => 2
|
||
]);
|
||
}
|
||
} else if ($parameter['type'] == 2) {
|
||
//拒绝
|
||
$data['refund_status'] = 6; //驳回
|
||
}
|
||
$save = $RetailBillOrderModel->updateOrder(array('id' => $bill_order['id']), $data);
|
||
if ($save) {
|
||
if ($parameter['type'] == 2) {
|
||
//驳回
|
||
$parameter['bill_order'] = $bill_order;
|
||
$res = $this->reject_apply($parameter);
|
||
} else {
|
||
$res['status'] = 1;
|
||
$res['msg'] = '操作成功';
|
||
}
|
||
} else {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '暂无修改';
|
||
}
|
||
if ($res['status'] == 0) {
|
||
M()->rollback();
|
||
} else {
|
||
M()->commit();
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
|
||
/**
|
||
* 审核同意判断
|
||
* @param $parameter
|
||
* [
|
||
* platform_type 商户平台类型 1默认系统 2微礼平台
|
||
* ]
|
||
* @return mixed\
|
||
*/
|
||
protected function examine_agree($parameter)
|
||
{
|
||
$platform_type = $parameter['platform_type'] ? $parameter['platform_type'] : 1;
|
||
$RetailBillOnlineModel = D("RetailBillOnline");
|
||
$bill_order = $parameter['bill_order'];
|
||
$online_status = $RetailBillOnlineModel->getFindField(array('bill_id' => $bill_order['id']), "status");
|
||
if ($bill_order['order_type'] == 2) {
|
||
switch ($online_status) {
|
||
case 1:
|
||
$data['refund_status'] = 5; //商家已收货待商家发货或退款
|
||
break;
|
||
case 2:
|
||
$res['status'] = 0;
|
||
$res['msg'] = "客户未确认收货";
|
||
return $res;
|
||
break;
|
||
case 3:
|
||
if ($parameter['deliver_state'] == 1) {
|
||
$data['refund_status'] = 3; //待客户发货
|
||
} else {
|
||
$data['refund_status'] = 5; //商家已收货待商家发货或退款
|
||
}
|
||
break;
|
||
}
|
||
} else if ($bill_order['order_type'] == 3) {
|
||
if ($parameter['deliver_state'] == 1) {
|
||
$data['refund_status'] = 3; //需要客户发货
|
||
} else {
|
||
$data['refund_status'] = 5; //商家已收货待商家发货或退款
|
||
}
|
||
}
|
||
if ($bill_order['order_type'] == 2 && $online_status == 1 && $platform_type == 2) {
|
||
//第三方平台取消订单
|
||
$BillOrderModel = new RetailBillOrderModel();
|
||
$third_order = $BillOrderModel->getFindField([
|
||
'order_no' => $bill_order['refund_no']
|
||
], 'third_order');
|
||
$ThirdClass = new Order();
|
||
$ThirdResult = $ThirdClass->orderCancel([
|
||
'merchant_id' => $parameter['merchant_id'],
|
||
'third_order' => $third_order
|
||
]);
|
||
if ($ThirdResult['status'] == 0) {
|
||
$result['status'] = 0;
|
||
$result['msg'] = 'Q3 ERP平台取消订单失败' . $ThirdResult['message'];
|
||
return $result;
|
||
}
|
||
} else if (($bill_order['order_type'] == 2 && $online_status == 3 && $parameter['deliver_state'] == 0) || ($bill_order['order_type'] == 3 && $online_status == 1 && $parameter['deliver_state'] == 0)) {
|
||
//同意并且是不需要客户发货的商家库存默认增加
|
||
if ($platform_type == 2 && $bill_order['order_type'] == 2) {
|
||
//第三方平台退货订单
|
||
$ThirdOrderClass = new Order();
|
||
$thirdResult = $ThirdOrderClass->returnGoods([
|
||
'merchant_id' => $parameter['merchant_id'],
|
||
'store_id' => $parameter['store_id'],
|
||
'order_no' => $bill_order['order_no']
|
||
]);
|
||
if ($thirdResult['status'] == 0) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '暂无修改' . $thirdResult['message'];
|
||
return $res;
|
||
}
|
||
}
|
||
$param = array(
|
||
'bill_order' => $bill_order
|
||
);
|
||
$stock_state = A("Api/Inventory")->CustomerStockInc($param);
|
||
if ($stock_state['status'] == 0) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '操作失败';
|
||
return $res;
|
||
}
|
||
}
|
||
$res['status'] = 1;
|
||
$res['msg'] = '成功';
|
||
$res['data'] = $data;
|
||
return $res;
|
||
}
|
||
|
||
/**
|
||
* 驳回修改原订单
|
||
*/
|
||
protected function reject_apply($parameter)
|
||
{
|
||
$RetailBillOrderModel = D("RetailBillOrder");
|
||
$RetailBillOnlineModel = D("RetailBillOnline");
|
||
$RetailBillReason = D("RetailBillReason");
|
||
$RetailSellModel = D("RetailSell");
|
||
$bill_order = $parameter['bill_order'];
|
||
if ($bill_order['order_type'] == 2) {
|
||
$online_status = $RetailBillOnlineModel->getFindField(array('bill_id' => $bill_order['id']), "status");
|
||
if ($online_status == 1) {
|
||
$RetailBillOrderModel->updateOrder(array("order_no" => $bill_order['refund_no']), array("refund_status" => 0));
|
||
}
|
||
}
|
||
$RetailBillReason->updateOrder(array('id' => $parameter['reason_id']), array('reject_reason' => $parameter['reason']));
|
||
$r_sell_list = $RetailSellModel->getSelect(array('bill_id' => $bill_order['id']), "sell_id,num");
|
||
if ($r_sell_list) {
|
||
foreach ($r_sell_list as $val) {
|
||
$RetailSellModel->toSetDec(array('id' => $val['sell_id']), "wait_refund_num", $val['num']);
|
||
}
|
||
$res['status'] = 1;
|
||
$res['msg'] = '操作成功';
|
||
} else {
|
||
$RetailBillOrderModel->updateOrder(array('id' => $bill_order['id']), array('refund_status' => 1));
|
||
$res['status'] = 0;
|
||
$res['msg'] = '暂无修改';
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
|
||
/**
|
||
* 商家收货
|
||
* @param $parameter
|
||
* [
|
||
* platform_type 商户平台类型 1默认系统 2微礼平台
|
||
* merchant_id
|
||
* store_id
|
||
* order_no 订单号
|
||
* refund_status 1申请售后服务 4待商家收货
|
||
* ]
|
||
* @return array|mixed
|
||
*/
|
||
public function BusinessReceiving($parameter)
|
||
{
|
||
$info = $this->VerificationOrder($parameter);
|
||
if ($info['status'] == 0) {
|
||
return $info;
|
||
}
|
||
M()->startTrans();
|
||
if (!empty($parameter['goods_list'])) {
|
||
//串号绑定
|
||
$SellClass = new Sell();
|
||
$goodsResult = $SellClass->confirmSellGoods([
|
||
'merchant_id' => $parameter['merchant_id'],
|
||
'store_id' => $parameter['store_id'],
|
||
'type' => 2,
|
||
'goods_list' => $parameter['goods_list']
|
||
]);
|
||
if ($goodsResult['status'] != 1) {
|
||
M()->rollback();
|
||
$goodsResult['msg'] = $goodsResult['message'];
|
||
return $goodsResult;
|
||
}
|
||
}
|
||
$bill_order = $info['bill_order'];
|
||
$RetailBillOrderModel = new RetailBillOrderModel();
|
||
$save = $RetailBillOrderModel->updateOrder(array('id' => $bill_order['id']), array('refund_status' => 5));
|
||
if ($save) {
|
||
/*修改库存*/
|
||
if ($parameter['platform_type'] == 2 && $bill_order['order_type'] == 2) {
|
||
//第三方平台
|
||
$ThirdOrderClass = new Order();
|
||
$thirdResult = $ThirdOrderClass->returnGoods([
|
||
'merchant_id' => $parameter['merchant_id'],
|
||
'store_id' => $parameter['store_id'],
|
||
'order_no' => $bill_order['order_no']
|
||
]);
|
||
if ($thirdResult['status'] == 0) {
|
||
M()->rollback();
|
||
$res['status'] = 0;
|
||
$res['msg'] = '暂无修改';
|
||
return $res;
|
||
}
|
||
}
|
||
$param = array(
|
||
'bill_order' => $bill_order
|
||
);
|
||
$stock_state = A("Api/Inventory")->CustomerStockInc($param);
|
||
if ($stock_state['status'] == 1) {
|
||
M()->commit();
|
||
$res['status'] = 1;
|
||
$res['msg'] = '操作成功';
|
||
} else {
|
||
M()->rollback();
|
||
$res['status'] = 0;
|
||
$res['msg'] = '暂无修改';
|
||
}
|
||
} else {
|
||
M()->rollback();
|
||
$res['status'] = 0;
|
||
$res['msg'] = '暂无修改';
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
|
||
/**
|
||
* @param $parameter
|
||
* [
|
||
* 取消订单 * 申请退款换货(取消订单) 2020/3/5
|
||
* @param order_no 订单号
|
||
* @param refund_list 退款sell列表
|
||
* @param apply_reason 申请退款换货原因
|
||
* @param type 类型 1取消订单 2退款(订单已完成) 3换货(订单已完成)
|
||
* @param user_address 换货的收货地址
|
||
* @param $user_name 姓名
|
||
* @param $user_phone 手机号
|
||
* @param $reason_img 图片id,拼接
|
||
* @param user_type 1.客户 2商家
|
||
* platform_type 商户平台类型 1默认系统 2微礼平台
|
||
* ]
|
||
* @return array|mixed
|
||
*/
|
||
public function order_cancel($parameter)
|
||
{
|
||
$order_no = $parameter['order_no'];
|
||
$refund_list = $parameter['refund_list'];
|
||
$type = $parameter['type'];
|
||
$apply_reason = $parameter['apply_reason'];
|
||
$reason_img = $parameter['reason_img'];
|
||
$user_address = $parameter['user_address'];
|
||
$user_name = $parameter['user_name'];
|
||
$user_phone = $parameter['user_phone'];
|
||
$uid = $parameter['uid'];
|
||
$merchant_id = $parameter['merchant_id'];
|
||
$store = $parameter['store'];
|
||
$user_type = $parameter['user_type'] ? $parameter['user_type'] : 1;
|
||
if (empty($order_no) || empty($type)) {
|
||
$result['status'] = 0;
|
||
$result['msg'] = '缺少参数';
|
||
return $result;
|
||
}
|
||
if (!in_array($type, array(1, 2, 3))) {
|
||
$result['status'] = 0;
|
||
$result['msg'] = '操作类型有误,请刷新重试';
|
||
return $result;
|
||
}
|
||
if (empty($apply_reason) && ($type == 2 || $type == 3)) {
|
||
$result['status'] = 0;
|
||
$result['msg'] = '请输入售后原因';
|
||
return $result;
|
||
}
|
||
if ($type == 3 && (empty($user_address) || empty($user_name) || empty($user_phone))) {
|
||
$result['status'] = 0;
|
||
$result['msg'] = '请选择收货地址';
|
||
return $result;
|
||
}
|
||
switch ($type) {
|
||
case 2:
|
||
//no break
|
||
case 3:
|
||
//退款
|
||
if (empty($refund_list)) {
|
||
$result['status'] = 0;
|
||
$result['msg'] = '缺少参数';
|
||
return $result;
|
||
}
|
||
$refund_list = htmlspecialchars_decode($refund_list);
|
||
$refund_list = json_decode($refund_list, true);
|
||
if (!is_array($refund_list) || count($refund_list) == 0) {
|
||
$result['status'] = 0;
|
||
$result['msg'] = '退款商品信息有误';
|
||
return $result;
|
||
}
|
||
break;
|
||
}
|
||
$BillModel = D('RetailBillOrder');
|
||
$bMap = array(
|
||
$BillModel::ORDER_NO => strval($order_no),
|
||
$BillModel::MERCHANT_ID => $merchant_id,
|
||
$BillModel::UID => $uid,
|
||
$BillModel::TYPE => 1,
|
||
$BillModel::ORDER_TYPE => 1,
|
||
$BillModel::REFUND_STATUS => array('in', '0,1')
|
||
);
|
||
$billOrder = $BillModel->getFind($bMap);
|
||
if (empty($billOrder)) {
|
||
$result['status'] = 0;
|
||
$result['msg'] = '未找到该订单';
|
||
return $result;
|
||
}
|
||
$customer_order = $BillModel->getFind([
|
||
'merchant_id' => $merchant_id,
|
||
'refund_no' => $billOrder['order_no'],
|
||
'pay_state' => 1,
|
||
'refund_status' => 1
|
||
]);
|
||
if (!empty($customer_order)) {
|
||
$result['status'] = 0;
|
||
$result['msg'] = '该订单已申请售后,请完成后再次申请';
|
||
return $result;
|
||
}
|
||
if (C('IS_SET_REDIS')) {
|
||
$ApiRedis = new \Addons\Api\RedisController();
|
||
$redis_key = "customer_" . $billOrder['order_no'];
|
||
if ($ApiRedis::$redis->exists($redis_key)) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = "正在操作该订单,请稍后重试";
|
||
return $res;
|
||
}
|
||
$ApiRedis::$redis->setex($redis_key, 10, $billOrder['order_no']);
|
||
}
|
||
$store_id = $billOrder['store_id'];
|
||
if ($type == 2 || $type == 3) {
|
||
$overtime = overtime($store, $billOrder, 1);
|
||
if ($overtime['status'] == 0) {
|
||
if (C('IS_SET_REDIS')) {
|
||
$ApiRedis::$redis->delete($redis_key);
|
||
}
|
||
//不满足售后服务时间范围
|
||
return $overtime;
|
||
}
|
||
}
|
||
switch ($type) {
|
||
case 1:
|
||
case 2:
|
||
if ($billOrder[$BillModel::UPGRADE_STATE] == 1) {
|
||
if (C('IS_SET_REDIS')) {
|
||
$ApiRedis::$redis->delete($redis_key);
|
||
}
|
||
$result['status'] = 0;
|
||
$result['msg'] = '升级为PLUS会员的订单无法退款';
|
||
return $result;
|
||
}
|
||
// 判断是否送次卡
|
||
if (!empty($billOrder[$BillModel::VID])) {
|
||
$judge_refund = A('PayApi/Refund')->judgeRefund($billOrder);
|
||
if ($judge_refund['status'] == 0) {
|
||
if (C('IS_SET_REDIS')) {
|
||
$ApiRedis::$redis->delete($redis_key);
|
||
}
|
||
return $judge_refund;
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
// 判断是否为购买次卡
|
||
$CardOrder = D('RetailCardOrder');
|
||
$co_map = array(
|
||
$CardOrder::MERCHANT_ID => $billOrder[$BillModel::MERCHANT_ID],
|
||
$CardOrder::STORE_ID => $billOrder[$BillModel::STORE_ID],
|
||
$CardOrder::VIP_ID => $billOrder[$BillModel::VID],
|
||
$CardOrder::BILL_ID => $billOrder[$BillModel::ID],
|
||
);
|
||
$card_order = $CardOrder->getFind($co_map);
|
||
if (!empty($card_order)) {
|
||
if (C('IS_SET_REDIS')) {
|
||
$ApiRedis::$redis->delete($redis_key);
|
||
}
|
||
$result['status'] = 0;
|
||
$result['msg'] = '该订单为购买次卡活动订单,无法退款换货';
|
||
return $result;
|
||
}
|
||
$BillOnlineModel = D('RetailBillOnline');
|
||
$oMap = array(
|
||
$BillOnlineModel::BILL_ID => $billOrder[$BillModel::ID]
|
||
);
|
||
$billOnline = $BillOnlineModel->getFind($oMap);//查询线上订单表
|
||
$BillDisModel = D('RetailBillDiscount');
|
||
$BillOnlineModel = D('RetailBillOnline');
|
||
$SellModel = D('RetailSell');
|
||
if ($billOnline['order_genre'] == 4) {
|
||
$advance_info = D("RetailBillAdvance")->getFind(array("bill_id" => $billOrder['id']));
|
||
}
|
||
switch ($type) {
|
||
case 1:
|
||
if (($billOnline['order_genre'] != 4 || ($billOnline['order_genre'] == 4 && $advance_info['state'] == 2)) && $billOnline[$BillOnlineModel::STATUS] != 1) {
|
||
$result['status'] = 0;
|
||
$result['msg'] = '该订单已发货,不能取消';
|
||
if (C('IS_SET_REDIS')) {
|
||
$ApiRedis::$redis->delete($redis_key);
|
||
}
|
||
return $result;
|
||
}
|
||
$refundSellList = $SellModel->getSelect(array($SellModel::BILL_ID => $billOrder[$BillModel::ID]));
|
||
$new_discount = $BillDisModel->getFind(array($BillDisModel::BILL_ID => $billOrder[$BillModel::ID]));
|
||
$refundData = array(
|
||
'allMoney' => round($billOrder[$BillModel::ALL_MONEY] - $billOnline[$BillOnlineModel::DELIVERY_FEE] + $new_discount['pay_coupon_fee'], 2),
|
||
'refundSellList' => $refundSellList,
|
||
'new_discount' => $new_discount,
|
||
'status' => 1
|
||
);
|
||
if ($billOnline['order_genre'] == 4 && $advance_info['state'] == 1) {
|
||
//退定金
|
||
$refundData['allMoney'] = round($advance_info['all_earnest_price'], 2);
|
||
}
|
||
break;
|
||
case 2:
|
||
case 3:
|
||
if ($billOnline[$BillOnlineModel::STATUS] != 3) {
|
||
if (C('IS_SET_REDIS')) {
|
||
$ApiRedis::$redis->delete($redis_key);
|
||
}
|
||
$result['status'] = 0;
|
||
$result['msg'] = '该订单未完成,不能进行退款或换货';
|
||
return $result;
|
||
}
|
||
$rMap = array(
|
||
$BillModel::REFUND_NO => strval($order_no),
|
||
$BillModel::STORE_ID => $store_id,
|
||
$BillModel::MERCHANT_ID => $merchant_id,
|
||
$BillModel::UID => $uid,
|
||
$BillModel::TYPE => 1,
|
||
$BillModel::ORDER_TYPE => array('in', '2,3'),//退款订单 换货订单
|
||
$BillModel::REFUND_STATUS => ['in', [1, 3, 4, 5]],//申请退款换货的订单 换货中、待用户发货 换货中、待用户发货 商家已收货、待商家发货或退款
|
||
$BillModel::PAY_STATE => 1,//已支付
|
||
);
|
||
$refundOrder = $BillModel->getFind($rMap);
|
||
if (!empty($refundOrder)) {
|
||
if (C('IS_SET_REDIS')) {
|
||
$ApiRedis::$redis->delete($redis_key);
|
||
}
|
||
$result['status'] = 0;
|
||
$result['msg'] = '已经存在该订单退款或换货操作,完成后才可继续操作';
|
||
return $result;
|
||
}
|
||
$refundData = getRefundList($refund_list, $billOrder[$BillModel::ID], $type);
|
||
break;
|
||
}
|
||
if ($refundData['status'] == 0) {
|
||
if (C('IS_SET_REDIS')) {
|
||
$ApiRedis::$redis->delete($redis_key);
|
||
}
|
||
return $refundData;
|
||
}
|
||
$allMoney = $refundData['allMoney'];//退款换货总金额
|
||
$refund_list = $refundData['refundSellList'];//退款换货列表
|
||
$new_discount = $refundData['new_discount'];//新优惠数据
|
||
//验证指定商品代金券不能部分退款
|
||
$PlugClass = new Plug();
|
||
$payCouponResult = $PlugClass->PlugPermissions([
|
||
'merchant_id' => $merchant_id,
|
||
'identification_arr' => [
|
||
'wxpaycoupon'
|
||
]
|
||
]);
|
||
//验证指定商品代金券不能部分退款
|
||
if ($type == 2 && ($allMoney != $billOrder['all_money']) && !empty($payCouponResult['data']['plug']['wxpaycoupon'])) {
|
||
$PayCouponClass = new PayCoupon();
|
||
$verifyResult = $PayCouponClass->payCouponRefundVerify([
|
||
'bill_id' => $billOrder['id'],
|
||
'refund_all_money' => $allMoney,
|
||
'all_money' => $billOrder['all_money']
|
||
]);
|
||
if ($verifyResult['status'] != 1) {
|
||
$verifyResult['msg'] = $verifyResult['message'];
|
||
return $verifyResult;
|
||
}
|
||
}
|
||
$time = time();
|
||
$newBill = array(
|
||
$BillModel::ALL_MONEY => $allMoney,
|
||
$BillModel::PAY_STATE => 1,
|
||
$BillModel::REFUND_STATUS => 1,
|
||
$BillModel::TYPE => 1,
|
||
$BillModel::ADD_TIME => $time,
|
||
$BillModel::REFUND_NO => $billOrder[$BillModel::ORDER_NO],
|
||
$BillModel::REFUND_MONEY => $allMoney,
|
||
$BillModel::UID => $billOrder[$BillModel::UID],
|
||
$BillModel::VID => $billOrder[$BillModel::VID],
|
||
$BillModel::STORE_ID => $store_id,
|
||
$BillModel::MERCHANT_ID => $merchant_id,
|
||
$BillModel::PAY_TYPE => $billOrder[$BillModel::PAY_TYPE]
|
||
);
|
||
if ($user_type == 2 && $type == 1 && $billOnline['order_genre'] == 4) {
|
||
$newBill[$BillModel::REFUND_STATUS] = 5;
|
||
}
|
||
switch ($type) {
|
||
case 1:
|
||
case 2:
|
||
$newBill[$BillModel::ORDER_TYPE] = 2;
|
||
break;
|
||
case 3:
|
||
$newBill[$BillModel::ORDER_TYPE] = 3;
|
||
break;
|
||
}
|
||
$newBill = $BillModel->addBillOrder($newBill);
|
||
if ($newBill) {
|
||
$result['order_no'] = $newBill['order_no'];
|
||
$result['order_id'] = $newBill['id'];
|
||
$new_discount[$BillDisModel::BILL_ID] = $newBill[$BillModel::ID];
|
||
unset($new_discount['pay_coupon_id_str']);
|
||
unset($new_discount['pay_coupon_fee']);
|
||
$add_dis = $BillDisModel->addOrder($new_discount);
|
||
$old_online = $BillOnlineModel->getFind(array($BillOnlineModel::BILL_ID => $billOrder[$BillModel::ID]));
|
||
$result['old_online'] = $old_online;
|
||
switch ($type) {
|
||
case 1:
|
||
$old_online[$BillOnlineModel::CANCEL_TIME]++;
|
||
$online_save[$BillOnlineModel::CANCEL_TIME] = $old_online[$BillOnlineModel::CANCEL_TIME];
|
||
break;
|
||
case 2:
|
||
case 3:
|
||
$old_online[$BillOnlineModel::REFUND_TIME]++;
|
||
$online_save[$BillOnlineModel::REFUND_TIME] = $old_online[$BillOnlineModel::REFUND_TIME];
|
||
break;
|
||
}
|
||
$old_online[$BillOnlineModel::BILL_ID] = $newBill[$BillModel::ID];
|
||
if ($type == 3) {
|
||
$old_online[$BillOnlineModel::STATUS] = 1;
|
||
$old_online['user_address'] = $user_address;
|
||
$old_online['user_name'] = $user_name;
|
||
$old_online['user_phone'] = $user_phone;
|
||
$old_online['delivery_type'] = 1;
|
||
}
|
||
$add_online = $BillOnlineModel->addOrder($old_online);
|
||
if ($add_dis && $add_online) {
|
||
$success_array = array();
|
||
$num = 0;
|
||
foreach ($refund_list as $key => $val) {
|
||
$newSell = $val;
|
||
unset($newSell[$SellModel::ID]);
|
||
$newSell[$SellModel::BILL_ID] = $newBill[$BillModel::ID];
|
||
$newSell[$SellModel::SELL_ID] = $val[$SellModel::ID];
|
||
$newSell[$SellModel::ADD_TIME] = time();
|
||
if ($type == 3) {
|
||
$newSell[$SellModel::SELL_TYPE] = 3;//换货
|
||
} else {
|
||
$newSell[$SellModel::SELL_TYPE] = 2;//退款
|
||
}
|
||
$newSell[$SellModel::PAY_STATE] = 0;//未成功退款
|
||
$add_sell = $SellModel->addOrder($newSell);
|
||
if ($add_sell) {
|
||
$newSell[$SellModel::ID] = $add_sell;
|
||
$success_array[] = $newSell;
|
||
$SellModel->toSetInc(array($SellModel::ID => $val[$SellModel::ID]), $SellModel::WAIT_REFUND_NUM, $val[$SellModel::NUM]);
|
||
}
|
||
$num += $val[$SellModel::NUM];
|
||
}
|
||
$result['success_array'] = $success_array;
|
||
if (count($success_array) == count($refund_list)) {
|
||
//可进行退换货操作
|
||
switch ($billOnline['order_genre']) {
|
||
//预售订单
|
||
case 4:
|
||
$advance_info['bill_id'] = $newBill['id'];
|
||
$advance_info['buy_number'] = $num;
|
||
$advance_new = D("RetailBillAdvance")->addOrder($advance_info);
|
||
break;
|
||
}
|
||
$RetailBillReasonModel = D("RetailBillReason");
|
||
if (empty($reason_img) || $reason_img == 'undefined') {
|
||
$reason_img = '';
|
||
}
|
||
$reason_data = array(
|
||
'bill_id' => $newBill['id'],
|
||
'apply_reason' => $apply_reason ? $apply_reason : '',
|
||
'goods_img_id' => $reason_img,
|
||
'add_time' => time()
|
||
);
|
||
$RetailBillReasonModel->addOrder($reason_data);
|
||
$result['status'] = 1;
|
||
$data = array(
|
||
'refund_status' => 1,
|
||
'cancel_time' => 1,
|
||
'refund_time' => 1
|
||
);
|
||
$result['data'] = $data;
|
||
switch ($type) {
|
||
case 1:
|
||
case 2:
|
||
$result['msg'] = '退款申请发起成功';
|
||
$result['state'] = '待商家确认退款';
|
||
break;
|
||
case 3:
|
||
$result['msg'] = '换货申请发起成功';
|
||
$result['state'] = '待商家确认换货';
|
||
break;
|
||
}
|
||
$BillModel->updateOrder($bMap, array($BillModel::REFUND_STATUS => 1));//修改原订单状态
|
||
$BillOnlineModel->updateOrder($oMap, $online_save);
|
||
switch ($type) {
|
||
case 1:
|
||
//新版推送
|
||
$sockets = array(
|
||
'login_string' => '',
|
||
'username' => '',
|
||
'type' => 2,
|
||
'message' => "有线上订单申请取消订单",
|
||
);
|
||
sendSockets($store_id, $sockets, -1);
|
||
break;
|
||
case 2:
|
||
//新版推送
|
||
$sockets = array(
|
||
'login_string' => '',
|
||
'username' => '',
|
||
'type' => 3,
|
||
'message' => "有线上订单申请退款",
|
||
);
|
||
sendSockets($store_id, $sockets, -1);
|
||
break;
|
||
case 3:
|
||
//新版推送
|
||
$sockets = array(
|
||
'login_string' => '',
|
||
'username' => '',
|
||
'type' => 6,
|
||
'message' => "有线上订单申请换货",
|
||
);
|
||
sendSockets($store_id, $sockets, -1);
|
||
break;
|
||
}
|
||
} else {
|
||
$BillModel->deleteOrder(array($BillModel::ID => $newBill[$BillModel::ID]));
|
||
$SellModel->deleteOrder(array($SellModel::BILL_ID => $newBill[$BillModel::ID]));
|
||
foreach ($success_array as $value) {
|
||
$SellModel->toSetDec(array($SellModel::ID => $value[$SellModel::SELL_ID]), $SellModel::WAIT_REFUND_NUM, $value[$SellModel::NUM]);
|
||
}
|
||
$result['status'] = 0;
|
||
$result['msg'] = '数据操作有误2';
|
||
}
|
||
} else {
|
||
$BillModel->deleteOrder(array($BillModel::ID => $newBill[$BillModel::ID]));
|
||
$result['status'] = 0;
|
||
$result['msg'] = '退款请求失败,请稍后重试';
|
||
}
|
||
} else {
|
||
$result['status'] = 0;
|
||
$result['msg'] = '退款或换货请求失败,请稍后重试1';
|
||
}
|
||
if (C('IS_SET_REDIS')) {
|
||
$ApiRedis::$redis->delete($redis_key);
|
||
}
|
||
return $result;
|
||
}
|
||
} |