ShopsDailySalesSt.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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\ShopsDailySalesSt as ShopStModel;
  14. /**
  15. * 门店销售数据每日统计模型
  16. * Class ShopsDailySalesSt
  17. * @package app\common\model
  18. */
  19. class ShopsDailySalesSt extends ShopStModel
  20. {
  21. public function listSt($from,$to,$param=[]){
  22. $where = [];
  23. if (isset($param['shop_name']) && !empty($param['shop_name'])){
  24. $where[] = ['yoshop_shops.shop_name','like','%'.$param['shop_name'].'%'];
  25. }
  26. if (isset($param['region_id']) && $param['region_id']>0){
  27. $where[] = ['yoshop_shops.region_id','=',$param['region_id']];
  28. }
  29. if (isset($param['city_id']) && $param['city_id']>0){
  30. $where[] = ['yoshop_shops.city_id','=',$param['city_id']];
  31. }
  32. if (isset($param['province_id']) && $param['province_id']>0){
  33. $where[] = ['yoshop_shops.province_id','=',$param['province_id']];
  34. }
  35. return Shops::alias('yoshop_shops')
  36. ->leftJoin('areas','yoshop_shops.city_id=areas.id')
  37. ->field('yoshop_shops.shop_id,yoshop_shops.shop_name,yoshop_shops.city_id,areas.name')
  38. ->where('yoshop_shops.is_delete',0)
  39. ->where($where)
  40. ->withSum(['monthlyBonusTotalMoney'=>function($query) use ($from,$to){//订单总金额(含运费)
  41. $query->whereBetweenTime('st_time',$from,$to);
  42. }],'total_price')
  43. ->withSum(['monthlyBonusExpr'=>function($query) use ($from,$to){//运费
  44. $query->whereBetweenTime('st_time',$from,$to);
  45. }],'express_price')
  46. ->withSum(['monthlyBonusGoods'=>function($query) use ($from,$to){//订单商品总金额
  47. $query->whereBetweenTime('st_time',$from,$to);
  48. }],'total_goods_price')
  49. ->withSum(['monthlyBonusPay'=>function($query) use ($from,$to){//实付金额
  50. $query->whereBetweenTime('st_time',$from,$to);
  51. }],'total_pay_price')
  52. ->withSum(['monthlyBonusCoupon'=>function($query) use ($from,$to){//平台优惠券抵扣
  53. $query->whereBetweenTime('st_time',$from,$to);
  54. }],'coupon_money')
  55. ->withSum(['monthlyBonusOrder'=>function($query) use ($from,$to){//订单数
  56. $query->whereBetweenTime('st_time',$from,$to);
  57. }],'order_count')
  58. ->withSum(['monthlyBonusBoss'=>function($query) use ($from,$to){//店老板基本分佣
  59. $query->whereBetweenTime('st_time',$from,$to);
  60. }],'boss_comm_money')
  61. ->withSum(['monthlyBonusMg'=>function($query) use ($from,$to){//店长基本分佣
  62. $query->whereBetweenTime('st_time',$from,$to);
  63. }],'mg_comm_money')
  64. ->withSum(['monthlyBonusSel'=>function($query) use ($from,$to){//全部店员基本分佣
  65. $query->whereBetweenTime('st_time',$from,$to);
  66. }],'seller_comm_money')
  67. ->paginate();
  68. }
  69. public function listsExport($from,$to,$param=[]){
  70. $where = [];
  71. if (isset($param['shop_name']) && !empty($param['shop_name'])){
  72. $where[] = ['yoshop_shops.shop_name','like','%'.$param['shop_name'].'%'];
  73. }
  74. if (isset($param['region_id']) && $param['region_id']>0){
  75. $where[] = ['yoshop_shops.region_id','=',$param['region_id']];
  76. }
  77. if (isset($param['city_id']) && $param['city_id']>0){
  78. $where[] = ['yoshop_shops.city_id','=',$param['city_id']];
  79. }
  80. if (isset($param['province_id']) && $param['province_id']>0){
  81. $where[] = ['yoshop_shops.province_id','=',$param['province_id']];
  82. }
  83. $list = Shops::alias('yoshop_shops')
  84. ->leftJoin('areas','yoshop_shops.city_id=areas.id')
  85. ->field('yoshop_shops.shop_id,yoshop_shops.shop_name,yoshop_shops.city_id,areas.name')
  86. ->where('yoshop_shops.is_delete',0)
  87. ->where($where)
  88. ->withSum(['monthlyBonusTotalMoney'=>function($query) use ($from,$to){
  89. $query->whereBetweenTime('st_time',$from,$to);
  90. }],'total_price')
  91. ->withSum(['monthlyBonusExpr'=>function($query) use ($from,$to){
  92. $query->whereBetweenTime('st_time',$from,$to);
  93. }],'express_price')
  94. ->withSum(['monthlyBonusGoods'=>function($query) use ($from,$to){
  95. $query->whereBetweenTime('st_time',$from,$to);
  96. }],'total_goods_price')
  97. ->withSum(['monthlyBonusPay'=>function($query) use ($from,$to){
  98. $query->whereBetweenTime('st_time',$from,$to);
  99. }],'total_pay_price')
  100. ->withSum(['monthlyBonusCoupon'=>function($query) use ($from,$to){
  101. $query->whereBetweenTime('st_time',$from,$to);
  102. }],'coupon_money')
  103. ->withSum(['monthlyBonusOrder'=>function($query) use ($from,$to){
  104. $query->whereBetweenTime('st_time',$from,$to);
  105. }],'order_count')
  106. ->withSum(['monthlyBonusBoss'=>function($query) use ($from,$to){
  107. $query->whereBetweenTime('st_time',$from,$to);
  108. }],'boss_comm_money')
  109. ->withSum(['monthlyBonusMg'=>function($query) use ($from,$to){
  110. $query->whereBetweenTime('st_time',$from,$to);
  111. }],'mg_comm_money')
  112. ->withSum(['monthlyBonusSel'=>function($query) use ($from,$to){
  113. $query->whereBetweenTime('st_time',$from,$to);
  114. }],'seller_comm_money')
  115. ->select();
  116. $data['header'] = ['序号', '门店名称', '门店城市','应付总金额(含运费)', '应付商品总金额', '实付金额','运费','平台优惠券抵扣','订单数','店老板基本分佣','店长基本分佣','全部店员基本分佣'];
  117. $data['filename'] = date('Y-m-d',$from).'~'.date('Y-m-d',$to).'门店购买统计';
  118. $data['data'] = [];
  119. foreach ($list as $key=>$arr){
  120. $new_list['key'] = $key+1;
  121. $new_list['shop_name'] = $arr['shop_name'];
  122. $new_list['name'] = $arr['name'];
  123. $new_list['monthly_bonus_total_money_sum'] = $arr['monthly_bonus_total_money_sum']?:0;
  124. $new_list['goods_sum'] = $arr['monthly_bonus_goods_sum']?:0;
  125. $new_list['pay_sum'] = $arr['monthly_bonus_pay_sum']?:0;
  126. $new_list['monthly_bonus_expr_sum'] = $arr['monthly_bonus_expr_sum']?:0;
  127. $new_list['coupon_sum'] = $arr['monthly_bonus_coupon_sum']?:0;
  128. $new_list['order_sum'] = $arr['monthly_bonus_order_sum']?:0;
  129. $new_list['boss_sum'] = $arr['monthly_bonus_boss_sum']?:0;
  130. $new_list['mg_sum'] = $arr['monthly_bonus_mg_sum']?:0;
  131. $new_list['sel_sum'] = $arr['monthly_bonus_sel_sum']?:0;
  132. $data['data'][] = $new_list;
  133. }
  134. return $data;
  135. }
  136. /**
  137. * 门店业绩销售额排名
  138. * @param $from
  139. * @param $to
  140. * @param $page
  141. * @return mixed
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\DbException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. */
  146. public function listStRank($from,$to,$page){
  147. $pageP = $page - 1;
  148. $shopM = new Shops();
  149. $i = 1;
  150. return self::whereBetweenTime('st_time',$from,$to)
  151. ->field('sum(total_pay_price) as sale_volume,sum(order_count) as order_count,shop_id')
  152. ->group('shop_id')->order('sale_volume desc')->paginate(15)
  153. ->each(function(&$item) use ($shopM,$pageP,&$i){
  154. $shop = Shops::where('shop_id',$item['shop_id'])
  155. ->field('shop_id,shop_name,province_id,city_id,region_id')->find();
  156. $item['shop_name'] = $shop['shop_name'];
  157. $item['region'] = $shopM->getRegion($shop);
  158. $item['sort'] = $pageP*15 + $i;
  159. $i ++ ;
  160. });
  161. }
  162. /**
  163. * 门店业绩销售额排名
  164. * @param $from
  165. * @param $to
  166. * @return array
  167. * @throws \think\db\exception\DataNotFoundException
  168. * @throws \think\db\exception\DbException
  169. * @throws \think\db\exception\ModelNotFoundException
  170. */
  171. public function listsRankExport($from,$to){
  172. $shopM = new Shops();
  173. $list = self::whereBetweenTime('st_time',$from,$to)
  174. ->field('sum(total_pay_price) as sale_volume,sum(order_count) as order_count,shop_id')
  175. ->group('shop_id')->order('sale_volume desc')->select()
  176. ->each(function($item) use ($shopM){
  177. $shop = Shops::where('shop_id',$item['shop_id'])
  178. ->field('shop_id,shop_name,province_id,city_id,region_id')->find();
  179. $item['shop_name'] = $shop['shop_name'];
  180. $item['region'] = $shopM->getRegion($shop);
  181. });
  182. $data['header'] = ['排名', '门店名称', '门店地区', '订单数', '销售额'];
  183. $data['filename'] = date('Y-m-d',$from).'~'.date('Y-m-d',$to).'门店业绩排名';
  184. $data['data'] = [];
  185. foreach ($list as $key=>$arr){
  186. $new_list['key'] = $key+1;
  187. $new_list['shop_name'] = $arr['shop_name'];
  188. $new_list['region'] = $arr['region'];
  189. $new_list['order_count'] = $arr['order_count'];
  190. $new_list['sale_volume'] = $arr['sale_volume'];
  191. $data['data'][] = $new_list;
  192. }
  193. return $data;
  194. }
  195. }