all(); $page_size = $input['page_size']; $page_index = $input['page_index']; $num = ($page_index - 1) * $page_size; $search_name = $input['search_name']; $type = $input['type']; // $search_name=''; // $type=''; $where = []; $data = Store::where($where); if ($type) { $data->where('type', $type); } if ($search_name) { $data->where('name', 'like', '%' . $search_name . '%'); } $count = $data->count(); if ($count == 0) { $this->error('400001', '没有数据'); } $list = $data->with('agent', 'manager:id,nickname,mobile,level') ->skip($num)->take($page_size) ->orderBy('id', 'desc') ->get(); if (empty($list)) { return $this->error('400002', '没有获取到数据'); } return $this->success_list($list, 'success', $count); } //添加门店信息 public function uploadStore(Request $request) { $input = $request->all(); $rules = [ 'name' => [ 'required', Rule::unique('store') ->where(function ($query) { $query->where('deleted_at', null); }) ], ]; $messages = [ 'name.required' => '门店名称不能为空.', 'name.unique' => '门店名称已存在.', ]; $validator = Validator::make($input, $rules, $messages); if ($validator->fails()) { return $this->error('400013', $validator->errors()->first()); } if ($input['type'] == 1 || $input['type'] == 3) { if (count($input['member']) != 1) { return $this->error('400111', '工作室与优享店只能有一个成员'); } } foreach ($input['member'] as $key => $val) { $man = User::where('mobile', $val)->first(); if ($man) { // if($man->store_id){ // return $this->error('400117','代理('.$val.')已有所属门店'); // } if ($input['type'] == 1) { if (!in_array($man->level, [2, 3])) { return $this->error('400113', '代理(' . $val . ')等级与门店类型不符'); } } elseif ($input['type'] == 2 || $input['type'] == 3) { if ($man->level != 3) { return $this->error('400113', '店铺成员必须为代理公司'); } } } else { return $this->error('400119', '代理(' . $val . ')信息不存在'); } } if (!in_array($input['stationMobile'], $input['member'])) { return $this->error('400120', '提交代理信息有误'); } $manager = User::where('mobile', $input['stationMobile'])->first(); if ($manager) { if ($manager->warea_id) { $data['warea_id'] = $manager->warea_id; $data['man_id'] = $manager->id; } else { return $this->error('400118', '代理(' . $input['stationMobile'] . ')未选择战区'); } } else { return $this->error('400119', '代理(' . $input['stationMobile'] . ')信息不存在'); } $data['name'] = $input['name']; $data['contents'] = $input['contents']; $data['type'] = $input['type']; $data['status'] = 1; $data['address'] = $input['address']; $data['province'] = $input['province']; $data['city'] = $input['city']; $data['area'] = $input['area']; $data['size'] = $input['size']; $data['decorate_account'] = $input['decorate']; $data['img'] = json_encode($input['img']); DB::beginTransaction(); try { $row = Store::create($data); foreach ($input['member'] as $key => $val) { $uid = User::where('mobile', $val)->value('id'); if ($uid) { if ($val == $input['stationMobile']) { StoreUser::create(['store_id' => $row->id, 'user_id' => $uid, 'user_mobile' => $val]); } else { StoreUser::create(['store_id' => $row->id, 'user_id' => $uid, 'user_mobile' => $val]); } } } DB::commit(); Log::info('管理员:' . Auth::user()->name . '(id=' . Auth::user()->id . ')添加门店信息成功(id=' . $row->id . ')'); return $this->success([]); } catch (Exception $e) { DB::rollBack(); return $e->getMessage(); return $this->error(); } } //修改门店信息 public function updateStore(Request $request) { $input = $request->all(); $rules = [ 'name' => [ 'required', Rule::unique('store') ->ignore($input['id'], 'id') ->where(function ($query) { $query->where('deleted_at', null); }) ], ]; $messages = [ 'name.required' => '门店名称不能为空.', 'name.unique' => '门店名称已存在.', ]; $validator = Validator::make($input, $rules, $messages); if ($validator->fails()) { return $this->error('400013', $validator->errors()->first()); } if (count($input['member']) != count(array_unique($input['member']))) { return $this->error('450001', '店铺成员重复提交'); } $oldAgent = StoreUser::where('store_id', $input['id'])->pluck('user_mobile')->toArray(); $inter = array_intersect($oldAgent, $input['member']);//交际 $oldDiff = array_diff($oldAgent, $inter);//删除 $AgeDiff = array_diff($input['member'], $inter);//新增 if ($input['type'] == 1 || $input['type'] == 3) { if (count($input['member']) != 1) { return $this->error('400111', '工作室和优享店只能有一个代理'); } } if ($AgeDiff) { foreach ($AgeDiff as $key => $val) { $man = User::where('mobile', $val)->first(); if ($man) { // if($man->store_id){ // return $this->error('400117','代理('.$val.')已有所属门店'); // } if ($input['type'] == 1) { if (!in_array($man->level, [2, 3])) { return $this->error('400113', '代理(' . $val . ')等级与门店类型不符'); } } elseif ($input['type'] == 2 || $input['type'] == 3) { if ($man->level != 3) { return $this->error('400113', '店铺成员必须为代理公司'); } } } else { return $this->error('400119', '代理(' . $val . ')信息不存在'); } } } if (!in_array($input['stationMobile'], $input['member'])) { return $this->error('400120', '提交代理信息有误'); } $manager = User::where('mobile', $input['stationMobile'])->first(); if ($manager) { if ($manager->warea_id) { $data['warea_id'] = $manager->warea_id; $data['man_id'] = $manager->id; } else { return $this->error('400118', '代理(' . $input['stationMobile'] . ')未选择战区'); } } else { return $this->error('400119', '代理(' . $input['stationMobile'] . ')信息不存在'); } $data['name'] = $input['name']; $data['contents'] = $input['contents']; $data['type'] = $input['type']; $data['address'] = $input['address']; $data['province'] = $input['province']; $data['city'] = $input['city']; $data['area'] = $input['area']; $data['size'] = $input['size']; $data['decorate_account'] = $input['decorate']; $data['img'] = json_encode($input['img']); DB::beginTransaction(); try { Store::where('id', $input['id'])->update($data); if ($AgeDiff) { foreach ($AgeDiff as $key => $val) { $u_id = User::where('mobile', $val)->value('id'); if ($val == $input['stationMobile']) { StoreUser::create(['store_id' => $input['id'], 'user_id' => $u_id, 'user_mobile' => $val]); } else { StoreUser::create(['store_id' => $input['id'], 'user_id' => $u_id, 'user_mobile' => $val]); } } } if ($oldDiff) { foreach ($oldDiff as $key => $val) { StoreUser::where('user_mobile', $val)->where('store_id', $input['id'])->delete(); } } DB::commit(); Log::info('管理员:' . Auth::user()->name . '(id=' . Auth::user()->id . ')修改门店信息成功(id=' . $input['id'] . ')'); return $this->success([]); } catch (Exception $e) { DB::rollBack(); // return $e->getMessage(); return $this->error(); } } /* 门店禁用/启用 */ public function shelvedStore(Request $request) { $store = Store::find($request->post('id')); $store->status = ($user->status == 0) ? '1' : '0'; $row = $store->save(); if ($row) { Log::info('管理员:' . Auth::user()->name . '(id=' . Auth::user()->id . ')禁用门店信息成功(id=' . $row->id . ')'); return $this->success([]); } return $this->error(); } //删除门店信息 public function destoryStore(Request $request) { $input = $request->all(); $store = Store::find($input['id']); DB::beginTransaction(); try { $store->delete(); StoreUser::where('store_id', $input['id'])->delete(); DB::commit(); return $this->success([]); } catch (Exception $e) { DB::rollBack(); return $this->error(); } } public function uploadStoreImg(Request $request) { $input = $request->all(); $path_url = 'public/store'; $path = $request->file('file')->store($path_url); $url = Storage::url($path); if ($url) { return $this->success($url); } return $this->error(); } public function getStoreRewardInfo(Request $request) { $input = $request->all(); $page_index = $input['page_index']; $page_size = $input['page_size']; $num = $page_size * ($page_index - 1); // $month = $input['month']; $where = []; // if ($month) { // $where['month'] = $month; // } $count = StoreRewardSet::where($where)->count(); $list = StoreRewardSet::with(['rewards'])->where($where) ->skip($num)->take($page_size) ->orderBy('month', 'desc') ->get(); return $this->success_list($list, '成功', $count); } public function addStoreRewardInfo(Request $request) { $input = $request->all(); $rules = [ 'month' => [ 'required', Rule::unique('store_rewards_set'), // ->ignore($input['id'], 'id') // ->where(function ($query) { // $query->where('deleted_at', null); // }) ], ]; $messages = [ 'month.required' => '月份不能为空.', 'month.unique' => '月份已存在.', ]; $validator = Validator::make($input, $rules, $messages); if ($validator->fails()) { return $this->error('400013', $validator->errors()->first()); } $row = StoreRewardSet::create([ 'month' => $input['month'], 'account_rate' => $input['account_rate'], // 'goods' => json_encode($input['goods']), // 'money' => $input['money'], 'enjoy_hard' => $input['enjoy_hard'], 'enjoy_other' => $input['enjoy_other'], 'over_enjoy_hard' => $input['over_enjoy_hard'], 'over_enjoy_other' => $input['over_enjoy_other'], 'experience_hard' => $input['experience_hard'], 'experience_other' => $input['experience_other'], ]); foreach($input['goods'] as $key=>$val){ StoreRewards::create([ 'month'=>$input['month'], 'name'=>$val['name'], 'price'=>$val['price'], ]); } if ($row) { return $this->success([]); } return $this->error(); } public function updateStoreRewardInfo(Request $request) { $input = $request->all(); $rules = [ 'month' => [ 'required', Rule::unique('store_rewards_set') ->ignore($input['id'], 'id') ], ]; $messages = [ 'month.required' => '月份不能为空.', 'month.unique' => '月份已存在.', ]; $validator = Validator::make($input, $rules, $messages); if ($validator->fails()) { return $this->error('400013', $validator->errors()->first()); } $row = StoreRewardSet::where('id',$input['id'])->update([ 'month' => $input['month'], 'account_rate' => $input['account_rate'], 'enjoy_hard' => $input['enjoy_hard'], 'enjoy_other' => $input['enjoy_other'], 'over_enjoy_hard' => $input['over_enjoy_hard'], 'over_enjoy_other' => $input['over_enjoy_other'], 'experience_hard' => $input['experience_hard'], 'experience_other' => $input['experience_other'], ]); $before_ids=StoreRewards::where('month',$input['month'])->pluck('id')->toArray(); // return $before_ids; $after_ids=[]; foreach($input['goods'] as $key=>$val){ if(array_key_exists('isAdd',$val)){ StoreRewards::create([ 'month'=>$input['month'], 'name'=>$val['name'], 'price'=>$val['price'], ]); }else{ $after_ids[]=$val['id']; } } $diff_ids=array_diff($before_ids,$after_ids); StoreRewards::whereIn('id',$diff_ids)->delete(); if ($row) { return $this->success([]); } return $this->error(); } public function deleteStoreRewardInfo(Request $request) { $input = $request->all(); $row = StoreRewardSet::where('id', $input['id'])->delete(); if ($row) { return $this->success([]); } return $this->error(); } public function getStoreRewardList(Request $request) { $input = $request->all(); $page_index = $input['page_index']; $page_size = $input['page_size']; $num = $page_size * ($page_index - 1); $month = $input['month']; $search_name = $input['search_name']; $type = $input['type'];//店铺级别 1工作室 3优享店 2体验店 //<<<<<<< HEAD // $where = []; // $where['store.type'] = $type; // $alr_store_ids=StoreRewardInfo::where('month',$month)->groupBy('store_id')->pluck('store_id'); // $count=Store::whereIn('id',$alr_store_ids) // ->where('type',$type) // ->where('name','like','%'.$search_name.'%') // ->count(); // $list=Store::whereIn('id',$alr_store_ids) // ->where('type',$type) // ->where('name','like','%'.$search_name.'%') //======= $alr_store_ids=StoreRewardInfo::where('month',$month)->groupBy('store_id')->pluck('store_id'); $count=Store::whereIn('id',$alr_store_ids)->where('name','like','%'.$search_name.'%')->where('type',$type)->count(); $list=Store::whereIn('id',$alr_store_ids) ->where('name','like','%'.$search_name.'%') ->where('type',$type) //>>>>>>> store_rewards ->skip($num)->take($page_size) ->orderBy('account', 'desc') ->get(); foreach($list as $key=>$val){ //<<<<<<< HEAD $list[$key]->rewards=StoreRewardInfo::where('month',$month)->where('store_id',$val->id) ->select('content','rewards','type','created_at','money') ->groupBy('store_id','month','type')->get(); //======= // $list[$key]->rewards=StoreRewardInfo::where('month',$month)->where('store_id',$val->id) // ->groupBy('type')->get(); //>>>>>>> store_rewards } return $this->success_list($list,'成功',$count); } public function getStoreNoRewardList1(Request $request) { $input = $request->all(); $page_index = $input['page_index']; $page_size = $input['page_size']; $num = $page_size * ($page_index - 1); $month = $input['month']; $search_name = $input['search_name']; $type = $input['type'];//店铺级别 1工作室 3优享店 2体验店 $where = []; $where['store.type'] = $type; $start_time = Carbon::parse($month)->startOfMonth()->toDateTimeString(); $end_time = Carbon::parse($month)->endOfMonth()->toDateTimeString(); $alr_store_ids=StoreRewardInfo::where('month',$month)->groupBy('store_id')->pluck('store_id'); $store_ids=Store::whereNotIn('id',$alr_store_ids)->pluck('id'); $data = Store::where($where)->whereIn('store.id',$store_ids) ->join('store_user as su', 'su.store_id', '=', 'store.id') ->join('users', 'users.id', '=', 'su.user_id') ->leftJoin('order_test as ot', function ($query) use ($start_time, $end_time) { $query->on('ot.user_id', '=', 'users.id') ->whereIn('ot.status', [3, 4]) ->whereBetween('ot.created_at', [$start_time, $end_time]); }) ->groupBy('store.id'); $count=$data->get()->count(); $list=$data->select('store.id', 'store.name', 'store.type','store.account as store_account','store.decorate_account', DB::raw('sum(ifNull(ot.money,0)) as account'), DB::raw('sum(ifNull(ot.total,0)) as total')) ->skip($num)->take($page_size) ->orderBy('account', 'desc') ->get(); foreach ($list as $key => $val) { $user_ids = StoreUser::where('store_id', $val->id)->pluck('user_id'); $list[$key]->goods = []; if ($user_ids) { $order_ids = Ordertest::whereIn('user_id', $user_ids) ->whereIn('status', [3, 4]) ->whereBetween('created_at', [$start_time, $end_time]) ->pluck('id'); $info = Orderdetail::join('goods_test as gt','gt.id','=','order_detail.goods_id') ->whereIn('order_detail.order_id', $order_ids)->groupBy('gt.main_attr') ->select('gt.main_attr','gt.unit', DB::raw('sum(order_detail.num) as total') ,DB::raw('sum(order_detail.money) as account'))->get(); $list[$key]->goods = $info; $rewardInfo=StoreRewardInfo::where('month',$month)->where('store_id',$val->id)->first(); $list[$key]->reward=$rewardInfo; } } return $this->success_list($list,'成功',$count); } public function getStoreNoRewardList(Request $request) { $input = $request->all(); $page_index = $input['page_index']; $page_size = $input['page_size']; $num = $page_size * ($page_index - 1); $month = $input['month']; $search_name = $input['search_name']; $type = $input['type'];//店铺级别 1工作室 3优享店 2体验店 $where = []; $where['store.type'] = $type; $start_time = Carbon::parse($month)->startOfMonth()->toDateTimeString(); $end_time = Carbon::parse($month)->endOfMonth()->toDateTimeString(); $alr_store_ids=StoreRewardInfo::where('month',$month)->groupBy('store_id')->pluck('store_id'); $count=Store::where('type',$type)->where('name','like','%'.$search_name.'%')->whereNotIn('id',$alr_store_ids)->count(); $list=Store::where('store.type',$type)->where('store.name','like','%'.$search_name.'%')->whereNotIn('store.id',$alr_store_ids) ->join('store_user as su','su.store_id','=','store.id') ->leftJoin('order_test as ot',function($query) use ($start_time, $end_time){ $query->on('ot.user_id','=','su.user_id') ->whereIn('ot.status',[3,4]) ->whereBetween('ot.created_at', [$start_time, $end_time]); }) ->select('store.name','store.id','store.account as store_account','store.decorate_account','store.type',DB::raw('sum(ifNull(ot.money,0)) as account'),DB::raw('sum(ifNull(ot.total,0)) as total')) ->groupBy('store.id') ->skip($num)->take($page_size) ->orderBy('account', 'desc') ->get(); foreach($list as $key=>$val){ $user_ids=StoreUser::where('store_id',$val->id)->pluck('user_id'); if($type==2){ $original_orders_ids=Ordertest::whereIn('status',[3,4])->where('user_id',$user_ids)->whereBetween('created_at', [$start_time, $end_time])->pluck('id')->toArray(); $manual_orders_ids=Ordertest::whereIn('status',[3,4])->where('store_id',$val->id) ->whereBetween('created_at', [$start_time, $end_time])->pluck('id')->toArray(); $order_ids=array_unique(array_merge($original_orders_ids,$manual_orders_ids)); }else{ $order_ids=Ordertest::whereIn('status',[3,4])->where('user_id',$user_ids)->whereBetween('created_at', [$start_time, $end_time])->pluck('id'); } $goods=Ordertest::join('order_detail as od','od.order_id','=','order_test.id') ->join('goods_test as gt','gt.id','=','od.goods_id')->whereIn('order_test.id',$order_ids) ->groupBy('gt.main_attr') ->select('gt.main_attr','gt.unit', DB::raw('sum(od.num) as total') ,DB::raw('sum(od.money) as account'))->get(); $list[$key]->goods=$goods; } return $this->success_list($list,'成功',$count); } public function settlement(Request $request) { $input = $request->all(); $store_id = $input['store_id']; $month = $input['month']; $info=$input['info']; $reward=StoreRewardInfo::where('month',$month)->where('store_id',$store_id)->get(); if(count($reward)>0){ return $this->error('450001','该门店当月已结算奖励'); } $start_time = Carbon::parse($month)->startOfMonth()->toDateTimeString(); $end_time = Carbon::parse($month)->endOfMonth()->toDateTimeString(); $set = StoreRewardSet::where('month', $month)->first(); $user_ids = StoreUser::where('store_id', $store_id)->pluck('user_id'); if (count($user_ids) <= 0) { return $this->error('门店无代理,不能结算'); } $account = Ordertest::whereIn('user_id', $user_ids) ->whereIn('status', [3, 4])->whereBetween('created_at', [$start_time, $end_time]) ->sum('money'); if ($account <= 0) { return $this->error('当月该门店总金额为0,不能结算'); } $store = Store::where('id',$store_id)->first(); DB::beginTransaction(); try { if ($store->type == 1) {//工作室 $goods = $input['goods']; $amount = 0; foreach ($goods as $key => $val) { $amount += $val['price'] * $val['number']; } if ($amount > $account * $set->account_rate) { return $this->error('450001','商品总价超出门店总金额,不能结算'); } foreach ($goods as $key => $val) { StoreRewardInfo::create([ 'month' => $month, 'store_id' => $store_id, 'type' => 1, 'rewards' => json_encode($goods, JSON_UNESCAPED_UNICODE), 'rewards_id' => $val['id'], 'content' => json_encode($info, JSON_UNESCAPED_UNICODE), 'num' => $val['number'], 'money' => null ]); } } elseif ($store->type == 2) {//体验店 $goods = $input['goods']; $amount = 0;//总数量 foreach ($goods as $key => $val) { $amount += $val['number'] * $val['price']; } Store::where('id',$store_id)->increment('account',$amount); StoreRewardInfo::create([ 'month' => $month, 'store_id' => $store_id, 'type' => 2, 'rewards_id' => null, 'num' => null, 'rewards' => json_encode($goods, JSON_UNESCAPED_UNICODE), 'content' => json_encode($info, JSON_UNESCAPED_UNICODE), 'money' => $amount, ]); } else {//优享店 $decorate = $store->decorate_account;//装修金额 if (empty($decorate)) { return $this->error('当前门店无装修金额,不能结算'); } $goods = $input['goods']; $amount = 0; foreach ($goods as $key => $val) { $amount += $val['price'] * $val['number']; } if ($amount > $account * $set->account_rate) { return $this->error('商品总价超出门店总金额,不能结算'); } foreach ($goods as $key => $val) { StoreRewardInfo::create([ 'month' => $month, 'store_id' => $store_id, 'type' => 1, 'rewards' => json_encode($goods, JSON_UNESCAPED_UNICODE), 'rewards_id' => $val['id'], 'content' => json_encode($info, JSON_UNESCAPED_UNICODE), 'num' => $val['number'], 'money' => null ]); } $goods_price = $input['goods_price']; $amount = 0;//总数量 foreach ($goods_price as $key => $val) { $amount += $val['number1'] * $val['price1'] + $val['number2'] * $val['price2']; } Store::where('id',$store_id)->increment('account',$amount); StoreRewardInfo::create([ 'month' => $month, 'store_id' => $store_id, 'type' => 2, 'rewards_id' => null, 'num' => null, 'rewards' => json_encode($goods_price, JSON_UNESCAPED_UNICODE), 'content' => json_encode($info, JSON_UNESCAPED_UNICODE), 'money' => $amount, ]); } DB::commit(); return $this->success([]); }catch(Exception $e){ DB::rollBack(); return $this->error($e->getMessage()); } } public function undoStoreRewardsInfo(Request $request){ $input=$request->all(); $month=$input['month']; $store_id=$input['store_id']; $next_month=date("Y-m",strtotime('+1 month',strtotime($month))); $curr_month=date("Y-m"); if($next_month!=$curr_month){ return $this->error('450001','当前月份兑换不能撤销'); } DB::beginTransaction(); try{ $amount=StoreRewardInfo::where('month',$month)->where('store_id',$store_id)->where('type',2)->value('money'); if($amount>0){ Store::where('id',$store_id)->decrement('money',$amount); } StoreRewardInfo::where('month',$month)->where('store_id',$store_id)->delete(); DB::commit(); return $this->success([]); }catch(Exception $e){ DB::rollBack(); return $this->error(); } } public function getStoreInfo(Request $request){ $input=$request->all(); $month=$input['month']; $info=StoreRewardSet::where('month',$month)->first(); $goods=StoreRewards::where('month',$month)->get(); if($info){ $info->goods=$goods; return $this->success($info); }else{ return $this->error('450001','当月未设置兑换信息'); } } //导入门店 public function import(Request $request){ if(!$request->hasFile('file')){ exit('上传文件为空!'); } $data=[]; $array = Excel::toArray(new ReportImport, request()->file('file')); DB::beginTransaction(); try{ foreach($array[0] as $key => $val){ if(!empty($val[0]) && $val[0]!='昵称'){ $user=User::where('mobile',$val[2])->select('id','nickname','warea_id')->first(); if(empty($user)){ return $this->error('450001',$val[0].'手机号不存在'); } if(!isset($user->warea_id) || empty($user->warea_id) ){ return $this->error('450001',$val[0].'没有战区'); } $data[$key]['name']=$val[0].$val[4]; $data[$key]['type']=1; $data[$key]['status']=1; $data[$key]['man_id']=$user->id; if($val[4]=='工作室'){ $type=1; }elseif($val[4]=='优享店'){ $type=3; }else{ $type=2; } $row=Store::create(['name'=>$val[0].$val[4],'type'=>$type,'status'=>1,'man_id'=>$user->id,'warea_id'=>$user->warea_id]); StoreUser::create(['user_id'=>$user->id,'user_mobile'=>$val[2],'store_id'=>$row->id]); } } DB::commit(); return $this->success([]); }catch(\Exception $e){ DB::rollback(); return $this->error(); } } }