订单退款,供应链订单同时退款,退相关的收益,写入流水
This commit is contained in:
parent
695110cb59
commit
38a8699fd7
@ -167,7 +167,7 @@ class CommissionDao
|
||||
// 订单为自提,且佣金大于0,下单的店铺退佣金
|
||||
$financeDao->user = $refundOrder->order->user;
|
||||
$financeDao->order = $refundOrder->order;
|
||||
$financeDao->platformIn($commission, 'commission_to_store_refund');
|
||||
$financeDao->platformIn($commission, 'commission_to_store_refund', $refundOrder->order['mer_id']);
|
||||
app()->make(MerchantRepository::class)->subLockMoney($refundOrder->order['mer_id'], 'order', $refundOrder->order['order_id'], $commission);
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ namespace app\common\dao\store\consumption;
|
||||
use app\common\dao\BaseDao;
|
||||
use app\common\dao\store\StoreActivityOrderDao;
|
||||
use app\common\dao\store\StoreActivityUserDao;
|
||||
use app\common\dao\system\financial\FinancialDao;
|
||||
use app\common\model\store\consumption\StoreConsumption;
|
||||
use app\common\model\store\consumption\StoreConsumptionUser;
|
||||
use app\common\model\store\order\StoreGroupOrder;
|
||||
@ -400,6 +401,11 @@ class StoreConsumptionUserDao extends BaseDao
|
||||
'mark' => '订单退款,获得' . $title . $refundOrder['refund_consumption'] . ",退款订单ID:{$refundOrder['order_id']}",
|
||||
'balance' => 0
|
||||
]);
|
||||
$financeDao = new FinancialDao();
|
||||
$financeDao->user = $order->user;
|
||||
$financeDao->order = $order;
|
||||
$financeDao->platformIn($refundOrder['refund_consumption'], 'platform_consumption_refund');
|
||||
$financeDao->save();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -15,6 +15,7 @@ namespace app\common\dao\store\order;
|
||||
|
||||
|
||||
use app\common\dao\BaseDao;
|
||||
use app\common\dao\system\financial\FinancialDao;
|
||||
use app\common\model\store\order\StoreGroupOrderOther;
|
||||
use app\common\model\store\order\StoreOrderOther;
|
||||
use app\common\model\store\order\StoreOrderProductOther;
|
||||
@ -22,10 +23,12 @@ use app\common\model\store\order\StoreOrderStatusOther;
|
||||
use app\common\model\store\order\StoreRefundOrder;
|
||||
use app\common\model\store\order\StoreRefundOrderOther;
|
||||
use app\common\model\store\order\StoreRefundProductOther;
|
||||
use app\common\model\system\merchant\FinancialRecord;
|
||||
use app\common\repositories\store\order\StoreOrderStatusRepository;
|
||||
use app\common\repositories\store\product\ProductAssistSetRepository;
|
||||
use app\common\repositories\store\product\ProductGroupBuyingRepository;
|
||||
|
||||
use app\common\repositories\system\merchant\MerchantRepository;
|
||||
use think\db\BaseQuery;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
@ -280,8 +283,8 @@ class StoreOrderOtherDao extends BaseDao
|
||||
public function fieldExists($field, $value, ?int $except = null): bool
|
||||
{
|
||||
return ($this->getModel()::getDB())->when($except, function ($query) use ($field, $except) {
|
||||
$query->where($field, '<>', $except);
|
||||
})->where($field, $value)->count() > 0;
|
||||
$query->where($field, '<>', $except);
|
||||
})->where($field, $value)->count() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -769,48 +772,82 @@ class StoreOrderOtherDao extends BaseDao
|
||||
})->where('StoreOrderOther.uid', $uid)->count();
|
||||
}
|
||||
|
||||
public function refund($id)
|
||||
/**
|
||||
* 供应链订单退款
|
||||
* @param $refundOrder
|
||||
* @return void
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function refund($refundOrder)
|
||||
{
|
||||
$refundOrder = StoreRefundOrder::where('refund_order_id', $id)->with(['refundProduct.product', 'order'])->find();
|
||||
$refundProducts = [];
|
||||
foreach ($refundOrder->refundProduct->toArray() as $item) {
|
||||
$refundProducts[$item['product']['product_id']] = ['num' => $item['refund_num']];
|
||||
$refundProducts[$item['product']['product_source_id']] = ['num' => $item['refund_num']];
|
||||
}
|
||||
$order = StoreOrderOther::where('order_sn', $refundOrder->order['order_sn'])->find();
|
||||
$refundOrderArray = $refundOrder->toArray();
|
||||
unset($refundOrderArray['return_order_id']);
|
||||
unset($refundOrderArray['refund_order_id'], $refundOrderArray['refundProduct'], $refundOrderArray['order']);
|
||||
Db::startTrans();
|
||||
try {
|
||||
$model = new StoreRefundOrderOther();
|
||||
$model->setAttrs($refundOrderArray);
|
||||
$model->order_sn = $refundOrderArray['refund_order_sn'];
|
||||
$model->order_id = $order['order_id'];
|
||||
$model->uid = $order['uid'];
|
||||
$model->mer_id = $order['mer_id'];
|
||||
$model->save();
|
||||
$products = [];
|
||||
$refundTotal = 0.00;
|
||||
$orderProducts = StoreOrderProductOther::where('order_id', $order['order_id'])->select();
|
||||
foreach ($orderProducts as $orderProduct) {
|
||||
$refundProduct = $refundProducts[$orderProduct['product_id']] ?? [];
|
||||
if (!empty($refundProduct)) {
|
||||
$products[] = [
|
||||
'refund_order_id' => $model->refund_order_id,
|
||||
'order_product_id' => $orderProduct['order_product_id'],
|
||||
'refund_price' => $orderProduct['total_price'],
|
||||
'refund_consumption' => 0,
|
||||
'platform_refund_price' => 0,
|
||||
'refund_postage' => 0,
|
||||
'refund_integral' => 0,
|
||||
'refund_num' => $refundProduct['num'],
|
||||
];
|
||||
$orderProduct->refund_num -= $refundProduct['num'];
|
||||
$orderProduct->is_refund = 1;
|
||||
$orderProduct->save();
|
||||
if (empty($refundProduct)) {
|
||||
continue;
|
||||
}
|
||||
$price = bcdiv($orderProduct['total_price'], $orderProduct['product_num'], 2);
|
||||
$refundPrice = bcmul($price, $refundProduct['num'], 2);
|
||||
$refundTotal = bcadd($refundTotal, $refundPrice, 2);
|
||||
$products[] = [
|
||||
'order_product_id' => $orderProduct['order_product_id'],
|
||||
'refund_price' => $refundPrice,
|
||||
'refund_consumption' => 0,
|
||||
'platform_refund_price' => 0,
|
||||
'refund_postage' => 0,
|
||||
'refund_integral' => 0,
|
||||
'refund_num' => $refundProduct['num'],
|
||||
];
|
||||
$orderProduct->refund_num -= $refundProduct['num'];
|
||||
$orderProduct->is_refund = 1;
|
||||
$orderProduct->save();
|
||||
}
|
||||
$model = new StoreRefundOrderOther();
|
||||
$model->setAttrs($refundOrderArray);
|
||||
$model->order_id = $order['order_id'];
|
||||
$model->uid = $order['uid'];
|
||||
$model->mer_id = $order['mer_id'];
|
||||
$model->refund_price = $refundTotal;
|
||||
$model->save();
|
||||
|
||||
foreach ($products as &$product) {
|
||||
$product['refund_order_id'] = $model->refund_order_id;
|
||||
}
|
||||
if (count($products) > 0) {
|
||||
StoreRefundProductOther::getDB()->insertAll($products);
|
||||
}
|
||||
|
||||
$financeDao = new FinancialDao();
|
||||
$financeDao->user = $order->user;
|
||||
$financeDao->order = $order;
|
||||
$financeDao->order->order_id = $model->refund_order_id;
|
||||
$financeDao->platformIn($refundTotal, 'supply_chain_refund', $model->mer_id);
|
||||
|
||||
if ($refundOrder->order->status == -1){
|
||||
$margin = FinancialRecord::where('order_id', $refundOrder['order_id'])
|
||||
->where('mer_id', $model->mer_id)
|
||||
->where('financial_type', 'auto_margin')
|
||||
->value('number');
|
||||
if ($margin) {
|
||||
$financeDao->platformOut($margin, 'auto_margin_refund', $model->mer_id);
|
||||
}
|
||||
}
|
||||
$financeDao->save();
|
||||
|
||||
app()->make(MerchantRepository::class)->subLockMoney($model->mer_id, 'order', $model['refund_order_id'], $refundTotal);
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
|
@ -93,7 +93,7 @@ class FinancialDao extends BaseDao
|
||||
* @param $financialType
|
||||
* @param $merId
|
||||
*/
|
||||
public function platformOut($number, $financialType, $merId = '')
|
||||
public function platformOut($number, $financialType, $merId = 0)
|
||||
{
|
||||
$this->setData($number, $financialType, 0, 2, $merId);
|
||||
}
|
||||
@ -104,7 +104,7 @@ class FinancialDao extends BaseDao
|
||||
* @param $financialType
|
||||
* @param $merId
|
||||
*/
|
||||
public function platformIn($number, $financialType, $merId = '')
|
||||
public function platformIn($number, $financialType, $merId = 0)
|
||||
{
|
||||
$this->setData($number, $financialType, 1, 2, $merId);
|
||||
}
|
||||
|
@ -305,8 +305,10 @@ class StoreOrderRepository extends BaseRepository
|
||||
$financeDao->user = $groupOrder->user;
|
||||
$financialType = $presell ? 'order_presell' : 'order';
|
||||
// 平台订单收入流水账单数据
|
||||
$financeDao->platformIn($order->total_price, $financialType, 0);
|
||||
$financeDao->platformOut($order->consumption_money, 'platform_consumption', 0);
|
||||
$financeDao->platformIn($order->total_price, $financialType);
|
||||
if ($order->consumption_money > 0) {
|
||||
$financeDao->platformOut($order->consumption_money, 'platform_consumption');
|
||||
}
|
||||
|
||||
if ($order->source == 103) {
|
||||
$_payPrice = $order->procure_price;
|
||||
|
@ -19,6 +19,7 @@ use app\common\dao\store\consumption\StoreConsumptionUserDao;
|
||||
use app\common\dao\store\order\StoreOrderOtherDao;
|
||||
use app\common\dao\store\order\StoreRefundOrderDao;
|
||||
use app\common\dao\store\StoreActivityOrderDao;
|
||||
use app\common\dao\system\financial\FinancialDao;
|
||||
use app\common\model\store\order\StoreGroupOrder;
|
||||
use app\common\model\store\order\StoreOrder;
|
||||
use app\common\model\store\order\StoreRefundOrder;
|
||||
@ -923,7 +924,6 @@ class StoreRefundOrderRepository extends BaseRepository
|
||||
if ($refund['refund_type'] == 1) {
|
||||
//TODO 退款单同意退款
|
||||
$refund = $this->doRefundPrice($id, $_refund_price);
|
||||
(new StoreOrderOtherDao())->refund($id);
|
||||
$data['status'] = 3;
|
||||
$orderStatus['change_message'] = '退款成功';
|
||||
$orderStatus['change_type'] = $storeOrderStatusRepository::ORDER_STATUS_CREATE;
|
||||
@ -1190,6 +1190,8 @@ class StoreRefundOrderRepository extends BaseRepository
|
||||
|
||||
(new CommissionDao())->refundByOrder($refundOrder);
|
||||
|
||||
(new StoreOrderOtherDao())->refund($refundOrder);
|
||||
|
||||
$productIds = CloudProduct::where('activity_id', 2)->column('product_id');
|
||||
StoreActivityOrderProduct::where('user_id', $refundOrder->order['uid'])
|
||||
->whereIn('product_id', $productIds)
|
||||
@ -1332,8 +1334,6 @@ class StoreRefundOrderRepository extends BaseRepository
|
||||
$_refundRate = bcmul($commission_rate, bcsub($item['data']['refund_price'], $extension, 2), 2);
|
||||
$refundRate = bcadd($refundRate, $_refundRate, 2);
|
||||
}
|
||||
$margin = FinancialRecord::getInstance()->where('mer_id', $res->mer_id)->where('order_sn', $item['sn'])->where('financial_type', 'auto_margin')->value('number');
|
||||
$refundPrice = bcsub($refundPrice, $margin, 2);
|
||||
$refundPriceAll = bcadd($refundPriceAll, $refundPrice, 2);
|
||||
|
||||
try {
|
||||
@ -1347,16 +1347,17 @@ class StoreRefundOrderRepository extends BaseRepository
|
||||
if (in_array($item['type'], [4, 5])) $server = AlipayService::create();
|
||||
if (in_array($item['type'], [1, 3, 6])) $server = WechatService::create();
|
||||
$server->payOrderRefund($item['sn'], $item['data']);
|
||||
if ($item['type'] == 10) {
|
||||
$make = app()->make(StoreOrderProfitsharingRepository::class);
|
||||
if ($orderType === 'presell') {
|
||||
$make->refundPresallPrice($res, $item['data']['refund_price'], $refundPrice);
|
||||
} else {
|
||||
$make->refundPrice($res, $item['data']['refund_price'], $refundPrice);
|
||||
}
|
||||
} else {
|
||||
app()->make(MerchantRepository::class)->subLockMoney($res->mer_id, $orderType, $item['id'], $refundPrice);
|
||||
}
|
||||
// 目前是代销模式,下单的店铺不需要退钱,由供应链退款
|
||||
// if ($item['type'] == 10) {
|
||||
// $make = app()->make(StoreOrderProfitsharingRepository::class);
|
||||
// if ($orderType === 'presell') {
|
||||
// $make->refundPresallPrice($res, $item['data']['refund_price'], $refundPrice);
|
||||
// } else {
|
||||
// $make->refundPrice($res, $item['data']['refund_price'], $refundPrice);
|
||||
// }
|
||||
// } else {
|
||||
// app()->make(MerchantRepository::class)->subLockMoney($res->mer_id, $orderType, $item['id'], $refundPrice);
|
||||
// }
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
throw new ValidateException($e->getMessage());
|
||||
@ -1364,25 +1365,13 @@ class StoreRefundOrderRepository extends BaseRepository
|
||||
}
|
||||
}
|
||||
|
||||
app()->make(FinancialRecordRepository::class)->inc([
|
||||
'order_id' => $res->refund_order_id,
|
||||
'order_sn' => $res->refund_order_sn,
|
||||
'user_info' => $res->user->nickname,
|
||||
'user_id' => $res->uid,
|
||||
'financial_type' => 'refund_true',
|
||||
'number' => $refundPriceAll,
|
||||
'type' => 1,
|
||||
], $res->mer_id);
|
||||
|
||||
app()->make(FinancialRecordRepository::class)->inc([
|
||||
'order_id' => $res->refund_order_id,
|
||||
'order_sn' => $res->refund_order_sn,
|
||||
'user_info' => $res->user->nickname,
|
||||
'user_id' => $res->uid,
|
||||
'type' => 1,
|
||||
'financial_type' => 'refund_charge',
|
||||
'number' => $refundRate,
|
||||
], $res->mer_id);
|
||||
$financeDao = new FinancialDao();
|
||||
$financeDao->order = $res->order;
|
||||
$financeDao->order->order_id = $res->refund_order_id;
|
||||
$financeDao->order->order_sn = $res->refund_order_sn;
|
||||
$financeDao->user = $res->user;
|
||||
$financeDao->platformOut($refundPriceAll, 'order_refund');
|
||||
$financeDao->save();
|
||||
|
||||
// 业务流程调整,暂时注释
|
||||
// event('refund.after', compact('id', 'res'));
|
||||
|
Loading…
x
Reference in New Issue
Block a user