12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055 |
- <?php
- /**
- * Created by Sublime.
- * User: Williamslife Wang
- * Date: 17/10/18
- * Time: 上午11:20
- */
- namespace App\Http\Controllers\Admin;
- use App\CustomerCoupons;
- use App\CustomerGiftReceives;
- use App\CustomerMonthGift;
- use App\CustomerVip;
- use App\Goods;
- use App\GoodsSkus;
- use App\Http\Controllers\Controller;
- use App\Admin;
- use App\Order;
- use App\OrderGoodsSkus;
- use function GuzzleHttp\Psr7\str;
- use Illuminate\Http\Request;
- use App\CustomerInfo;
- use App\CustomerDeposit;
- use App\CustomerConsum;
- use App\CustomerDepositPackages;
- use Illuminate\Support\Facades\DB;
- class CustomerDepositController extends Controller
- {
- /**
- * @return \Illuminate\View\View
- */
- public function customerList(Request $request)
- {
- $admin_id = (int)$request->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);
- }
- }
|