No Description

CustomerDepositController.php 38KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. <?php
  2. /**
  3. * Created by Sublime.
  4. * User: Williamslife Wang
  5. * Date: 17/10/18
  6. * Time: 上午11:20
  7. */
  8. namespace App\Http\Controllers\Admin;
  9. use App\CustomerCoupons;
  10. use App\CustomerGiftReceives;
  11. use App\CustomerMonthGift;
  12. use App\CustomerVip;
  13. use App\Goods;
  14. use App\GoodsSkus;
  15. use App\Http\Controllers\Controller;
  16. use App\Admin;
  17. use App\Order;
  18. use App\OrderGoodsSkus;
  19. use function GuzzleHttp\Psr7\str;
  20. use Illuminate\Http\Request;
  21. use App\CustomerInfo;
  22. use App\CustomerDeposit;
  23. use App\CustomerConsum;
  24. use App\CustomerDepositPackages;
  25. use Illuminate\Support\Facades\DB;
  26. class CustomerDepositController extends Controller
  27. {
  28. /**
  29. * @return \Illuminate\View\View
  30. */
  31. public function customerList(Request $request)
  32. {
  33. $admin_id = (int)$request->input('admin_id');
  34. $self_role = session('role_name');
  35. $self_id = session('admin_id');
  36. $saler_ids = null;
  37. if($self_role == '管理员' || $self_role == '分销管理员'){
  38. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  39. $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  40. }
  41. $phone = trim( $request->input('phone') );
  42. $name = trim( $request->input('name') );
  43. $page = (int)$request->input('page');
  44. $pageSize = 200;
  45. if($page<=0){
  46. $page = 1;
  47. }
  48. $offset = ($page-1) * $pageSize;
  49. $count = CustomerInfo::where(function($query) use($phone, $name, $admin_id, $saler_ids){
  50. if($phone) $query->where('phone', $phone);
  51. if($name) $query->where('name', 'like', '%'. $name. '%');
  52. if($admin_id) $query->where('admin_id', $admin_id);
  53. if( !empty($saler_ids) ) $query->whereIn('admin_id', $saler_ids);
  54. })->count();
  55. if ($count > 1) {
  56. // 总页数
  57. $pages = ceil($count/$pageSize);
  58. }else{
  59. // 总页数
  60. $pages = 1;
  61. }
  62. $result = CustomerInfo::where(function($query) use($phone, $name, $admin_id, $saler_ids){
  63. if($phone) $query->where('phone', $phone);
  64. if($name) $query->where('name', 'like', '%'. $name. '%');
  65. if($admin_id) $query->where('admin_id', $admin_id);
  66. if( !empty($saler_ids) ) $query->whereIn('admin_id', $saler_ids);
  67. })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  68. $result = json_decode(json_encode($result),true);
  69. foreach($result as $k=>&$v){
  70. $v['phonei'] = substr($v['phone'],0,3).'****'.substr($v['phone'],7);
  71. }
  72. return view('deposit/customerList', ['result' =>$result,
  73. 'page' =>$page,
  74. 'count' =>$count,
  75. 'pages' =>$pages,
  76. 'phone' =>$phone,
  77. 'name' =>$name,
  78. ]);
  79. }
  80. /**
  81. * @return \Illuminate\View\View
  82. */
  83. public function index(Request $request)
  84. {
  85. $phone = $request->input('phone');
  86. $page = (int)$request->input('page');
  87. $pageSize = 20;
  88. if($page<=0){
  89. $page = 1;
  90. }
  91. $offset = ($page-1) * $pageSize;
  92. $count = CustomerDeposit::where(function($query) use($phone){
  93. if($phone) $query->where('phone', $phone);
  94. })->where('is_del',0)->count();
  95. if ($count > 1) {
  96. // 总页数
  97. $pages = ceil($count/$pageSize);
  98. }else{
  99. // 总页数
  100. $pages = 1;
  101. }
  102. $result = CustomerDeposit::where(function($query) use($phone){
  103. if($phone) $query->where('phone', $phone);
  104. })->where('is_del',0)->orderBy('pay_time', 'desc')->offset($offset)->limit($pageSize)->get();
  105. $result = json_decode(json_encode($result),true);
  106. foreach($result as $k=>&$v){
  107. $v['phone'] = substr($v['phone'],0,3).'****'.substr($v['phone'],7);
  108. if($v['payment_type'] == 1){
  109. $v['payment_type'] = '微信支付';
  110. }elseif($v['payment_type'] == 3){
  111. $v['payment_type'] = '个体户支付';
  112. }else{
  113. $v['payment_type'] = '';
  114. }
  115. }
  116. return view('deposit/index', ['result' =>$result,
  117. 'page' =>$page,
  118. 'count' =>$count,
  119. 'pages' =>$pages,
  120. 'phone' =>$phone
  121. ]);
  122. }
  123. /**
  124. * @return \Illuminate\View\View
  125. */
  126. public function create(Request $request)
  127. {
  128. $phone = $request->input('phone');
  129. //查询套餐
  130. $packages = CustomerDepositPackages::where('is_del', 0)->orderBy('pay_amount', 'asc')->get();
  131. $phonei = substr($phone,0,3).'****'.substr($phone,7);
  132. $self_role = session('role_name');
  133. $team_id = null;
  134. if($self_role == '管理员' || $self_role == '分销管理员'){
  135. $admin_id = session('admin_id');
  136. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  137. }
  138. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id){
  139. if($team_id>0) $query->where('team_id', $team_id);
  140. })->where('id','>', 1)->where('is_use',1)->get();
  141. $adminList = json_decode(json_encode($adminList), true);
  142. return view('deposit/create', ['packages'=>$packages, 'phone'=>$phone, 'phonei'=>$phonei, 'adminlist'=>$adminList, 'self_role'=>$self_role]);
  143. }
  144. /**
  145. * @param Request $request
  146. * @return \Illuminate\Http\RedirectResponse
  147. */
  148. public function store(Request $request)
  149. {
  150. $pay_money = $request->input('pay_money');
  151. $pack_required = 'required';
  152. if($pay_money>0){
  153. $pack_required = '';
  154. }
  155. $this->validate($request, [
  156. 'phone' => 'required',
  157. 'package_id' => $pack_required,
  158. 'pay_time' => 'required',
  159. ], [
  160. 'phone.required' => '手机号不能为空',
  161. 'package_id.required' => '必须选择套餐',
  162. 'pay_time.required' => '充值时间必须选择',
  163. ]);
  164. $admin_id = (int)$request->input('admin_id');
  165. if(!$admin_id) $admin_id = session('admin_id');
  166. $deposit = new CustomerDeposit();
  167. $deposit->phone = trim($request->input('phone'));
  168. $deposit->admin_id = $admin_id;
  169. $deposit->pay_time = $request->input('pay_time');
  170. $deposit->payment_type = $request->input('payment_type');
  171. if($pay_money>0){
  172. //自定义充值
  173. $deposit->pay_amount = $pay_money;
  174. $deposit->discount = 0;
  175. $deposit->deposit_amount = $pay_money;
  176. $deposit->note = '自定义充值';
  177. }else{
  178. $package_id = (int)$request->input('package_id');
  179. $package_info = CustomerDepositPackages::where('id', $package_id)->first();
  180. $deposit->pay_amount = $package_info->pay_amount;
  181. $deposit->discount = $package_info->discount;
  182. $deposit->deposit_amount = $deposit->pay_amount + $deposit->discount;
  183. $deposit->note = $package_info->note;
  184. }
  185. if($deposit->save()){
  186. //变更余额
  187. $name = trim( $request->input('name') );
  188. $customerInfo = CustomerInfo::where('phone', $deposit->phone)->first();
  189. if(!empty($customerInfo)){
  190. $customerInfo->balance = $customerInfo->balance + $deposit->deposit_amount;
  191. $customerInfo->deposit_total = $customerInfo->deposit_total + $deposit->deposit_amount;
  192. $customerInfo->deposit_times = $customerInfo->deposit_times + 1;
  193. $customerInfo->save();
  194. }else{
  195. $customerInfo = new CustomerInfo();
  196. $customerInfo->phone = $deposit->phone;
  197. $customerInfo->deposit_times = 1;
  198. $customerInfo->balance = $deposit->deposit_amount;
  199. $customerInfo->deposit_total = $deposit->deposit_amount;
  200. if(!empty($name)) $customerInfo->name = $name;
  201. $customerInfo->admin_id = $admin_id;
  202. $customerInfo->save();
  203. }
  204. }
  205. return redirect('/admin/deposit/index?phone='.$deposit->phone)->with('info', '添加成功');
  206. }
  207. /**
  208. * @param $id
  209. * @return \Illuminate\View\View
  210. */
  211. public function edit($id)
  212. {
  213. $deposit = CustomerDeposit::findOrFail($id);
  214. return view('deposit/edit', ['deposit' => $deposit, 'id'=>$id]);
  215. }
  216. /**
  217. * @param Request $request
  218. * @return \Illuminate\Http\RedirectResponse
  219. */
  220. public function update(Request $request)
  221. {
  222. $id = (int)$request->input('id');
  223. $this->validate($request, [
  224. 'phone' => 'required',
  225. 'pay_amount' => 'required',
  226. 'discount' => 'required',
  227. ], [
  228. 'phone.required' => '手机号不能为空',
  229. 'pay_amount.required' => '参数有误',
  230. 'discount.required' => '参数有误',
  231. ]);
  232. $template = Templates::findOrFail($id);
  233. $template->url = trim($request->input('url'));
  234. $template->note = trim($request->input('note'));
  235. //图片上传 阿里云oss
  236. if ($request->hasFile('img') && $request->file('img')->isValid()) {
  237. $file = $request->file('img');
  238. $ossClient=new oss();
  239. // 上传阿里云
  240. $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
  241. $img=$file['oss-request-url'];
  242. $template->img=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
  243. }
  244. $template->save();
  245. return redirect('/admin/template/index')->with('info', '修改模板成功');
  246. }
  247. //获余额
  248. public function getBalance($phone){
  249. $result = CustomerInfo::where('phone', $phone)->pluck('balance');
  250. if(empty($result)){
  251. // exit('0');
  252. echo '0';die;
  253. }
  254. // exit($result);
  255. echo (string)$result;die;
  256. }
  257. /**
  258. * @return \Illuminate\View\View
  259. */
  260. public function consumList(Request $request)
  261. {
  262. $phone = $request->input('phone');
  263. $page = (int)$request->input('page');
  264. $pageSize = 20;
  265. if($page<=0){
  266. $page = 1;
  267. }
  268. $offset = ($page-1) * $pageSize;
  269. $count = CustomerConsum::where(function($query) use($phone){
  270. if($phone) $query->where('phone', $phone);
  271. })->where('is_del',0)->count();
  272. if ($count > 1) {
  273. // 总页数
  274. $pages = ceil($count/$pageSize);
  275. }else{
  276. // 总页数
  277. $pages = 1;
  278. }
  279. $result = CustomerConsum::where(function($query) use($phone){
  280. if($phone) $query->where('phone', $phone);
  281. })->where('is_del',0)->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  282. $result = json_decode(json_encode($result),true);
  283. foreach($result as $k=>&$v){
  284. $v['phone'] = substr($v['phone'],0,3).'****'.substr($v['phone'],7);
  285. }
  286. return view('deposit/consumList', ['result' =>$result,
  287. 'page' =>$page,
  288. 'count' =>$count,
  289. 'pages' =>$pages,
  290. 'phone' =>$phone
  291. ]);
  292. }
  293. /**
  294. * 编辑充值方式
  295. */
  296. public function editPayType(Request $request){
  297. $id = (int)$request->input('id');
  298. $payment_type = $request->input('payment_type');
  299. $data = array();
  300. if(isset($payment_type)){
  301. if(empty($payment_type)){
  302. $payment_type = null;
  303. }
  304. $data['payment_type'] = (int)$payment_type;
  305. }
  306. if($id>0 && !empty($data)){
  307. $res = CustomerDeposit::where('id', $id)->update($data);
  308. }
  309. return 1;
  310. }
  311. /**
  312. * vip用户列表
  313. * @param Request $request
  314. */
  315. public function vipCustomerList(Request $request) {
  316. $page = (int)$request->input('page');
  317. $name = $request->input('name');
  318. $phone = $request->input('phone');
  319. $admin_id = (int)$request->input('admin_id');
  320. $team_id = (int)$request->input('team_id');
  321. $self_role = session('role_name');
  322. if($self_role == '管理员'){
  323. //只能看自己团队的
  324. $self_id = session('admin_id');
  325. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  326. }
  327. //假如有团队筛选,检索销售队员
  328. $saler_ids = null;
  329. if($team_id>0 && !$admin_id){
  330. $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  331. }
  332. $pageSize = 20;
  333. if($page<=0){
  334. $page = 1;
  335. }
  336. $offset = ($page-1) * $pageSize;
  337. $count = CustomerVip::where(function($query) use ($name,$phone,$admin_id,$saler_ids) {
  338. if($phone) $query->where('phone', $phone);
  339. if($name) $query->where('name', 'like', '%'. $name. '%');
  340. if($admin_id) $query->where('admin_id', $admin_id);
  341. if( !empty($saler_ids) ) $query->whereIn('admin_id', $saler_ids);
  342. })->where('is_del',0)->count();
  343. if ($count > 1) {
  344. // 总页数
  345. $pages = ceil($count/$pageSize);
  346. }else{
  347. // 总页数
  348. $pages = 1;
  349. }
  350. $result = CustomerVip::where(function($query) use($phone, $name, $admin_id, $saler_ids){
  351. if($phone) $query->where('phone', $phone);
  352. if($name) $query->where('name', 'like', '%'. $name. '%');
  353. if($admin_id) $query->where('admin_id', $admin_id);
  354. if( !empty($saler_ids) ) $query->whereIn('admin_id', $saler_ids);
  355. })->where('is_del',0)->orderBy('id','desc')->offset($offset)->limit($pageSize)->get();
  356. $result = json_decode(json_encode($result),true);
  357. $result = array_map(function($value){
  358. $item = $value;
  359. $currentDate = date('Y-m-d',time());
  360. //生日套餐
  361. $birthdayGift = CustomerGiftReceives::getCustomerReceivesLog($value['phone'],1);
  362. if($birthdayGift){
  363. $item['birthday_gift'] = $birthdayGift[0]['dtime'].' 领取';
  364. } else {
  365. $item['birthday_gift'] = '未使用';
  366. }
  367. //每月礼包
  368. $num = CustomerGiftReceives::getCustomerResidualTimes($value['phone'],$value['vip_end_time']);
  369. $item['month_gift'] = '剩余'.$num.'次';
  370. //代金券
  371. $coupons = CustomerCoupons::where('phone',$value['phone'])->where('is_use',0)->where('end_time','>=',$currentDate)->count();
  372. $item['coupon'] = '剩余'.$coupons.'张';
  373. $admin = Admin::find($value['admin_id']);
  374. //销售人员
  375. $item['admin_name'] = $admin->realname;
  376. if($item['payment_type'] == 1){
  377. $item['payment'] = '微信支付';
  378. } else if($item['payment_type'] == 3){
  379. $item['payment'] = '个体户支付';
  380. } else {
  381. $item['payment'] = '';
  382. }
  383. return $item;
  384. },$result);
  385. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
  386. if($self_role == '管理员') $query->where('id', $team_id);
  387. })->where('type', 1)->get();
  388. $teamList = json_decode(json_encode($teamList), true);
  389. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
  390. if($self_role == '管理员') $query->where('team_id', $team_id);
  391. })->where('id','>', 1)->where('is_use',1)->get();
  392. $adminList = json_decode(json_encode($adminList), true);
  393. return view('customer/vipCustomerList', [
  394. 'result' =>$result,
  395. 'page' =>$page,
  396. 'count' =>$count,
  397. 'pages' =>$pages,
  398. 'phone' =>$phone,
  399. 'name' =>$name,
  400. 'teamlist' =>$teamList,
  401. 'adminlist' =>$adminList,
  402. 'team_id' =>$team_id,
  403. 'admin_id' =>$admin_id,
  404. 'self_role' =>$self_role,
  405. ]);
  406. }
  407. /**
  408. * 添加vip用户页面
  409. * @param Request $request
  410. */
  411. public function addVipCustomer(Request $request) {
  412. $self_role = session('role_name');
  413. $team_id = null;
  414. if($self_role == '管理员'){
  415. //只能看自己团队的
  416. $self_id = session('admin_id');
  417. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  418. }
  419. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
  420. if($self_role == '管理员') $query->where('team_id', $team_id);
  421. })->where('id','>', 1)->where('is_use',1)->get();
  422. $adminList = json_decode(json_encode($adminList), true);
  423. return view('customer/vipCustomerCreate',[
  424. 'adminlist' => $adminList,
  425. ]);
  426. }
  427. /**
  428. * 添加vip用户信息
  429. * @param Request $request
  430. */
  431. public function storeVipCustomer(Request $request) {
  432. $this->validate($request, [
  433. 'phone' => 'required|unique:customer_vip,phone',
  434. 'birthday' => 'required',
  435. 'admin_id' => 'required',
  436. ], [
  437. 'phone.required' => '手机号不能为空',
  438. 'phone.unique' => '该用户已经添加过',
  439. 'birthday.required' => '必须填写用户生日',
  440. 'admin_id.required' => '销售必选',
  441. ]);
  442. //添加vip用户
  443. $customerVip = new CustomerVip();
  444. $customerVip->phone = trim($request->input('phone'));
  445. $customerVip->name = trim($request->input('name'));
  446. $customerVip->admin_id = $request->input('admin_id');
  447. $customerVip->level = 1;
  448. $customerVip->discount = 95;
  449. $customerVip->create_time = date('Y-m-d H:i:s',time());
  450. $customerVip->vip_end_time = date('Y-m-d H:i:s',strtotime('+1 year'));
  451. $customerVip->birthday = date('Y',time()).'-'.$request->input('birthday');
  452. $customerVip->price = $request->input('price');
  453. $customerVip->payment_type = $request->input('payment_type',1);
  454. $customerVip->is_del = 0;
  455. $customerVip->pay_date = date('Y-m-d',time());
  456. $admin = Admin::find($request->input('admin_id'));
  457. $customerVip->team_id = $admin['team_id'];
  458. if($customerVip->save()) {
  459. //添加优惠券
  460. for($i=1;$i<=6;$i++){
  461. $customerCoupons = new CustomerCoupons();
  462. $customerCoupons->phone = trim($request->input('phone'));
  463. $customerCoupons->coupon_price = 30;
  464. $customerCoupons->create_time = date('Y-m-d H:i:s',time());
  465. $customerCoupons->end_time = date('Y-m-d',strtotime('+3 months'));
  466. $customerCoupons->is_use = 0;
  467. $customerCoupons->save();
  468. }
  469. }
  470. return redirect('/admin/customer/vipList')->with('info', '添加成功');
  471. }
  472. /**
  473. * 编辑vip用户页面
  474. * @param Request $request
  475. */
  476. public function editVipCustomer(Request $request) {
  477. $id = $request->input('id');
  478. $self_role = session('role_name');
  479. $team_id = null;
  480. if($self_role == '管理员'){
  481. //只能看自己团队的
  482. $self_id = session('admin_id');
  483. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  484. }
  485. $customer = CustomerVip::findOrFail($id);
  486. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
  487. if($self_role == '管理员') $query->where('team_id', $team_id);
  488. })->where('id','>', 1)->where('is_use',1)->get();
  489. $adminList = json_decode(json_encode($adminList), true);
  490. return view('customer/vipCustomerEdit',[
  491. 'info'=>$customer,
  492. 'adminlist' => $adminList,
  493. ]);
  494. }
  495. /**
  496. * 编辑vip用户信息
  497. * @param Request $request
  498. */
  499. public function updateVipCustomer(Request $request) {
  500. $id = $request->input('id');
  501. //修改vip用户信息
  502. $customerVip = CustomerVip::findOrFail($id);
  503. $customerVip->name = $request->input('name');
  504. $customerVip->birthday = date('Y',time()).'-'.$request->input('birthday');
  505. $customerVip->admin_id = $request->input('admin_id');
  506. $customerVip->price = $request->input('price');
  507. $customerVip->payment_type = $request->input('payment_type',1);
  508. if($customerVip->save()){
  509. return redirect('/admin/customer/vipList')->with('info', '编辑成功');
  510. } else {
  511. return redirect('/admin/customer/vipUpdate?id='.$id)->with('info', '编辑失败');
  512. }
  513. }
  514. /**
  515. * 启用vip用户
  516. * @param Request $request
  517. */
  518. public function vipUp(Request $request) {
  519. $id = $request->input('id');
  520. $res = CustomerVip::where('id',$id)->update(['is_del'=>0]);
  521. if($res) {
  522. // exit('1');
  523. echo '1';die;
  524. }
  525. // exit('0');
  526. echo '0';die;
  527. }
  528. /**
  529. * 禁用vip用户
  530. * @param Request $request
  531. */
  532. public function vipDel(Request $request) {
  533. $id = $request->input('id');
  534. $res = CustomerVip::where('id',$id)->update(['is_del'=>1]);
  535. if($res) {
  536. // exit('1');
  537. echo '1';die;
  538. }
  539. // exit('0');
  540. echo '0';die;
  541. }
  542. /**
  543. * 会员用户礼品列表
  544. * @param Request $request
  545. */
  546. public function monthGiftList(Request $request) {
  547. $page = $request->input('page',1);
  548. $type = $request->input('type','-1');
  549. $pageSize = 20;
  550. if($page<=0){
  551. $page = 1;
  552. }
  553. $offset = ($page-1) * $pageSize;
  554. $count = CustomerMonthGift::where(function($query) use ($type) {
  555. if($type != '-1') $query->where('type',$type);
  556. })->count();
  557. if ($count > 1) {
  558. // 总页数
  559. $pages = ceil($count/$pageSize);
  560. }else{
  561. // 总页数
  562. $pages = 1;
  563. }
  564. $result = CustomerMonthGift::where(function($query) use ($type) {
  565. if($type != '-1') $query->where('type',$type);
  566. })->orderBy('id','desc')->offset($offset)->limit($pageSize)->get();
  567. $result = json_decode(json_encode($result),true);
  568. $result = array_map(function($value){
  569. $item = $value;
  570. $sku = GoodsSkus::select('propsName','price','is_white','quantity','is_weigh')->where('id', $value['sku_id'])->where('is_del', 0)->first();
  571. $goods = Goods::select('name','picUrl','goodsCategoryName')->where('is_del', 0)->where('id', $value['goods_id'])->first();
  572. $item['sku'] = $sku;
  573. $item['goods'] = $goods;
  574. return $item;
  575. },$result);
  576. return view('customer/customerGiftList',[
  577. 'page' => $page,
  578. 'pages' => $pages,
  579. 'count' => $count,
  580. 'result'=> $result,
  581. 'type' => $type,
  582. ]);
  583. }
  584. /**
  585. * 添加礼品页面
  586. */
  587. public function addMonthGift() {
  588. return view('customer/customerGiftAdd');
  589. }
  590. /**
  591. * 编辑礼品页面
  592. * @param Request $request
  593. */
  594. public function editMonthGift(Request $request) {
  595. $id = $request->input('id');
  596. $gift = CustomerMonthGift::findOrFail($id);
  597. $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();
  598. return view('customer/customerGiftEdit',[
  599. 'info' => $gift,
  600. 'good' => $goods,
  601. ]);
  602. }
  603. /**
  604. * 添加礼品信息
  605. * @param Request $request
  606. */
  607. public function monthGiftStore(Request $request) {
  608. #数据校验
  609. $this->validate( $request ,[
  610. 'mtime' => 'required',
  611. ],[
  612. 'mtime.required' => '日期必填',
  613. ]
  614. );
  615. #数据处理
  616. $type = $request->input('type');
  617. $mtime = date('Y-m-01',strtotime($request->input('mtime')));
  618. $skus = $request->input('skus');
  619. $sku_id = $skus[0];
  620. $gnum = $request->input('gnum');
  621. $num = $gnum[0];
  622. //根据规格id反向查询商品goods_id
  623. $sku = GoodsSkus::find($sku_id);
  624. $goods_id = $sku['goodsCode'];
  625. #数据存储
  626. $gift = new CustomerMonthGift();
  627. $gift->mtime = $mtime;
  628. $gift->goods_id = $goods_id;
  629. $gift->sku_id = $sku_id;
  630. $gift->type = $type;
  631. $gift->num = $num;
  632. $gift->create_time = date('Y-m-d H:i:s',time());
  633. $res = $gift->save();
  634. #结果返回
  635. if($res){
  636. return redirect('/admin/customer/giftList')->with('info','添加成功');
  637. } else {
  638. return back()->with('info','添加失败');
  639. }
  640. }
  641. /**
  642. * 编辑礼品信息
  643. * @param Request $request
  644. */
  645. public function monthGiftUpdate(Request $request) {
  646. #数据校验
  647. $this->validate( $request ,[
  648. 'mtime' => 'required',
  649. ],[
  650. 'mtime.required' => '日期必填',
  651. ]
  652. );
  653. #数据处理
  654. $type = $request->input('type');
  655. $id = $request->input('id');
  656. $mtime = date('Y-m-01',strtotime($request->input('mtime')));
  657. $skus = $request->input('skus');
  658. $sku_id = $skus[0];
  659. $gnum = $request->input('gnum');
  660. $num = $gnum[0];
  661. //根据规格id反向查询商品goods_id
  662. $sku = GoodsSkus::find($sku_id);
  663. $goods_id = $sku['goodsCode'];
  664. #数据存储
  665. $gift = CustomerMonthGift::findOrFail($id);
  666. $gift->mtime = $mtime;
  667. $gift->goods_id = $goods_id;
  668. $gift->sku_id = $sku_id;
  669. $gift->type = $type;
  670. $gift->num = $num;
  671. $res = $gift->save();
  672. #结果返回
  673. if($res){
  674. return redirect('/admin/customer/giftList')->with('info','修改成功');
  675. } else {
  676. return back()->with('info','修改失败');
  677. }
  678. }
  679. /**
  680. * 查询会员每月礼品领取记录
  681. * @param Request $request
  682. */
  683. public function monthGiftLog(Request $request) {
  684. $phone = $request->input('phone');
  685. #查询会员领取情况以及基本信息
  686. $customerInformation = CustomerVip::getCustomerInformation($phone);
  687. $data = array();
  688. #生成12个月数组
  689. $startDate = $customerInformation->create_time;
  690. $currentTime = strtotime($startDate);
  691. $cyear = floor(date("Y",$currentTime));
  692. $cMonth = floor(date("m",$currentTime));
  693. for($i=0;$i<12;$i++){
  694. $nMonth = $cMonth+$i;
  695. $cyear = $nMonth == 13 ? ($cyear+1) : $cyear;
  696. $nMonth = $nMonth > 12 ? $nMonth-12 : $nMonth;
  697. $date = $cyear."-".$nMonth."-1";
  698. $firstday = date('Y-m-01', strtotime($date));
  699. $lastday = date('Y-m-t', strtotime($date));
  700. $data[$i]['start_time'] = $firstday;
  701. $data[$i]['end_time'] = $lastday;
  702. }
  703. $list = array();
  704. #将数据对应填写
  705. foreach ($data as $value) {
  706. $monthGiftLog = CustomerGiftReceives::where('phone',$phone)->where('gift_type',0)
  707. ->where('dtime','>=',$value['start_time'])->where('dtime','<=',$value['end_time'])->first();
  708. if($monthGiftLog){
  709. $value['order_id'] = $monthGiftLog['order_id'];
  710. $value['dtime'] = $monthGiftLog['dtime'];
  711. $value['status'] = '已使用';
  712. $giftInfo = CustomerMonthGift::find($monthGiftLog['gift_id']);
  713. $goodsId = $giftInfo->goods_id;
  714. $goodsInfo = Goods::find($goodsId);
  715. $value['goods_id'] = $goodsId;
  716. $value['goods_name'] = $goodsInfo->name;
  717. } else if($value['end_time'] < date('Y-m-d')){
  718. $value['status'] = '已过期';
  719. } else {
  720. $value['status'] = '未使用';
  721. }
  722. $list[] = $value;
  723. }
  724. return view('customer/customermonthgiftlist',['data'=>$list]);
  725. }
  726. /**
  727. * 查询会员代金券领取记录
  728. * @param Request $request
  729. * @return \Illuminate\View\View
  730. */
  731. public function couponLog(Request $request) {
  732. $phone = $request->input('phone');
  733. $couponList = CustomerCoupons::getCustomerCouponsLog($phone);
  734. $couponList = array_map(function($value){
  735. $item = $value;
  736. if($value['is_use'] == 1){
  737. $item['status'] = '已使用';
  738. } else if(time() <= strtotime($value['end_time'])){
  739. $item['status'] = '未使用';
  740. $item['use_time'] = '';
  741. } else {
  742. $item['status'] = '已过期';
  743. $item['use_time'] = '';
  744. }
  745. return $item;
  746. },$couponList);
  747. return view('customer/customercouponlist',[
  748. 'data'=>$couponList
  749. ]);
  750. }
  751. /**
  752. * 查询会员生日套餐领取记录
  753. * @param Request $request
  754. * @return \Illuminate\View\View
  755. */
  756. public function birthdayGiftLog(Request $request) {
  757. $phone = $request->input('phone');
  758. $log = CustomerGiftReceives::getCustomerReceivesLog($phone,1);
  759. if(count($log) < 1){
  760. $data = array();
  761. } else {
  762. #找到对应的商品列表
  763. //1.找到对应的订单
  764. $orderId = $log[0]['order_id'];
  765. //2.获取所有的生日套餐的gift_id
  766. $giftList = CustomerMonthGift::where('type',1)->pluck('id');
  767. //3.从订单商品表中查找到该订单中gift_id是生日套餐的商品id
  768. if(is_array($giftList)){
  769. $orderGoodsSkus = OrderGoodsSkus::where('order_id',$orderId)->whereIn('gift_id',$giftList)->get();
  770. } else {
  771. $orderGoodsSkus = OrderGoodsSkus::where('order_id',$orderId)->where('gift_id',$giftList)->get();
  772. }
  773. foreach($orderGoodsSkus as $k=>$value) {
  774. $goods = Goods::find($value['goods_id']);
  775. $data[$k]['order_id'] = $orderId;
  776. $data[$k]['goods_id'] = $goods['id'];
  777. $data[$k]['goods_name'] = $goods['name'];
  778. $data[$k]['dtime'] = $log[0]['dtime'];
  779. }
  780. $data = json_decode(json_encode($data),true);
  781. }
  782. return view('customer/customerbirthdaygiftlist',['data'=>$data]);
  783. }
  784. /**
  785. * 会员用户录入报表导出
  786. * @param Request $request
  787. * @return bool
  788. */
  789. public function vipExport(Request $request) {
  790. $name = $request->input('name');
  791. $phone = $request->input('phone');
  792. $admin_id = (int)$request->input('admin_id');
  793. $team_id = (int)$request->input('team_id');
  794. $self_role = session('role_name');
  795. if($self_role == '管理员'){
  796. //只能看自己团队的
  797. $self_id = session('admin_id');
  798. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  799. }
  800. //假如有团队筛选,检索销售队员
  801. $saler_ids = null;
  802. if($team_id>0 && !$admin_id){
  803. $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  804. }
  805. $result = CustomerVip::where(function($query) use($phone, $name, $admin_id, $saler_ids){
  806. if($phone) $query->where('phone', $phone);
  807. if($name) $query->where('name', 'like', '%'. $name. '%');
  808. if($admin_id) $query->where('admin_id', $admin_id);
  809. if( !empty($saler_ids) ) $query->whereIn('admin_id', $saler_ids);
  810. })->where('is_del',0)->orderBy('id','desc')->get();
  811. $result = json_decode(json_encode($result),true);
  812. $result = array_map(function($value){
  813. $item = $value;
  814. $currentDate = date('Y-m-d',time());
  815. //生日套餐
  816. $birthdayGift = CustomerGiftReceives::getCustomerReceivesLog($value['phone'],1);
  817. if($birthdayGift){
  818. $item['birthday_gift'] = $birthdayGift[0]['dtime'].' 领取';
  819. } else {
  820. $item['birthday_gift'] = '未使用';
  821. }
  822. //每月礼包
  823. $num = CustomerGiftReceives::getCustomerResidualTimes($value['phone'],$value['vip_end_time']);
  824. $item['month_gift'] = '剩余'.$num.'次';
  825. //代金券
  826. $coupons = CustomerCoupons::where('phone',$value['phone'])->where('is_use',0)->where('end_time','>=',$currentDate)->count();
  827. $item['coupon'] = '剩余'.$coupons.'张';
  828. $admin = Admin::find($value['admin_id']);
  829. //销售人员
  830. $item['admin_name'] = $admin->realname;
  831. if($item['payment_type'] == 1){
  832. $item['payment'] = '微信支付';
  833. } else if($item['payment_type'] == 3){
  834. $item['payment'] = '个体户支付';
  835. } else {
  836. $item['payment'] = '';
  837. }
  838. $item['phone'] = is_numeric($item['phone']) ? substr_replace($item['phone'], '****', 3, 4) : $item['phone'];
  839. $item['birthday'] = date('m-d',strtotime($item['birthday']));
  840. return $item;
  841. },$result);
  842. $indexKey = ['name','phone','vip_end_time','birthday','admin_name','price','payment','coupon','birthday_gift','month_gift'];
  843. $title = ['姓名', '手机号', '到期时间', '生日', '销售', '金额', '支付方式', '代金券', '生日套餐', '每月礼品'];
  844. $filename = 'customer_'.date('Y-m-d_H').'.xlsx';
  845. return Order::export_excel($result, $filename, $indexKey, $title);
  846. }
  847. /**
  848. * 会员报表
  849. * @param Request $request
  850. * @return \Illuminate\View\View
  851. */
  852. public function membershipReport(Request $request) {
  853. $date = $request->input('date');
  854. $team_id = $request->input('team_id');
  855. $page = $request->input('page',1);
  856. $pageSize = 20;
  857. if($page<=0){
  858. $page = 1;
  859. }
  860. $offset = ($page-1) * $pageSize;
  861. $count = CustomerVip::select(DB::raw('sum(`price`) as total_price,count(1) as number'),'team_id','pay_date','payment_type')
  862. ->where(function($query) use($date,$team_id){
  863. if($date) $query->where('pay_date',$date);
  864. if($team_id != 0) $query->where('team_id',$team_id);
  865. })->where('is_del',0)->groupBy('pay_date','team_id','payment_type')->get();
  866. $count = count($count);
  867. if ($count > 1) {
  868. // 总页数
  869. $pages = ceil($count/$pageSize);
  870. }else{
  871. // 总页数
  872. $pages = 1;
  873. }
  874. $list = CustomerVip::select(DB::raw('sum(`price`) as total_price,count(1) as number'),'team_id','pay_date','payment_type')
  875. ->where(function($query) use($date,$team_id){
  876. if($date) $query->where('pay_date',$date);
  877. if($team_id) $query->where('team_id',$team_id);
  878. })->where('is_del',0)->groupBy('pay_date','team_id','payment_type')->orderBy('pay_date','desc')->offset($offset)->limit($pageSize)->get();
  879. $result = json_decode(json_encode($list),true);
  880. $result = array_map(function($value){
  881. $item = $value;
  882. $team = DB::table('teams')->where('id',$item['team_id'])->first();
  883. $item['team_name'] = $team->name;
  884. if($item['payment_type'] == 1){
  885. $item['payment'] = '微信支付';
  886. } else if($item['payment_type'] == 3){
  887. $item['payment'] = '个体户支付';
  888. } else {
  889. $item['payment'] = '';
  890. }
  891. return $item;
  892. },$result);
  893. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id){
  894. })->where('type', 1)->get();
  895. $teamList = json_decode(json_encode($teamList), true);
  896. return view('customer/vipCustomerReport',[
  897. 'page' => $page,
  898. 'pages' => $pages,
  899. 'result' => $result,
  900. 'date' => $date,
  901. 'team_id' => $team_id,
  902. 'teamlist' => $teamList,
  903. 'count' => $count
  904. ]);
  905. }
  906. /**
  907. * 会员报表导出
  908. * @param Request $request
  909. * @return bool
  910. */
  911. public function membershipReportExport(Request $request){
  912. $date = $request->input('date');
  913. $team_id = $request->input('team_id');
  914. $list = CustomerVip::select(DB::raw('sum(`price`) as total_price,count(1) as number'),'team_id','pay_date','payment_type')
  915. ->where(function($query) use($date,$team_id){
  916. if($date) $query->where('pay_date',$date);
  917. if($team_id) $query->where('team_id',$team_id);
  918. })->where('is_del',0)->groupBy('pay_date','team_id','payment_type')->orderBy('pay_date','desc')->get();
  919. $list = json_decode(json_encode($list),true);
  920. $result = array_map(function($value){
  921. $item = $value;
  922. $team = DB::table('teams')->where('id',$item['team_id'])->first();
  923. $item['team_name'] = $team->name;
  924. if($item['payment_type'] == 1){
  925. $item['payment'] = '微信支付';
  926. } else if($item['payment_type'] == 3){
  927. $item['payment'] = '个体户支付';
  928. } else {
  929. $item['payment'] = '';
  930. }
  931. return $item;
  932. },$list);
  933. $indexKey = ['pay_date','team_name','number','total_price','payment'];
  934. $title = ['日期', '团队', '新增会员数', '充值金额','充值方式'];
  935. $filename = 'customer_export_'.date('Y-m-d_H').'.xlsx';
  936. return Order::export_excel($result, $filename, $indexKey, $title);
  937. }
  938. }