调试订单退款

This commit is contained in:
DESKTOP-GMUNQ1B\Administrator 2024-12-14 14:46:35 +08:00
parent d02f33f201
commit 12314cdf42
10 changed files with 325 additions and 11 deletions

View File

@ -18,6 +18,7 @@ use app\common\model\system_store\SystemStoreStaff;
use app\common\model\user\User;
use app\common\model\user\UserAddress;
use app\common\service\Curl;
use app\common\service\RefundOrderService;
use Exception;
use support\Log;
use think\facade\Db;
@ -464,19 +465,11 @@ class OrderController extends BaseApiController
/**
* 订单退款申请
*/
public function apply_refund()
public function apply_refund(RefundOrderService $refundOrderService)
{
$params = (new OrderValidate())->post()->goCheck('add');
$uid = $this->userId;
//拆单逻辑
// $res = OrderLogic::dealRefund($uid, $params);
$detail = StoreOrder::where('id', $params['id'])->where('uid',$uid)->where('status','<>',2)->where('paid',1)->find();
if ($detail) {
$res = StoreOrderLogic::refund($detail, ['order_id' => $detail['order_id']]);
if ($res != false) {
return $this->success($res);
}
}
$refundOrderService->refund($uid, $params);
return $this->success('申请成功');
}

View File

@ -51,7 +51,7 @@ class OrderList extends BaseAdminDataLists implements ListsSearchInterface
->select()
->each(function ($item) {
$item['goods_list'] = StoreOrderCartInfo::where('oid', $item['id'])
->field('product_id,cart_num,verify_code,is_writeoff,writeoff_time,cart_info')->limit(3)->select()
->field('id,product_id,cart_num,verify_code,is_writeoff,writeoff_time,cart_info,status')->select()
->each(function ($v) use ($item) {
$v['store_name'] = '';
$v['image'] = '';

View File

@ -123,4 +123,13 @@ class StoreOrder extends BaseModel
Log::error('store_order:更新后'.$e->getMessage());
}
}
public function allowRefund(): bool
{
if (in_array($this->status, [0, 1]) && $this->paid == 1) {
return true;
}
return false;
}
}

View File

@ -16,6 +16,10 @@ class StoreOrderCartInfo extends BaseModel
protected $json = ['cart_info'];
protected $jsonAssoc = true;
const StatusWait = 0; //待核销
const StatusWriteOff = 1; //核销完成
const StatusRefund = 2; //退款
public function goodsName()
{
return $this->hasOne(StoreBranchProduct::class,'id','product_id')->bind(['store_name','image','unit','price']);

View File

@ -0,0 +1,157 @@
<?php
namespace app\common\service;
use app\common\enum\OrderEnum;
use app\common\logic\CommissionnLogic;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_finance_flow\StoreFinanceFlow;
use app\common\model\store_finance_flow_product\StoreFinanceFlowProduct;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\service\pay\PayTool;
use Exception;
use support\exception\BusinessException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
class RefundOrderService
{
public function refund($userId, $params): void
{
Db::startTrans();
try {
$order = StoreOrder::where('id', $params['id'])->where('uid', $userId)->find();
if (empty($order)) {
throw new BusinessException('订单不存在');
}
if (!$order->allowRefund()) {
throw new BusinessException('订单不能退款');
}
$orderCartProducts = StoreOrderCartInfo::where('oid', $order['id'])->whereIn('id', $params['old_cart_id'])->select();
foreach ($orderCartProducts as $orderCartProduct) {
if ($orderCartProduct['is_pay'] != 1 || $orderCartProduct['status'] != StoreOrderCartInfo::StatusWait) {
throw new BusinessException('订单商品状态异常');
}
}
$refundAmount = $this->calculate($orderCartProducts);
$this->refundMoney($order, $refundAmount);
$this->updateProductStock($orderCartProducts);
$this->updateOrderProductStatus($order, $params['old_cart_id']);
$this->updateOrderStatus($order, $refundAmount, count($params['old_cart_id']));
$this->refundCommission($order, $orderCartProducts);
Db::commit();
} catch (Exception $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* 计算退款金额
* @param $orderCartProducts
* @return string
*/
public function calculate($orderCartProducts): string
{
$amount = '0.00';
foreach ($orderCartProducts as $orderCartProduct) {
if ($orderCartProduct['is_pay'] == 1 && $orderCartProduct['status'] == StoreOrderCartInfo::StatusWait) {
$amount = bcadd($amount, $orderCartProduct['total_price'], 2);
}
}
return $amount;
}
/**
* 资金原路退回
* @param $order
* @param $refundAmount
* @return void
*/
public function refundMoney($order, $refundAmount): void
{
$payTool = PayTool::getInstance($order['pay_type']);
$payTool->refund($refundAmount, $order);
}
/**
* 更新商品库存
* @param $orderCartProducts
* @throws Exception
*/
public function updateProductStock($orderCartProducts): void
{
$updateData = [];
foreach ($orderCartProducts as $product) {
$storeBranchProductId = StoreBranchProduct::where('store_id', $product['store_id'])
->where('product_id', $product['product_id'])
->withTrashed()->value('id');
if ($storeBranchProductId) {
$updateData[] = [
'id' => $storeBranchProductId,
'stock' => ['inc', $product['cart_num']],
'sales' => ['dec', $product['cart_num']],
];
}
}
(new StoreBranchProduct())->saveAll($updateData);
}
/**
* 更新订单商品状态
* @param $order
* @param $orderCartIds
* @return void
* @throws DbException
*/
public function updateOrderProductStatus($order, $orderCartIds): void
{
StoreOrderCartInfo::whereIn('id', $orderCartIds)->where('oid', $order['id'])->update(['is_pay' => -1, 'status' => OrderEnum::REFUND_STATUS_FINISH]);
}
/**
* 更新订单状态
* @param $order
* @param $refundAmount
* @param $refundNum
* @return void
* @throws DbException
*/
public function updateOrderStatus($order, $refundAmount, $refundNum): void
{
$orderProductCount = StoreOrderCartInfo::where('oid', $order['id'])->count();
if (($order['refund_num'] + $refundNum) == $orderProductCount) {
// 全部退款完成,订单状态改为已退款
$order->refund_status = OrderEnum::REFUND_STATUS_FINISH;
$order->status = OrderEnum::RECEIVED_BACK;
}
$order->refund_price = bcadd($order['refund_price'], $refundAmount, 2);
$order->refund_num += $refundNum;
$order->save();
}
/**
* 退佣金
* @param $order
* @param $storeOrderProducts
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function refundCommission($order, $storeOrderProducts): void
{
$productIds = array_unique(array_column($storeOrderProducts->toArray(), 'product_id'));
StoreFinanceFlowProduct::where('oid', $order['id'])->whereIn('product_id', $productIds)->update(['number' => 0, 'update_time' => strtotime(time())]);
$village_uid = StoreFinanceFlow::where('order_id', $order['id'])->where('financial_type', 14)->value('other_uid');
$brigade_uid = StoreFinanceFlow::where('order_id', $order['id'])->where('financial_type', 15)->value('other_uid');
$transaction_id = StoreFinanceFlow::where('order_id', $order['id'])->value('transaction_id');
StoreFinanceFlow::where('order_id', $order['id'])->update(['delete_time' => time()]);
CommissionnLogic::setStore($order, $village_uid, $brigade_uid, $transaction_id);
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace app\common\service\pay;
use app\common\logic\CapitalFlowLogic;
use app\common\model\user\User;
/**
* 余额支付
* Class BalancePay
* @package app\common\service\pay
*/
class BalancePay extends PayTool
{
public function refund($amount, $order)
{
$user = User::where('id', $order['uid'])->findOrEmpty();
$capitalFlowDao = new CapitalFlowLogic($user);
$capitalFlowDao->userIncome('now_money_refund', 'system_back', $order['id'], $amount);
$user->now_money = bcadd($user['now_money'], $amount, 2);
$user->save();
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace app\common\service\pay;
use app\common\enum\OrderEnum;
use app\common\enum\YesNoEnum;
use app\common\model\store_cash_finance_flow\StoreCashFinanceFlow;
/**
* 现金支付
* Class CashPay
* @package app\common\service\pay
*/
class CashPay extends PayTool
{
public function refund($amount, $order)
{
$model = new StoreCashFinanceFlow();
$model->store_id = $order['store_id'] ?? 0;
$model->cash_price = $order->pay_price;
$model->receivable = $order->pay_price;
$model->remark = '退款';
$model->type = 1;
$model->status = YesNoEnum::YES;
$model->save();
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace app\common\service\pay;
use app\common\enum\PayEnum;
/**
* 支付
* Class PayTool
* @package app\common\service\pay
*/
abstract class PayTool
{
/**
* @param int $type
* @param $params
* @return BalancePay|CashPay|PurchaseFundPay|WeChatPay
*/
public static function getInstance(int $type, $params = [])
{
return match ($type) {
PayEnum::WECHAT_PAY, PayEnum::WECHAT_PAY_BARCODE, PayEnum::WECHAT_PAY_MINI => new WeChatPay($params),
PayEnum::BALANCE_PAY => new BalancePay($params),
PayEnum::PURCHASE_FUNDS => new PurchaseFundPay($params),
default => new CashPay(),
};
}
abstract function refund($amount, $order);
}

View File

@ -0,0 +1,25 @@
<?php
namespace app\common\service\pay;
use app\common\logic\CapitalFlowLogic;
use app\common\model\user\User;
/**
* 采购款支付
* Class PurchaseFundPay
* @package app\common\service\pay
*/
class PurchaseFundPay extends PayTool
{
public function refund($amount, $order)
{
$user = User::where('id', $order['uid'])->findOrEmpty();
$capitalFlowDao = new CapitalFlowLogic($user);
$capitalFlowDao->userIncome('purchase_refund', 'system_back', $order['id'], $amount, '', 1);
$user->purchase_funds = bcadd($user['purchase_funds'], $amount, 2);
$user->save();
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace app\common\service\pay;
use support\exception\BusinessException;
/**
* 微信支付
* Class WeChatPay
* @package app\common\service\pay
*/
class WeChatPay extends PayTool
{
public function refund($amount, $order)
{
$totalFee = (int)bcmul($amount, 100);
try {
$wechat = new PayService(1);
$order = [
'out_trade_no' => $order['order_id'],
'out_refund_no' => 'BO' . time(),
'amount' => [
'refund' => $totalFee,
'total' => $order['pay_price'],
'currency' => 'CNY',
],
];
$res = $wechat->wechat->refund($order);
if ($res['status'] == 'PROCESSING') {
return true;
}
return false;
} catch (\Exception $e) {
\support\Log::info($e->extra['message'] ?? $e->getMessage());
throw new BusinessException($e->extra['message'] ?? $e->getMessage());
}
}
}