123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- declare (strict_types = 1);
- namespace app\store\model\user;
- use app\common\library\helper;
- use app\common\model\User;
- use app\common\model\user\CommissionSteps as CommissionStepsModel;
- use think\facade\Db;
- /**
- * 阶梯达量奖励配置模型
- * Class CommissionSteps
- * @package app\common\model
- */
- class CommissionSteps extends CommissionStepsModel
- {
- /**
- *
- */
- public function add($shopId, $data)
- {
- Db::startTrans();
- try {
- // 先删除全部
- static::deleteAll(['shop_id' => $shopId]);
- // 店员
- if (count($data['staff_commission_steps']) > 2) { // 最多2级
- $this->error = '店员阶梯数最多2级';
- return false;
- }
- if (!empty($data['staff_commission_steps'])) {
- // 添加
- $dataset = [];
- foreach ($data['staff_commission_steps'] as $key=>$item) {
- $step = $key+1;
- $dataset[] = [
- 'shop_id' => $shopId,
- 'role' => User::SHOP_SELLER,
- 'step' => $step,
- 'sale_amount' => $item['sale_amount'] * 10000,
- 'bonus_ratio' => $item['bonus_ratio'],
- ];
- }
- (new static)->addAll($dataset);
- }
- // 门店身份
- if (count($data['identity_commission_steps']) > 2) { // 最多2级
- $this->error = '阶梯数最多2级';
- return false;
- }
- if (!empty($data['identity_commission_steps'])) {
- // 添加
- $dataset = [];
- foreach ($data['identity_commission_steps'] as $key=>$item) {
- $step = $key+1;
- $dataset[] = [ // 店老板
- 'shop_id' => $shopId,
- 'role' => User::SHOP_BOSS,
- 'step' => $step,
- 'sale_amount' => $item['sale_amount'] * 10000,
- 'bonus_ratio' => $item['boss_bonus_ratio'],
- ];
- $dataset[] = [ // 店长
- 'shop_id' => $shopId,
- 'role' => User::SHOP_MG,
- 'step' => $step,
- 'sale_amount' => $item['sale_amount'] * 10000,
- 'bonus_ratio' => $item['manager_bonus_ratio'],
- ];
- $dataset[] = [ // 厨师
- 'shop_id' => $shopId,
- 'role' => User::SHOP_CHEF,
- 'step' => $step,
- 'sale_amount' => $item['sale_amount'] * 10000,
- 'bonus_ratio' => $item['chef_bonus_ratio'],
- ];
- }
- (new static)->addAll($dataset);
- }
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- /**
- * 获取门店阶梯佣金配置
- * 适用于:门店分佣配置页面
- */
- public function getCommissionSteps($shopId)
- {
- // 店员阶梯
- $staff_commission_steps = self::where('shop_id', $shopId)
- ->where('role', User::SHOP_SELLER)
- ->order('step', 'asc')
- ->field('step,round(sale_amount/10000, 2) as sale_amount,bonus_ratio')
- ->select();
- // 店老板、店长、其他身份阶梯
- $identity_commission_steps = self::where('shop_id', $shopId)
- ->where('role', '<>', User::SHOP_SELLER)
- ->where('role', '>', User::NORMAL_USER)
- ->where('role', '<', User::COMMISSION_USER)
- ->order(['step' => 'asc', 'role' => 'asc'])
- ->field('role,step,sale_amount,bonus_ratio')
- ->select();
- $res_identity_commission_steps = [];
- if (!empty($identity_commission_steps)) {
- foreach($identity_commission_steps as $item) {
- $res_steps['step'] = $item['step'];
- $res_steps['sale_amount'] = helper::bcsub($item['sale_amount'] / 10000, 0, 2);
- if ($item['role'] == User::SHOP_BOSS) {
- $res_steps['boss_bonus_ratio'] = $item['bonus_ratio'];
- }
- if ($item['role'] == User::SHOP_MG) {
- $res_steps['manager_bonus_ratio'] = $item['bonus_ratio'];
- }
- if ($item['role'] == User::SHOP_CHEF) {
- $res_steps['chef_bonus_ratio'] = $item['bonus_ratio'];
- }
- $res_identity_commission_steps[$item['step']] = $res_steps;
- }
- }
- $data['staff_commission_steps'] = $staff_commission_steps;
- $data['identity_commission_steps'] = array_values($res_identity_commission_steps);
- return $data;
- }
- }
|