BonusHistory.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\api\model\user;
  4. use app\api\model\Order;
  5. use app\api\model\User;
  6. use app\api\service\User as UserService;
  7. use app\common\model\user\BonusHistory as BonusHistoryModel;
  8. use app\common\library\helper;
  9. use app\api\model\user\CommissionsDetail;
  10. /**
  11. * 奖励金明细模型
  12. * Class BonusHistory
  13. * @package app\api\model\user
  14. */
  15. class BonusHistory extends BonusHistoryModel
  16. {
  17. /**
  18. * 获取上月达量奖励金
  19. */
  20. public static function lastMonthBonus($userId)
  21. {
  22. $data = self::where('user_id', $userId)->where('year_month', date("Ym", strtotime("-1 month")))->value('bonus_money');
  23. return helper::bcsub($data,0,2);
  24. }
  25. public static function getCurrYearBonus($userId){
  26. $sum = self::where('user_id', $userId)->where('year',intval(date('Y')))
  27. ->where('clearing_status',1)->sum('bonus_money');
  28. return helper::bcsub($sum,0,2);
  29. }
  30. /**
  31. * 获取列表
  32. * @param int $type 结算状态,0:待结算,1:已结算
  33. */
  34. public function getList($type = 0)
  35. {
  36. // 当前用户ID
  37. $user = UserService::getCurrentLoginUser(true);
  38. $currYear = intval(date('Y'));
  39. $currMonth = intval(date('m'));
  40. if ($user->role == User::SHOP_BOSS){
  41. return $this->where('user_id', '=', $user->user_id)
  42. ->where('clearing_status', $type)
  43. ->where('is_delete', 0)
  44. ->field('sum(sales_volume) as sales_volume,sum(bonus_money) as bonus_money,sum(refund_volume) as refund_volume,sum(overtime_receipt_sum) as overtime_receipt_sum,`year`,`month`,`year_month`,shop_id')
  45. ->group('year_month')
  46. ->order(['year_month' => 'desc'])
  47. ->paginate(20)
  48. ->each(function($item) use ($currYear,$currMonth){
  49. $item->curr_month = false;
  50. $item->bonus_money = helper::bcsub($item->bonus_money, 0, 2);
  51. $item->overtime_receipt_sum = helper::bcsub($item->overtime_receipt_sum, 0, 2);
  52. $item->refund_volume = helper::bcsub($item->refund_volume, 0, 2);
  53. if($item->year == $currYear && $item->month == $currMonth){
  54. $item->curr_month = true;
  55. }
  56. })
  57. ->toArray();
  58. }
  59. $o = $this->where('user_id', '=', $user->user_id);
  60. if($user->role == User::SHOP_SELLER || $user->role == User::SHOP_MG){
  61. $o = $this->where('shop_id',$user->shop_id)->where('role',$user->role);
  62. }
  63. // 获取列表数据
  64. return $o->where('user_id', '=', $user->user_id)
  65. ->where('clearing_status', $type)
  66. ->where('is_delete', 0)
  67. ->order(['year_month' => 'desc'])
  68. ->paginate(20)
  69. ->each(function($item) use ($currYear,$currMonth){
  70. $item->curr_month = false;
  71. $item->bonus_money = helper::bcsub($item->bonus_money, 0, 2);
  72. $item->overtime_receipt_sum = helper::bcsub($item->overtime_receipt_sum, 0, 2);
  73. $item->refund_volume = helper::bcsub($item->refund_volume, 0, 2);
  74. if($item->year == $currYear && $item->month == $currMonth){
  75. $item->curr_month = true;
  76. }
  77. })
  78. ->toArray();
  79. }
  80. public function getShopBonusList($month,$year,$type = 0)
  81. {
  82. // 当前用户ID
  83. $userId = UserService::getCurrentLoginUserId();
  84. return $this->alias('bs')->leftJoin('shops sp','bs.shop_id=sp.shop_id')
  85. ->where('bs.user_id', '=', $userId)
  86. ->where('bs.clearing_status', $type)
  87. ->where('bs.year', $year)
  88. ->where('bs.month', $month)
  89. ->where('bs.is_delete', 0)
  90. ->field('bs.id,bs.shop_id,bs.sales_volume,bs.bonus_money,bs.bonus_rate,bs.overtime_receipt_sum,bs.refund_volume,sp.shop_name')
  91. ->order(['bs.year_month' => 'desc'])
  92. ->paginate(20)
  93. ->each(function($item) {
  94. $item->bonus_money = helper::bcsub($item->bonus_money, 0, 2);
  95. $item->overtime_receipt_sum = helper::bcsub($item->overtime_receipt_sum, 0, 2);
  96. $item->refund_volume = helper::bcsub($item->refund_volume, 0, 2);
  97. })
  98. ->toArray();
  99. }
  100. /**
  101. * 获取店老板或店长的某月奖励金v.1.36
  102. * @param $shopId
  103. * @param $role
  104. * @param $year
  105. * @param $month
  106. * @return string
  107. */
  108. public function lastMonthUserBonus36($shopId,$role,$year,$month): string
  109. {
  110. $data = self::where('shop_id', $shopId)
  111. ->where('role', $role)
  112. ->where('year', $year)
  113. ->where('month', $month)
  114. ->where('clearing_status',1)
  115. ->sum('bonus_money');
  116. return helper::bcsub($data,0,2);
  117. }
  118. public function getRoleBonus($shop,$from,$to,$year,$month): array
  119. {
  120. $shopId = $shop['shop_id'];
  121. $data['boss_bonus'] = $this->lastMonthUserBonus36($shopId,User::SHOP_BOSS,$year,$month);
  122. $data['mg_bonus'] = $this->lastMonthUserBonus36($shopId,User::SHOP_MG,$year,$month);
  123. $data['seller_bonus'] = $this->lastMonthUserBonus36($shopId,User::SHOP_SELLER,$year,$month);
  124. $data['chef_bonus'] = $this->lastMonthUserBonus36($shopId,User::SHOP_CHEF,$year,$month);
  125. $boss_comm = CommissionsDetail::sumUserCommission($shopId,User::SHOP_BOSS,$from,$to);
  126. $mg_comm = CommissionsDetail::sumUserCommission($shopId,User::SHOP_MG,$from,$to);
  127. $sel_comm = CommissionsDetail::sumUserCommission($shopId,User::SHOP_SELLER,$from,$to);
  128. $data['boss_total_bonus'] = helper::bcadd($data['boss_bonus'] ,$boss_comm,4);
  129. $data['mg_total_bonus'] = helper::bcadd($data['mg_bonus'] ,$mg_comm,4);
  130. $temp = helper::bcadd($data['boss_total_bonus'],$data['mg_total_bonus'],4);
  131. $data['seller_total_bonus'] = helper::bcadd($data['seller_bonus'] ,$sel_comm,4);
  132. $data['total_bonus'] = helper::bcadd($data['seller_total_bonus'] ,$temp,4);
  133. $data['order_count'] = Order::getUserOrderCount39($shopId,$from,$to,true);//成交订单数 V1.3.92
  134. $wheres[] = ['shop_id','=',$shopId];
  135. $wheres[] = ['role','=',User::SHOP_SELLER];
  136. $wheres[] = ['commission_level','=',1];
  137. $wheres[] = ['order_create_time','>=',$from];
  138. $wheres[] = ['order_create_time','<=',$to];
  139. $wheres[] = ['clearing_status','=',1];
  140. $data['sales_volume'] = CommissionsDetail::sumOrderSaleVolume($wheres); //销售额 V1.3.92
  141. return $data;
  142. }
  143. }