添加:省公司区域提成奖励权重值

This commit is contained in:
wuhui_zzw 2023-10-24 17:10:27 +08:00
parent 5448537816
commit 83e48188ef
4 changed files with 176 additions and 89 deletions

View File

@ -12,7 +12,7 @@ use Yunshop\Hotel\common\models\HotelSetting;
use Yunshop\StoreCashier\common\models\CashierGoods;
use Yunshop\StoreCashier\common\models\StoreSetting;
use Yunshop\StoreCashier\store\models\StoreGoods;
use Yunshop\WeightValue\models\WeightValue;
class OrderCreatedNewService
{
@ -129,90 +129,101 @@ class OrderCreatedNewService
public function addAreaDividendData($area_agents,$areaLevel)
{
if ($area_agents->isEmpty()) {
\Log::debug('区域分红' . $this->order->id .$areaLevel.'团队为空');
return ;
try{
if ($area_agents->isEmpty()) {
\Log::debug('区域分红' . $this->order->id .$areaLevel.'团队为空');
return ;
}
$same_level_num = $area_agents->count('id'); //同级人数
\Log::debug('区域分红' . $this->order->id .$areaLevel.'同级人数'.$same_level_num);
$areaDividendData = [
'uniacid' => \YunShop::app()->uniacid,
'order_sn' => $this->order->order_sn,
'order_id' => $this->order->id,
// 'amount' => $amount,
'order_amount' => $this->order->price,
'settle_days' => $this->getSet()['settle_days'] ? : 0,
'create_month' => date('Y-m'),
'same_level_number' => $same_level_num,
'created_at' => $this->order->create_time->timestamp,
'extra_plugin_id' => $this->extra_plugin_id,
'extra_id' => $this->extra_id,
];
if (app('plugins')->isEnabled('coupon-store') && $this->extra_plugin_id == 144) {
$coupon_count = \Yunshop\CouponStore\models\Coupon::uniacid()->where('order_id', $this->order->id)->count();
$areaDividendData['order_amount'] = bcdiv($this->order->price, $coupon_count, 2);
}
foreach ($area_agents as $agent) {
if($agent['agent_at'] > $areaDividendData['created_at']){
\Log::debug("区域分红{$this->order->id}: {$areaLevel}{$agent['id']}成为区域代理的时间大于下单时间");
continue;
}
\Log::debug("省公司权重值处理 - 开始处理",['uid'=>$agent['member_id'],'order_id'=>$areaDividendData['order_id']]);
(new WeightValue())->areaGiveInit($agent['member_id'],$this->order);
$exist = AreaDividend::where([
'member_id' => $agent['member_id'],
'area_level' => $agent['agent_level'],
'order_sn' => $areaDividendData['order_sn'],
'extra_plugin_id' => $areaDividendData['extra_plugin_id'],
'extra_id' => $areaDividendData['extra_id'],
])->count();
if ($exist) {
\Log::debug("区域分红 订单{$areaDividendData['order_sn']}:", "{$agent['member_id']}的分红记录已存在");
continue;
}
$areaDividendData['member_id'] = $agent['member_id'];
$areaDividendData['area_level'] = $agent['agent_level'];
$areaDividendData['agent_area'] = $this->getAreaName($agent);
$areaDividendData['dividend_rate'] = $this->dividendRate($agent);
$areaDividendData['lower_level_rate'] = $this->lowerLevelRate($agent);
$dividend_amount = $this->getDividendAmount($areaDividendData,$agent);
if ($dividend_amount <= 0) {
\Log::debug("区域分红 订单{$areaDividendData['order_sn']}:", "{$agent['member_id']}的分红金额为0");
continue;
}
if ($this->alone_dividend_rate !== false) {
$areaDividendData['dividend_rate'] = $this->alone_dividend_rate;
$areaDividendData['lower_level_rate'] = $this->lower_level_rate;
}
$areaDividendData['dividend_amount'] = $dividend_amount;
$areaDividendData['amount'] = $this->amount;
//todo 防止多队列支付先走
$order = \app\common\models\Order::find($this->order->id);
if ($order->status >= 3) {
$areaDividendData['recrive_at'] = strtotime($order->finish_time);
}
$dividendModel = AreaDividend::create($areaDividendData);
if ($this->is_fix) {
\Log::debug("区域分红 订单{$areaDividendData['order_sn']}:", "is_fix");
continue;
}
event(new CreatedAreaDividendBonusEvent($this->order, $agent['member_id'], $areaDividendData['dividend_amount'], $dividendModel->id));
OrderCreatedService::setAareaDividend($areaDividendData, $agent);
\Log::debug("区域分红 订单{$areaDividendData['order_sn']}:", "{$agent['member_id']}的分红记录生成成功");
}
if ($this->commission !== false) {
//$this->commission = (!empty($dividend_amount) && $dividend_amount>0) ? $dividend_amount : $this->commission;
// 区级 3(分红a1) - 1(分销) = 2(分红a2)
// 市级 10(分红b1) - (1(分销) + 2(分红a2) = 7(分红b2))
// 省级 40(分红c1) - (7(分红b2) + 3(分红a1)) = 30
$this->commission += $dividend_amount;
}
}catch(\Exception $e){
\Log::debug('区域分红 - addAreaDividendData 错误抛出',$e->getMessage());
}
$same_level_num = $area_agents->count('id'); //同级人数
\Log::debug('区域分红' . $this->order->id .$areaLevel.'同级人数'.$same_level_num);
$areaDividendData = [
'uniacid' => \YunShop::app()->uniacid,
'order_sn' => $this->order->order_sn,
'order_id' => $this->order->id,
// 'amount' => $amount,
'order_amount' => $this->order->price,
'settle_days' => $this->getSet()['settle_days'] ? : 0,
'create_month' => date('Y-m'),
'same_level_number' => $same_level_num,
'created_at' => $this->order->create_time->timestamp,
'extra_plugin_id' => $this->extra_plugin_id,
'extra_id' => $this->extra_id,
];
if (app('plugins')->isEnabled('coupon-store') && $this->extra_plugin_id == 144) {
$coupon_count = \Yunshop\CouponStore\models\Coupon::uniacid()->where('order_id', $this->order->id)->count();
$areaDividendData['order_amount'] = bcdiv($this->order->price, $coupon_count, 2);
}
foreach ($area_agents as $agent) {
if($agent['agent_at'] > $areaDividendData['created_at']){
\Log::debug("区域分红{$this->order->id}: {$areaLevel}{$agent['id']}成为区域代理的时间大于下单时间");
continue;
}
$exist = AreaDividend::where([
'member_id' => $agent['member_id'],
'area_level' => $agent['agent_level'],
'order_sn' => $areaDividendData['order_sn'],
'extra_plugin_id' => $areaDividendData['extra_plugin_id'],
'extra_id' => $areaDividendData['extra_id'],
])->count();
if ($exist) {
\Log::info("订单{$areaDividendData['order_sn']}:", "{$agent['member_id']}的分红记录已存在");
continue;
}
$areaDividendData['member_id'] = $agent['member_id'];
$areaDividendData['area_level'] = $agent['agent_level'];
$areaDividendData['agent_area'] = $this->getAreaName($agent);
$areaDividendData['dividend_rate'] = $this->dividendRate($agent);
$areaDividendData['lower_level_rate'] = $this->lowerLevelRate($agent);
$dividend_amount = $this->getDividendAmount($areaDividendData,$agent);
if ($dividend_amount <= 0) {
continue;
}
if ($this->alone_dividend_rate !== false) {
$areaDividendData['dividend_rate'] = $this->alone_dividend_rate;
$areaDividendData['lower_level_rate'] = $this->lower_level_rate;
}
$areaDividendData['dividend_amount'] = $dividend_amount;
$areaDividendData['amount'] = $this->amount;
//todo 防止多队列支付先走
$order = \app\common\models\Order::find($this->order->id);
if ($order->status >= 3) {
$areaDividendData['recrive_at'] = strtotime($order->finish_time);
}
$dividendModel = AreaDividend::create($areaDividendData);
if ($this->is_fix) {
continue;
}
event(new CreatedAreaDividendBonusEvent($this->order, $agent['member_id'], $areaDividendData['dividend_amount'], $dividendModel->id));
OrderCreatedService::setAareaDividend($areaDividendData, $agent);
\Log::info("订单{$areaDividendData['order_sn']}:", "{$agent['member_id']}的分红记录生成成功");
}
if ($this->commission !== false) {
//$this->commission = (!empty($dividend_amount) && $dividend_amount>0) ? $dividend_amount : $this->commission;
// 区级 3(分红a1) - 1(分销) = 2(分红a2)
// 市级 10(分红b1) - (1(分销) + 2(分红a2) = 7(分红b2))
// 省级 40(分红c1) - (7(分红b2) + 3(分红a1)) = 30
$this->commission += $dividend_amount;
}
}
protected function getAreaName($agent)

View File

@ -30,10 +30,10 @@ class Button{
foreach($list as $handleListIndex => $handleListItem){
$h5link = $handleListItem['h5_link'];
// 判断:当前用户是否存在收藏 不存在隐藏典藏室按钮
if(strpos($h5link,'/member/collection_room') !== false){
$hasCount = (int)CollectionRoomModel::where('member_id',$uid)->count();
if($hasCount <= 0) unset($list[$handleListIndex]);
}
// if(strpos($h5link,'/member/collection_room') !== false){
// $hasCount = (int)CollectionRoomModel::where('member_id',$uid)->count();
// if($hasCount <= 0) unset($list[$handleListIndex]);
// }
// 判断:当前用户是否存在对应等级的权重值信息 存在则显示,不存在则隐藏
if(strpos($h5link,'/member/weight_value_home') !== false && strpos($h5link,'team_level_id=') !== false){
// 获取等级id

View File

@ -141,7 +141,7 @@ class IndexController extends BaseController{
// ->count(DB::raw('DISTINCT(uid)'));
// debug($hasNum);
// (new WeightValue())->areaGiveInit(5,Order::find(161));
debug("调试中...");
}

View File

@ -219,7 +219,7 @@ class WeightValue extends BaseModel{
];
}
/**
* Common: 购买商品赠送权重值 - 获取应得权重值
* Common: 赠送权重值 - 获取应得权重值
* Author: wu-hui
* Time: 2023/10/23 15:28
* @param $currentSale
@ -376,6 +376,82 @@ class WeightValue extends BaseModel{
}
/**
* Common: 区域代理提成 - 开始处理
* Author: wu-hui
* Time: 2023/10/24 16:57
* @param $uid
* @param $model
*/
public function areaGiveInit($uid,$model){
DB::beginTransaction();
try{
// 当前订单全部商品
$goodsList = $this->getGoodsList($model->id);
// 获取当前用户及上级信息 由于是【区域分红】这里只使用本人的信息进行处理 上级&代理商不进行处理
$agents = $this->getParent($uid);
$currentUser = array_values($agents['current_user'])[0];
// 用户当前持有的权重值
$currentAgentHasWeightValue = (float)$currentUser['weight_value']['quantity'] ?? 0;
// 循环处理信息
foreach($goodsList as $goodsInfo){
// 获取当前用户应得的权重值
$totalWeightValue = (float)sprintf("%.2f",$this->getAreaGiveWeightValue($goodsInfo,$currentUser));
// 应得权重值大于0 记录改变信息并且修改持有权重值
if((float)$totalWeightValue > 0){
$changeAfter = sprintf("%.2f",($currentAgentHasWeightValue + $totalWeightValue));
$changeRecord[] = [
'uniacid' => $model->uniacid,
'member_id' => $currentUser['uid'],// 用户id
'team_dividend_agency_level_id' => $currentUser['team_dividend_agency_level_id'],// 经销商等级id
'goods_id' => $goodsInfo['goods_id'],// 商品id
'order_id' => $model->id,// 订单id
'order_goods_id' => $goodsInfo['id'],// 订单商品id
'change_type' => 1,// 变更类型0=减少1=增加
'change_quantity' => $totalWeightValue,// 变更数量
'change_front' => (float)$currentAgentHasWeightValue,// 变更前拥有的数量
'change_after' => $changeAfter,// 变更后拥有的数量
'remark' => "区域提成:订单{$model->order_sn}",// 备注
'created_at' => time(),// 变更时间
'source' => 3,
];
$currentUser['weight_value']['quantity'] = $currentAgentHasWeightValue = $changeAfter;
}
}
if($changeRecord){
self::updateInfo($currentUser['weight_value']);
WeightValueLog::insert($changeRecord);
}
DB::commit();
}catch(\Exception $e){
\Log::debug('------ 省公司权重值处理 - 错误抛出------',$e->getMessage());
DB::rollBack();
}
return;
}
/**
* Common: 区域代理提成 - 获取应得权重值
* Author: wu-hui
* Time: 2023/10/24 16:48
* @param $goodsInfo
* @param $currentUser
* @return float
*/
private function getAreaGiveWeightValue($goodsInfo,$currentUser){
$deservedValue = 0;// 应得权重值(固定值||比例)
if((int)$goodsInfo['province_gift_type'] == 0) $deservedValue = $goodsInfo['province_quantity'];// 赠送固定值
else if($currentUser['teamDividend']) $deservedValue = $this->getRankGive($goodsInfo['province_ladder'],(int)$currentUser['teamDividend']['ranking']);// 阶梯赠送
else $deservedValue = (float)0;
// 判断:是固定值还是比例,固定金额需要乘以商品购买数量,比例根据商品实际支付价格的比例
if((int)$goodsInfo['province_calculation_type'] == 1) return (float)sprintf("%.2f",$goodsInfo['payment_amount'] * $deservedValue / 100);// 实际支付金额的比例
else return (float)sprintf("%.2f",$goodsInfo['total'] * $deservedValue);// 总数量 * 应得权重值 = 总应得权重值
}
/**
* Common: 修改信息
* 必须是包含member_id、team_dividend_agency_level_id、quantity的二维数组