diff --git a/app/common/repositories/system/merchant/MerchantRepository.php b/app/common/repositories/system/merchant/MerchantRepository.php index 13c2f6be..393d8d43 100644 --- a/app/common/repositories/system/merchant/MerchantRepository.php +++ b/app/common/repositories/system/merchant/MerchantRepository.php @@ -15,23 +15,25 @@ namespace app\common\repositories\system\merchant; use app\common\dao\system\merchant\MerchantDao; +use app\common\dao\system\serve\ServeOrderDao; use app\common\model\store\order\StoreOrder; use app\common\model\store\product\ProductReply; use app\common\model\system\merchant\Merchant; use app\common\repositories\BaseRepository; use app\common\repositories\store\coupon\StoreCouponRepository; use app\common\repositories\store\coupon\StoreCouponUserRepository; +use app\common\repositories\store\order\StoreOrderRepository; use app\common\repositories\store\product\ProductCopyRepository; use app\common\repositories\store\product\ProductRepository; use app\common\repositories\store\product\SpuRepository; use app\common\repositories\store\shipping\ShippingTemplateRepository; use app\common\repositories\store\StoreCategoryRepository; use app\common\repositories\system\attachment\AttachmentRepository; +use app\common\repositories\system\serve\ServeOrderRepository; use app\common\repositories\user\UserBillRepository; use app\common\repositories\user\UserRelationRepository; use app\common\repositories\user\UserVisitRepository; use app\common\repositories\wechat\RoutineQrcodeRepository; -use crmeb\jobs\AutoMarginJob; use crmeb\jobs\ClearMerchantStoreJob; use crmeb\services\QrcodeService; use crmeb\services\UploadService; @@ -691,7 +693,35 @@ class MerchantRepository extends BaseRepository 'mer_id' => $this->merId, 'financial_record_sn' => $financeSn . $index ]; - Queue::push(AutoMarginJob::class, ['merId' => $this->merId, 'margin' => $margin, 'orderId' => $order['order_id']]); + + $orderInfo = [ + 'type_id' => $merchant['type_id'], + 'is_margin' => $merchant['is_margin'], + 'margin' => $margin, + ]; + $values = [ + 'status' => 1, + 'is_del' => 0, + 'mer_id' => $merchant['mer_id'], + 'type' => ServeOrderRepository::TYPE_MARGIN, + 'meal_id'=> $merchant['type_id'], + 'pay_type' => ServeOrderRepository::PAY_TYPE_BALANCE, + 'order_info' => json_encode($orderInfo,JSON_UNESCAPED_UNICODE), + 'pay_price' => $margin, + 'store_order_id' => $order['order_id'], + ]; + $values['order_sn'] = app()->make(StoreOrderRepository::class)->getNewOrderId('cs'); + $values['pay_time'] = date('y_m-d H:i:s', time()); + if (!app()->make(ServeOrderDao::class)->create($values)) { + throw new \Exception('serve_order 保存出错'); + } + $merchant->paid_margin = bcadd($margin, $merchant->paid_margin, 2); + $merchant->ot_margin = $merchant->paid_margin; + $merchant->is_margin = MerchantRepository::PaidMargin; + if ($merchant->save() === false) { + throw new \Exception('merchant 保存出错'); + } + return [$income, $finance, true]; } diff --git a/app/listener/paySuccessOrder.php b/app/listener/paySuccessOrder.php index e1a8f3fc..4649c99f 100644 --- a/app/listener/paySuccessOrder.php +++ b/app/listener/paySuccessOrder.php @@ -27,95 +27,107 @@ class paySuccessOrder $this->event = $event; $this->finance = []; $this->index = 1; - $financialRecordRepository = app()->make(FinancialRecordRepository::class); - $this->financeSn = $financialRecordRepository->getSn(); - $merchant = Merchant::find($event['order']['mer_id']); + Db::startTrans(); + try { + $financialRecordRepository = app()->make(FinancialRecordRepository::class); + $this->financeSn = $financialRecordRepository->getSn(); + $merchant = Merchant::find($event['order']['mer_id']); - if (!$merchant || $merchant['street_id'] == 0) { - Log::error('商户地址不存在'); - return; - } - $this->streetId = $merchant['street_id']; - - $commission_rate = ($event['order']['commission_rate'] / 100); - //该笔订单平台总手续费 - $this->totalAmount = bcmul((string)$event['order']['total_price'], (string)$commission_rate, 2); - $this->remain = $this->totalAmount; - - //镇团队佣金 - $this->calculate(Merchant::TypeTownServer, 'commission_to_town_rate'); - //村团队佣金 - $this->calculate(Merchant::TypeVillageServer, 'commission_to_village_rate'); - //小组服务团队佣金 - $this->calculate(Merchant::TypeTeamServer, 'commission_to_service_team_rate'); - - //订单购物详情表是否有云仓数据 - $orderProduct = Db::name('store_order_product') - ->where('order_id', $event['order']['order_id']) - ->where('is_refund', 0) - ->whereIn('source', [StoreCartDao::SOURCE_STORE_CLOUD, StoreCartDao::SOURCE_CLOUD]) - ->group('source_id') - ->column('source,source_id,sum(product_price) total'); - foreach ($orderProduct as $item) { - $this->totalAmount = bcmul((string)$item['total'], (string)$commission_rate, 2); - if ($this->totalAmount <= 0) { - continue; + if (!$merchant || $merchant['street_id'] == 0) { + throw new \Exception('商户地址不存在', 200); } + $this->streetId = $merchant['street_id']; - //入口商户佣金 - $entryMerId = $item['source_id']; - $entryMerchant = app()->make(MerchantDao::class)->get($entryMerId); - if ($entryMerchant['type_id'] == Merchant::TypeStore) { - $merchantRate = systemConfig('commission_to_merchant_rate'); - $merchantAmount = bcmul($this->totalAmount, (string)($merchantRate / 100), 2); - if ($merchantAmount > 0) { - $this->remain = bcsub($this->remain, $merchantAmount, 2); - $this->finance[] = [ - 'order_id' => $this->event['order']['order_id'], - 'order_sn' => $this->event['order']['order_sn'], - 'user_info' => $this->event['order']->user->nickname, - 'user_id' => $this->event['order']['uid'], - 'financial_type' => 'commission_to_entry_merchant', - 'financial_pm' => 1, - 'type' => 1, - 'number' => $merchantAmount, - 'mer_id' => $entryMerId, - 'financial_record_sn' => $this->financeSn . $this->index - ]; - $this->index++; - /** @var MerchantRepository $merchantRepo */ - $merchantRepo = app()->make(MerchantRepository::class); - $merchantRepo->merId = $entryMerId; - $merchantRepo->forceMargin = true; - [$merchantAmount, $this->finance, $increase] = $merchantRepo->autoMargin($merchantAmount, $event['order'], $this->finance, $this->financeSn, $this->index); - if ($increase) { - $this->index++; - } + $commission_rate = ($event['order']['commission_rate'] / 100); + //该笔订单平台总手续费 + $this->totalAmount = bcmul((string)$event['order']['total_price'], (string)$commission_rate, 2); + $this->remain = $this->totalAmount; + + //镇团队佣金 + $this->calculate(Merchant::TypeTownServer, 'commission_to_town_rate'); + //村团队佣金 + $this->calculate(Merchant::TypeVillageServer, 'commission_to_village_rate'); + //小组服务团队佣金 + $this->calculate(Merchant::TypeTeamServer, 'commission_to_service_team_rate'); + + //订单购物详情表是否有云仓数据 + $orderProduct = Db::name('store_order_product') + ->where('order_id', $event['order']['order_id']) + ->where('is_refund', 0) + ->whereIn('source', [StoreCartDao::SOURCE_STORE_CLOUD, StoreCartDao::SOURCE_CLOUD]) + ->group('source_id') + ->column('source,source_id,sum(product_price) total'); + foreach ($orderProduct as $item) { + $this->totalAmount = bcmul((string)$item['total'], (string)$commission_rate, 2); + if ($this->totalAmount <= 0) { + continue; + } + + //入口商户佣金 + $entryMerId = $item['source_id']; + $entryMerchant = app()->make(MerchantDao::class)->get($entryMerId); + if ($entryMerchant['type_id'] == Merchant::TypeStore) { + $merchantRate = systemConfig('commission_to_merchant_rate'); + $merchantAmount = bcmul($this->totalAmount, (string)($merchantRate / 100), 2); if ($merchantAmount > 0) { - app()->make(MerchantRepository::class)->addLockMoney($entryMerId, 'order', $event['order']['order_id'], (float)$merchantAmount); + $this->remain = bcsub($this->remain, $merchantAmount, 2); + $this->finance[] = [ + 'order_id' => $this->event['order']['order_id'], + 'order_sn' => $this->event['order']['order_sn'], + 'user_info' => $this->event['order']->user->nickname, + 'user_id' => $this->event['order']['uid'], + 'financial_type' => 'commission_to_entry_merchant', + 'financial_pm' => 1, + 'type' => 1, + 'number' => $merchantAmount, + 'mer_id' => $entryMerId, + 'financial_record_sn' => $this->financeSn . $this->index + ]; + $this->index++; + /** @var MerchantRepository $merchantRepo */ + $merchantRepo = app()->make(MerchantRepository::class); + $merchantRepo->merId = $entryMerId; + $merchantRepo->forceMargin = true; + [$merchantAmount, $this->finance, $increase] = $merchantRepo->autoMargin($merchantAmount, $event['order'], $this->finance, $this->financeSn, $this->index); + if ($increase) { + $this->index++; + } + if ($merchantAmount > 0) { + app()->make(MerchantRepository::class)->addLockMoney($entryMerId, 'order', $event['order']['order_id'], (float)$merchantAmount); + } } } + + //云仓佣金 + $this->calculate(Merchant::TypeCloudWarehouse, 'commission_to_cloud_rate'); } - //云仓佣金 - $this->calculate(Merchant::TypeCloudWarehouse, 'commission_to_cloud_rate'); + //平台佣金 + $this->finance[] = [ + 'order_id' => $this->event['order']['order_id'], + 'order_sn' => $this->event['order']['order_sn'], + 'user_info' => $this->event['order']->user->nickname, + 'user_id' => $this->event['order']['uid'], + 'financial_type' => 'commission_to_platform', + 'financial_pm' => 1, + 'type' => 1, + 'number' => $this->remain, + 'mer_id' => 0, + 'financial_record_sn' => $this->financeSn . $this->index + ]; + + if (!$financialRecordRepository->insertAll($this->finance)) { + throw new \Exception('财务流水保存出错'); + } + Db::commit(); + } catch (\Exception $e) { + if ($e->getCode() == 200) { + Db::commit(); + } else { + Db::rollback(); + } + Log::error('', ['code' => $e->getCode(), 'message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]); } - - //平台佣金 - $this->finance[] = [ - 'order_id' => $this->event['order']['order_id'], - 'order_sn' => $this->event['order']['order_sn'], - 'user_info' => $this->event['order']->user->nickname, - 'user_id' => $this->event['order']['uid'], - 'financial_type' => 'commission_to_platform', - 'financial_pm' => 1, - 'type' => 1, - 'number' => $this->remain, - 'mer_id' => 0, - 'financial_record_sn' => $this->financeSn . $this->index - ]; - - $financialRecordRepository->insertAll($this->finance); } public function calculate($type, $field)