CommissionSteps.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\store\model\user;
  4. use app\common\library\helper;
  5. use app\common\model\User;
  6. use app\common\model\user\CommissionSteps as CommissionStepsModel;
  7. use think\facade\Db;
  8. /**
  9. * 阶梯达量奖励配置模型
  10. * Class CommissionSteps
  11. * @package app\common\model
  12. */
  13. class CommissionSteps extends CommissionStepsModel
  14. {
  15. /**
  16. *
  17. */
  18. public function add($shopId, $data)
  19. {
  20. Db::startTrans();
  21. try {
  22. // 先删除全部
  23. static::deleteAll(['shop_id' => $shopId]);
  24. // 店员
  25. if (count($data['staff_commission_steps']) > 2) { // 最多2级
  26. $this->error = '店员阶梯数最多2级';
  27. return false;
  28. }
  29. if (!empty($data['staff_commission_steps'])) {
  30. // 添加
  31. $dataset = [];
  32. foreach ($data['staff_commission_steps'] as $key=>$item) {
  33. $step = $key+1;
  34. $dataset[] = [
  35. 'shop_id' => $shopId,
  36. 'role' => User::SHOP_SELLER,
  37. 'step' => $step,
  38. 'sale_amount' => $item['sale_amount'] * 10000,
  39. 'bonus_ratio' => $item['bonus_ratio'],
  40. ];
  41. }
  42. (new static)->addAll($dataset);
  43. }
  44. // 门店身份
  45. if (count($data['identity_commission_steps']) > 2) { // 最多2级
  46. $this->error = '阶梯数最多2级';
  47. return false;
  48. }
  49. if (!empty($data['identity_commission_steps'])) {
  50. // 添加
  51. $dataset = [];
  52. foreach ($data['identity_commission_steps'] as $key=>$item) {
  53. $step = $key+1;
  54. $dataset[] = [ // 店老板
  55. 'shop_id' => $shopId,
  56. 'role' => User::SHOP_BOSS,
  57. 'step' => $step,
  58. 'sale_amount' => $item['sale_amount'] * 10000,
  59. 'bonus_ratio' => $item['boss_bonus_ratio'],
  60. ];
  61. $dataset[] = [ // 店长
  62. 'shop_id' => $shopId,
  63. 'role' => User::SHOP_MG,
  64. 'step' => $step,
  65. 'sale_amount' => $item['sale_amount'] * 10000,
  66. 'bonus_ratio' => $item['manager_bonus_ratio'],
  67. ];
  68. $dataset[] = [ // 厨师
  69. 'shop_id' => $shopId,
  70. 'role' => User::SHOP_CHEF,
  71. 'step' => $step,
  72. 'sale_amount' => $item['sale_amount'] * 10000,
  73. 'bonus_ratio' => $item['chef_bonus_ratio'],
  74. ];
  75. }
  76. (new static)->addAll($dataset);
  77. }
  78. Db::commit();
  79. return true;
  80. } catch (\Exception $e) {
  81. Db::rollback();
  82. $this->error = $e->getMessage();
  83. return false;
  84. }
  85. }
  86. /**
  87. * 获取门店阶梯佣金配置
  88. * 适用于:门店分佣配置页面
  89. */
  90. public function getCommissionSteps($shopId)
  91. {
  92. // 店员阶梯
  93. $staff_commission_steps = self::where('shop_id', $shopId)
  94. ->where('role', User::SHOP_SELLER)
  95. ->order('step', 'asc')
  96. ->field('step,round(sale_amount/10000, 2) as sale_amount,bonus_ratio')
  97. ->select();
  98. // 店老板、店长、其他身份阶梯
  99. $identity_commission_steps = self::where('shop_id', $shopId)
  100. ->where('role', '<>', User::SHOP_SELLER)
  101. ->where('role', '>', User::NORMAL_USER)
  102. ->where('role', '<', User::COMMISSION_USER)
  103. ->order(['step' => 'asc', 'role' => 'asc'])
  104. ->field('role,step,sale_amount,bonus_ratio')
  105. ->select();
  106. $res_identity_commission_steps = [];
  107. if (!empty($identity_commission_steps)) {
  108. foreach($identity_commission_steps as $item) {
  109. $res_steps['step'] = $item['step'];
  110. $res_steps['sale_amount'] = helper::bcsub($item['sale_amount'] / 10000, 0, 2);
  111. if ($item['role'] == User::SHOP_BOSS) {
  112. $res_steps['boss_bonus_ratio'] = $item['bonus_ratio'];
  113. }
  114. if ($item['role'] == User::SHOP_MG) {
  115. $res_steps['manager_bonus_ratio'] = $item['bonus_ratio'];
  116. }
  117. if ($item['role'] == User::SHOP_CHEF) {
  118. $res_steps['chef_bonus_ratio'] = $item['bonus_ratio'];
  119. }
  120. $res_identity_commission_steps[$item['step']] = $res_steps;
  121. }
  122. }
  123. $data['staff_commission_steps'] = $staff_commission_steps;
  124. $data['identity_commission_steps'] = array_values($res_identity_commission_steps);
  125. return $data;
  126. }
  127. }