文件
mashangyoujiu/Application/Pay/Controller/RefundController.class.php

245 行
11 KiB
PHP
可执行文件

<?php
namespace Pay\Controller;
use Think\Controller;
//[1.1.58]
class RefundController extends Controller
{
//收银机订单退款 2018-11-16
public function CashRegisterOrderRefund_v1()
{
$storeid = I('post.storeid');
$adminid = I('post.adminid');
$out_trade_no = I('post.out_trade_no');
$goods_info = $_POST['goods_info'];
if (empty($storeid) || empty($adminid) || empty($out_trade_no) || empty($goods_info)) {
$result['status'] = 0;
$result['msg'] = '缺少参数';
$this->ajaxReturn($result);
}
$admin = M('retail_cashier')->where(array('id' => $adminid, 'storeid' => $storeid))->find();
if (empty($admin)) {
$result['status'] = 0;
$result['msg'] = '收银员账号有误';
$this->ajaxReturn($result);
}
$PayOrder = D('PayOrder');
$Order = $PayOrder->getFind(array('out_trade_no' => $out_trade_no, 'storeid' => $storeid, 'order_state' => 'SUCCESS'));
if (empty($Order)) {
$result['status'] = 0;
$result['msg'] = '订单不存在';
$this->ajaxReturn($result);
}
$refundCount = $PayOrder->getCount(array('pid' => $Order['id']));
if ($refundCount >= 10) {
$result['status'] = 0;
$result['msg'] = '该订单退款次数已达上限';
$this->ajaxReturn($result);
}
$billOrder = M('retail_bill')->where(array('pay_orderid' => $Order['id']))->find();// 历史订单 原订单
if (empty($billOrder['sellid'])) {
$result['status'] = 0;
$result['msg'] = '订单信息有误';
$this->ajaxReturn($result);
} else if ($billOrder['type'] != 2) {
$result['status'] = 0;
$result['msg'] = '该接口只支持线下退款';
$this->ajaxReturn($result);
}
$tempGoods = json_decode($goods_info, true);
$sellGoods = M('retail_sell')->where(array('id' => array('in', $billOrder['sellid'])))->field('*,num as agoods_count')->select();
/* 优惠购买,全额退款 */
if ((!empty($billOrder['exchange_money']) && $billOrder['exchange_money'] > 0) || (!empty($billOrder['small_changes_money']) && $billOrder['small_changes_money'] > 0)) {
$tempGoods = $sellGoods;
}
$tempRefundSellList = array();
$allMoney = 0;
$allRefundCount = 0;
foreach ($tempGoods as &$value) {
foreach ($sellGoods as &$val) {
if ($value['id'] == $val['id']) {
if ($val['can_exchange_weigh'] == 2) {
if ($value['agoods_count'] != $val['num']) {
$result['status'] = 0;
$result['msg'] = '称重商品必须全部退款';
$this->ajaxReturn($result);
} else {
$allMoney += round($val['price'] * $value['agoods_count'], 2);
$temp = $val;
$temp['num'] = $value['agoods_count'];
$temp['allmoney'] = round($val['price'] * $value['agoods_count'], 2);
$temp['status'] = 2;
$temp['refund_num'] = round($val['refund_num'] + $value['agoods_count'], 3);
$tempRefundSellList[] = $temp;
if ($value['agoods_count'] == $val['num']) {
$allRefundCount++;
}
}
} else {
if (round($value['agoods_count'], 3) <= round($val['num'] - $val['refund_num'])) {
$allMoney += round($val['price'] * $value['agoods_count'], 2);
$temp = $val;
$temp['num'] = $value['agoods_count'];
$temp['allmoney'] = round($val['price'] * $value['agoods_count'], 2);
$temp['status'] = 2;
$temp['refund_num'] = round($val['refund_num'] + $value['agoods_count'], 3);
$tempRefundSellList[] = $temp;
if ($value['agoods_count'] == $val['num']) {
$allRefundCount++;
}
continue;
} else {
$result['status'] = 0;
$result['msg'] = '该订单已全退款';
$this->ajaxReturn($result);
}
}
}
}
}
if (!$allMoney) {
$result['status'] = 0;
$result['msg'] = '商品退款信息错误';
$this->ajaxReturn($result);
}
if ((!empty($billOrder['exchange_money']) && $billOrder['exchange_money'] > 0) || (!empty($billOrder['small_changes_money']) && $billOrder['small_changes_money'] > 0) || ($billOrder['discount'] && $billOrder['discount'] < 1)) {
if (count($sellGoods) != $allRefundCount) {
$result['status'] = 0;
$result['msg'] = '使用优惠券抹零等只能全额退款';
$this->ajaxReturn($result);
}
if ($allMoney != $billOrder['allmoney']) {
$allMoney = $billOrder['allmoney'];
}
}
$item = array(
'refund_money' => $allMoney,
'admin_id' => $adminid,
'refund_sell_goods' => $tempRefundSellList,
'out_refund_no' => 're' . A('Pay')->createOrderTradeNo($Order['id']),
'storeid' => $storeid,
'bill_id' => $billOrder['id'],
'merchantid' => $billOrder['merchantid'],
'old_order_no' => $billOrder['order_id']
);
switch (intval($billOrder['paytype'])) {
case 1:
case 2:
case 7:
//现金 银行卡 其他
$refund['status'] = 1;
$refund['msg'] = '退款成功';
break;
case 3:
//微信
if ($Order['m_pay_type'] == 3) {
$refund = A('PayContinent')->refund($Order['trade_no'], $admin['merchantid'], $allMoney, $item['out_refund_no']);
} else {
$refund = A('Pay/WxPay')->newRefund_v1($Order['trade_no'], $item, $Order);
}
break;
case 4:
//支付宝
if ($Order['m_pay_type'] == 3) {
$refund = A('PayContinent')->refund($Order['trade_no'], $admin['merchantid'], $allMoney, $item['out_refund_no']);
} else {
$refund = A('Pay/AliPay')->newRefund_v1($Order['trade_no'], $item, $Order);
}
break;
case 5:
//会员余额支付
$refund = $this->VipRefund_v1($billOrder, $item);
break;
default:
$refund['status'] = 0;
$refund['msg'] = '订单信息有误';
}
if ($refund['status'] == 1) {
$add['pid'] = $Order['id'];
$add['order_type'] = 3;
$add['pay_type'] = $Order['pay_type'];
$add['trade_amount'] = $item['refund_money'];
$add['storeid'] = $Order['storeid'];
$add['adminid'] = $item['adminid'];
$add['order_state'] = 'REFUND';
$add['cdate'] = time();
$add['goods_info'] = json_encode($item['refund_goods_info']);
$add['remark'] = '退款成功';
$add['out_trade_no'] = $item['out_refund_no'];
if (!empty($refund['data'])) {
$add['extend_content'] = json_encode($refund['data']);
$add['trade_no'] = $refund['data']['refund_id_0'];
}
$addRefund_id = $PayOrder->addOrder($add);
if (!$addRefund_id) {
$result['status'] = 0;
$result['msg'] = "已提交退款申请<br>更新订单失败";
} else {
$item['refund_pay_order_id'] = $addRefund_id;
$res = A('Sellapi/Refund')->OrderRefund_v1($item);// 退商品
if ($res['status'] == 0) {
$result = $res;
} else {
if ($refund['rc_id']) {
M('recharge_order')->where(array('id' => $refund['rc_id']))->save(array('billid' => $res['new_bill_id']));
}
$result = $res;
$result['status'] = 1;
$result['msg'] = "退款成功";
}
}
} else {
$result = $refund;
}
$this->ajaxReturn($result);
}
/*
* 会员卡支付退款
* */
protected function VipRefund_v1($billOrder, $item)
{
if (empty($billOrder['vipid'])) {
$result['status'] = 0;
$result['msg'] = '会员订单信息有误';
return $result;
} else {
$add = M('retail_vip')->where(array('id' => $billOrder['vipid']))->setInc('balance', $item['refund_money']);// 增加余额
if ($add) {
$itemRecahrge = array(
'vid' => $billOrder['vipid'],
'status' => 1,
'money' => $item['refund_money'],
'allmoney' => $item['refund_money'],
'type' => 3,
'storeid' => $billOrder['storeid'],
'addtime' => time(),
'remark' => '会员门店退款退回' . $item['refund_money'] . '元',
'state' => 1,
'merchantid' => $billOrder['merchantid'],
'adminid' => $item['admin_id'],
'usertype' => 3
);
$addOrder = M('recharge_order')->add($itemRecahrge);// 添加会员退款余额记录
if ($addOrder) {
$result['status'] = 1;
$result['msg'] = '余额退回成功';
$result['rc_id'] = $addOrder;
} else {
M('retail_vip')->where(array('id' => $billOrder['vipid']))->setDec('balance', $item['refund_money']);
$result['status'] = 0;
$result['msg'] = '余额退回失败';
}
} else {
$result['status'] = 0;
$result['msg'] = '余额退回失败';
}
return $result;
}
}
}