123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- declare (strict_types = 1);
- namespace app\api\model\user;
- use app\api\model\Order;
- use app\api\model\User;
- use app\api\service\User as UserService;
- use app\common\model\user\BonusHistory as BonusHistoryModel;
- use app\common\library\helper;
- use app\api\model\user\CommissionsDetail;
- /**
- * 奖励金明细模型
- * Class BonusHistory
- * @package app\api\model\user
- */
- class BonusHistory extends BonusHistoryModel
- {
-
- /**
- * 获取上月达量奖励金
- */
- public static function lastMonthBonus($userId)
- {
- $data = self::where('user_id', $userId)->where('year_month', date("Ym", strtotime("-1 month")))->value('bonus_money');
- return helper::bcsub($data,0,2);
- }
- public static function getCurrYearBonus($userId){
- $sum = self::where('user_id', $userId)->where('year',intval(date('Y')))
- ->where('clearing_status',1)->sum('bonus_money');
- return helper::bcsub($sum,0,2);
- }
- /**
- * 获取列表
- * @param int $type 结算状态,0:待结算,1:已结算
- */
- public function getList($type = 0)
- {
- // 当前用户ID
- $user = UserService::getCurrentLoginUser(true);
- $currYear = intval(date('Y'));
- $currMonth = intval(date('m'));
- if ($user->role == User::SHOP_BOSS){
- return $this->where('user_id', '=', $user->user_id)
- ->where('clearing_status', $type)
- ->where('is_delete', 0)
- ->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')
- ->group('year_month')
- ->order(['year_month' => 'desc'])
- ->paginate(20)
- ->each(function($item) use ($currYear,$currMonth){
- $item->curr_month = false;
- $item->bonus_money = helper::bcsub($item->bonus_money, 0, 2);
- $item->overtime_receipt_sum = helper::bcsub($item->overtime_receipt_sum, 0, 2);
- $item->refund_volume = helper::bcsub($item->refund_volume, 0, 2);
- if($item->year == $currYear && $item->month == $currMonth){
- $item->curr_month = true;
- }
- })
- ->toArray();
- }
- $o = $this->where('user_id', '=', $user->user_id);
- if($user->role == User::SHOP_SELLER || $user->role == User::SHOP_MG){
- $o = $this->where('shop_id',$user->shop_id)->where('role',$user->role);
- }
- // 获取列表数据
- return $o->where('user_id', '=', $user->user_id)
- ->where('clearing_status', $type)
- ->where('is_delete', 0)
- ->order(['year_month' => 'desc'])
- ->paginate(20)
- ->each(function($item) use ($currYear,$currMonth){
- $item->curr_month = false;
- $item->bonus_money = helper::bcsub($item->bonus_money, 0, 2);
- $item->overtime_receipt_sum = helper::bcsub($item->overtime_receipt_sum, 0, 2);
- $item->refund_volume = helper::bcsub($item->refund_volume, 0, 2);
- if($item->year == $currYear && $item->month == $currMonth){
- $item->curr_month = true;
- }
- })
- ->toArray();
- }
- public function getShopBonusList($month,$year,$type = 0)
- {
- // 当前用户ID
- $userId = UserService::getCurrentLoginUserId();
- return $this->alias('bs')->leftJoin('shops sp','bs.shop_id=sp.shop_id')
- ->where('bs.user_id', '=', $userId)
- ->where('bs.clearing_status', $type)
- ->where('bs.year', $year)
- ->where('bs.month', $month)
- ->where('bs.is_delete', 0)
- ->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')
- ->order(['bs.year_month' => 'desc'])
- ->paginate(20)
- ->each(function($item) {
- $item->bonus_money = helper::bcsub($item->bonus_money, 0, 2);
- $item->overtime_receipt_sum = helper::bcsub($item->overtime_receipt_sum, 0, 2);
- $item->refund_volume = helper::bcsub($item->refund_volume, 0, 2);
- })
- ->toArray();
- }
- /**
- * 获取店老板或店长的某月奖励金v.1.36
- * @param $shopId
- * @param $role
- * @param $year
- * @param $month
- * @return string
- */
- public function lastMonthUserBonus36($shopId,$role,$year,$month): string
- {
- $data = self::where('shop_id', $shopId)
- ->where('role', $role)
- ->where('year', $year)
- ->where('month', $month)
- ->where('clearing_status',1)
- ->sum('bonus_money');
- return helper::bcsub($data,0,2);
- }
- public function getRoleBonus($shop,$from,$to,$year,$month): array
- {
- $shopId = $shop['shop_id'];
- $data['boss_bonus'] = $this->lastMonthUserBonus36($shopId,User::SHOP_BOSS,$year,$month);
- $data['mg_bonus'] = $this->lastMonthUserBonus36($shopId,User::SHOP_MG,$year,$month);
- $data['seller_bonus'] = $this->lastMonthUserBonus36($shopId,User::SHOP_SELLER,$year,$month);
- $data['chef_bonus'] = $this->lastMonthUserBonus36($shopId,User::SHOP_CHEF,$year,$month);
- $boss_comm = CommissionsDetail::sumUserCommission($shopId,User::SHOP_BOSS,$from,$to);
- $mg_comm = CommissionsDetail::sumUserCommission($shopId,User::SHOP_MG,$from,$to);
- $sel_comm = CommissionsDetail::sumUserCommission($shopId,User::SHOP_SELLER,$from,$to);
- $data['boss_total_bonus'] = helper::bcadd($data['boss_bonus'] ,$boss_comm,4);
- $data['mg_total_bonus'] = helper::bcadd($data['mg_bonus'] ,$mg_comm,4);
- $temp = helper::bcadd($data['boss_total_bonus'],$data['mg_total_bonus'],4);
- $data['seller_total_bonus'] = helper::bcadd($data['seller_bonus'] ,$sel_comm,4);
- $data['total_bonus'] = helper::bcadd($data['seller_total_bonus'] ,$temp,4);
- $data['order_count'] = Order::getUserOrderCount39($shopId,$from,$to,true);//成交订单数 V1.3.92
- $wheres[] = ['shop_id','=',$shopId];
- $wheres[] = ['role','=',User::SHOP_SELLER];
- $wheres[] = ['commission_level','=',1];
- $wheres[] = ['order_create_time','>=',$from];
- $wheres[] = ['order_create_time','<=',$to];
- $wheres[] = ['clearing_status','=',1];
- $data['sales_volume'] = CommissionsDetail::sumOrderSaleVolume($wheres); //销售额 V1.3.92
- return $data;
- }
- }
|