| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- <?php
- namespace app\models;
- use yii\behaviors\TimestampBehavior;
- /**
- * This is the model class for table "{{%team_grades_pool_detail}}".
- *
- * @property integer $id
- * @property integer $pool_id
- * @property integer $user_id
- * @property float $amount
- * @property float $profit
- * @property float $price
- * @property integer $level
- * @property integer $is_send
- * @property integer $send_time
- * @property integer $is_delete
- * @property integer $created_at
- * @property integer $updated_at
- */
- class TeamGradesPoolDetail extends \yii\db\ActiveRecord
- {
- const SEND_STATUS_NO = 0;
- const SEND_STATUS_YES = 1;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%team_grades_pool_detail}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['id', 'user_id', 'is_send', 'send_time', 'created_at', 'updated_at', 'pool_id', 'is_delete', 'level'], 'integer'],
- [['amount', 'price', 'profit'], 'number']
- ];
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => '',
- 'pool_id' => '奖金池id',
- 'user_id' => '用户id',
- 'amount' => '分红金额',
- 'profit' => '分红比例',
- 'price' => '分红实际金额',
- 'level' => '分红时候的等级',
- 'is_send' => '是否发放',
- 'send_time' => '发放时间',
- 'is_delete' => '',
- 'created_at' => '',
- 'updated_at' => '',
- ];
- }
- /**
- * 整体流程
- * 从下单用户开始往上级找 作为一条分红记录
- * 比如当前推荐关系为 A->B->C->D->E
- * 如果E下单了商品,则A、B、C、D、E均为分红团队成员则每个人的的业绩分红金额分别增加商品中设置的分红金额
- * 如果B的等级大于A的等级 则A的分红金额不增加
- *
- * 代码流程
- * 从下单人开始将下单用户作为开团队分红的一支记录 pool
- * 寻找下单人的上级 直到上级为0 将所有的上级放入 pool_detail,如果pool_detail存在且上级为团队分红人员则增加上级分红金额且增加记录pool_detail_ext
- * 如果记录已经存在pool_detail_ext 则不增加金额以及记录
- *
- * @param Order $order
- */
- public static function setPoolDetail($order) {
- debug_log(['开始分红记录'], 'team_grades_pool_detail.log');
- $t = \Yii::$app->db->beginTransaction();
- try {
- if ($order) {
- $user_id = $order->user_id;
- // $userTeamGrades = TeamGrades::getUserTeamGrades($user_id);
- // if (empty($userTeamGrades['id'])) {
- // throw new \Exception('该用户不是团队分红人员');
- // }
- $store_id = $order->store_id;
- $team_grades_setting = Option::get('team_grades_setting', $store_id, 'team_grades_setting')['value'];
- if (!empty($team_grades_setting)) {
- $team_grades_setting = json_decode($team_grades_setting, true);
- }
- if (empty($team_grades_setting)) {
- throw new \Exception('团队业绩分红设置未开启');
- }
- if (!intval($team_grades_setting['amount_rule_type'])) {
- // 获取商品业绩分红金额
- $amount = 0;
- $order_detail_goods_id = OrderDetail::find()->where(['order_id' => $order->id, 'is_delete' => 0])->select('goods_id, num')->asArray()->all();
- if (!empty($order_detail_goods_id)) {
- foreach ($order_detail_goods_id as $order_detail_item) {
- $order_detail_goods_amount = TeamGradesGoods::findOne(['goods_id' => $order_detail_item['goods_id'], 'is_delete' => 0])->goods_team_grades ?: 0;
- $amount = bcadd(bcmul($order_detail_goods_amount, $order_detail_item['num'], 2), $amount, 2);
- }
- }
- } else {
- $amount = $order->pay_price;
- }
- debug_log(['amount' => $amount], 'team_grades_pool_detail.log');
- if ($amount <= 0) {
- throw new \Exception('商品未设置业绩分红值');
- }
- // 是否过滤高等级
- $filter_high_level = intval($team_grades_setting['filter_high_level']);
- debug_log(['是否过滤高等级' => $filter_high_level], 'team_grades_pool_detail.log');
- /* begin 2025/04/27 11:48:36 当奖励模式为【级差模式】时,不过滤高等级 WPing丶 */
- if($team_grades_setting['bonus_type'] == 1) {
- $filter_high_level = 0;
- }
- /* end */
- // 获取未发放的奖金池
- $pool = TeamGradesPool::getPool($store_id, $user_id, $team_grades_setting['bonus_type']);
- debug_log(['获取未发放的奖金池' => $pool], 'team_grades_pool_detail.log');
- if (!$pool) {
- throw new \Exception('未找到未发放的奖金池');
- }
- // $pool_detail = self::findOne(['order_id' => $order->id, 'pool_id' => $pool['id'], 'store_id' => $store_id]);
- // debug_log(['该订单已存在该奖金池明细' => $pool_detail], 'team_grades_pool_detail.log');
- // if ($pool_detail) {
- // throw new \Exception('该订单已存在该奖金池明细');
- // }
- // $user_list = self::getParentTeam($user_id, $user_id, $store_id);
- $user_list = OldUserTreePath::find()->alias('ut')
- ->where(['ut.child_id' => $user_id])
- ->andWhere('ut.child_id != ut.parent_id')
- ->orderBy(['ut.parent_level' => SORT_DESC])
- ->select(['ut.*'])
- ->asArray()
- ->all();
- debug_log(['user_list' => $user_list], 'team_grades_pool_detail.log');
- //向奖金池记录增加记录
- // if (!empty($user_list)) {
- // $user_list = array_column($user_list, 'parent_id');
- // }
- array_unshift($user_list, [
- 'parent_id' => $user_id,
- 'parent_level' => 0
- ]);
- foreach ($user_list as $user_index => &$user_item) {
- $user_item['parent_level'] = $user_index;
- }
- unset($user_item);
- $team_grades_level = 0;
- foreach ($user_list as $user_item) {
- $init_amount = $amount;
- $userTeamGrades = TeamGrades::getUserTeamGrades($user_item['parent_id']);
- if (!empty($userTeamGrades['id'])) {
- if ($filter_high_level) {
- if ($team_grades_level < $userTeamGrades['team_grades_level']) {
- $team_grades_level = $userTeamGrades['team_grades_level'];
- } else {
- $init_amount = 0;
- }
- }
- } else {
- // $init_amount = 0;//如果该用户不是团队业绩分红成员就将业绩改成0
- continue;//新要求:这里只显示 有分红权限的用户 。所以直接给跳出循环
- }
- // 判断是否在团队范围内 不再范围内业绩就改成0
- if ($user_item['parent_level'] < $team_grades_setting['team_start_num'] || $user_item['parent_level'] > $team_grades_setting['team_end_num']) {
- $init_amount = 0;
- }
- debug_log(['init_amount' => $init_amount], 'team_grades_pool_detail.log');
- // 向明细表增加记录
- $pool_detail = self::findOne(['user_id' => $user_item['parent_id'], 'pool_id' => $pool['id']]) ?: new self();
- $pool_detail->pool_id = $pool['id'];
- $pool_detail->user_id = $user_item['parent_id'];
- // $pool_detail->amount = bcadd($pool_detail->amount, $init_amount);
- if (!$pool_detail->save()) {
- throw new \Exception(json_encode($pool_detail->errors, JSON_UNESCAPED_UNICODE));
- }
- // 向明细记录明细表增加记录
- $pool_detail_ext = TeamGradesPoolDetailExt::findOne([
- 'order_id' => $order->id,
- 'pool_detail_id' => $pool_detail->id,
- 'user_id' => $user_item['parent_id'],
- ]);
- if ($pool_detail_ext) {
- continue;
- }
- $pool_detail_ext = new TeamGradesPoolDetailExt();
- $pool_detail_ext->goods_amount = $amount;
- $pool_detail_ext->amount = $init_amount;
- $pool_detail_ext->order_id = $order->id;
- $pool_detail_ext->pool_detail_id = $pool_detail->id;
- $pool_detail_ext->user_id = $user_item['parent_id'];
- if (!$pool_detail_ext->save()) {
- throw new \Exception(json_encode($pool_detail_ext->errors, JSON_UNESCAPED_UNICODE));
- }
- // 更新奖金池分红
- $pool_detail->amount = bcadd($pool_detail->amount, $init_amount, 2);
- if (!$pool_detail->save()) {
- throw new \Exception(json_encode($pool_detail->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- $t->commit();
- return ;
- }
- throw new \Exception('订单不存在');
- } catch (\Exception $e) {
- debug_log(['getMessage' => $e->getMessage()], 'team_grades_pool_detail.log');
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 发放奖金池根据用户团队分红等级内设置的佣金比例
- */
- public static function sendPool($pool_id)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- // 获取未发放的奖金池
- $pool = TeamGradesPool::findOne(['id' => $pool_id, 'is_send' => 0]);
- if (!$pool) {
- throw new \Exception('奖金池不存在');
- }
- // 获取未发放的奖金池明细 准备发放 2025年4月28日09:59:18新增排序防止级差发放错乱
- $pool_detail_list = self::find()->alias('pd')
- ->leftJoin(['tg' => TeamGrades::tableName()], 'pd.user_id = tg.user_id')
- ->where(['pd.pool_id' => $pool_id, 'pd.is_send' => self::SEND_STATUS_NO])
- ->orderBy(['tg.team_grades_level' => SORT_ASC])
- ->asArray()->all();
- if (!empty($pool_detail_list)) {
- $last_tiered_bonus = 0;
- foreach ($pool_detail_list as $pool_detail_item) {
- $price = 0;
- $profit = 0;
- // 获取用户是否是团队分红成员且有团队等级
- $userTeamGrades = TeamGrades::getUserTeamGrades($pool_detail_item['user_id']);
- if (!empty($userTeamGrades['id'])) {
- /* begin 2025/04/27 14:22:43 WPing丶 新增级差模式调整后的代码,上方注释为源代码已兼容 */
- if($pool->bonus_type == 0) {//业绩阶梯模式
- $result = self::getPrice($pool_detail_item['amount'], $userTeamGrades['team_grades_level'], $pool->store_id);
- $price = $result['price'];
- $profit = $result['profit'];
- } elseif($pool->bonus_type == 1) {//级差模式
- $result = self::getPrice($pool_detail_item['amount'], $userTeamGrades['team_grades_level'], $pool->store_id, $last_tiered_bonus);
- $price = $result['price'];
- $profit = $result['profit'];
- $last_tiered_bonus = $result['tiered_bonus'];
- } else {
- throw new \Exception('奖励类型【'.$pool->bonus_type.'】不是期望值');
- }
- /* end */
- }
- $userPoolDetail = self::findOne($pool_detail_item['id']);
- $userPoolDetail->price = $price;
- $userPoolDetail->profit = $profit;
- $userPoolDetail->is_send = self::SEND_STATUS_YES;
- $userPoolDetail->send_time = time();
- $userPoolDetail->level = $userTeamGrades['team_grades_level'];
- if (!$userPoolDetail->save()) {
- throw new \Exception(json_encode($userPoolDetail->errors, JSON_UNESCAPED_UNICODE));
- }
- if ($price > 0) {
- //更新团队分红累计金额
- $userTeamGrades_ = TeamGrades::findOne($userTeamGrades['id']);
- $userTeamGrades_->price = bcadd($userTeamGrades_->price, $price, 2);
- $userTeamGrades_->total_price = bcadd($userTeamGrades_->total_price, $price, 2);
- if (!$userTeamGrades_->save()) {
- throw new \Exception(json_encode($userTeamGrades_->errors, JSON_UNESCAPED_UNICODE));
- }
- //判断用户是否存在 存在则开始发放
- $user = User::findOne(['id' => $pool_detail_item['user_id'], 'is_delete' => 0]);
- if ($user) {
- $user->price = bcadd($user->price, $price, 2);
- $user->total_price = bcadd($user->total_price, $price, 2);
- if (!$user->save()) {
- throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
- }
- $result = UserShareMoney::set($price, $pool_detail_item['user_id'], 0, 0, 11, $pool->store_id, 0, '团队业绩分红');
- if (!$result) {
- throw new \Exception('保存失败');
- }
- }
- }
- }
- }
- //如果所有明细发放完毕 则将奖金池状态设置为已发放
- $pool_detail_list = self::find()->where(['pool_id' => $pool_id, 'is_send' => self::SEND_STATUS_NO])->asArray()->all();
- if (empty($pool_detail_list)) {
- $pool->is_send = self::SEND_STATUS_YES;
- $pool->send_time = time();
- if (!$pool->save()) {
- throw new \Exception(json_encode($pool->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //通过业绩金额 计算分红金额
- public static function getPrice($amount, $team_grades_level, $store_id, $last_profit = 0)
- {
- try {
- $price = 0;
- $profit = 0;
- $userTeamGradesLevel = TeamGradesLevel::getLevelInfo($team_grades_level, $store_id);
- $team_grades_setting = Option::get('team_grades_setting', $store_id, 'team_grades_setting')['value'];
- $team_grades_setting = json_decode($team_grades_setting, true);
- $bonus_type = $team_grades_setting['bonus_type'];
- if ($userTeamGradesLevel) {
- if($bonus_type == 0) {
- if ($userTeamGradesLevel->level_reward_setting) {
- $level_reward_setting = json_decode($userTeamGradesLevel->level_reward_setting, true);
-
- $max_level_reward = [];
- $open = true;
- //遍历循环奖励 查询业绩金额是否符合条件
- if (!empty($level_reward_setting)) {
- foreach ($level_reward_setting as $level_reward_item) {
- if (isset($level_reward_item['profit']) && isset($level_reward_item['min']) && isset($level_reward_item['max'])) {
- //如果业绩条件成立 就开始使用成立条件对应的佣金比例 乘以 业绩金额 算出来实际到账佣金
- if ($amount >= $level_reward_item['min'] && $amount <= $level_reward_item['max']) {
- // 计算出实际到账佣金
- $price = bcmul($amount, bcdiv($level_reward_item['profit'], 100, 2), 2);
- $profit = $level_reward_item['profit'];
- $open = false;
- break;
- }
-
- //拿到最大的业绩区间
- if (empty($max_level_reward) || $max_level_reward['max'] < $level_reward_item['max']) {
- $max_level_reward = $level_reward_item;
- }
- }
- }
- }
- //如果不在以上业绩区间内 就用最大的业绩区间的比例去计算实际到账佣金
- if ($open && !empty($max_level_reward) && $amount > $max_level_reward['max']) {
- $price = bcmul($amount, bcdiv($max_level_reward['profit'], 100, 2), 2);
- $profit = $max_level_reward['profit'];
- }
-
- }
- } elseif ($bonus_type == 1) {// 级差模式
- $profit = bcsub($userTeamGradesLevel->tiered_bonus, $last_profit, 2);
- $price = bcmul($amount, bcdiv($profit, 100, 2), 2);
- $tiered_bonus = $userTeamGradesLevel->tiered_bonus;
- if($profit <= 0) {
- $profit = 0;
- $price = 0;
- $tiered_bonus = 0;
- }
- } else {
- throw new \Exception('奖励类型【'.$pool->bonus_type.'】不是期望值');
- }
- }
- } catch (\Exception $e) {
- debug_log([
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage(),
- 'file' => $e->getFile()
- ], 'TeamGradesPoolDetail.log');
- }
- return [
- 'price' => $price,
- 'profit' => $profit,
- 'tiered_bonus' => $tiered_bonus?:0,
- ];
- }
- }
|