Shops.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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\library\helper;
  14. use app\common\model\ShopIdentity;
  15. use app\common\model\Shops as ShopModel;
  16. use app\common\model\user\CommissionsDetail;
  17. use Exception;
  18. use think\facade\Db;
  19. use think\facade\Log;
  20. use app\common\model\chef\ChefAreas as RegionModel;
  21. use app\store\model\user\BonusHistory;
  22. use app\common\model\store\Setting as SettingModel;
  23. use app\store\model\user\CommissionSteps;
  24. use app\common\enum\Setting as SettingEnum;
  25. use app\store\model\Order as OrderModel;
  26. /**
  27. * 门店模型
  28. * Class Goods
  29. * @package app\api\model
  30. */
  31. class Shops extends ShopModel
  32. {
  33. /**
  34. * 隐藏字段
  35. * @var array
  36. */
  37. public $hidden = [
  38. 'update_time',
  39. 'is_delete'
  40. ];
  41. // 追加字段
  42. protected $append = ['region'];
  43. public function getRegionAttr($value, $data)
  44. {
  45. $regionNames = $this->getRegionNames($data);
  46. return "{$regionNames['province']}{$regionNames['city']}";
  47. }
  48. public function getRegion($info)
  49. {
  50. if (empty($info))return '';
  51. $regionNames = $this->getRegionNames($info->toArray());
  52. return "{$regionNames['province']}{$regionNames['city']}{$regionNames['region']}";
  53. }
  54. /**
  55. * 获取省市区名称
  56. * @param array $data
  57. * @return mixed
  58. */
  59. private function getRegionNames(array $data)
  60. {
  61. static $dataset = [];
  62. $id = $data[$this->getPk()];
  63. if (!isset($dataset[$id])) {
  64. $dataset[$id] = [
  65. 'province' => RegionModel::getNameById($data['province_id']??0),
  66. 'city' => RegionModel::getNameById($data['city_id']??0),
  67. 'region' => RegionModel::getNameById($data['region_id']??0),
  68. ];
  69. }
  70. return $dataset[$id];
  71. }
  72. public static function getList($conditions){
  73. $model = new self();
  74. $from = 1633017600 ;$to = time();
  75. if ($conditions and count($conditions)){
  76. foreach ($conditions as $key=>$c){
  77. if ($key == 'is_delete'){
  78. $model = $model->where($key,$c);
  79. }
  80. if ($key == "shop_id" && $c > 0){
  81. $model = $model->where($key,$c);
  82. }
  83. if ($key == "shop_name" && trim($c,' ')){
  84. $model = $model->whereLike($key,'%'.$c.'%');
  85. }
  86. if ($key == 'contact_mobile' && trim($c,' ')){
  87. $model = $model->whereLike($key,'%'.$c.'%');
  88. }
  89. if ($key == "region_id" && $c>0 && trim($c)){
  90. $model = $model->where($key,$c);
  91. }
  92. if ($key == "city_id" && $c>0 && trim($c)){
  93. $model = $model->where($key,$c);
  94. }
  95. if ($key == "province_id" && $c>0 && trim($c)){
  96. $model = $model->where($key,$c);
  97. }
  98. if ($key =='betweenTime'){
  99. Log::debug("bt::".json_encode($c));
  100. if (isset($c[0]) && $c[0] && isset($c[1]) && $c[1]){
  101. $times = between_time($c);
  102. $from = $times['start_time'];
  103. $to = $times['end_time'] + 86400;
  104. }
  105. }
  106. }
  107. if ($from && $to){
  108. $model = $model->whereBetweenTime('create_time',$from,$to);
  109. }
  110. }
  111. $model = $model->withCount(['sellers' => function($query) {
  112. $query->where('role',\app\api\model\User::SHOP_SELLER);
  113. }])
  114. /* ->withSum('storeSales','total_price')
  115. ->withSum('storeSalesSub','distributor_total_money')*/
  116. ->withSum(['storeCommission'=>function($query){
  117. $query->where('clearing_status',1);
  118. }],'clearing_money')
  119. ->withCount(['shopConsumers'=>function($query) {
  120. $query->whereIn('role',[\app\api\model\User::NORMAL_USER,\app\api\model\User::COMMISSION_USER]);
  121. }]);
  122. $res = $model->order('shop_id','desc')->paginate(15)->toArray();
  123. return $res;
  124. }
  125. public static function shopBossUserId($shopId){
  126. $shop = self::where('shop_id',$shopId)->find();
  127. return $shop?$shop->boss_user_id:0;
  128. }
  129. public static function shopMgUserId($shopId){
  130. $shop = self::where('shop_id',$shopId)->find();
  131. return $shop?$shop->manager_user_id:0;
  132. }
  133. /**
  134. * 店员统计
  135. * @return \think\model\relation\HasMany
  136. */
  137. public function sellers(): \think\model\relation\HasMany
  138. {
  139. return $this->hasMany(User::class,'shop_id','shop_id');
  140. }
  141. /**
  142. * 店铺销售额
  143. * @return \think\model\relation\HasMany
  144. */
  145. public function storeSales(): \think\model\relation\HasMany
  146. {
  147. return $this->hasMany(OrderGoods::class,'shop_id','shop_id');
  148. }
  149. /**
  150. * 店铺销售额
  151. * @return \think\model\relation\HasMany
  152. */
  153. public function storeSalesSub(): \think\model\relation\HasMany
  154. {
  155. return $this->hasMany(OrderGoods::class,'shop_id','shop_id');
  156. }
  157. /**
  158. * 统计店铺总佣金
  159. * @return \think\model\relation\HasMany
  160. */
  161. public function storeCommission(): \think\model\relation\HasMany
  162. {
  163. return $this->hasMany(CommissionsDetail::class,'shop_id','shop_id');
  164. }
  165. /**
  166. * 门店关联消费者
  167. * @return \think\model\relation\HasMany
  168. */
  169. public function shopConsumers(): \think\model\relation\HasMany
  170. {
  171. return $this->hasMany(User::class,'bind_shop_id','shop_id');
  172. }
  173. /**
  174. * 门店关联门店身份
  175. * @return \think\model\relation\HasMany
  176. */
  177. public function shopIdentity(): \think\model\relation\HasMany
  178. {
  179. return $this->hasMany(ShopIdentity::class,'shop_id','shop_id');
  180. }
  181. /**
  182. * @param $data
  183. * @param bool $needShopId
  184. * @return bool|array
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\DbException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. */
  189. public function add($data,$needShopId =false){
  190. if (isset($data['boss_mobile']) && $data['boss_mobile'] && isset($data['manager_mobile']) && $data['manager_mobile'] && ($data['boss_mobile'] == $data['manager_mobile'])){
  191. Log::error('boss:'.$data['boss_mobile'].',manager:'.$data['manager_mobile']);
  192. $this->error = '店老板与店长手机号不能一样哦';
  193. return false;
  194. }
  195. // 店老板不能同时在其他门店担任职务 不能是分销员(除老板身份外)
  196. if (!empty($data['boss_mobile'])) {
  197. $bossOld = User::where('mobile', $data['boss_mobile'])->find();
  198. if ($bossOld && $bossOld['role'] > User::SHOP_BOSS){
  199. $this->error = '同一账号不支持多种身份哦';
  200. return false;
  201. }
  202. if ($bossOld){
  203. $data['boss_nick_name'] = $bossOld['nick_name'];
  204. }
  205. }
  206. // 店长不能同时在其他门店担任职务 不能是分销员
  207. if (!empty($data['manager_mobile']) && User::where('mobile', $data['manager_mobile'])->where('role', '>', User::NORMAL_USER)
  208. ->where('role', '<=', User::COMMISSION_USER)->find()) {
  209. $this->error = '同一账号不支持多种身份';
  210. return false;
  211. }
  212. $manager_user_id = 0;
  213. Db::startTrans();
  214. try {
  215. $model = new self();
  216. $model->shop_name = $data['shop_name']??"";
  217. $model->addr = $data['addr']??"";
  218. $model->contact = $data['contact'] ?? '';
  219. $model->contact_mobile = $data['contact_mobile'] ?? '';
  220. $model->province_id = $data['province_id']??0;
  221. $model->city_id = $data['city_id']??0;
  222. $model->region_id = $data['region_id']??0;
  223. $model->detail = $data['detail']??'';
  224. $model->is_virtual = $data['is_virtual']??0;
  225. $model->save();
  226. $shopId = $model->shop_id;
  227. Log::error("add shop_id::".json_encode($shopId));
  228. if (!empty($data['is_virtual']) && $data['is_virtual'] == Shops::SHOP_TYPE_VIRTUAL) { // 虚拟门店 @v1.3.6
  229. // 生成虚拟的店老板手机号
  230. $boss_mobile = User::makeVirtualMobile(1);
  231. if (!$boss_mobile) {
  232. $this->error = '店老板手机号存在,请重试';
  233. return false;
  234. }
  235. $data['boss_mobile'] = $boss_mobile;
  236. $data['boss_nick_name'] = User::makeVirtualName('系统店老板');
  237. $boss_user_id = User::createUserByMobile($data['boss_nick_name'],$data['boss_mobile'],$shopId,User::SHOP_BOSS);
  238. // 生成虚拟的默认店员手机号
  239. $staff_mobile = User::makeVirtualMobile(2);
  240. if (!$staff_mobile) {
  241. $this->error = '店员手机号存在,请重试';
  242. return false;
  243. }
  244. $staf_nick_name = User::makeVirtualName('系统店员');;
  245. $staff_user_id = User::createUserByMobile($staf_nick_name,$staff_mobile,$shopId,User::SHOP_SELLER);
  246. $model->contact = User::makeVirtualName('系统店面联系人');
  247. $model->contact_mobile = User::makeVirtualMobile(3);
  248. } else { // 原有逻辑
  249. $boss_user_id = User::createUserByMobile($data['boss_nick_name'],$data['boss_mobile'],$shopId,User::SHOP_BOSS, $shopId);
  250. if ($boss_user_id === false){
  251. throw new \Exception('系统繁忙', 500);
  252. }
  253. if (isset($data['manager_mobile']) && $data['manager_mobile']){
  254. $managerNickname = $data['manager_nick_name']??$data['manager_mobile'];
  255. $manager_user_id = User::createUserByMobile($managerNickname,$data['manager_mobile'],$shopId,User::SHOP_MG, $shopId);
  256. if ($manager_user_id === false){
  257. throw new \Exception('系统繁忙', 500);
  258. }
  259. }
  260. }
  261. Log::error('boss_user::'.json_encode($boss_user_id));
  262. Log::error('manager_user_id::'.json_encode($manager_user_id));
  263. $model->boss_user_id = $boss_user_id;
  264. $model->manager_user_id = $manager_user_id;
  265. $model->save();
  266. Db::commit();
  267. if ($needShopId){
  268. return ['shop_id'=>$shopId,'boss_user_id'=>$boss_user_id,'manager_user_id'=>$manager_user_id,'boss_nick_name'=>$data['boss_nick_name']];
  269. }
  270. return true;
  271. } catch (\Exception $e){
  272. Db::rollback();
  273. Log::error(__METHOD__.$e->getMessage());
  274. $this->error = $e->getMessage();
  275. return false;
  276. }
  277. }
  278. public static function detail($id){
  279. return self::with(['bossUser'=>function($query){
  280. $query->field('user_id,nick_name,mobile');
  281. },'mgUser'=>function($query){
  282. $query->field('user_id,nick_name,mobile');
  283. }, 'shopIdentity' => function($query) {
  284. $query->field('shop_id,role_id,fc_percent');
  285. }])->find($id);
  286. }
  287. public function bossUser(){
  288. return $this->hasOne(User::class, 'user_id','boss_user_id');
  289. }
  290. public function mgUser(){
  291. return $this->hasOne(User::class, 'user_id','manager_user_id');
  292. }
  293. /**
  294. * 编辑门店
  295. * @param $data
  296. * @return bool|int
  297. */
  298. public function edit($data){
  299. Db::startTrans();
  300. try {
  301. $shop = self::find($data['shop_id']);
  302. $shop->shop_name = $data['shop_name'];
  303. $shop->addr = $data['addr']??"";
  304. $shop->contact = $data['contact']??'';
  305. $shop->contact_mobile = $data['contact_mobile'];
  306. $shop->province_id = $data['province_id']??0;
  307. $shop->city_id = $data['city_id']??0;
  308. $shop->region_id = $data['region_id']??0;
  309. $shop->detail = $data['detail']??'';
  310. $shop->save();
  311. Db::commit();
  312. return true;
  313. }catch (\Exception $e){
  314. Db::rollback();
  315. Log::error(__METHOD__.',变更shop_id::'.$data['shop_id'].','.$e->getMessage());
  316. $this->error = $e->getMessage();
  317. return false;
  318. }
  319. }
  320. /**
  321. * 设置门店自提开关
  322. */
  323. public function setPickup(array $data){
  324. $this->is_pickup = $data['is_pickup'];
  325. return $this->save();
  326. }
  327. public function shopPerform($shopId,$from,$to): array
  328. {
  329. $res = self::field('shop_id,create_time')
  330. ->withCount(['straightUsersCnt'=>function($query) use ($from,$to){
  331. $query->whereBetweenTime('create_time',$from,$to);
  332. }])
  333. /* ->withCount(['recOrdersCnt'=>function($query) use ($from,$to){
  334. $query->whereBetweenTime('create_time',$from,$to);
  335. }])
  336. ->withSum(['recOrdersCnt'=>function($query) use ($from,$to){
  337. $query->whereBetweenTime('create_time',$from,$to);
  338. }],'total_pay_price')*/
  339. ->withSum(['commissionTotal'=>function($query) use ($from,$to){
  340. $query->where('clearing_status',1)->whereBetweenTime('create_time',$from,$to);
  341. }],'clearing_money')->where('shop_id',$shopId)->find()->toArray();
  342. $staff = User::where('shop_id',$shopId)->column('user_id');
  343. $res['straight_users_cnt_count'] = User::whereIn('upper_user_id',$staff)->where('bind_shop_id',$shopId)->count();
  344. //$res['rec_orders_cnt_sum'] = $this->getRecOrdersAllSum($shopId,$from,$to);
  345. $res['rec_orders_cnt_sum'] = OrderGoods::sumShopGiveOutOrder($shopId,$from,$to);
  346. $res['rec_orders_cnt_count'] = $this->getRecOrdersAllCnt($shopId,$from,$to);
  347. $res['shop_bonus'] = BonusHistory::bonusAmount($shopId, 2); // 店内
  348. return $res;
  349. }
  350. public function straightUsersCnt(){
  351. return $this->hasMany(User::class,'bind_shop_id','shop_id');
  352. }
  353. public function recOrdersCnt(){
  354. return $this->hasMany(OrderGoods::class,'shop_id','shop_id');
  355. }
  356. public function commissionTotal(){
  357. return $this->hasMany(CommissionsDetail::class,'shop_id','shop_id');
  358. }
  359. /**
  360. * 单店铺的销售额
  361. * @param $shopId
  362. * @param $from
  363. * @param $to
  364. * @return float
  365. */
  366. /* public function getRecOrdersAllSum($shopId,$from,$to){
  367. $orderGoodsId = CommissionsDetail::where('shop_id',$shopId)->whereBetweenTime('create_time',$from,$to)
  368. ->column('order_goods_id');
  369. $orderGoodsId = array_unique($orderGoodsId);
  370. $temp = helper::bcsub(OrderGoods::whereIn('order_goods_id',$orderGoodsId)->whereBetweenTime('create_time',$from,$to)
  371. ->sum('total_price'),OrderGoods::whereIn('order_goods_id',$orderGoodsId)->whereBetweenTime('create_time',$from,$to)
  372. ->sum('distributor_total_money'),4);
  373. return helper::bcsub($temp ,OrderGoods::whereIn('order_goods_id',$orderGoodsId)->whereBetweenTime('create_time',$from,$to)
  374. ->sum('coupon_money'),4);
  375. }*/
  376. /**
  377. * 单店铺的订单数量
  378. * @param $shopId
  379. * @param $from
  380. * @param $to
  381. * @return int
  382. */
  383. public function getRecOrdersAllCnt($shopId,$from,$to){
  384. $orderIds = CommissionsDetail::where('shop_id',$shopId)->whereBetweenTime('create_time',$from,$to)
  385. ->column('order_id');
  386. return count(array_unique($orderIds));
  387. }
  388. /**
  389. * 顾客上级是店员时,推广费用最大占比
  390. * 推广费用最大占比 = 顾客购买优惠比例 + 店员最高阶梯佣金比例 + 店内其他身份最高阶梯佣金比例+门店日常佣金总比例
  391. *
  392. * 顾客上级是分销员时,推广费用最大占比
  393. * 推广费用最大占比 = 顾客购买优惠比例 + 一级佣金比例 + 二级佣金比例
  394. */
  395. public function getRecMaxPercent($shopId) {
  396. $distributor_percent = SettingModel::getItem(SettingEnum::DISTRIBUTOR)['shopping_discount']; // 顾客购买优惠比例
  397. $shop = self::detail($shopId);
  398. $staff_max_percent = CommissionSteps::where('shop_id', $shopId)->where('role', User::SHOP_SELLER)->max('bonus_ratio') ?? 0;
  399. $boss_max_percent = CommissionSteps::where('shop_id', $shopId)->where('role', User::SHOP_BOSS)->max('bonus_ratio') ?? 0;
  400. $manager_max_percent = CommissionSteps::where('shop_id', $shopId)->where('role', User::SHOP_MG)->max('bonus_ratio') ?? 0;
  401. $chef_max_percent = CommissionSteps::where('shop_id', $shopId)->where('role', User::SHOP_CHEF)->max('bonus_ratio') ?? 0;
  402. $chef_count = User::where('role', User::SHOP_CHEF)->where('shop_id', $shopId)->count();
  403. // 厨师佣金百分比
  404. $chef_percent = ShopIdentity::where('shop_id', $shopId)->where('role_id', User::SHOP_CHEF)->value('fc_percent') ?? 0;
  405. $chef_percent_total = $chef_count * $chef_percent;
  406. // 门店日常佣金百分比
  407. $shop_percent_total = $shop['staff_percent'] + $shop['manager_percent'] + $shop['boss_percent'] + $chef_percent_total;
  408. $distributorGradeSetting = SettingModel::getItem(SettingEnum::DISTRIBUTOR_GRADE)['distributor'][2];
  409. $distributorStepSetting = SettingModel::getItem(SettingEnum::DISTRIBUTOR_STEP)['distributor'][1];
  410. $staff_rec_max_percent = $distributor_percent + $staff_max_percent + $boss_max_percent + $manager_max_percent + $chef_max_percent + $shop_percent_total;
  411. $commission_rec_max_percent = $distributor_percent + $distributorGradeSetting['first_commission'] + $distributorGradeSetting['second_commission'] + $distributorStepSetting['bonus_ratio'];
  412. return [$staff_rec_max_percent, $commission_rec_max_percent];
  413. }
  414. //日基础数据统计模块
  415. public function monthlyBonusTotalMoney(){
  416. return $this->hasMany(ShopsDailySalesSt::class,'shop_id','shop_id');
  417. }
  418. public function monthlyBonusExpr(){
  419. return $this->hasMany(ShopsDailySalesSt::class,'shop_id','shop_id');
  420. }
  421. public function monthlyBonusGoods(){
  422. return $this->hasMany(ShopsDailySalesSt::class,'shop_id','shop_id');
  423. }
  424. public function monthlyBonusPay(){
  425. return $this->hasMany(ShopsDailySalesSt::class,'shop_id','shop_id');
  426. }
  427. public function monthlyBonusCoupon(){
  428. return $this->hasMany(ShopsDailySalesSt::class,'shop_id','shop_id');
  429. }
  430. public function monthlyBonusOrder(){
  431. return $this->hasMany(ShopsDailySalesSt::class,'shop_id','shop_id');
  432. }
  433. public function monthlyBonusBoss(){
  434. return $this->hasMany(ShopsDailySalesSt::class,'shop_id','shop_id');
  435. }
  436. public function monthlyBonusMg(){
  437. return $this->hasMany(ShopsDailySalesSt::class,'shop_id','shop_id');
  438. }
  439. public function monthlyBonusSel(){
  440. return $this->hasMany(ShopsDailySalesSt::class,'shop_id','shop_id');
  441. }
  442. //关联月度奖励金统计模块
  443. public function monthBonusTotal(){
  444. return $this->hasMany(ShopsMonthlyBonusSt::class,'shop_id','shop_id');
  445. }
  446. public function monthBonusBossTotal(){
  447. return $this->hasMany(ShopsMonthlyBonusSt::class,'shop_id','shop_id');
  448. }
  449. public function monthBonusBoss(){
  450. return $this->hasMany(ShopsMonthlyBonusSt::class,'shop_id','shop_id');
  451. }
  452. public function monthBonusMgTotal(){
  453. return $this->hasMany(ShopsMonthlyBonusSt::class,'shop_id','shop_id');
  454. }
  455. public function monthBonusMg(){
  456. return $this->hasMany(ShopsMonthlyBonusSt::class,'shop_id','shop_id');
  457. }
  458. public function monthBonusSellerTotal(){
  459. return $this->hasMany(ShopsMonthlyBonusSt::class,'shop_id','shop_id');
  460. }
  461. public function monthBonusSellerl(){
  462. return $this->hasMany(ShopsMonthlyBonusSt::class,'shop_id','shop_id');
  463. }
  464. //获取门店销售及佣金统计
  465. public function getSaleList($conditions){
  466. $model = new Shops();
  467. $from = 1633017600;
  468. $to = time();
  469. $city_id_arr = [];
  470. $regin = new RegionModel;
  471. if ($conditions and count($conditions)){
  472. foreach ($conditions as $key=>$c){
  473. if ($key == "shop_name" && trim($c,' ')){
  474. $model = $model->whereLike($key,'%'.$c.'%');
  475. }
  476. if($key=='city_name' && trim($c,' ')){
  477. $city_id_arr = $regin->where('name','like','%'.$c.'%')->where('level',2)->column("id");
  478. }
  479. if ($key =='betweenTime'){
  480. if (isset($c[0]) && $c[0] && isset($c[1]) && $c[1]){
  481. $times = between_time_format($c);
  482. $from = $times['start_time'];
  483. $to = $times['end_time'];
  484. }
  485. }
  486. }
  487. }
  488. if($city_id_arr){
  489. $model = $model->where("city_id",'in',$city_id_arr);
  490. }
  491. $model = $model->with(['storeCommission'=>function($query) use ($from,$to){
  492. $query->where('clearing_status','in',[0,1])->whereBetweenTime('order_create_time',$from,$to);
  493. }]);
  494. $order = new OrderModel;
  495. $list = $model->order('shop_id','desc')->paginate(15)->each(function($item)use ($order){
  496. $order_id_arr = [];
  497. $order_amount = 0;
  498. $boss_amount = 0;
  499. $boss_amount_js = 0;
  500. $dz_amount = 0;
  501. $dz_amount_js = 0;
  502. $dy_amount = 0;
  503. $dy_amount_js = 0;
  504. foreach($item['storeCommission'] as $r){
  505. $order_id_arr[] = $r['order_id'];
  506. if($r['role']==2){ //店老板
  507. $boss_amount += $r['clearing_money'];
  508. if($r['clearing_status']==1){
  509. $boss_amount_js +=$r['clearing_money'];
  510. }
  511. }
  512. if($r['role']==3){ //店长
  513. $dz_amount +=$r['clearing_money'];
  514. if($r['clearing_status']==1){
  515. $dz_amount_js +=$r['clearing_money'];
  516. }
  517. }
  518. if($r['role']==4){ //店员
  519. $dy_amount +=$r['clearing_money'];
  520. if($r['clearing_status']==1){
  521. $dy_amount_js +=$r['clearing_money'];
  522. }
  523. $order_amount +=$r['order_sale_volume'];
  524. }
  525. }
  526. $item['boss_amount'] = helper::bcadd($boss_amount,0,2);
  527. $item['boss_amount_js'] = helper::bcadd($boss_amount_js,0,2);
  528. $item['dz_amount'] = helper::bcadd($dz_amount,0,2);
  529. $item['dz_amount_js'] = helper::bcadd($dz_amount_js,0,2);
  530. $item['dy_amount'] = helper::bcadd($dy_amount,0,2);
  531. $item['dy_amount_js'] = helper::bcadd($dy_amount_js,0,2);
  532. $order_id_arr = array_unique($order_id_arr);
  533. $item['order_id_arr'] = $order_id_arr;
  534. $item['order_cnt'] = count($order_id_arr);
  535. $item['order_amount'] = helper::bcadd($order_amount,0,2);
  536. })->toArray();
  537. return $list;
  538. }
  539. }