input('admin_id'); $self_role = session('role_name'); $self_id = session('admin_id'); $saler_ids = null; if($self_role == '管理员' || $self_role == '分销管理员'){ $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id'); $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id'); } $phone = trim( $request->input('phone') ); $name = trim( $request->input('name') ); $page = (int)$request->input('page'); $pageSize = 200; if($page<=0){ $page = 1; } $offset = ($page-1) * $pageSize; $count = CustomerInfo::where(function($query) use($phone, $name, $admin_id, $saler_ids){ if($phone) $query->where('phone', $phone); if($name) $query->where('name', 'like', '%'. $name. '%'); if($admin_id) $query->where('admin_id', $admin_id); if( !empty($saler_ids) ) $query->whereIn('admin_id', $saler_ids); })->count(); if ($count > 1) { // 总页数 $pages = ceil($count/$pageSize); }else{ // 总页数 $pages = 1; } $result = CustomerInfo::where(function($query) use($phone, $name, $admin_id, $saler_ids){ if($phone) $query->where('phone', $phone); if($name) $query->where('name', 'like', '%'. $name. '%'); if($admin_id) $query->where('admin_id', $admin_id); if( !empty($saler_ids) ) $query->whereIn('admin_id', $saler_ids); })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get(); $result = json_decode(json_encode($result),true); foreach($result as $k=>&$v){ $v['phonei'] = substr($v['phone'],0,3).'****'.substr($v['phone'],7); } return view('deposit/customerList', ['result' =>$result, 'page' =>$page, 'count' =>$count, 'pages' =>$pages, 'phone' =>$phone, 'name' =>$name, ]); } /** * @return \Illuminate\View\View */ public function index(Request $request) { $phone = $request->input('phone'); $page = (int)$request->input('page'); $pageSize = 20; if($page<=0){ $page = 1; } $offset = ($page-1) * $pageSize; $count = CustomerDeposit::where(function($query) use($phone){ if($phone) $query->where('phone', $phone); })->where('is_del',0)->count(); if ($count > 1) { // 总页数 $pages = ceil($count/$pageSize); }else{ // 总页数 $pages = 1; } $result = CustomerDeposit::where(function($query) use($phone){ if($phone) $query->where('phone', $phone); })->where('is_del',0)->orderBy('pay_time', 'desc')->offset($offset)->limit($pageSize)->get(); $result = json_decode(json_encode($result),true); foreach($result as $k=>&$v){ $v['phone'] = substr($v['phone'],0,3).'****'.substr($v['phone'],7); if($v['payment_type'] == 1){ $v['payment_type'] = '微信支付'; }elseif($v['payment_type'] == 3){ $v['payment_type'] = '个体户支付'; }else{ $v['payment_type'] = ''; } } return view('deposit/index', ['result' =>$result, 'page' =>$page, 'count' =>$count, 'pages' =>$pages, 'phone' =>$phone ]); } /** * @return \Illuminate\View\View */ public function create(Request $request) { $phone = $request->input('phone'); //查询套餐 $packages = CustomerDepositPackages::where('is_del', 0)->orderBy('pay_amount', 'asc')->get(); $phonei = substr($phone,0,3).'****'.substr($phone,7); $self_role = session('role_name'); $team_id = null; if($self_role == '管理员' || $self_role == '分销管理员'){ $admin_id = session('admin_id'); $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id'); } $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id){ if($team_id>0) $query->where('team_id', $team_id); })->where('id','>', 1)->where('is_use',1)->get(); $adminList = json_decode(json_encode($adminList), true); return view('deposit/create', ['packages'=>$packages, 'phone'=>$phone, 'phonei'=>$phonei, 'adminlist'=>$adminList, 'self_role'=>$self_role]); } /** * @param Request $request * @return \Illuminate\Http\RedirectResponse */ public function store(Request $request) { $pay_money = $request->input('pay_money'); $pack_required = 'required'; if($pay_money>0){ $pack_required = ''; } $this->validate($request, [ 'phone' => 'required', 'package_id' => $pack_required, 'pay_time' => 'required', ], [ 'phone.required' => '手机号不能为空', 'package_id.required' => '必须选择套餐', 'pay_time.required' => '充值时间必须选择', ]); $admin_id = (int)$request->input('admin_id'); if(!$admin_id) $admin_id = session('admin_id'); $deposit = new CustomerDeposit(); $deposit->phone = trim($request->input('phone')); $deposit->admin_id = $admin_id; $deposit->pay_time = $request->input('pay_time'); $deposit->payment_type = $request->input('payment_type'); if($pay_money>0){ //自定义充值 $deposit->pay_amount = $pay_money; $deposit->discount = 0; $deposit->deposit_amount = $pay_money; $deposit->note = '自定义充值'; }else{ $package_id = (int)$request->input('package_id'); $package_info = CustomerDepositPackages::where('id', $package_id)->first(); $deposit->pay_amount = $package_info->pay_amount; $deposit->discount = $package_info->discount; $deposit->deposit_amount = $deposit->pay_amount + $deposit->discount; $deposit->note = $package_info->note; } if($deposit->save()){ //变更余额 $name = trim( $request->input('name') ); $customerInfo = CustomerInfo::where('phone', $deposit->phone)->first(); if(!empty($customerInfo)){ $customerInfo->balance = $customerInfo->balance + $deposit->deposit_amount; $customerInfo->deposit_total = $customerInfo->deposit_total + $deposit->deposit_amount; $customerInfo->deposit_times = $customerInfo->deposit_times + 1; $customerInfo->save(); }else{ $customerInfo = new CustomerInfo(); $customerInfo->phone = $deposit->phone; $customerInfo->deposit_times = 1; $customerInfo->balance = $deposit->deposit_amount; $customerInfo->deposit_total = $deposit->deposit_amount; if(!empty($name)) $customerInfo->name = $name; $customerInfo->admin_id = $admin_id; $customerInfo->save(); } } return redirect('/admin/deposit/index?phone='.$deposit->phone)->with('info', '添加成功'); } /** * @param $id * @return \Illuminate\View\View */ public function edit($id) { $deposit = CustomerDeposit::findOrFail($id); return view('deposit/edit', ['deposit' => $deposit, 'id'=>$id]); } /** * @param Request $request * @return \Illuminate\Http\RedirectResponse */ public function update(Request $request) { $id = (int)$request->input('id'); $this->validate($request, [ 'phone' => 'required', 'pay_amount' => 'required', 'discount' => 'required', ], [ 'phone.required' => '手机号不能为空', 'pay_amount.required' => '参数有误', 'discount.required' => '参数有误', ]); $template = Templates::findOrFail($id); $template->url = trim($request->input('url')); $template->note = trim($request->input('note')); //图片上传 阿里云oss if ($request->hasFile('img') && $request->file('img')->isValid()) { $file = $request->file('img'); $ossClient=new oss(); // 上传阿里云 $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His')); $img=$file['oss-request-url']; $template->img=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img); } $template->save(); return redirect('/admin/template/index')->with('info', '修改模板成功'); } //获余额 public function getBalance($phone){ $result = CustomerInfo::where('phone', $phone)->pluck('balance'); if(empty($result)){ // exit('0'); echo '0';die; } // exit($result); echo (string)$result;die; } /** * @return \Illuminate\View\View */ public function consumList(Request $request) { $phone = $request->input('phone'); $page = (int)$request->input('page'); $pageSize = 20; if($page<=0){ $page = 1; } $offset = ($page-1) * $pageSize; $count = CustomerConsum::where(function($query) use($phone){ if($phone) $query->where('phone', $phone); })->where('is_del',0)->count(); if ($count > 1) { // 总页数 $pages = ceil($count/$pageSize); }else{ // 总页数 $pages = 1; } $result = CustomerConsum::where(function($query) use($phone){ if($phone) $query->where('phone', $phone); })->where('is_del',0)->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get(); $result = json_decode(json_encode($result),true); foreach($result as $k=>&$v){ $v['phone'] = substr($v['phone'],0,3).'****'.substr($v['phone'],7); } return view('deposit/consumList', ['result' =>$result, 'page' =>$page, 'count' =>$count, 'pages' =>$pages, 'phone' =>$phone ]); } /** * 编辑充值方式 */ public function editPayType(Request $request){ $id = (int)$request->input('id'); $payment_type = $request->input('payment_type'); $data = array(); if(isset($payment_type)){ if(empty($payment_type)){ $payment_type = null; } $data['payment_type'] = (int)$payment_type; } if($id>0 && !empty($data)){ $res = CustomerDeposit::where('id', $id)->update($data); } return 1; } /** * vip用户列表 * @param Request $request */ public function vipCustomerList(Request $request) { $page = (int)$request->input('page'); $name = $request->input('name'); $phone = $request->input('phone'); $admin_id = (int)$request->input('admin_id'); $team_id = (int)$request->input('team_id'); $self_role = session('role_name'); if($self_role == '管理员'){ //只能看自己团队的 $self_id = session('admin_id'); $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id'); } //假如有团队筛选,检索销售队员 $saler_ids = null; if($team_id>0 && !$admin_id){ $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id'); } $pageSize = 20; if($page<=0){ $page = 1; } $offset = ($page-1) * $pageSize; $count = CustomerVip::where(function($query) use ($name,$phone,$admin_id,$saler_ids) { if($phone) $query->where('phone', $phone); if($name) $query->where('name', 'like', '%'. $name. '%'); if($admin_id) $query->where('admin_id', $admin_id); if( !empty($saler_ids) ) $query->whereIn('admin_id', $saler_ids); })->where('is_del',0)->count(); if ($count > 1) { // 总页数 $pages = ceil($count/$pageSize); }else{ // 总页数 $pages = 1; } $result = CustomerVip::where(function($query) use($phone, $name, $admin_id, $saler_ids){ if($phone) $query->where('phone', $phone); if($name) $query->where('name', 'like', '%'. $name. '%'); if($admin_id) $query->where('admin_id', $admin_id); if( !empty($saler_ids) ) $query->whereIn('admin_id', $saler_ids); })->where('is_del',0)->orderBy('id','desc')->offset($offset)->limit($pageSize)->get(); $result = json_decode(json_encode($result),true); $result = array_map(function($value){ $item = $value; $currentDate = date('Y-m-d',time()); //生日套餐 $birthdayGift = CustomerGiftReceives::getCustomerReceivesLog($value['phone'],1); if($birthdayGift){ $item['birthday_gift'] = $birthdayGift[0]['dtime'].' 领取'; } else { $item['birthday_gift'] = '未使用'; } //每月礼包 $num = CustomerGiftReceives::getCustomerResidualTimes($value['phone'],$value['vip_end_time']); $item['month_gift'] = '剩余'.$num.'次'; //代金券 $coupons = CustomerCoupons::where('phone',$value['phone'])->where('is_use',0)->where('end_time','>=',$currentDate)->count(); $item['coupon'] = '剩余'.$coupons.'张'; $admin = Admin::find($value['admin_id']); //销售人员 $item['admin_name'] = $admin->realname; if($item['payment_type'] == 1){ $item['payment'] = '微信支付'; } else if($item['payment_type'] == 3){ $item['payment'] = '个体户支付'; } else { $item['payment'] = ''; } return $item; },$result); $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){ if($self_role == '管理员') $query->where('id', $team_id); })->where('type', 1)->get(); $teamList = json_decode(json_encode($teamList), true); $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){ if($self_role == '管理员') $query->where('team_id', $team_id); })->where('id','>', 1)->where('is_use',1)->get(); $adminList = json_decode(json_encode($adminList), true); return view('customer/vipCustomerList', [ 'result' =>$result, 'page' =>$page, 'count' =>$count, 'pages' =>$pages, 'phone' =>$phone, 'name' =>$name, 'teamlist' =>$teamList, 'adminlist' =>$adminList, 'team_id' =>$team_id, 'admin_id' =>$admin_id, 'self_role' =>$self_role, ]); } /** * 添加vip用户页面 * @param Request $request */ public function addVipCustomer(Request $request) { $self_role = session('role_name'); $team_id = null; if($self_role == '管理员'){ //只能看自己团队的 $self_id = session('admin_id'); $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id'); } $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){ if($self_role == '管理员') $query->where('team_id', $team_id); })->where('id','>', 1)->where('is_use',1)->get(); $adminList = json_decode(json_encode($adminList), true); return view('customer/vipCustomerCreate',[ 'adminlist' => $adminList, ]); } /** * 添加vip用户信息 * @param Request $request */ public function storeVipCustomer(Request $request) { $this->validate($request, [ 'phone' => 'required|unique:customer_vip,phone', 'birthday' => 'required', 'admin_id' => 'required', ], [ 'phone.required' => '手机号不能为空', 'phone.unique' => '该用户已经添加过', 'birthday.required' => '必须填写用户生日', 'admin_id.required' => '销售必选', ]); //添加vip用户 $customerVip = new CustomerVip(); $customerVip->phone = trim($request->input('phone')); $customerVip->name = trim($request->input('name')); $customerVip->admin_id = $request->input('admin_id'); $customerVip->level = 1; $customerVip->discount = 95; $customerVip->create_time = date('Y-m-d H:i:s',time()); $customerVip->vip_end_time = date('Y-m-d H:i:s',strtotime('+1 year')); $customerVip->birthday = date('Y',time()).'-'.$request->input('birthday'); $customerVip->price = $request->input('price'); $customerVip->payment_type = $request->input('payment_type',1); $customerVip->is_del = 0; $customerVip->pay_date = date('Y-m-d',time()); $admin = Admin::find($request->input('admin_id')); $customerVip->team_id = $admin['team_id']; if($customerVip->save()) { //添加优惠券 for($i=1;$i<=6;$i++){ $customerCoupons = new CustomerCoupons(); $customerCoupons->phone = trim($request->input('phone')); $customerCoupons->coupon_price = 30; $customerCoupons->create_time = date('Y-m-d H:i:s',time()); $customerCoupons->end_time = date('Y-m-d',strtotime('+3 months')); $customerCoupons->is_use = 0; $customerCoupons->save(); } } return redirect('/admin/customer/vipList')->with('info', '添加成功'); } /** * 编辑vip用户页面 * @param Request $request */ public function editVipCustomer(Request $request) { $id = $request->input('id'); $self_role = session('role_name'); $team_id = null; if($self_role == '管理员'){ //只能看自己团队的 $self_id = session('admin_id'); $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id'); } $customer = CustomerVip::findOrFail($id); $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){ if($self_role == '管理员') $query->where('team_id', $team_id); })->where('id','>', 1)->where('is_use',1)->get(); $adminList = json_decode(json_encode($adminList), true); return view('customer/vipCustomerEdit',[ 'info'=>$customer, 'adminlist' => $adminList, ]); } /** * 编辑vip用户信息 * @param Request $request */ public function updateVipCustomer(Request $request) { $id = $request->input('id'); //修改vip用户信息 $customerVip = CustomerVip::findOrFail($id); $customerVip->name = $request->input('name'); $customerVip->birthday = date('Y',time()).'-'.$request->input('birthday'); $customerVip->admin_id = $request->input('admin_id'); $customerVip->price = $request->input('price'); $customerVip->payment_type = $request->input('payment_type',1); if($customerVip->save()){ return redirect('/admin/customer/vipList')->with('info', '编辑成功'); } else { return redirect('/admin/customer/vipUpdate?id='.$id)->with('info', '编辑失败'); } } /** * 启用vip用户 * @param Request $request */ public function vipUp(Request $request) { $id = $request->input('id'); $res = CustomerVip::where('id',$id)->update(['is_del'=>0]); if($res) { // exit('1'); echo '1';die; } // exit('0'); echo '0';die; } /** * 禁用vip用户 * @param Request $request */ public function vipDel(Request $request) { $id = $request->input('id'); $res = CustomerVip::where('id',$id)->update(['is_del'=>1]); if($res) { // exit('1'); echo '1';die; } // exit('0'); echo '0';die; } /** * 会员用户礼品列表 * @param Request $request */ public function monthGiftList(Request $request) { $page = $request->input('page',1); $type = $request->input('type','-1'); $pageSize = 20; if($page<=0){ $page = 1; } $offset = ($page-1) * $pageSize; $count = CustomerMonthGift::where(function($query) use ($type) { if($type != '-1') $query->where('type',$type); })->count(); if ($count > 1) { // 总页数 $pages = ceil($count/$pageSize); }else{ // 总页数 $pages = 1; } $result = CustomerMonthGift::where(function($query) use ($type) { if($type != '-1') $query->where('type',$type); })->orderBy('id','desc')->offset($offset)->limit($pageSize)->get(); $result = json_decode(json_encode($result),true); $result = array_map(function($value){ $item = $value; $sku = GoodsSkus::select('propsName','price','is_white','quantity','is_weigh')->where('id', $value['sku_id'])->where('is_del', 0)->first(); $goods = Goods::select('name','picUrl','goodsCategoryName')->where('is_del', 0)->where('id', $value['goods_id'])->first(); $item['sku'] = $sku; $item['goods'] = $goods; return $item; },$result); return view('customer/customerGiftList',[ 'page' => $page, 'pages' => $pages, 'count' => $count, 'result'=> $result, 'type' => $type, ]); } /** * 添加礼品页面 */ public function addMonthGift() { return view('customer/customerGiftAdd'); } /** * 编辑礼品页面 * @param Request $request */ public function editMonthGift(Request $request) { $id = $request->input('id'); $gift = CustomerMonthGift::findOrFail($id); $goods = Goods::select('gs.id as sku_id', 'goods.id as goods_id', 'goods.name', 'gs.propsName as props_name', 'gs.price', 'gs.is_weigh')->leftJoin('goods_skus as gs', 'gs.goodsCode', '=', 'goods.id')->where('gs.id', $gift->sku_id)->orderBy('goods.id', 'desc')->first(); return view('customer/customerGiftEdit',[ 'info' => $gift, 'good' => $goods, ]); } /** * 添加礼品信息 * @param Request $request */ public function monthGiftStore(Request $request) { #数据校验 $this->validate( $request ,[ 'mtime' => 'required', ],[ 'mtime.required' => '日期必填', ] ); #数据处理 $type = $request->input('type'); $mtime = date('Y-m-01',strtotime($request->input('mtime'))); $skus = $request->input('skus'); $sku_id = $skus[0]; $gnum = $request->input('gnum'); $num = $gnum[0]; //根据规格id反向查询商品goods_id $sku = GoodsSkus::find($sku_id); $goods_id = $sku['goodsCode']; #数据存储 $gift = new CustomerMonthGift(); $gift->mtime = $mtime; $gift->goods_id = $goods_id; $gift->sku_id = $sku_id; $gift->type = $type; $gift->num = $num; $gift->create_time = date('Y-m-d H:i:s',time()); $res = $gift->save(); #结果返回 if($res){ return redirect('/admin/customer/giftList')->with('info','添加成功'); } else { return back()->with('info','添加失败'); } } /** * 编辑礼品信息 * @param Request $request */ public function monthGiftUpdate(Request $request) { #数据校验 $this->validate( $request ,[ 'mtime' => 'required', ],[ 'mtime.required' => '日期必填', ] ); #数据处理 $type = $request->input('type'); $id = $request->input('id'); $mtime = date('Y-m-01',strtotime($request->input('mtime'))); $skus = $request->input('skus'); $sku_id = $skus[0]; $gnum = $request->input('gnum'); $num = $gnum[0]; //根据规格id反向查询商品goods_id $sku = GoodsSkus::find($sku_id); $goods_id = $sku['goodsCode']; #数据存储 $gift = CustomerMonthGift::findOrFail($id); $gift->mtime = $mtime; $gift->goods_id = $goods_id; $gift->sku_id = $sku_id; $gift->type = $type; $gift->num = $num; $res = $gift->save(); #结果返回 if($res){ return redirect('/admin/customer/giftList')->with('info','修改成功'); } else { return back()->with('info','修改失败'); } } /** * 查询会员每月礼品领取记录 * @param Request $request */ public function monthGiftLog(Request $request) { $phone = $request->input('phone'); #查询会员领取情况以及基本信息 $customerInformation = CustomerVip::getCustomerInformation($phone); $data = array(); #生成12个月数组 $startDate = $customerInformation->create_time; $currentTime = strtotime($startDate); $cyear = floor(date("Y",$currentTime)); $cMonth = floor(date("m",$currentTime)); for($i=0;$i<12;$i++){ $nMonth = $cMonth+$i; $cyear = $nMonth == 13 ? ($cyear+1) : $cyear; $nMonth = $nMonth > 12 ? $nMonth-12 : $nMonth; $date = $cyear."-".$nMonth."-1"; $firstday = date('Y-m-01', strtotime($date)); $lastday = date('Y-m-t', strtotime($date)); $data[$i]['start_time'] = $firstday; $data[$i]['end_time'] = $lastday; } $list = array(); #将数据对应填写 foreach ($data as $value) { $monthGiftLog = CustomerGiftReceives::where('phone',$phone)->where('gift_type',0) ->where('dtime','>=',$value['start_time'])->where('dtime','<=',$value['end_time'])->first(); if($monthGiftLog){ $value['order_id'] = $monthGiftLog['order_id']; $value['dtime'] = $monthGiftLog['dtime']; $value['status'] = '已使用'; $giftInfo = CustomerMonthGift::find($monthGiftLog['gift_id']); $goodsId = $giftInfo->goods_id; $goodsInfo = Goods::find($goodsId); $value['goods_id'] = $goodsId; $value['goods_name'] = $goodsInfo->name; } else if($value['end_time'] < date('Y-m-d')){ $value['status'] = '已过期'; } else { $value['status'] = '未使用'; } $list[] = $value; } return view('customer/customermonthgiftlist',['data'=>$list]); } /** * 查询会员代金券领取记录 * @param Request $request * @return \Illuminate\View\View */ public function couponLog(Request $request) { $phone = $request->input('phone'); $couponList = CustomerCoupons::getCustomerCouponsLog($phone); $couponList = array_map(function($value){ $item = $value; if($value['is_use'] == 1){ $item['status'] = '已使用'; } else if(time() <= strtotime($value['end_time'])){ $item['status'] = '未使用'; $item['use_time'] = ''; } else { $item['status'] = '已过期'; $item['use_time'] = ''; } return $item; },$couponList); return view('customer/customercouponlist',[ 'data'=>$couponList ]); } /** * 查询会员生日套餐领取记录 * @param Request $request * @return \Illuminate\View\View */ public function birthdayGiftLog(Request $request) { $phone = $request->input('phone'); $log = CustomerGiftReceives::getCustomerReceivesLog($phone,1); if(count($log) < 1){ $data = array(); } else { #找到对应的商品列表 //1.找到对应的订单 $orderId = $log[0]['order_id']; //2.获取所有的生日套餐的gift_id $giftList = CustomerMonthGift::where('type',1)->pluck('id'); //3.从订单商品表中查找到该订单中gift_id是生日套餐的商品id if(is_array($giftList)){ $orderGoodsSkus = OrderGoodsSkus::where('order_id',$orderId)->whereIn('gift_id',$giftList)->get(); } else { $orderGoodsSkus = OrderGoodsSkus::where('order_id',$orderId)->where('gift_id',$giftList)->get(); } foreach($orderGoodsSkus as $k=>$value) { $goods = Goods::find($value['goods_id']); $data[$k]['order_id'] = $orderId; $data[$k]['goods_id'] = $goods['id']; $data[$k]['goods_name'] = $goods['name']; $data[$k]['dtime'] = $log[0]['dtime']; } $data = json_decode(json_encode($data),true); } return view('customer/customerbirthdaygiftlist',['data'=>$data]); } /** * 会员用户录入报表导出 * @param Request $request * @return bool */ public function vipExport(Request $request) { $name = $request->input('name'); $phone = $request->input('phone'); $admin_id = (int)$request->input('admin_id'); $team_id = (int)$request->input('team_id'); $self_role = session('role_name'); if($self_role == '管理员'){ //只能看自己团队的 $self_id = session('admin_id'); $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id'); } //假如有团队筛选,检索销售队员 $saler_ids = null; if($team_id>0 && !$admin_id){ $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id'); } $result = CustomerVip::where(function($query) use($phone, $name, $admin_id, $saler_ids){ if($phone) $query->where('phone', $phone); if($name) $query->where('name', 'like', '%'. $name. '%'); if($admin_id) $query->where('admin_id', $admin_id); if( !empty($saler_ids) ) $query->whereIn('admin_id', $saler_ids); })->where('is_del',0)->orderBy('id','desc')->get(); $result = json_decode(json_encode($result),true); $result = array_map(function($value){ $item = $value; $currentDate = date('Y-m-d',time()); //生日套餐 $birthdayGift = CustomerGiftReceives::getCustomerReceivesLog($value['phone'],1); if($birthdayGift){ $item['birthday_gift'] = $birthdayGift[0]['dtime'].' 领取'; } else { $item['birthday_gift'] = '未使用'; } //每月礼包 $num = CustomerGiftReceives::getCustomerResidualTimes($value['phone'],$value['vip_end_time']); $item['month_gift'] = '剩余'.$num.'次'; //代金券 $coupons = CustomerCoupons::where('phone',$value['phone'])->where('is_use',0)->where('end_time','>=',$currentDate)->count(); $item['coupon'] = '剩余'.$coupons.'张'; $admin = Admin::find($value['admin_id']); //销售人员 $item['admin_name'] = $admin->realname; if($item['payment_type'] == 1){ $item['payment'] = '微信支付'; } else if($item['payment_type'] == 3){ $item['payment'] = '个体户支付'; } else { $item['payment'] = ''; } $item['phone'] = is_numeric($item['phone']) ? substr_replace($item['phone'], '****', 3, 4) : $item['phone']; $item['birthday'] = date('m-d',strtotime($item['birthday'])); return $item; },$result); $indexKey = ['name','phone','vip_end_time','birthday','admin_name','price','payment','coupon','birthday_gift','month_gift']; $title = ['姓名', '手机号', '到期时间', '生日', '销售', '金额', '支付方式', '代金券', '生日套餐', '每月礼品']; $filename = 'customer_'.date('Y-m-d_H').'.xlsx'; return Order::export_excel($result, $filename, $indexKey, $title); } /** * 会员报表 * @param Request $request * @return \Illuminate\View\View */ public function membershipReport(Request $request) { $date = $request->input('date'); $team_id = $request->input('team_id'); $page = $request->input('page',1); $pageSize = 20; if($page<=0){ $page = 1; } $offset = ($page-1) * $pageSize; $count = CustomerVip::select(DB::raw('sum(`price`) as total_price,count(1) as number'),'team_id','pay_date','payment_type') ->where(function($query) use($date,$team_id){ if($date) $query->where('pay_date',$date); if($team_id != 0) $query->where('team_id',$team_id); })->where('is_del',0)->groupBy('pay_date','team_id','payment_type')->get(); $count = count($count); if ($count > 1) { // 总页数 $pages = ceil($count/$pageSize); }else{ // 总页数 $pages = 1; } $list = CustomerVip::select(DB::raw('sum(`price`) as total_price,count(1) as number'),'team_id','pay_date','payment_type') ->where(function($query) use($date,$team_id){ if($date) $query->where('pay_date',$date); if($team_id) $query->where('team_id',$team_id); })->where('is_del',0)->groupBy('pay_date','team_id','payment_type')->orderBy('pay_date','desc')->offset($offset)->limit($pageSize)->get(); $result = json_decode(json_encode($list),true); $result = array_map(function($value){ $item = $value; $team = DB::table('teams')->where('id',$item['team_id'])->first(); $item['team_name'] = $team->name; if($item['payment_type'] == 1){ $item['payment'] = '微信支付'; } else if($item['payment_type'] == 3){ $item['payment'] = '个体户支付'; } else { $item['payment'] = ''; } return $item; },$result); $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id){ })->where('type', 1)->get(); $teamList = json_decode(json_encode($teamList), true); return view('customer/vipCustomerReport',[ 'page' => $page, 'pages' => $pages, 'result' => $result, 'date' => $date, 'team_id' => $team_id, 'teamlist' => $teamList, 'count' => $count ]); } /** * 会员报表导出 * @param Request $request * @return bool */ public function membershipReportExport(Request $request){ $date = $request->input('date'); $team_id = $request->input('team_id'); $list = CustomerVip::select(DB::raw('sum(`price`) as total_price,count(1) as number'),'team_id','pay_date','payment_type') ->where(function($query) use($date,$team_id){ if($date) $query->where('pay_date',$date); if($team_id) $query->where('team_id',$team_id); })->where('is_del',0)->groupBy('pay_date','team_id','payment_type')->orderBy('pay_date','desc')->get(); $list = json_decode(json_encode($list),true); $result = array_map(function($value){ $item = $value; $team = DB::table('teams')->where('id',$item['team_id'])->first(); $item['team_name'] = $team->name; if($item['payment_type'] == 1){ $item['payment'] = '微信支付'; } else if($item['payment_type'] == 3){ $item['payment'] = '个体户支付'; } else { $item['payment'] = ''; } return $item; },$list); $indexKey = ['pay_date','team_name','number','total_price','payment']; $title = ['日期', '团队', '新增会员数', '充值金额','充值方式']; $filename = 'customer_export_'.date('Y-m-d_H').'.xlsx'; return Order::export_excel($result, $filename, $indexKey, $title); } }