Shop.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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\controller;
  13. use app\common\enum\Setting as SettingEnum;
  14. use app\common\library\helper;
  15. use app\common\model\store\Setting as SettingModel;
  16. use app\store\model\ConfigPercent;
  17. use app\store\model\OrderGoods;
  18. use app\store\model\ShopIdentity;
  19. use app\store\model\Shops;
  20. use think\facade\Log;
  21. use app\store\model\User;
  22. use app\store\model\user\BonusHistory;
  23. use app\store\model\user\CommissionSteps;
  24. use Exception;
  25. use app\store\model\user\CommissionDetail;
  26. use app\store\model\user\UserCommissionsView as UserCommissionsViewModel;
  27. /**
  28. * 门店管理
  29. * Class Shop
  30. * @package app\store\controller
  31. */
  32. class Shop extends Controller
  33. {
  34. //门店个人基本佣金列表
  35. public function usercommissionlist(){
  36. $model = new UserCommissionsViewModel;
  37. $param = request()->get();
  38. $list = $model->getList($param);
  39. return $this->renderSuccess(compact('list'));
  40. }
  41. //个人佣金列表
  42. public function compersonlist(){
  43. $model = new CommissionDetail;
  44. $param = request()->get();
  45. $list = $model->getList($param);
  46. return $this->renderSuccess(compact('list'));
  47. }
  48. //门店销售额及佣金列表
  49. public function salelist(){
  50. $model = new Shops();
  51. $param = request()->get();
  52. $list = $model->getSaleList($param);
  53. return $this->renderSuccess(compact('list'));
  54. }
  55. /**
  56. * 门店管理首页
  57. * @return array
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function list()
  63. {
  64. $model = new Shops();
  65. //$cs = $this->postForm();
  66. $cs = request()->get();
  67. $list = $model->getList($cs);
  68. $data = &$list['data'];
  69. //dd($data);
  70. $from = 1633017600 ;$to = time();
  71. //$shopsModel = new Shops();
  72. $ogModel = new OrderGoods();
  73. foreach ($data as $k=>$item){
  74. //$data[$k]['store_sales_sum'] =$shopsModel->getRecOrdersAllSum($item['shop_id'],$from,$to);
  75. $data[$k]['store_sales_sum'] = $ogModel->sumShopGiveOutOrder($item['shop_id'],$from,$to);
  76. }
  77. return $this->renderSuccess(compact('list'));
  78. }
  79. /**
  80. * 新增门店
  81. * @return array
  82. */
  83. public function add()
  84. {
  85. //{"form":{"shop_name":"xixi", "addr": "gold","boss_nick_name":"啧啧啧","boss_mobile":"13233332222","manager_nick_name":"啧啧啧","manager_mobile":"13233332222","contact":"李德华", "contact_mobile": "15155551111",
  86. // "chef_percent": 0,
  87. // "shop_identity": [{"role_id":5,"mobile":"13333333333"},{"role_id":5,"mobile":"13333333332"}]
  88. //}}
  89. $model = new Shops();
  90. if ($model->add($this->postForm())) {
  91. return $this->renderSuccess('添加成功');
  92. }
  93. return $this->renderError($model->getError() ?: '添加失败');
  94. }
  95. /**
  96. * 店铺详情
  97. *
  98. * shopIdentity:{shop_id,role_id,user_id}
  99. * @param $id
  100. * @return array
  101. */
  102. public function detail($id){
  103. $model = new Shops();
  104. $list = $model->detail($id);
  105. $list['boss_nick_name'] = $list['bossUser']['nick_name'];
  106. $list['boss_mobile'] = $list['bossUser']['mobile'];
  107. $list['manager_nick_name'] = $list['mgUser']['nick_name'] ?? '';
  108. $list['manager_mobile'] = $list['mgUser']['mobile'] ?? '';
  109. unset($list['bossUser'],$list['mgUser']);
  110. $list['regionArr'] = [$list->province_id,$list->city_id,$list->region_id];
  111. // 门店身份用户
  112. $list['identityUser'] = User::where('role', '>', User::SHOP_SELLER)
  113. ->where('role', '<', User::COMMISSION_USER)
  114. ->order('role', 'asc')->where('shop_id', $list['shop_id'])
  115. ->field('user_id,role,shop_id,mobile')
  116. ->select();
  117. $other = 0;
  118. $chefPercent = 0;
  119. if (!empty($list['shopIdentity'])) {
  120. foreach ($list['shopIdentity'] as $item) {
  121. $other = $other + User::where('role', $item['role_id'])->where('shop_id', $list['shop_id'])->count()*$item['fc_percent'];
  122. if ($item['role_id'] == User::SHOP_CHEF) {
  123. $chefPercent = $item['fc_percent'];
  124. }
  125. }
  126. }
  127. $list['chef_percent'] = $chefPercent;
  128. // 门店阶梯达量奖励配置
  129. $commissionStepsModel = new CommissionSteps();
  130. $commissionSteps = $commissionStepsModel->getCommissionSteps($id);
  131. $list['staff_commission_steps'] = $commissionSteps['staff_commission_steps'];
  132. $list['identity_commission_steps'] = $commissionSteps['identity_commission_steps'];
  133. // 顾客上级是店员时,推广费用最大占比
  134. $list['staf_rec_max_percent'] = $model->getRecMaxPercent($id)[0];
  135. // 顾客上级是分销员时,推广费用最大占比
  136. $list['commission_rec_max_percent'] = $model->getRecMaxPercent($id)[1];
  137. $list['fc_percent_total'] = $list['staff_percent'] + $list['manager_percent'] + $list['boss_percent'] + $other;
  138. $list['region'] = $model->getRegion($list);
  139. return $this->renderSuccess(compact('list'));
  140. }
  141. /**
  142. * 店铺详情
  143. *
  144. * shopIdentity:{shop_id,role_id,user_id}
  145. * @param $id
  146. * @return array
  147. */
  148. public function distributordetail(){
  149. $list['distributor_percent'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR);
  150. $list['distributor_grade'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR_GRADE)['distributor'];
  151. $list['distributor_step'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR_STEP)['distributor'];
  152. // 顾客上级是分销员时,推广费用最大占比
  153. $list['commission_rec_max_percent'] = $list['distributor_percent']['shopping_discount']+$list['distributor_grade'][2]['first_commission']+$list['distributor_grade'][2]['second_commission']+$list['distributor_step'][1]['bonus_ratio'];
  154. return $this->renderSuccess(compact('list'));
  155. }
  156. /**
  157. * 业绩表
  158. * @param $id
  159. * @return array
  160. * @throws \think\db\exception\DbException
  161. */
  162. public function commissionList($id){
  163. $m = new User();
  164. //dd($this->request->get('betweenTime',null));
  165. $betweenTime = $this->request->get('betweenTime',null);
  166. $from = 1633017600 ;$to = time();
  167. if (!empty($betweenTime)) {
  168. $times = between_time($betweenTime);
  169. $from = $times['start_time'];
  170. $to = $times['end_time'] + 86400;
  171. }
  172. $list = $m->staffPerformanceList($id,$from,$to);
  173. /* $shopM = new Shops();
  174. $perform = $shopM->shopPerform($id,$from,$to);*/
  175. return $this->renderSuccess(compact('list'));
  176. }
  177. /**
  178. * 业绩表统计数据
  179. * @param $id
  180. * @return array
  181. * @throws \think\db\exception\DbException
  182. */
  183. public function commissionTotal($id){
  184. $betweenTime = $this->request->get('betweenTime',null);
  185. $from = 1633017600 ;$to = time();
  186. if (!empty($betweenTime) && isset($betweenTime[0]) && $betweenTime[0] && isset($betweenTime[1]) && $betweenTime[1]) {
  187. $times = between_time($betweenTime);
  188. $from = $times['start_time'];
  189. $to = $times['end_time'] + 86400;
  190. }
  191. $shopM = new Shops();
  192. $list = $shopM->shopPerform($id,$from,$to);
  193. $boss_commission= User::getBossCommission($id);
  194. $boss_bonus = BonusHistory::bonusAmount($id); // 店老板
  195. return $this->renderSuccess(compact('list', 'boss_commission', 'boss_bonus'));
  196. }
  197. /**
  198. * 新增门店身份用户
  199. */
  200. public function addIdentityUser($id) {
  201. $model = new ShopIdentity();
  202. if ($model->addIdentityUser($id, $this->postForm())) {
  203. return $this->renderSuccess('保存成功');
  204. }
  205. return $this->renderError($model->getError() ?: '添加失败');
  206. }
  207. /**
  208. * 新增门店身份配置
  209. */
  210. public function addShopIdentity($id) {
  211. $model = new ShopIdentity();
  212. if ($model->addShopIdentity($id, $this->postForm())) {
  213. return $this->renderSuccess('保存成功');
  214. }
  215. return $this->renderError($model->getError() ?: '添加失败');
  216. }
  217. /**
  218. * 阶梯达量奖励配置
  219. */
  220. public function addCommissionSteps($id) {
  221. $model = new CommissionSteps();
  222. if ($model->add($id, $this->postForm())) {
  223. return $this->renderSuccess('保存成功');
  224. }
  225. return $this->renderError($model->getError() ?: '添加失败');
  226. }
  227. /**
  228. * 编辑
  229. * //{"form":{"shop_name":"xixi", "addr": "gold","boss_nick_name":"啧啧啧","boss_mobile":"13233332222","manager_nick_name":"啧啧啧","manager_mobile":"13233332222","contact":"李德华", "contact_mobile": "15155551111",
  230. * // "chef_percent": 0,
  231. * // "shop_identity": [{"role_id":5,"mobile":"13333333333"},{"role_id":5,"mobile":"13333333332"}]
  232. * //}}
  233. * @return array
  234. */
  235. public function edit(){
  236. $model = new Shops();
  237. if ($model->edit($this->postForm())) {
  238. return $this->renderSuccess('编辑成功');
  239. }
  240. return $this->renderError($model->getError() ?: '编辑失败');
  241. }
  242. /**
  243. * 获取门店自提开关
  244. */
  245. public function getPickup(int $id) {
  246. $model = new Shops();
  247. $is_pickup = $model->where('shop_id', $id)->value('is_pickup');
  248. return $this->renderSuccess(compact('is_pickup'));
  249. }
  250. /**
  251. * 设置门店自提开关
  252. */
  253. public function setPickup(int $id) {
  254. $post = $this->postForm();
  255. $model = Shops::get($id);
  256. if ($model->setPickup($post)) {
  257. return $this->renderSuccess('保存成功');
  258. }
  259. return $this->renderError($model->getError() ?: '保存失败');
  260. }
  261. /**
  262. * 店员交接,离职
  263. * @return array
  264. */
  265. public function sellerHandover(){
  266. $oldSeller = intval($this->request->get('fromUserId',0));
  267. $toSeller = intval($this->request->get('toUserId',0));
  268. if (!$oldSeller || !$toSeller){
  269. return $this->renderError('参数有误');
  270. }
  271. $m = new User();
  272. try {
  273. $m->consumersHandover($oldSeller,$toSeller);
  274. $m->updateUserRole($oldSeller);
  275. }catch (\Exception $e){
  276. log_record($e->getMessage(),'error');
  277. return $this->renderError('系统繁忙');
  278. }
  279. $list = ['flag'=>'成功'];
  280. return $this->renderSuccess($list);
  281. }
  282. /**
  283. * 顾客交接
  284. * @return array
  285. */
  286. public function consumerHandover(){
  287. $oldSeller = intval($this->request->get('fromUserId',0));
  288. $toSeller = intval($this->request->get('toUserId',0));
  289. if (!$oldSeller || !$toSeller){
  290. return $this->renderError('参数有误');
  291. }
  292. $m = new User();
  293. try {
  294. $m->consumersHandover($oldSeller,$toSeller);
  295. }catch (\Exception $e){
  296. log_record($e->getMessage(),'error');
  297. return $this->renderError('系统繁忙');
  298. }
  299. $list = ['flag'=>'成功'];
  300. return $this->renderSuccess($list);
  301. }
  302. /**
  303. * 通过手机号查找角色
  304. */
  305. public function mobileUser(){
  306. $mobile = strval($this->request->get('mobile',''));
  307. $role = User::getUserByMobile($mobile);
  308. //dd($role);
  309. if(in_array($role,[User::SHOP_BOSS,User::SHOP_MG,User::SHOP_SELLER])){
  310. return $this->renderError('该店长已是其他门店的员工,请先办理离职后再尝试');
  311. }
  312. $list = ['flag'=>$role];
  313. return $this->renderSuccess($list);
  314. }
  315. /**
  316. * 员工列表
  317. * @param $id
  318. * @return array
  319. * @throws \think\db\exception\DbException
  320. */
  321. public function staffList($id){
  322. $list = User::staffList($id);
  323. $boss = User::getBoss($id);
  324. $page = request()->get('page',1);
  325. if ($boss && $page==1){
  326. array_unshift($list['data'],$boss);
  327. }
  328. //dd($list);
  329. return $this->renderSuccess(compact('list'));
  330. }
  331. }