ShopsMonthlyBonusSt.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\model;
  13. use app\common\model\chef\ChefAreas;
  14. use app\common\model\ShopsMonthlyBonusSt as ShopStModel;
  15. /**
  16. * 门店销售数据每日统计模型
  17. * Class ShopsMonthlyBonusSt
  18. * @package app\common\model
  19. */
  20. class ShopsMonthlyBonusSt extends ShopStModel
  21. {
  22. public function listSt($from,$to,$param=[]){
  23. $where = [];
  24. if (isset($param['shop_name']) && !empty($param['shop_name'])){
  25. $where[] = ['yoshop_shops.shop_name','like','%'.$param['shop_name'].'%'];
  26. }
  27. if (isset($param['region_id']) && $param['region_id']>0){
  28. $where[] = ['yoshop_shops.region_id','=',$param['region_id']];
  29. }
  30. if (isset($param['city_id']) && $param['city_id']>0){
  31. $where[] = ['yoshop_shops.city_id','=',$param['city_id']];
  32. }
  33. if (isset($param['province_id']) && $param['province_id']>0){
  34. $where[] = ['yoshop_shops.province_id','=',$param['province_id']];
  35. }
  36. return Shops::alias('yoshop_shops')
  37. ->leftJoin('areas','yoshop_shops.city_id=areas.id')
  38. ->field('yoshop_shops.shop_id,yoshop_shops.shop_name,yoshop_shops.city_id,areas.name')
  39. ->where('yoshop_shops.is_delete',0)
  40. ->where($where)
  41. ->withSum(['monthBonusTotal'=>function($query) use ($from,$to){
  42. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  43. }],'total_bonus')
  44. ->withSum(['monthBonusBossTotal'=>function($query) use ($from,$to){
  45. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  46. }],'boss_total_bonus')
  47. ->withSum(['monthBonusBoss'=>function($query) use ($from,$to){
  48. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  49. }],'boss_bonus')
  50. ->withSum(['monthBonusMgTotal'=>function($query) use ($from,$to){
  51. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  52. }],'mg_total_bonus')
  53. ->withSum(['monthBonusMg'=>function($query) use ($from,$to){
  54. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  55. }],'mg_bonus')
  56. ->withSum(['monthBonusSellerTotal'=>function($query) use ($from,$to){
  57. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  58. }],'seller_total_bonus')
  59. ->withSum(['monthBonusSellerl'=>function($query) use ($from,$to){
  60. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  61. }],'seller_bonus')
  62. ->paginate(15);
  63. }
  64. public function listsExport($from,$to,$param=[]){
  65. $where = [];
  66. if (isset($param['shop_name']) && !empty($param['shop_name'])){
  67. $where[] = ['yoshop_shops.shop_name','like','%'.$param['shop_name'].'%'];
  68. }
  69. if (isset($param['region_id']) && $param['region_id']>0){
  70. $where[] = ['yoshop_shops.region_id','=',$param['region_id']];
  71. }
  72. if (isset($param['city_id']) && $param['city_id']>0){
  73. $where[] = ['yoshop_shops.city_id','=',$param['city_id']];
  74. }
  75. if (isset($param['province_id']) && $param['province_id']>0){
  76. $where[] = ['yoshop_shops.province_id','=',$param['province_id']];
  77. }
  78. $list = Shops::alias('yoshop_shops')
  79. ->leftJoin('areas','yoshop_shops.city_id=areas.id')
  80. ->field('yoshop_shops.shop_id,yoshop_shops.shop_name,yoshop_shops.city_id,areas.name')
  81. ->where('yoshop_shops.is_delete',0)
  82. ->where($where)
  83. ->withSum(['monthBonusTotal'=>function($query) use ($from,$to){
  84. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  85. }],'total_bonus')
  86. ->withSum(['monthBonusBossTotal'=>function($query) use ($from,$to){
  87. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  88. }],'boss_total_bonus')
  89. ->withSum(['monthBonusBoss'=>function($query) use ($from,$to){
  90. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  91. }],'boss_bonus')
  92. ->withSum(['monthBonusMgTotal'=>function($query) use ($from,$to){
  93. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  94. }],'mg_total_bonus')
  95. ->withSum(['monthBonusMg'=>function($query) use ($from,$to){
  96. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  97. }],'mg_bonus')
  98. ->withSum(['monthBonusSellerTotal'=>function($query) use ($from,$to){
  99. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  100. }],'seller_total_bonus')
  101. ->withSum(['monthBonusSellerl'=>function($query) use ($from,$to){
  102. $query->where('st_year_month','>=',$from)->where('st_year_month','<=',$to);
  103. }],'seller_bonus')
  104. ->select();
  105. $data['header'] = ['序号', '门店名称', '门店城市', '门店分佣', '店老板总分佣','店老板达成奖励金','店长总分佣','店老板达成奖励金','全部店员总分佣','全部店员达成奖励金'];
  106. $data['filename'] = $from.'~'.$to.'门店奖励金';
  107. $data['data'] = [];
  108. foreach ($list as $key=>$arr){
  109. $new_list['key'] = $key+1;
  110. $new_list['shop_name'] = $arr['shop_name'];
  111. $new_list['name'] = $arr['name'];
  112. $new_list['total_sum'] = $arr['month_bonus_total_sum']?:0;
  113. $new_list['boss_total_sum'] = $arr['month_bonus_boss_total_sum']?:0;
  114. $new_list['boss_sum'] = $arr['month_bonus_boss_sum']?:0;
  115. $new_list['mg_total_sum'] = $arr['month_bonus_mg_total_sum']?:0;
  116. $new_list['mg_sum'] = $arr['month_bonus_mg_sum']?:0;
  117. $new_list['seller_total_sum'] = $arr['month_bonus_seller_total_sum']?:0;
  118. $new_list['sellerl_sum'] = $arr['month_bonus_sellerl_sum']?:0;
  119. $data['data'][] = $new_list;
  120. }
  121. return $data;
  122. }
  123. /**
  124. * 门店销售额及佣金-达量奖励金
  125. * @param $param
  126. * @return mixed
  127. * @author: zjwhust
  128. * @Time: 2022/5/13 9:36
  129. */
  130. public function shopBonusStat($param){
  131. // 检索查询条件
  132. $filter = $this->getQueryFilter($param);
  133. // 获取数据列表
  134. $query = $this->alias('bonus')
  135. ->field('
  136. shops.shop_id,
  137. shops.shop_name,
  138. shops.city_id,
  139. areas.name,
  140. sum(bonus.order_count) as order_count_total,
  141. sum(bonus.sales_volume) as sales_volume_total,
  142. sum(bonus.boss_bonus) as boss_bonus_total,
  143. sum(bonus.mg_bonus) as mg_bonus_total,
  144. sum(bonus.seller_bonus) as seller_bonus_total,
  145. sum(bonus.chef_bonus) as chef_bonus_total
  146. ')
  147. ->leftJoin('shops', 'shops.shop_id = bonus.shop_id')
  148. ->leftJoin('areas','shops.city_id=areas.id')
  149. ->where('shops.is_delete',0)
  150. ->where($filter)
  151. // ->order(['bonus.id' => 'desc'])
  152. ->group('bonus.shop_id')
  153. ->paginate();
  154. return $query;
  155. }
  156. /**
  157. * 设置检索查询条件
  158. * @param array $param
  159. * @return array
  160. */
  161. private function getQueryFilter(array $param): array
  162. {
  163. // 默认参数
  164. $params = $this->setQueryDefaultValue($param, [
  165. ]);
  166. // 检索查询条件
  167. $filter = [];
  168. // 结算月份起止时间
  169. if (isset($param['betweenTime'])&&!empty($params['betweenTime'])){
  170. $times = between_time($param['betweenTime']);
  171. }else{
  172. $times['end_time'] = strtotime(date('Y-m-01 00:00:00')) - 1 ;//上月的最后一秒
  173. $times['start_time'] =strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
  174. }
  175. $from = date('Ym',$times['start_time']);
  176. $to = date('Ym',$times['end_time']);
  177. $filter[] = ['bonus.st_year_month', '>=', $from];
  178. $filter[] = ['bonus.st_year_month', '<=', $to ];
  179. //门店名称
  180. if(isset($params['shopName'])&&!empty($params['shopName'])){
  181. $filter[] = ['shops.shop_name','like', "%{$params['shopName']}%"];
  182. }
  183. //门店ID
  184. if(isset($params['shopId'])&&$params['shopId']>0){
  185. $filter[] = ['shops.shop_id','=',$params['shopId']];
  186. }
  187. //门店城市ID
  188. if(isset($params['cityId'])&&$params['cityId']>0){
  189. $filter[] = ['shops.city_id','=',$params['cityId']];
  190. }
  191. //门店城市名称
  192. if(isset($params['cityName'])&&!empty($params['cityName'])){
  193. $cityIds = ChefAreas::where('name','like','%'.$params['cityName'].'%')->where('level',2)->column('id');
  194. $filter[] = ['shops.city_id','in',$cityIds];
  195. }
  196. return $filter;
  197. }
  198. }