修改订单退款的错误

This commit is contained in:
lewis 2025-01-08 16:56:52 +08:00
parent f0eda34869
commit 784c274f28
2 changed files with 136 additions and 22 deletions

View File

@ -133,7 +133,9 @@ class StoreOrderController extends BaseAdminController
return $this->fail('无该订单请检查');
}
$params['id'] = $detail['id'];
$params['old_cart_id'] = StoreOrderCartInfo::where('oid', $detail['id'])->column('id');
if (empty($params['product_arr'])) {
$params['old_cart_id'] = StoreOrderCartInfo::where('oid', $detail['id'])->where('status', '<>', 2)->column('id');
}
$refundOrderService->refund($detail['uid'], $params);
return $this->success('退款成功',[],1,1);
}

View File

@ -5,6 +5,7 @@ namespace app\common\service;
use app\common\enum\OrderEnum;
use app\common\logic\CapitalFlowLogic;
use app\common\logic\CommissionnLogic;
use app\common\logic\StoreFinanceFlowLogic;
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;
@ -34,17 +35,38 @@ class RefundOrderService
if (!$order->allowRefund()) {
throw new BusinessException('订单不能退款');
}
$orderCartProducts = StoreOrderCartInfo::where('oid', $order['id'])->whereIn('id', $params['old_cart_id'])->select();
if ($order->status == 2 && $order->is_writeoff == 1) {
$this->refundStoreMoney($order);
$this->refundCommission($order);
$orderCartInfoWhere = ['oid' => $order['id']];
$refundNum = 0;
if (!empty($params['old_cart_id'])) {
$orderCartInfoWhere['id'] = $params['old_cart_id'];
$refundNum = count($params['old_cart_id']);
}
$refundAmount = $this->calculate($orderCartProducts);
if (!empty($params['product_arr'])) {
$orderCartInfoWhere['product_id'] = array_column($params['product_arr'], 'product_id');
$refundNum = count($params['product_arr']);
}
$orderCartProducts = StoreOrderCartInfo::where($orderCartInfoWhere)->select();
if (isset($params['refund_price'])) {
$refundAmount = $params['refund_price'];
} else {
$refundAmount = $this->calculate($orderCartProducts);
}
if (bcadd($order->refund_price, $refundAmount, 2) > $order->pay_price) {
throw new BusinessException('退款金额错误');
}
// if ($order->status == 2 && $order->is_writeoff == 1) {
// $this->refundStoreMoney($order, $refundAmount);
// $this->refundCommission($order);
// }
$productInfo = reset_index($params['product_arr'] ?? [], 'product_id');
$this->refundMoney($order, $refundAmount);
$this->updateProductStock($orderCartProducts);
$this->updateOrderProductStatus($order, $params['old_cart_id']);
$this->updateOrderStatus($order, $refundAmount, count($params['old_cart_id']));
$this->resetStoreFinanceFlow($order, $orderCartProducts);
$this->updateProductStock($orderCartProducts, $productInfo);
$this->updateOrderCartInfo($order, $orderCartProducts, $refundAmount, $productInfo);
$this->updateOrderStatus($order, $refundAmount, $refundNum);
$this->resetStoreFinanceFlow($order, $orderCartProducts, $productInfo);
// if ($order->status == 2 && $order->is_writeoff == 1) {
// $this->financeSettle($order);
// }
Db::commit();
} catch (Exception $e) {
Db::rollback();
@ -81,20 +103,25 @@ class RefundOrderService
/**
* 更新商品库存
* @param $orderCartProducts
* @param $productInfo
* @throws Exception
*/
public function updateProductStock($orderCartProducts): void
public function updateProductStock($orderCartProducts, $productInfo = []): void
{
$updateData = [];
foreach ($orderCartProducts as $product) {
if (empty($productInfo[$product['product_id']]['cart_num'])) {
continue;
}
$number = $productInfo[$product['product_id']]['cart_num'];
$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']],
'stock' => ['inc', $number],
'sales' => ['dec', $number],
];
}
}
@ -105,12 +132,44 @@ class RefundOrderService
* 更新订单商品状态
* @param $order
* @param $orderCartIds
* @param $productInfo
* @return void
* @throws DbException
*/
public function updateOrderProductStatus($order, $orderCartIds): void
public function updateOrderCartInfo($order, $orderCartProducts, $refundAmount, $productInfo = []): void
{
StoreOrderCartInfo::whereIn('id', $orderCartIds)->where('oid', $order['id'])->update(['is_pay' => -1, 'status' => OrderEnum::REFUND_STATUS_FINISH]);
if (!empty($productInfo)) {
foreach ($orderCartProducts as $orderCartProduct) {
$product = $productInfo[$orderCartProduct['product_id']] ?? [];
if (empty($product)) {
continue;
}
$cartRefundAmount = min($refundAmount, $orderCartProduct['total_price']);
$refundAmount = bcsub($refundAmount, $cartRefundAmount, 2);
$attrs = ['refund_num' => ['inc', $product['cart_num']]];
if ($cartRefundAmount > 0) {
$attrs['refund_amount'] = ['inc', $cartRefundAmount];
}
if ($cartRefundAmount == $orderCartProduct['total_price']) {
$attrs['status'] = 2;
}
StoreOrderCartInfo::where('oid', $order['id'])->where('product_id', $product['product_id'])->update($attrs);
}
} else {
$updateData = [];
foreach ($orderCartProducts as $orderCartProduct) {
if ($refundAmount <= 0) {
break;
}
$updateData[] = [
'id' => $orderCartProduct['id'],
'refund_amount' => ['inc', min($orderCartProduct['total_price'], $refundAmount)],
'status' => 2
];
$refundAmount = bcsub($refundAmount, $orderCartProduct['total_price'], 2);
}
(new StoreOrderCartInfo())->saveAll($updateData);
}
}
/**
@ -123,14 +182,13 @@ class RefundOrderService
*/
public function updateOrderStatus($order, $refundAmount, $refundNum): void
{
$orderProductCount = StoreOrderCartInfo::where('oid', $order['id'])->count();
if (($order['refund_num'] + $refundNum) == $orderProductCount) {
$order->refund_price = bcadd($order['refund_price'], $refundAmount, 2);
$order->refund_num += $refundNum;
if ($order->pay_price == $order->refund_price) {
// 全部退款完成,订单状态改为已退款
$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();
}
@ -138,13 +196,32 @@ class RefundOrderService
* 退佣金
* @param $order
* @param $storeOrderProducts
* @param $productInfo
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function resetStoreFinanceFlow($order, $storeOrderProducts): void
public function resetStoreFinanceFlow($order, $storeOrderProducts, $productInfo): void
{
foreach ($storeOrderProducts as $storeOrderProduct) {
$refundInfo = $productInfo[$storeOrderProduct['product_id']] ?? [];
$arr = StoreFinanceFlowProduct::where('oid', $order['id'])->where('product_id', $storeOrderProduct['product_id'])->select()->toArray();
foreach ($arr as $value) {
if (!empty($refundInfo['cart_num'])) {
$value['cart_num'] = $storeOrderProduct['cart_num'] - $refundInfo['cart_num'];
} else {
$value['cart_num'] = 0;
}
$value['total_price'] = bcmul($value['cart_num'], $value['price'], 2);
$value['number'] = bcmul($value['total_price'], $value['rate'], 2);
StoreFinanceFlowProduct::where('id', $value['id'])->update(['delete_time' => time()]);
unset($value['id']);
$value['create_time'] = strtotime($value['create_time']);
$value['update_time'] = strtotime($value['update_time']);
StoreFinanceFlowProduct::create($value);
}
}
$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');
@ -157,12 +234,13 @@ class RefundOrderService
/**
* 扣除店铺余额和押金
* @param $order
* @param $refundAmount
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function refundStoreMoney($order): void
public function refundStoreMoney($order, $refundAmount): void
{
$margin = StoreFinanceFlow::where('order_id', $order['id'])->where('financial_type', OrderEnum::ORDER_MARGIN)->value('number');
$income = StoreFinanceFlow::where('order_id', $order['id'])->where('financial_type', OrderEnum::MERCHANT_ORDER_OBTAINS)->value('number');
@ -171,14 +249,25 @@ class RefundOrderService
}
$store = SystemStore::where('id', $order['store_id'])->find();
if ($margin > 0) {
$margin = min($margin, $refundAmount);
$refundAmount = bcsub($refundAmount, $margin, 2);
$store->paid_deposit = bcsub($store->paid_deposit, $margin, 2);
}
if ($income > 0) {
if ($income > 0 && $refundAmount > 0) {
$income = min($income, $refundAmount);
$store->store_money = bcsub($store->store_money, $income, 2);
}
$store->save();
}
/**
* 退佣金
* @param $order
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function refundCommission($order)
{
$list = StoreFinanceFlow::where(['order_id' => $order['id'], 'financial_pm' => 1, 'status' => 0])->where('other_uid', '>', 0)->select();
@ -191,4 +280,27 @@ class RefundOrderService
}
}
/**
* 财务结算
* @param $order
* @return void
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function financeSettle($order)
{
$financeFlowLogic = new StoreFinanceFlowLogic();
$financeFlow = new StoreFinanceFlow();
$select_1 = $financeFlow->where(['order_id' => $order['id'], 'financial_pm' => 1, 'status' => 0])->where('other_uid', '>', 0)->select();
foreach ($select_1 as $v) {
if ($v['other_uid'] > 0) {
$financeFlowLogic->updateStatusUser($v['id'], $v['other_uid'], $v['number'], $v['order_id']);
}
}
$deposit = $financeFlow->where(['order_id' => $order['id'], 'financial_pm' => 0, 'financial_type' => 11, 'status' => 0])->value('number') ?? 0;
$money = $financeFlow->where(['order_id' => $order['id'], 'financial_pm' => 1, 'financial_type' => 2, 'status' => 0])->value('number') ?? 0;
$financeFlowLogic->updateStatusStore($order['id'], $order['store_id'], $money, $deposit);
}
}