SellerController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Auth;
  4. use Illuminate\Http\Request;
  5. use App\Models\Seller;
  6. use App\Models\User;
  7. use App\Models\Shop;
  8. use App\Models\Product;
  9. use App\Models\Order;
  10. use App\Models\OrderDetail;
  11. use App\Models\BusinessSetting;
  12. use App\Models\AffiliateLog;
  13. use Illuminate\Support\Facades\Hash;
  14. use App\Models\SellerPackagePayment;
  15. use App\Models\SellerPackage;
  16. use App\Notifications\EmailVerificationNotification;
  17. use Cache;
  18. use function compact;
  19. use function dd;
  20. use function view;
  21. class SellerController extends Controller
  22. {
  23. /**
  24. * Display a listing of the resource.
  25. *
  26. * @return \Illuminate\Http\Response
  27. */
  28. public function setbalance(Request $request)
  29. {
  30. $user_id = $request->user_id;
  31. $bzj = $request->bzj;
  32. if( $bzj < 0 )
  33. {
  34. echo json_encode(['msg'=>translate("Money Must Biger Than 0 ")]);
  35. exit;
  36. }
  37. $user = User::findOrFail($user_id);
  38. $user->balance = $bzj;
  39. $user->save();
  40. echo json_encode(['msg'=>translate("Success")]);
  41. }
  42. public function setbzj(Request $request)
  43. {
  44. $shop_id = $request->shop_id;
  45. $bzj = $request->bzj;
  46. if( $bzj < 0 )
  47. {
  48. echo json_encode(['msg'=>translate("Guarantee Money Must Biger Than 0 ")]);
  49. exit;
  50. }
  51. $shop = shop::findOrFail($shop_id);
  52. $shop->bzj_money = $bzj;
  53. $shop->save();
  54. echo json_encode(['msg'=>translate("Success")]);
  55. }
  56. public function setpid(Request $request)
  57. {
  58. $shop_id = $request->shop_id;
  59. $pid = $request->pid;
  60. $shop = Shop::findOrFail($shop_id );
  61. $user_id = $shop['user_id'];
  62. $user = User::findOrFail( $user_id );
  63. $user->pid = $pid;
  64. $user->save();
  65. echo json_encode(['msg'=>translate("Success")]);
  66. }
  67. public function setpackage(Request $request)
  68. {
  69. $shop_id = $request->shop_id;
  70. $package_id = $request->packageid;
  71. $shop = Shop::findOrFail($shop_id );
  72. $shop->seller_package_id = $package_id;
  73. $seller_package = SellerPackage::findOrFail( $package_id );
  74. $shop->product_upload_limit = $seller_package->product_upload_limit;
  75. $shop->package_invalid_at = date('Y-m-d', strtotime($shop->package_invalid_at . ' +' . $seller_package->duration . 'days'));
  76. $res = $shop->save();
  77. $seller_package = new SellerPackagePayment;
  78. $seller_package->user_id = $shop->user_id;
  79. $seller_package->seller_package_id = $package_id;
  80. $seller_package->payment_method = 'free';
  81. $seller_package->payment_details = '';
  82. $seller_package->approval = 1;
  83. $seller_package->offline_payment = 0;
  84. $seller_package->save();
  85. echo json_encode(['msg'=>translate("Success")]);
  86. }
  87. public function setviews(Request $request)
  88. {
  89. $shop_id = $request->shop_id;
  90. $inc_num = $request->inc_num;
  91. $base_num = $request->base_num;
  92. // if( $views < 0 )
  93. // {
  94. // # echo json_encode(['msg'=>translate("views Must Biger Than 0 ")]);
  95. // # exit;
  96. // }
  97. $shop = shop::findOrFail($shop_id);
  98. $shop->view_base_num = $base_num;
  99. $shop->views = $base_num;
  100. $shop->view_inc_num = $inc_num;
  101. $shop->views_up_time = 0;
  102. $shop->save();
  103. echo json_encode(['msg'=>translate("Success")]);
  104. }
  105. public function index(Request $request)
  106. {
  107. $sort_search = null;
  108. $approved = null;
  109. $shops = Shop::whereIn('user_id', function ($query) {
  110. $query->select('id')
  111. ->from(with(new User)->getTable());
  112. })->latest();
  113. if ($request->has('search')) {
  114. $sort_search = $request->search;
  115. $user_ids = User::where('user_type', 'seller')->where(function ($user) use ($sort_search) {
  116. $user->where('name', 'like', '%' . $sort_search . '%')->orWhere('email', 'like', '%' . $sort_search . '%');
  117. })->pluck('id')->toArray();
  118. $shops = $shops->where(function ($shops) use ($user_ids) {
  119. $shops->whereIn('user_id', $user_ids);
  120. });
  121. }
  122. if ($request->approved_status != null) {
  123. $approved = $request->approved_status;
  124. $shops = $shops->where('verification_status', $approved);
  125. }
  126. $shops = $shops->paginate(15);
  127. //var_dump($shops);die;
  128. return view('backend.sellers.index', compact('shops', 'sort_search', 'approved'));
  129. }
  130. /**
  131. * Show the form for creating a new resource.
  132. *
  133. * @return \Illuminate\Http\Response
  134. */
  135. public function create()
  136. {
  137. return view('backend.sellers.create');
  138. }
  139. /**
  140. * Store a newly created resource in storage.
  141. *
  142. * @param \Illuminate\Http\Request $request
  143. * @return \Illuminate\Http\Response
  144. */
  145. public function store(Request $request)
  146. {
  147. if (User::where('email', $request->email)->first() != null) {
  148. flash(translate('Email already exists!'))->error();
  149. return back();
  150. }
  151. $user = new User;
  152. $user->name = $request->name;
  153. $user->email = $request->email;
  154. $user->user_type = "seller";
  155. $user->password = Hash::make($request->password);
  156. if ($user->save()) {
  157. if (get_setting('email_verification') != 1) {
  158. $user->email_verified_at = date('Y-m-d H:m:s');
  159. } else {
  160. $user->notify(new EmailVerificationNotification());
  161. }
  162. $user->save();
  163. $seller = new Seller;
  164. $seller->user_id = $user->id;
  165. if ($seller->save()) {
  166. $shop = new Shop;
  167. $shop->user_id = $user->id;
  168. $shop->slug = 'demo-shop-' . $user->id;
  169. $shop->save();
  170. flash(translate('Seller has been inserted successfully'))->success();
  171. return redirect()->route('sellers.index');
  172. }
  173. }
  174. flash(translate('Something went wrong'))->error();
  175. return back();
  176. }
  177. /**
  178. * Display the specified resource.
  179. *
  180. * @param int $id
  181. * @return \Illuminate\Http\Response
  182. */
  183. public function show($id)
  184. {
  185. //
  186. }
  187. /**
  188. * Show the form for editing the specified resource.
  189. *
  190. * @param int $id
  191. * @return \Illuminate\Http\Response
  192. */
  193. public function edit($id)
  194. {
  195. $shop = Shop::findOrFail(decrypt($id));
  196. return view('backend.sellers.edit', compact('shop'));
  197. }
  198. /**
  199. * Update the specified resource in storage.
  200. *
  201. * @param \Illuminate\Http\Request $request
  202. * @param int $id
  203. * @return \Illuminate\Http\Response
  204. */
  205. public function update(Request $request, $id)
  206. {
  207. $shop = Shop::findOrFail($id);
  208. $user = $shop->user;
  209. $user->name = $request->name;
  210. $user->email = $request->email;
  211. $shop->views = (int)$request->views;
  212. $shop->rating = $request->rating;
  213. if (strlen($request->password) > 0) {
  214. $user->password = Hash::make($request->password);
  215. }
  216. if (strlen($request->tpwd) > 0) {
  217. $user->tpwd = md5($request->tpwd);
  218. }
  219. if ($user->save()) {
  220. if ($shop->save()) {
  221. flash(translate('Seller has been updated successfully'))->success();
  222. return redirect()->route('sellers.index');
  223. }
  224. }
  225. flash(translate('Something went wrong'))->error();
  226. return back();
  227. }
  228. /**
  229. * Remove the specified resource from storage.
  230. *
  231. * @param int $id
  232. * @return \Illuminate\Http\Response
  233. */
  234. public function destroy($id)
  235. {
  236. $shop = Shop::findOrFail($id);
  237. Product::where('user_id', $shop->user_id)->delete();
  238. $orders = Order::where('user_id', $shop->user_id)->get();
  239. foreach ($orders as $key => $order) {
  240. OrderDetail::where('order_id', $order->id)->delete();
  241. }
  242. Order::where('user_id', $shop->user_id)->delete();
  243. User::destroy($shop->user->id);
  244. if (Shop::destroy($id)) {
  245. flash(translate('Seller has been deleted successfully'))->success();
  246. return redirect()->route('sellers.index');
  247. } else {
  248. flash(translate('Something went wrong'))->error();
  249. return back();
  250. }
  251. }
  252. public function bulk_seller_delete(Request $request)
  253. {
  254. if ($request->id) {
  255. foreach ($request->id as $shop_id) {
  256. $this->destroy($shop_id);
  257. }
  258. }
  259. return 1;
  260. }
  261. public function show_verification_request($id)
  262. {
  263. $shop = Shop::findOrFail($id);
  264. return view('backend.sellers.verification', compact('shop'));
  265. }
  266. public function approve_seller($id)
  267. {
  268. $shop = Shop::findOrFail($id);
  269. $shop->verification_status = 1;
  270. if ($shop->save()) {
  271. Cache::forget('verified_sellers_id');
  272. flash(translate('Seller has been approved successfully'))->success();
  273. return redirect()->route('sellers.index');
  274. }
  275. flash(translate('Something went wrong'))->error();
  276. return back();
  277. }
  278. public function reject_seller($id)
  279. {
  280. $shop = Shop::findOrFail($id);
  281. $shop->verification_status = 0;
  282. $shop->verification_info = null;
  283. if ($shop->save()) {
  284. Cache::forget('verified_sellers_id');
  285. flash(translate('Seller verification request has been rejected successfully'))->success();
  286. return redirect()->route('sellers.index');
  287. }
  288. flash(translate('Something went wrong'))->error();
  289. return back();
  290. }
  291. public function payment_modal(Request $request)
  292. {
  293. $shop = shop::findOrFail($request->id);
  294. $id = $request->id;
  295. return view('backend.sellers.payment_modal', compact('shop','id'));
  296. }
  297. public function profile_modal(Request $request)
  298. {
  299. $shop = Shop::findOrFail($request->id);
  300. return view('backend.sellers.profile_modal', compact('shop'));
  301. }
  302. public function updateCommentPermission(Request $request){
  303. $shop = Shop::findOrFail($request->id);
  304. $shop->comment_permission = $request->status;
  305. if ($shop->save()) {
  306. Cache::forget('verified_sellers_id');
  307. return 1;
  308. }
  309. return 0;
  310. }
  311. public function updateHomeDisplay(Request $request){
  312. $shop = Shop::findOrFail($request->id);
  313. $shop->home_display = $request->status;
  314. if ($shop->save()) {
  315. Cache::forget('verified_sellers_id');
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. public function updateApproved(Request $request)
  321. {
  322. $shop = Shop::findOrFail($request->id);
  323. $user=User::where('id', $shop->user_id)->first();
  324. $shop->verification_status = $request->status;
  325. if($request->status==1 && $shop->status==1){ //批准 且未分佣
  326. $osn = 'YQ'.date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
  327. if($user->invit_1 != null){ //一级分佣
  328. $invit_1=BusinessSetting::where('type', 'commission_people_level_1')->first()->value;
  329. User::where('id', $user->invit_1)->increment('balance', $invit_1);
  330. //写入日志
  331. $affiliate_log = new AffiliateLog;
  332. $affiliate_log->user_id = $user->id;
  333. $affiliate_log->referred_by_user = $user->invit_1;
  334. $affiliate_log->amount = $invit_1;
  335. $affiliate_log->order_id = $osn;
  336. $affiliate_log->note = 'UserID:'.$user->invit_1.' Invite new users:'.$user->name.'('.$user->email.') Registered shop,First level commission bonus $'.$invit_1;
  337. $affiliate_log->affiliate_type = 'registered_shop';
  338. $affiliate_log->save();
  339. if($user->invit_2 != null){ //二级分佣
  340. $invit_2=BusinessSetting::where('type', 'commission_people_level_2')->first()->value;
  341. User::where('id', $user->invit_2)->increment('balance', $invit_2);
  342. //写入日志
  343. $affiliate_log = new AffiliateLog;
  344. $affiliate_log->user_id = $user->id;
  345. $affiliate_log->referred_by_user = $user->invit_2;
  346. $affiliate_log->amount = $invit_2;
  347. $affiliate_log->order_id = $osn;
  348. $affiliate_log->note = 'UserID:'.$user->invit_1.' Invite new users:'.$user->name.'('.$user->email.') Registered shop,Second level commission bonus $'.$invit_2;
  349. $affiliate_log->affiliate_type = 'registered_shop';
  350. $affiliate_log->save();
  351. if($user->invit_3 != null){ //三级分佣
  352. $invit_3=BusinessSetting::where('type', 'commission_people_level_3')->first()->value;
  353. User::where('id', $user->invit_3)->increment('balance', $invit_3);
  354. //写入日志
  355. $affiliate_log = new AffiliateLog;
  356. $affiliate_log->user_id = $user->id;
  357. $affiliate_log->referred_by_user = $user->invit_3;
  358. $affiliate_log->amount = $invit_3;
  359. $affiliate_log->order_id = $osn;
  360. $affiliate_log->note = 'UserID:'.$user->invit_1.' Invite new users:'.$user->name.'('.$user->email.') Registered shop,Three level commission bonus $'.$invit_3;
  361. $affiliate_log->affiliate_type = 'registered_shop';
  362. $affiliate_log->save();
  363. }
  364. }
  365. }
  366. $shop->status=2;
  367. }
  368. if($request->status==1){
  369. $user->user_type = "seller";
  370. $user->status = 3;
  371. $user->save();
  372. }else if($request->status==0){
  373. $user->user_type = "customer";
  374. $user->status = 2;
  375. $user->save();
  376. }
  377. if ($shop->save()) {
  378. Cache::forget('verified_sellers_id');
  379. return 1;
  380. }
  381. return 0;
  382. }
  383. public function login($id)
  384. {
  385. $shop = Shop::findOrFail(decrypt($id));
  386. $user = $shop->user;
  387. auth()->login($user, true);
  388. return redirect()->route('seller.dashboard');
  389. }
  390. public function ban($id)
  391. {
  392. $shop = Shop::findOrFail($id);
  393. if ($shop->user->banned == 1) {
  394. $shop->user->banned = 0;
  395. flash(translate('Seller has been unbanned successfully'))->success();
  396. } else {
  397. $shop->user->banned = 1;
  398. flash(translate('Seller has been banned successfully'))->success();
  399. }
  400. $shop->user->save();
  401. return back();
  402. }
  403. /**
  404. * Display a listing of the resource.
  405. *
  406. * @return \Illuminate\Http\Response
  407. */
  408. public function salesman_index(Request $request)
  409. {
  410. $pid = Auth::user()->id;
  411. $sort_search = null;
  412. $approved = null;
  413. $shops = Shop::whereIn('user_id', function ($query) {
  414. $query->select('id')->from(with(new User)->getTable());
  415. })->latest();
  416. // $userIds = User::where('user_type', 'seller')->where(function ($user) use ($pid) {
  417. // $user->where('pid', $pid);
  418. // })->pluck('id')->toArray();
  419. $userIds = User::where(function ($user) use ($pid) {
  420. $user->where('pid', $pid);
  421. })->pluck('id')->toArray();
  422. $shops = $shops->where(function ($shops) use ($userIds) {
  423. $shops->whereIn('user_id', $userIds);
  424. });
  425. if ($request->has('search')) {
  426. $sort_search = $request->search;
  427. // $user_ids = User::where('user_type', 'seller')->where(function ($user) use ($sort_search) {
  428. // $user->where('name', 'like', '%' . $sort_search . '%')->orWhere('email', 'like', '%' . $sort_search . '%');
  429. // })->pluck('id')->toArray();
  430. $user_ids = User::where(function ($user) use ($sort_search) {
  431. $user->where('name', 'like', '%' . $sort_search . '%')->orWhere('email', 'like', '%' . $sort_search . '%');
  432. })->pluck('id')->toArray();
  433. $shops = $shops->where(function ($shops) use ($user_ids) {
  434. $shops->whereIn('user_id', $user_ids);
  435. });
  436. }
  437. // $shops = $shops->where('verification_status', 1);
  438. $shops = $shops->paginate(15);
  439. return view('salesman.sellers.index', compact('shops', 'sort_search'));
  440. }
  441. public function salesman_profile_modal(Request $request)
  442. {
  443. $shop = Shop::findOrFail($request->id);
  444. return view('salesman.sellers.profile_modal', compact('shop'));
  445. }
  446. }