// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\store\controller; use app\common\enum\Setting as SettingEnum; use app\common\library\helper; use app\common\model\store\Setting as SettingModel; use app\store\model\ConfigPercent; use app\store\model\OrderGoods; use app\store\model\ShopIdentity; use app\store\model\Shops; use think\facade\Log; use app\store\model\User; use app\store\model\user\BonusHistory; use app\store\model\user\CommissionSteps; use Exception; use app\store\model\user\CommissionDetail; use app\store\model\user\UserCommissionsView as UserCommissionsViewModel; /** * 门店管理 * Class Shop * @package app\store\controller */ class Shop extends Controller { //门店个人基本佣金列表 public function usercommissionlist(){ $model = new UserCommissionsViewModel; $param = request()->get(); $list = $model->getList($param); return $this->renderSuccess(compact('list')); } //个人佣金列表 public function compersonlist(){ $model = new CommissionDetail; $param = request()->get(); $list = $model->getList($param); return $this->renderSuccess(compact('list')); } //门店销售额及佣金列表 public function salelist(){ $model = new Shops(); $param = request()->get(); $list = $model->getSaleList($param); return $this->renderSuccess(compact('list')); } /** * 门店管理首页 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function list() { $model = new Shops(); //$cs = $this->postForm(); $cs = request()->get(); $list = $model->getList($cs); $data = &$list['data']; //dd($data); $from = 1633017600 ;$to = time(); //$shopsModel = new Shops(); $ogModel = new OrderGoods(); foreach ($data as $k=>$item){ //$data[$k]['store_sales_sum'] =$shopsModel->getRecOrdersAllSum($item['shop_id'],$from,$to); $data[$k]['store_sales_sum'] = $ogModel->sumShopGiveOutOrder($item['shop_id'],$from,$to); } return $this->renderSuccess(compact('list')); } /** * 新增门店 * @return array */ public function add() { //{"form":{"shop_name":"xixi", "addr": "gold","boss_nick_name":"啧啧啧","boss_mobile":"13233332222","manager_nick_name":"啧啧啧","manager_mobile":"13233332222","contact":"李德华", "contact_mobile": "15155551111", // "chef_percent": 0, // "shop_identity": [{"role_id":5,"mobile":"13333333333"},{"role_id":5,"mobile":"13333333332"}] //}} $model = new Shops(); if ($model->add($this->postForm())) { return $this->renderSuccess('添加成功'); } return $this->renderError($model->getError() ?: '添加失败'); } /** * 店铺详情 * * shopIdentity:{shop_id,role_id,user_id} * @param $id * @return array */ public function detail($id){ $model = new Shops(); $list = $model->detail($id); $list['boss_nick_name'] = $list['bossUser']['nick_name']; $list['boss_mobile'] = $list['bossUser']['mobile']; $list['manager_nick_name'] = $list['mgUser']['nick_name'] ?? ''; $list['manager_mobile'] = $list['mgUser']['mobile'] ?? ''; unset($list['bossUser'],$list['mgUser']); $list['regionArr'] = [$list->province_id,$list->city_id,$list->region_id]; // 门店身份用户 $list['identityUser'] = User::where('role', '>', User::SHOP_SELLER) ->where('role', '<', User::COMMISSION_USER) ->order('role', 'asc')->where('shop_id', $list['shop_id']) ->field('user_id,role,shop_id,mobile') ->select(); $other = 0; $chefPercent = 0; if (!empty($list['shopIdentity'])) { foreach ($list['shopIdentity'] as $item) { $other = $other + User::where('role', $item['role_id'])->where('shop_id', $list['shop_id'])->count()*$item['fc_percent']; if ($item['role_id'] == User::SHOP_CHEF) { $chefPercent = $item['fc_percent']; } } } $list['chef_percent'] = $chefPercent; // 门店阶梯达量奖励配置 $commissionStepsModel = new CommissionSteps(); $commissionSteps = $commissionStepsModel->getCommissionSteps($id); $list['staff_commission_steps'] = $commissionSteps['staff_commission_steps']; $list['identity_commission_steps'] = $commissionSteps['identity_commission_steps']; // 顾客上级是店员时,推广费用最大占比 $list['staf_rec_max_percent'] = $model->getRecMaxPercent($id)[0]; // 顾客上级是分销员时,推广费用最大占比 $list['commission_rec_max_percent'] = $model->getRecMaxPercent($id)[1]; $list['fc_percent_total'] = $list['staff_percent'] + $list['manager_percent'] + $list['boss_percent'] + $other; $list['region'] = $model->getRegion($list); return $this->renderSuccess(compact('list')); } /** * 店铺详情 * * shopIdentity:{shop_id,role_id,user_id} * @param $id * @return array */ public function distributordetail(){ $list['distributor_percent'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR); $list['distributor_grade'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR_GRADE)['distributor']; $list['distributor_step'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR_STEP)['distributor']; // 顾客上级是分销员时,推广费用最大占比 $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']; return $this->renderSuccess(compact('list')); } /** * 业绩表 * @param $id * @return array * @throws \think\db\exception\DbException */ public function commissionList($id){ $m = new User(); //dd($this->request->get('betweenTime',null)); $betweenTime = $this->request->get('betweenTime',null); $from = 1633017600 ;$to = time(); if (!empty($betweenTime)) { $times = between_time($betweenTime); $from = $times['start_time']; $to = $times['end_time'] + 86400; } $list = $m->staffPerformanceList($id,$from,$to); /* $shopM = new Shops(); $perform = $shopM->shopPerform($id,$from,$to);*/ return $this->renderSuccess(compact('list')); } /** * 业绩表统计数据 * @param $id * @return array * @throws \think\db\exception\DbException */ public function commissionTotal($id){ $betweenTime = $this->request->get('betweenTime',null); $from = 1633017600 ;$to = time(); if (!empty($betweenTime) && isset($betweenTime[0]) && $betweenTime[0] && isset($betweenTime[1]) && $betweenTime[1]) { $times = between_time($betweenTime); $from = $times['start_time']; $to = $times['end_time'] + 86400; } $shopM = new Shops(); $list = $shopM->shopPerform($id,$from,$to); $boss_commission= User::getBossCommission($id); $boss_bonus = BonusHistory::bonusAmount($id); // 店老板 return $this->renderSuccess(compact('list', 'boss_commission', 'boss_bonus')); } /** * 新增门店身份用户 */ public function addIdentityUser($id) { $model = new ShopIdentity(); if ($model->addIdentityUser($id, $this->postForm())) { return $this->renderSuccess('保存成功'); } return $this->renderError($model->getError() ?: '添加失败'); } /** * 新增门店身份配置 */ public function addShopIdentity($id) { $model = new ShopIdentity(); if ($model->addShopIdentity($id, $this->postForm())) { return $this->renderSuccess('保存成功'); } return $this->renderError($model->getError() ?: '添加失败'); } /** * 阶梯达量奖励配置 */ public function addCommissionSteps($id) { $model = new CommissionSteps(); if ($model->add($id, $this->postForm())) { return $this->renderSuccess('保存成功'); } return $this->renderError($model->getError() ?: '添加失败'); } /** * 编辑 * //{"form":{"shop_name":"xixi", "addr": "gold","boss_nick_name":"啧啧啧","boss_mobile":"13233332222","manager_nick_name":"啧啧啧","manager_mobile":"13233332222","contact":"李德华", "contact_mobile": "15155551111", * // "chef_percent": 0, * // "shop_identity": [{"role_id":5,"mobile":"13333333333"},{"role_id":5,"mobile":"13333333332"}] * //}} * @return array */ public function edit(){ $model = new Shops(); if ($model->edit($this->postForm())) { return $this->renderSuccess('编辑成功'); } return $this->renderError($model->getError() ?: '编辑失败'); } /** * 获取门店自提开关 */ public function getPickup(int $id) { $model = new Shops(); $is_pickup = $model->where('shop_id', $id)->value('is_pickup'); return $this->renderSuccess(compact('is_pickup')); } /** * 设置门店自提开关 */ public function setPickup(int $id) { $post = $this->postForm(); $model = Shops::get($id); if ($model->setPickup($post)) { return $this->renderSuccess('保存成功'); } return $this->renderError($model->getError() ?: '保存失败'); } /** * 店员交接,离职 * @return array */ public function sellerHandover(){ $oldSeller = intval($this->request->get('fromUserId',0)); $toSeller = intval($this->request->get('toUserId',0)); if (!$oldSeller || !$toSeller){ return $this->renderError('参数有误'); } $m = new User(); try { $m->consumersHandover($oldSeller,$toSeller); $m->updateUserRole($oldSeller); }catch (\Exception $e){ log_record($e->getMessage(),'error'); return $this->renderError('系统繁忙'); } $list = ['flag'=>'成功']; return $this->renderSuccess($list); } /** * 顾客交接 * @return array */ public function consumerHandover(){ $oldSeller = intval($this->request->get('fromUserId',0)); $toSeller = intval($this->request->get('toUserId',0)); if (!$oldSeller || !$toSeller){ return $this->renderError('参数有误'); } $m = new User(); try { $m->consumersHandover($oldSeller,$toSeller); }catch (\Exception $e){ log_record($e->getMessage(),'error'); return $this->renderError('系统繁忙'); } $list = ['flag'=>'成功']; return $this->renderSuccess($list); } /** * 通过手机号查找角色 */ public function mobileUser(){ $mobile = strval($this->request->get('mobile','')); $role = User::getUserByMobile($mobile); //dd($role); if(in_array($role,[User::SHOP_BOSS,User::SHOP_MG,User::SHOP_SELLER])){ return $this->renderError('该店长已是其他门店的员工,请先办理离职后再尝试'); } $list = ['flag'=>$role]; return $this->renderSuccess($list); } /** * 员工列表 * @param $id * @return array * @throws \think\db\exception\DbException */ public function staffList($id){ $list = User::staffList($id); $boss = User::getBoss($id); $page = request()->get('page',1); if ($boss && $page==1){ array_unshift($list['data'],$boss); } //dd($list); return $this->renderSuccess(compact('list')); } }