diff --git a/app/common/repositories/store/order/StoreRefundOrderRepository.php b/app/common/repositories/store/order/StoreRefundOrderRepository.php index 523acd0e..5d014f67 100644 --- a/app/common/repositories/store/order/StoreRefundOrderRepository.php +++ b/app/common/repositories/store/order/StoreRefundOrderRepository.php @@ -1293,6 +1293,9 @@ class StoreRefundOrderRepository extends BaseRepository 'financial_type' => 'refund_charge', 'number' => $refundRate, ], $res->mer_id); + + event('refund.after', compact('id', 'res')); + return $res; } diff --git a/app/common/repositories/system/merchant/MerchantRepository.php b/app/common/repositories/system/merchant/MerchantRepository.php index 27492c7b..13c2f6be 100644 --- a/app/common/repositories/system/merchant/MerchantRepository.php +++ b/app/common/repositories/system/merchant/MerchantRepository.php @@ -691,7 +691,7 @@ class MerchantRepository extends BaseRepository 'mer_id' => $this->merId, 'financial_record_sn' => $financeSn . $index ]; - Queue::push(AutoMarginJob::class, ['merId' => $this->merId, 'margin' => $margin]); + Queue::push(AutoMarginJob::class, ['merId' => $this->merId, 'margin' => $margin, 'orderId' => $order['order_id']]); return [$income, $finance, true]; } diff --git a/app/event.php b/app/event.php index 5d49b7c4..f9f7d132 100644 --- a/app/event.php +++ b/app/event.php @@ -66,6 +66,7 @@ return [ 'order.paySuccessOrder'=>[\app\listener\paySuccessOrder::class], 'product.create'=>[\app\listener\ProductCreate::class], 'product.sell'=>[\app\listener\CloudProduct::class], //商品上下架 + 'refund.after'=>[\app\listener\AfterRefund::class], ], 'subscribe' => [], diff --git a/app/listener/AfterRefund.php b/app/listener/AfterRefund.php new file mode 100644 index 00000000..50424fcd --- /dev/null +++ b/app/listener/AfterRefund.php @@ -0,0 +1,74 @@ +refundOrder = $event['res']; + $financialRecords = FinancialRecord::getInstance()->where('order_id', $this->refundOrder['order_id'])->select(); + Log::info('refundCommissionCount:' . count($financialRecords)); + foreach ($financialRecords as $financialRecord) { + if (in_array($financialRecord['financial_type'], ['commission_to_cloud_warehouse', 'commission_to_entry_merchant', 'commission_to_service_team', 'commission_to_village', 'commission_to_town', ])) { + //佣金类型的退还佣金 + $this->subMoney($financialRecord); + $this->saveFinanceRecord($financialRecord); + } + if ($financialRecord['financial_type'] == 'auto_margin') { + Log::info("refundMargin, mer_id: {$financialRecord['mer_id']}, money: {$financialRecord['number']}"); + //佣金类型的扣除保证金 + ServeOrder::getInstance()->where('store_order_id', $financialRecord['order_id'])->update(['is_del' => 1]); + $merchant = app()->make(MerchantDao::class)->get($financialRecord['mer_id']); + $merchant->paid_margin = max(bcsub($merchant['paid_margin'], $financialRecord['number'], 2), 0); + $merchant->ot_margin = $merchant->paid_margin; + if ($merchant->paid_margin <= 0) { + $merchant->is_margin = MerchantRepository::NeedMargin; + } + $merchant->save(); + $this->saveFinanceRecord($financialRecord); + } + } + Log::info('refundCommissionEnd'); + } + + public function subMoney($financialRecord) + { + Log::info("refundCommission, mer_id: {$financialRecord['mer_id']}, money: {$financialRecord['number']}"); + /** @var MerchantRepository $merchantRepository */ + $merchantRepository = app()->make(MerchantRepository::class); + $merchantRepository->subLockMoney($financialRecord['mer_id'], 'order', $this->refundOrder['order_id'], (float)$financialRecord['number']); + } + + public function saveFinanceRecord($financialRecord) + { + /** @var FinancialRecordRepository $financialRecordRepository */ + $financialRecordRepository = app()->make(FinancialRecordRepository::class); + $financialRecordRepository->dec([ + 'order_id' => $this->refundOrder->refund_order_id, + 'order_sn' => $this->refundOrder->refund_order_sn, + 'user_info' => $this->refundOrder->user->nickname, + 'user_id' => $this->refundOrder->uid, + 'type' => 1, + 'financial_type' => $financialRecord['financial_type'] . '_refund', + 'number' => $financialRecord['number'], + ], $financialRecord['mer_id']); + } + +} diff --git a/crmeb/jobs/AutoMarginJob.php b/crmeb/jobs/AutoMarginJob.php index 8d757198..98daa4e5 100644 --- a/crmeb/jobs/AutoMarginJob.php +++ b/crmeb/jobs/AutoMarginJob.php @@ -43,6 +43,7 @@ class AutoMarginJob implements JobInterface 'pay_type' => ServeOrderRepository::PAY_TYPE_BALANCE, 'order_info' => json_encode($orderInfo,JSON_UNESCAPED_UNICODE), 'pay_price' => $data['margin'], + 'store_order_id' => $data['orderId'], ]; $values['order_sn'] = app()->make(StoreOrderRepository::class)->getNewOrderId('cs'); $values['pay_time'] = date('y_m-d H:i:s', time());