调试平台手续费分润

This commit is contained in:
luofei 2023-05-25 16:35:16 +08:00
parent 2070b17459
commit 8b74d9579a
4 changed files with 138 additions and 104 deletions

View File

@ -32,6 +32,19 @@ class Merchant extends BaseModel
const TypeCloudWarehouse = 11; //里海云仓
const TypeSupplyChain = 12; //市级供应链
const TypePlatform = 13; //供销平台
const TypeTeamServer = 14; //小组服务团
const TypeVillageServer = 15; //村服务团队
const TypeTownServer = 16; //镇服务团队
const TypeMap = [
self::TypeStore => '镇街店铺',
self::TypeCloudWarehouse => '里海云仓',
self::TypeSupplyChain => '市级供应链',
self::TypePlatform => '供销平台',
self::TypeTeamServer => '小组服务团',
self::TypeVillageServer => '村服务团队',
self::TypeTownServer => '镇服务团队',
];
/**
* @return string

View File

@ -12,10 +12,8 @@
namespace app\common\repositories\store\order;
use app\common\dao\store\order\StoreOrderDao;
use app\common\dao\system\merchant\FinancialRecordDao;
use app\common\model\store\order\StoreGroupOrder;
use app\common\model\store\order\StoreOrder;
use app\common\model\system\merchant\Merchant;
use app\common\model\user\User;
use app\common\repositories\BaseRepository;
use app\common\repositories\delivery\DeliveryOrderRepository;
@ -38,7 +36,6 @@ use app\common\repositories\user\UserBillRepository;
use app\common\repositories\user\UserBrokerageRepository;
use app\common\repositories\user\UserMerchantRepository;
use app\common\repositories\user\UserRepository;
use crmeb\jobs\AutoMarginJob;
use crmeb\jobs\PayGiveCouponJob;
use crmeb\jobs\ProductImportJob;
use crmeb\jobs\SendSmsJob;
@ -350,7 +347,10 @@ class StoreOrderRepository extends BaseRepository
];
}
if ($_payPrice > 0) {
$this->autoMargin($_payPrice, $order, $finance, $financeSn, $i);
/** @var MerchantRepository $merchantRepo */
$merchantRepo = app()->make(MerchantRepository::class);
$merchantRepo->merId = $order->mer_id;
[$_payPrice, $finance] = $merchantRepo->autoMargin($_payPrice, $order, $finance, $financeSn, $i++);
}
$finance[] = [
'order_id' => $order->order_id,
@ -456,39 +456,6 @@ class StoreOrderRepository extends BaseRepository
}
/**
* 自动扣除保证金
* @param $income
* @param $order
* @param $finance
* @param $financeSn
* @param $index
* @return void
*/
public function autoMargin(&$income, $order, &$finance, $financeSn, $index = 0)
{
$merchant = Merchant::find($order->mer_id);
//商户保证金未完全缴纳且设置了自动扣除比例
if ($merchant['margin'] > $merchant['paid_margin'] && $merchant['auto_margin_rate'] > 0 && $merchant['auto_margin_rate'] <= 100) {
$margin = bcmul($income, $merchant['auto_margin_rate'] / 100, 2);
$margin = min(bcsub($merchant['margin'], $merchant['paid_margin'], 2), $margin);
$income = max(bcsub($income, $margin, 2), 0);
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $order->user->nickname,
'user_id' => $order->uid,
'financial_type' => 'auto_margin',
'financial_pm' => 0,
'type' => 1,
'number' => $margin,
'mer_id' => $order->mer_id,
'financial_record_sn' => $financeSn . $index
];
Queue::push(AutoMarginJob::class, ['merId' => $order->mer_id, 'margin' => $margin]);
}
}
/**
* 自动打印
* @Author:Qinii

View File

@ -17,6 +17,7 @@ namespace app\common\repositories\system\merchant;
use app\common\dao\system\merchant\MerchantDao;
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;
@ -30,6 +31,7 @@ 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;
@ -63,6 +65,9 @@ class MerchantRepository extends BaseRepository
const RefundMargin = -1; //申请退还保证金
const RefuseMargin = -10; //拒绝退还保证金
public $merId;
public $forceMargin = false; //强制扣除保证金
/**
* MerchantRepository constructor.
* @param MerchantDao $dao
@ -653,5 +658,42 @@ class MerchantRepository extends BaseRepository
});
}
/**
* 自动扣除保证金
* @param $income
* @param $order
* @param $finance
* @param $financeSn
* @param $index
* @return array
*/
public function autoMargin($income, $order, $finance, $financeSn, $index = 0)
{
$merchant = Merchant::find($this->merId);
//商户保证金未完全缴纳且设置了自动扣除比例
if ($merchant['margin'] <= $merchant['paid_margin'] || ($this->forceMargin === false && $merchant['auto_margin_rate'] <= 0 && $merchant['auto_margin_rate'] > 100)) {
return [$income, $finance];
}
$rate = $this->forceMargin ? 100 : $merchant['auto_margin_rate'];
//商户保证金未完全缴纳且设置了自动扣除比例
$margin = bcmul($income, $rate / 100, 2);
$margin = min(bcsub($merchant['margin'], $merchant['paid_margin'], 2), $margin);
$income = max(bcsub($income, $margin, 2), 0);
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $order->user->nickname,
'user_id' => $order->uid,
'financial_type' => 'auto_margin',
'financial_pm' => 0,
'type' => 1,
'number' => $margin,
'mer_id' => $this->merId,
'financial_record_sn' => $financeSn . $index
];
Queue::push(AutoMarginJob::class, ['merId' => $this->merId, 'margin' => $margin]);
return [$income, $finance];
}
}

View File

@ -14,91 +14,103 @@ use think\facade\Queue;
class paySuccessOrder
{
public $totalAmount;
public $event;
public $finance = [];
public $streetId;
public $financeSn;
public $index = 1;
public function handle($event)
{
$this->event = $event;
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
$financeSn = $financialRecordRepository->getSn();
$this->financeSn = $financialRecordRepository->getSn();
$merchant = Merchant::find($event['order']['mer_id']);
if (!$merchant || $merchant['street_id'] == 0) {
Log::error('商户地址不存在');
return;
}
//小组服务团队mer_id
$mer_id = Db::name('merchant')->where('type_id', 14)
->where('street_id', $merchant['street_id'])
->where('status', 1)
->where('mer_state', 1)
->field('mer_id')->find();
$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) {
$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);
//订单购物详情表云仓数据手续费
$_order_rate = bcmul((string)$store_order_product_price, (string)$commission_rate, 2);
$this->autoMargin($source_id, $_order_rate, $event['order'], $financeSn);
if ($store_order_product_price <= 0) {
return true;
}
$nickname=Db::name('nickname')->where('uid',$event['order']['uid'])->value('nickname');
//小组服务团队
$GroupServiceTeam=bcmul($event['_order_rate'], "0.2", 2);
//镇管理人员
$TownshipManagement=bcmul($event['_order_rate'], "0.2", 2);
$finance[] = [
'order_id' => $event['order']['order_id'],
'order_sn' => $event['order']['order_sn'],
'user_info' => $nickname,
'user_id' => $event['order']['uid'],
'financial_type' => 'group_service_team',
'financial_pm' => 1,
'type' => 1,
'number' => $GroupServiceTeam,
'mer_id' => $event['order']['mer_id'],
'financial_record_sn' => $financeSn . 0
];
$finance[] = [
'order_id' => $event['order']['order_id'],
'order_sn' => $event['order']['order_sn'],
'user_info' => $nickname,
'user_id' => $event['order']['uid'],
'financial_type' => 'group_service_team',
'financial_pm' => 1,
'type' => 1,
'number' => $TownshipManagement,
'mer_id' => $event['order']['mer_id'],
'financial_record_sn' => $financeSn . 0
];
app()->make(MerchantRepository::class)->addLockMoney($mer_id, 'order', $event['order']['order_id'], $GroupServiceTeam);
app()->make(MerchantRepository::class)->addLockMoney($mer_id, 'order', $event['order']['order_id'], $TownshipManagement);
$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);
$financialRecordRepository->insertAll($finance);
}
public function autoMargin($mer_id, $income, $order, $financeSn)
{
$merchant = Merchant::find($mer_id);
//商户保证金未完全缴纳且设置了自动扣除比例
if ($merchant['margin'] > $merchant['paid_margin'] && $merchant['auto_margin_rate'] > 0 && $merchant['auto_margin_rate'] <= 100) {
$margin = bcmul($income, (string)$merchant['auto_margin_rate'], 2);
$margin = min(bcsub($merchant['margin'], $merchant['paid_margin'], 2), $margin);
$income = max(bcsub($income, $margin, 2), 0);
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $order->user->nickname,
'user_id' => $order->uid,
'financial_type' => 'cloud_merchant_auto_margin',
//入口商户佣金
$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' => $margin,
'mer_id' => $order->mer_id,
'financial_record_sn' => $financeSn . 0
'number' => $merchantAmount,
'mer_id' => $this->event['order']['mer_id'],
'financial_record_sn' => $this->financeSn . $this->index
];
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
$financialRecordRepository->insertAll($finance);
Queue::push(AutoMarginJob::class, ['merId' => $order->mer_id, 'margin' => $margin]);
$this->index++;
app()->make(MerchantRepository::class)->addLockMoney($source_id, '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');
$financialRecordRepository->insertAll($this->finance);
}
public function calculate($type, $field)
{
$merId = Db::name('merchant')->where('type_id', $type)
->where('street_id', $this->streetId)
->where('status', 1)
->where('mer_state', 1)
->value('mer_id');
$rate = systemConfig($field);
$typeName = Merchant::TypeMap[$type];
if (empty($merId) || $rate <= 0) {
Log::info("订单分佣:没有 $typeName 或比例为0");
return false;
}
$amount = bcmul($this->totalAmount, (string)($rate / 100), 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' => 'cloud_warehouse_commission',
'financial_pm' => 1,
'type' => 1,
'number' => $amount,
'mer_id' => $this->event['order']['mer_id'],
'financial_record_sn' => $this->financeSn . $this->index
];
$this->index++;
app()->make(MerchantRepository::class)->addLockMoney($merId, 'order', $this->event['order']['order_id'], (float)$amount);
return true;
}
}