diff --git a/app/common/dao/store/order/StoreCartDao.php b/app/common/dao/store/order/StoreCartDao.php index 846f19d0..2551b93b 100644 --- a/app/common/dao/store/order/StoreCartDao.php +++ b/app/common/dao/store/order/StoreCartDao.php @@ -27,7 +27,8 @@ use think\model\Relation; class StoreCartDao extends BaseDao { - const SOURCE_CLOUD = 101; //店铺云商品 + const SOURCE_STORE_CLOUD = 101; //店铺内云商品 + const SOURCE_CLOUD = 102; //云仓内店铺商品 protected function getModel(): string { diff --git a/app/listener/paySuccessOrder.php b/app/listener/paySuccessOrder.php index 9163844f..f99c1ec5 100644 --- a/app/listener/paySuccessOrder.php +++ b/app/listener/paySuccessOrder.php @@ -3,13 +3,13 @@ declare (strict_types=1); namespace app\listener; +use app\common\dao\store\order\StoreCartDao; +use app\common\dao\system\merchant\MerchantDao; use app\common\model\system\merchant\Merchant; use app\common\repositories\system\merchant\FinancialRecordRepository; use app\common\repositories\system\merchant\MerchantRepository; -use crmeb\jobs\AutoMarginJob; use think\facade\Db; use think\facade\Log; -use think\facade\Queue; class paySuccessOrder { @@ -33,51 +33,61 @@ class paySuccessOrder return; } $this->streetId = $merchant['street_id']; + //订单购物详情表是否有云仓数据 - $store_order_product_price = Db::name('store_order_product')->where('order_id', $event['order']['order_id']) - ->where('is_refund', 0)->where('source', 101)->sum('product_price'); - if ($store_order_product_price <= 0) { - return true; - } - $source_id = Db::name('store_order_product')->where('order_id', $event['order']['order_id'])->where('is_refund', 0)->where('source', 101)->value('source_id'); - $commission_rate = ($event['order']['commission_rate'] / 100); - //该笔订单平台总手续费 - $this->totalAmount = bcmul((string)$store_order_product_price, (string)$commission_rate, 2); + $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) { + $commission_rate = ($event['order']['commission_rate'] / 100); + //该笔订单平台总手续费 + $this->totalAmount = bcmul((string)$item['total'], (string)$commission_rate, 2); + if ($this->totalAmount <= 0) { + continue; + } - //入口商户佣金 - $merchantRate = systemConfig('commission_to_merchant_rate'); - $merchantAmount = bcmul($this->totalAmount, (string)($merchantRate / 100), 2); - /** @var MerchantRepository $merchantRepo */ - $merchantRepo = app()->make(MerchantRepository::class); - $merchantRepo->merId = $source_id; - $merchantRepo->forceMargin = true; - [$merchantAmount, $this->finance] = $merchantRepo->autoMargin($merchantAmount, $event['order'], $this->finance, $this->financeSn, $this->index); - $this->index++; - if ($merchantAmount > 0) { - $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' => 'entry_merchant_commission', - 'financial_pm' => 1, - 'type' => 1, - 'number' => $merchantAmount, - 'mer_id' => $this->event['order']['mer_id'], - 'financial_record_sn' => $this->financeSn . $this->index - ]; - $this->index++; - app()->make(MerchantRepository::class)->addLockMoney($source_id, 'order', $event['order']['order_id'], (float)$merchantAmount); - } + //入口商户佣金 + $entryMerId = $item['source_id']; + $entryMerchant = app()->make(MerchantDao::class)->get($entryMerId); + if ($entryMerchant == Merchant::TypeStore) { + $merchantRate = systemConfig('commission_to_merchant_rate'); + $merchantAmount = bcmul($this->totalAmount, (string)($merchantRate / 100), 2); + /** @var MerchantRepository $merchantRepo */ + $merchantRepo = app()->make(MerchantRepository::class); + $merchantRepo->merId = $entryMerId; + $merchantRepo->forceMargin = true; + [$merchantAmount, $this->finance] = $merchantRepo->autoMargin($merchantAmount, $event['order'], $this->finance, $this->financeSn, $this->index); + $this->index++; + if ($merchantAmount > 0) { + $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' => 'entry_merchant_commission', + 'financial_pm' => 1, + 'type' => 1, + 'number' => $merchantAmount, + 'mer_id' => $this->event['order']['mer_id'], + 'financial_record_sn' => $this->financeSn . $this->index + ]; + $this->index++; + app()->make(MerchantRepository::class)->addLockMoney($entryMerId, 'order', $event['order']['order_id'], (float)$merchantAmount); + } + } - //镇分管人员佣金 - $this->calculate(Merchant::TypeTeamServer, 'commission_to_town_manager_rate'); - //小组服务团队佣金 - $this->calculate(Merchant::TypeVillageServer, 'commission_to_town_team_rate'); - //镇级云仓佣金 - $this->calculate(Merchant::TypeTownServer, 'commission_to_cloud_rate'); - //镇级云仓佣金 - $this->calculate(Merchant::TypeCloudWarehouse, 'commission_to_cloud_rate'); + //镇团队佣金 + $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'); + //云仓佣金 + $this->calculate(Merchant::TypeTownServer, 'commission_to_cloud_rate'); + } $financialRecordRepository->insertAll($this->finance); }