添加:股东分红每个等级可以独立设置分红方式和结算方式

This commit is contained in:
wuhui_zzw 2023-11-03 18:10:54 +08:00
parent 4ab3844682
commit a38500fedf
6 changed files with 211 additions and 135 deletions

View File

@ -0,0 +1,26 @@
<?php
/**
* Author:
* Date: 2017/9/13
* Time: 下午7:00
*/
namespace Yunshop\ShareholderDividend\Listener;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Yunshop\ShareholderDividend\services\TimedTaskService;
class OrderPaidListener{
use DispatchesJobs;
public function subscribe(Dispatcher $events){
$events->listen(\app\common\events\order\AfterOrderPaidEvent::class, function ($event) {
date_default_timezone_set("PRC");
$model = $event->getOrderModel();
(new TimedTaskService())->shareholderDividendOrder($model->id);
});
}
}

View File

@ -4,8 +4,6 @@
namespace Yunshop\ShareholderDividend;
use app\common\services\Plugin;
class PluginApplication extends \app\common\services\PluginApplication
{
public function register()
@ -184,13 +182,12 @@ class PluginApplication extends \app\common\services\PluginApplication
]);
}
public function boot()
{
/*
* 定时任务处理
*
*/
public function boot(){
$events = app('events');
// 订单支付
$events->subscribe(\Yunshop\ShareholderDividend\Listener\OrderPaidListener::class);
}
public function cronConfig()

View File

@ -10,6 +10,7 @@ use app\common\models\Income;
use app\common\models\notice\MessageTemp;
use Yunshop\ShareholderDividend\models\ShareholderDividendModel;
use Yunshop\ShareholderDividend\models\TeamAgentModel;
use Yunshop\ShareholderDividend\services\TimedTaskService;
use Yunshop\TeamDividend\models\TeamDividendLevelModel;
use Illuminate\Support\Facades\Validator;
@ -23,8 +24,9 @@ class SetController extends BaseController
public function fix()
{
// (new TimedTaskService())->shareholderDividendOrder(79);
// 手动触发全网分红处理流程
(new \Yunshop\ShareholderDividend\services\TimedTaskService)->handle();
// (new \Yunshop\ShareholderDividend\services\TimedTaskService)->handle();
dd("全网分红处理完成");

View File

@ -21,9 +21,8 @@ use Yunshop\StoreCashier\common\models\StoreSetting;
*/
class CycleAmounyService
{
public static function getCycleAmount($cycle, $method)
{
// 根据周期 获取分红金额
public static function getCycleAmount($cycle, $method){
switch ($cycle) {
case 0:
$time_end = strtotime(Carbon::today()->toDateTimeString());
@ -44,71 +43,54 @@ class CycleAmounyService
'comment' => "cycle[{$cycle}]method[{$method}]",
'time' => date('Y-m-d H:i:s', time())
]);
$orderStatus = [
Order::WAIT_SEND,
Order::WAIT_RECEIVE,
Order::COMPLETE,
];
$orders = Order::select()->uniacid()
// ->whereBetween('finish_time', [$time_start, $time_end])
// ->where('status', Order::COMPLETE)
->whereBetween('created_at', [$time_start, $time_end])
->whereIn('status',$orderStatus) // 统计已支付-待发货,已支付-待收货,已完成订单
->whereIn('status',[Order::WAIT_SEND,Order::WAIT_RECEIVE,Order::COMPLETE]) // 统计已支付-待发货,已支付-待收货,已完成订单
->get();
$order_ids = $orders->pluck('id');
ExceptionLog::create([
'uniacid' => \YunShop::app()->uniacid,
'comment' => "start[{$time_start}]end[{$time_end}]order_ids[{$order_ids}]",
'time' => date('Y-m-d H:i:s', time())
]);
if ($method) {
$price = $orders->sum(function ($order) {
if (\YunShop::plugin()->get('store-cashier')) {
$cashier = CashierGoods::all();
$store_goods = StoreGoods::with('store')->get();
if ($order->plugin_id == 31) {
return $order->hasManyOrderGoods->sum(function ($order_goods) use ($cashier, $store_goods) {
$cashier_good = $cashier->where('goods_id', $order_goods->goods_id)->first();
if ($cashier_good) {
$price = proportionMath($order_goods->payment_amount,$cashier_good->shop_commission);
if (app('plugins')->isEnabled('consumer-reward') && \Setting::get('plugin.consumer_reward.is_open')==1) {
if (\Setting::get('plugin.consumer_reward.cashier_profit')) {
$price = proportionMath($order_goods->goods_price,$cashier_good->shop_commission);
$price = \Yunshop\ConsumerReward\common\services\AmountDividendService::amountPrice($order_goods->order_id, $price);
}
}
return $price;
}
});
} elseif ($order->plugin_id == 32) {
return $order->hasManyOrderGoods->sum(function ($order_goods) use ($cashier, $store_goods) {
$store_good = $store_goods->where('goods_id', $order_goods->goods_id)->first();
if ($store_good) {
$store_setting = StoreSetting::where('store_id', $store_good->store->id)->where('key', 'store')->first();
$shop_commission = (integer)$store_setting->value['shop_commission'];
$price = proportionMath($order_goods->payment_amount , $shop_commission);
if (app('plugins')->isEnabled('consumer-reward') && \Setting::get('plugin.consumer_reward.is_open')==1) {
if (\Setting::get('plugin.consumer_reward.store_profit')) {
$amount_price = $order_goods->payment_amount - $order_goods->goods_cost_price;
$price = \Yunshop\ConsumerReward\common\services\AmountDividendService::amountPrice($order_goods->order_id, $amount_price);
}
}
return $price;
}
});
} elseif ($order->plugin_id == 39) {
// plugin_id == 39 门店余额充值订单
$balanceOrder = PluginBalanceOrder::select()->where('order_id', $order->id)->first();
$storeSetting = StoreSetting::getStoreSettingByStoreId($balanceOrder->store_id)
->where('key', 'store_balance')
->first();
$storeSettingValue = $storeSetting->value;
return self::amountHandle($orders,$method);
}
return $order->hasManyOrderGoods->sum(function ($order_goods) use ($cashier, $store_goods, $storeSettingValue) {
$price = proportionMath($order_goods->payment_amount, $storeSettingValue['shop_commission']);
// 根据订单 获取分红金额
public static function getOrderAmount($orderId, $method){
$orders = Order::select()->uniacid()
->where('id',$orderId)
->whereIn('status',[Order::WAIT_SEND,Order::WAIT_RECEIVE,Order::COMPLETE]) // 统计已支付-待发货,已支付-待收货,已完成订单
->get();
return self::amountHandle($orders,$method);
}
// 统一处理
public static function amountHandle($orders,$method){
$order_ids = $orders->pluck('id');
if ($method) {
$price = $orders->sum(function ($order) {
if (\YunShop::plugin()->get('store-cashier')) {
$cashier = CashierGoods::all();
$store_goods = StoreGoods::with('store')->get();
if ($order->plugin_id == 31) {
return $order->hasManyOrderGoods->sum(function ($order_goods) use ($cashier, $store_goods) {
$cashier_good = $cashier->where('goods_id', $order_goods->goods_id)->first();
if ($cashier_good) {
$price = proportionMath($order_goods->payment_amount,$cashier_good->shop_commission);
if (app('plugins')->isEnabled('consumer-reward') && \Setting::get('plugin.consumer_reward.is_open')==1) {
if (\Setting::get('plugin.consumer_reward.cashier_profit')) {
$price = proportionMath($order_goods->goods_price,$cashier_good->shop_commission);
$price = \Yunshop\ConsumerReward\common\services\AmountDividendService::amountPrice($order_goods->order_id, $price);
}
}
return $price;
}
});
} elseif ($order->plugin_id == 32) {
return $order->hasManyOrderGoods->sum(function ($order_goods) use ($cashier, $store_goods) {
$store_good = $store_goods->where('goods_id', $order_goods->goods_id)->first();
if ($store_good) {
$store_setting = StoreSetting::where('store_id', $store_good->store->id)->where('key', 'store')->first();
$shop_commission = (integer)$store_setting->value['shop_commission'];
$price = proportionMath($order_goods->payment_amount , $shop_commission);
if (app('plugins')->isEnabled('consumer-reward') && \Setting::get('plugin.consumer_reward.is_open')==1) {
if (\Setting::get('plugin.consumer_reward.store_profit')) {
$amount_price = $order_goods->payment_amount - $order_goods->goods_cost_price;
@ -116,20 +98,39 @@ class CycleAmounyService
}
}
return $price;
});
} else {
return $order->hasManyOrderGoods->sum(function ($order_goods) {
$res = $order_goods->payment_amount - $order_goods->goods_cost_price;
return $res > 0 ? $res : 0;
});
}
}
});
} elseif ($order->plugin_id == 39) {
// plugin_id == 39 门店余额充值订单
$balanceOrder = PluginBalanceOrder::select()->where('order_id', $order->id)->first();
$storeSetting = StoreSetting::getStoreSettingByStoreId($balanceOrder->store_id)
->where('key', 'store_balance')
->first();
$storeSettingValue = $storeSetting->value;
return $order->hasManyOrderGoods->sum(function ($order_goods) use ($cashier, $store_goods, $storeSettingValue) {
$price = proportionMath($order_goods->payment_amount, $storeSettingValue['shop_commission']);
if (app('plugins')->isEnabled('consumer-reward') && \Setting::get('plugin.consumer_reward.is_open')==1) {
if (\Setting::get('plugin.consumer_reward.store_profit')) {
$amount_price = $order_goods->payment_amount - $order_goods->goods_cost_price;
$price = \Yunshop\ConsumerReward\common\services\AmountDividendService::amountPrice($order_goods->order_id, $amount_price);
}
}
return $price;
});
} else {
return $order->hasManyOrderGoods->sum(function ($order_goods) {
$res = $order_goods->payment_amount - $order_goods->goods_cost_price;
return $res > 0 ? $res : 0;
});
}
});
} else {
return $order->hasManyOrderGoods->sum(function ($order_goods) {
$res = $order_goods->payment_amount - $order_goods->goods_cost_price;
return $res > 0 ? $res : 0;
});
}
});
} else {
$price = $orders->sum('price') - $orders->sum('dispatch_price');
}
@ -139,9 +140,9 @@ class CycleAmounyService
$price = $price - $no_count_price;
return $price;
}
public static function getCycleGoodsAmount($method, $order_ids)
{
$goods = GoodsShareholderDividend::uniacid()->where('is_no_count', 1);

View File

@ -285,11 +285,8 @@ class TimedTaskService
}
}
}
// 股东分红 - 每个周期进行结算
private function shareholderDividendV2(){
//判断平台是否有开启 股东分红条件
$condition_dividend = $this->set['condition_dividend'];//1-开启
$condition_level_dividend = $this->set['condition_level_dividend'];//1-开启
$condition_black_list = BlacklistModel::uniacid()->get();//1-开启
$orderAmount = CycleAmounyService::getCycleAmount($this->set['culate_cycle'], $this->set['culate_method']); // 上一个周期金额
if ($orderAmount <= 0) {
\Log::info('========shareholderDividend():没有返现总金额');
@ -300,44 +297,80 @@ class TimedTaskService
]);
return;
}
$this->shareholderDividendHandle($orderAmount);
}
// 股东分红 - 订单支付成功后进行结算
public function shareholderDividendOrder($orderId){
\Log::debug('--- 股东分红 - 订单支付成功后进行结算 ----',$orderId);
$this->set = Setting::get('plugin.shareholder');
// 获取信息
$orderAmount = CycleAmounyService::getOrderAmount($orderId, $this->set['culate_method']);
if ($orderAmount <= 0) {
\Log::info('========shareholderDividend():没有返现总金额');
ExceptionLog::create([
'uniacid' => \YunShop::app()->uniacid,
'comment' => '没有返现总金额',
'time' => date('Y-m-d H:i:s', time())
]);
return;
}
$this->shareholderDividendHandle($orderAmount,'order',$orderId);
}
// 股东分红 - 统一处理
public function shareholderDividendHandle($orderAmount,$settlementType = 'cycle',$orderId = 0){
// 判断平台是否有开启 股东分红条件
$condition_dividend = $this->set['condition_dividend'];//1-开启
$condition_level_dividend = $this->set['condition_level_dividend'];//1-开启
$condition_black_list = BlacklistModel::uniacid()->get();//1-开启
$teamLevels = TeamDividendLevelModel::getList()->get(); // 团队等级
$typeName = $this->set['custom_name'] ?: '股东分红';
// 循环处理
foreach ($teamLevels as $teamLevel) {
if ($this->set['level_rate']['level_' . $teamLevel['id']]) {
// $teamAgents = TeamAgentModel::getTeamAgentByLevelId($teamLevel['id']); // 等级下团队代理
// 每次分红 遍历全部的经销商
$teamAgents = TeamAgentModel::uniacid()
->select([
'yz_team_dividend_agency.id',
'yz_team_dividend_agency.uid',
'yz_team_dividend_agency.parent_id',
'yz_team_dividend_agency.level',
'yz_team_dividend_agency.subordinate_num',
'yz_team_dividend_agency.relation',
'yz_weight_value.quantity'
])
->join('yz_weight_value', 'yz_weight_value.member_id', '=', 'yz_team_dividend_agency.uid')
->where('yz_team_dividend_agency.is_black',0)
->where('yz_weight_value.quantity','>',0)
->where('yz_weight_value.team_dividend_agency_level_id',$teamLevel->id)
->whereHas('hasOneMember');
// 判断:结算方式是否符合条件
$levelSettlementType = (int)$this->set['level_settlement_type']['level_' . $teamLevel['id']];
$rate = (float)$this->set['level_rate']['level_' . $teamLevel['id']]; // 等级分红比例
if ($rate > 0 && (($settlementType == 'cycle' && $levelSettlementType == 0) || ($settlementType == 'order' && $levelSettlementType == 1))) {
$levelBonusType = (float)$this->set['level_bonus_type']['level_' . $teamLevel['id']]; // 分红方式0=按权重值比例1=平均分
if($levelBonusType == 1){
$teamAgents = TeamAgentModel::getTeamAgentByLevelId($teamLevel['id']); // 等级下团队代理
}else{
// 每次分红 遍历全部的经销商
$teamAgents = TeamAgentModel::uniacid()
->select([
'yz_team_dividend_agency.id',
'yz_team_dividend_agency.uid',
'yz_team_dividend_agency.parent_id',
'yz_team_dividend_agency.level',
'yz_team_dividend_agency.subordinate_num',
'yz_team_dividend_agency.relation',
'yz_weight_value.quantity'
])
->join('yz_weight_value', 'yz_weight_value.member_id', '=', 'yz_team_dividend_agency.uid')
->where('yz_team_dividend_agency.is_black',0)
->where('yz_weight_value.quantity','>',0)
->where('yz_weight_value.team_dividend_agency_level_id',$teamLevel->id)
->whereHas('hasOneMember');
}
$teamAgentCount = $teamAgents->count(); // 同等级代理人数
$teamAgentsList = $teamAgents->get();
$rate = $this->set['level_rate']['level_' . $teamLevel['id']]; // 等级分红比例
$lvCountOrderAmount = sprintf("%.2f", $orderAmount / 100 * $rate);// 当前等级共分红金额
// 获取当前等级的全部权重值
foreach ($teamAgentsList as $item) {
/********* 权重值相关操作 START ***************************************************/
// 获取当前经销商的权重值
$currentWeightValue = (float)$item->quantity;
if($currentWeightValue <= 0){
\Log::debug("---- 固定分红 - 当前经销商没有权重值 -----:",['uid'=>$item->uid,'weight_value'=>$currentWeightValue]);
continue;
if($levelBonusType == 1){
// 平均分
$amount = sprintf("%.2f", $lvCountOrderAmount / $teamAgentCount);// 当前经销商实际可得金额
}else{
// 按照权重值比例 获取当前经销商的权重值
$currentWeightValue = (float)$item->quantity;
if($currentWeightValue <= 0){
\Log::debug("---- 固定分红 - 当前经销商没有权重值 -----:",['uid'=>$item->uid,'weight_value'=>$currentWeightValue]);
continue;
}
// 存在权重值 查看当前用户比例
[$totalQuantity,$weightValueRate] = WeightValue::getTotalAndRatio($teamLevel->id,$currentWeightValue);
$amount = sprintf("%.2f", $lvCountOrderAmount * $weightValueRate / 100);// 当前经销商实际可得金额
}
// 存在权重值 查看当前用户比例
[$totalQuantity,$weightValueRate] = WeightValue::getTotalAndRatio($teamLevel->id,$currentWeightValue);
$amount = sprintf("%.2f", $lvCountOrderAmount * $weightValueRate / 100);// 当前经销商实际可得金额
if($amount <= 0){
\Log::debug("---- 固定分红 - 当前经销商没有分红佣金 -----:",['uid'=>$item->uid,'weightValueRate'=>$weightValueRate,'weight_value'=>$currentWeightValue,'amount'=>$amount]);
continue;
@ -394,12 +427,13 @@ class TimedTaskService
'amount' => $check_amount,
'created_at' => time(),
'updated_at' => time(),
'weight_value_total' => $totalQuantity,// 分红时当前等级权重值总数
'weight_value_hold' => $currentWeightValue,// 分红时当前用户持有当前等级的权重值
'weight_value_rate' => $weightValueRate,// 分红时当前用户持有当前等级的权重值比例
'weight_value_total' => $totalQuantity ?? 0,// 分红时当前等级权重值总数
'weight_value_hold' => $currentWeightValue ?? 0,// 分红时当前用户持有当前等级的权重值
'weight_value_rate' => $weightValueRate ?? 0,// 分红时当前用户持有当前等级的权重值比例
'order_id' => $orderId ?? 0,// 订单id仅结算类型为订单才有
'settlement_type' => $levelSettlementType ?? 0,// 结算类型
];
$shareholderDividendModel = new ShareholderDividendModel();
$shareholderDividendModel->fill($shareData);
@ -416,19 +450,18 @@ class TimedTaskService
* 股东分红申请后事件,《股东分红额度》根据分红额度扣除'会员分红额度'
*/
event(new ShareholderDividendAppliedEvent($shareholderDividendModel));
$incomeData = [
'uniacid' => $shareData['uniacid'],
'member_id' => $shareData['member_id'],
'incometable_type' => ShareholderDividendModel::class,
'dividend_code' => IncomeService::SHAREHOLDER_DIVIDEND,
'uniacid' => $shareData['uniacid'],
'member_id' => $shareData['member_id'],
'incometable_type' => ShareholderDividendModel::class,
'dividend_code' => IncomeService::SHAREHOLDER_DIVIDEND,
'dividend_table_id' => $shareholderDividendModel->id,
'type_name' => $typeName,
'amount' => $shareData['amount'],
'status' => 0,
'pay_status' => 0,
'create_month' => date('Y-m'),
'created_at' => time()
'type_name' => $typeName,
'amount' => $shareData['amount'],
'status' => 0,
'pay_status' => 0,
'create_month' => date('Y-m'),
'created_at' => time()
];
$this->addIncome($incomeData);
// $this->dispatch((new addShareholderDividendJob($shareData))); // 添加分红数据
@ -453,6 +486,9 @@ class TimedTaskService
/**
* @param $incomeData
* 增加收入

View File

@ -143,15 +143,29 @@
@foreach($teamLevels as $key => $lelve)
<div class="input-group">
<div class="input-group-addon">{{$lelve->level_name}}</div>
<input type="text" name="setdata[level_rate][level_{{$lelve->id}}]"
class="form-control"
value="{{$set['level_rate']['level_'.$lelve->id]}}"/>
<input type="text" name="setdata[level_rate][level_{{$lelve->id}}]" class="form-control" value="{{$set['level_rate']['level_'.$lelve->id]}}"/>
<div class="input-group-addon">%</div>
<div class="input-group-addon">封顶金额</div>
<input type="text" name="setdata[level_capping][level_{{$lelve->id}}]"
class="form-control"
value="{{$set['level_capping']['level_'.$lelve->id]}}"/>
<input type="text" name="setdata[level_capping][level_{{$lelve->id}}]" class="form-control" value="{{$set['level_capping']['level_'.$lelve->id]}}"/>
<div class="input-group-addon"></div>
<div class='input-group-addon waytxt' style="padding: 0 12px;">
分红方式:
<label class="radio-inline">
<input type="radio" name="setdata[level_bonus_type][level_{{$lelve->id}}]" value="0" @if ((int)$set['level_bonus_type']['level_'.$lelve->id] != 1) checked @endif> 按权重值比例
</label>
<label class="radio-inline">
<input type="radio" name="setdata[level_bonus_type][level_{{$lelve->id}}]" value="1" @if ((int)$set['level_bonus_type']['level_'.$lelve->id] == 1) checked @endif> 平均分配
</label>
</div>
<div class='input-group-addon waytxt' style="padding: 0 12px;">
结算方式:
<label class="radio-inline">
<input type="radio" name="setdata[level_settlement_type][level_{{$lelve->id}}]" value="0" @if ((int)$set['level_settlement_type']['level_'.$lelve->id] != 1) checked @endif> 周期结算
</label>
<label class="radio-inline">
<input type="radio" name="setdata[level_settlement_type][level_{{$lelve->id}}]" value="1" @if ((int)$set['level_settlement_type']['level_'.$lelve->id] == 1) checked @endif> 订单支付后
</label>
</div>
</div>
@endforeach
</div>