SalesmanController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Staff;
  4. use Hash;
  5. use Illuminate\Http\Request;
  6. use App\Models\Salesman;
  7. use App\Models\User;
  8. use App\Models\Order;
  9. use function back;
  10. use function date;
  11. use function flash;
  12. use function redirect;
  13. use function translate;
  14. class SalesmanController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index( Request $request ) {
  22. $sort_search = NULL;
  23. $salesman = User::where('user_type', 'salesman')->orderBy('created_at', 'desc');
  24. if ( $request->has('search') )
  25. {
  26. $sort_search = $request->search;
  27. $salesman->where(function ( $q ) use ( $sort_search )
  28. {
  29. $q->where('name', 'like', '%' . $sort_search . '%')->orWhere('email', 'like', '%' . $sort_search . '%');
  30. });
  31. }
  32. $salesmans = $salesman->paginate(15);
  33. return view('backend.salesman.salesmans.index', compact('salesmans'));
  34. }
  35. /**
  36. * Show the form for creating a new resource.
  37. *
  38. * @return \Illuminate\Http\Response
  39. */
  40. public function create() {
  41. return view('backend.salesman.salesmans.create');
  42. }
  43. /**
  44. * Store a newly created resource in storage.
  45. *
  46. * @param \Illuminate\Http\Request $request
  47. *
  48. * @return \Illuminate\Http\Response
  49. */
  50. public function store( Request $request ) {
  51. if ( User::where('email', $request->email)->first() == NULL )
  52. {
  53. $user = new User;
  54. $user->name = $request->name;
  55. $user->email = $request->email;
  56. $user->phone = $request->phone;
  57. $user->customer_service_link = $request->customer_service_link;
  58. $user->user_type = "salesman";
  59. $user->email_verified_at = date('Y-m-d H:m:s');
  60. $user->password = Hash::make($request->password);
  61. if ( $user->save() )
  62. {
  63. flash(translate('Salesman has been inserted successfully'))->success();
  64. return redirect()->route('salesmans.index');
  65. }
  66. }
  67. flash(translate('Email already used'))->error();
  68. return back();
  69. }
  70. /**
  71. * Display the specified resource.
  72. *
  73. * @param int $id
  74. *
  75. * @return \Illuminate\Http\Response
  76. */
  77. public function show( $id ) {
  78. //
  79. }
  80. /**
  81. * Show the form for editing the specified resource.
  82. *
  83. * @param int $id
  84. *
  85. * @return \Illuminate\Http\Response
  86. */
  87. public function edit( $id ) {
  88. $user = User::findOrFail(decrypt($id));
  89. return view('backend.salesman.salesmans.edit', compact('user'));
  90. }
  91. /**
  92. * Update the specified resource in storage.
  93. *
  94. * @param \Illuminate\Http\Request $request
  95. * @param int $id
  96. *
  97. * @return \Illuminate\Http\Response
  98. */
  99. public function update( Request $request, $id ) {
  100. $user = User::findOrFail($id);
  101. $user->name = $request->name;
  102. $user->email = $request->email;
  103. $user->phone = $request->phone;
  104. $user->customer_service_link = $request->customer_service_link;
  105. if ( strlen($request->password) > 0 )
  106. {
  107. $user->password = Hash::make($request->password);
  108. }
  109. if ( $user->save() )
  110. {
  111. flash(translate('Salesman has been updated successfully'))->success();
  112. return redirect()->route('salesmans.index');
  113. }
  114. flash(translate('Something went wrong'))->error();
  115. return back();
  116. }
  117. /**
  118. * Remove the specified resource from storage.
  119. *
  120. * @param int $id
  121. *
  122. * @return \Illuminate\Http\Response
  123. */
  124. public function destroy( $id ) {
  125. User::destroy($id);
  126. flash(translate('Salesman has been deleted successfully'))->success();
  127. return redirect()->route('salesmans.index');
  128. }
  129. public function bulk_salesman_delete( Request $request ) {
  130. if ( $request->id )
  131. {
  132. foreach ( $request->id as $salesman_id )
  133. {
  134. $this->destroy($salesman_id);
  135. }
  136. }
  137. return 1;
  138. }
  139. public function login( $id ) {
  140. $user = User::findOrFail(decrypt($id));
  141. auth()->login($user, true);
  142. return redirect()->route('dashboard');
  143. }
  144. public function ban( $id ) {
  145. $user = User::findOrFail(decrypt($id));
  146. if ( $user->banned == 1 )
  147. {
  148. $user->banned = 0;
  149. flash(translate('Salesman UnBanned Successfully'))->success();
  150. }
  151. else
  152. {
  153. $user->banned = 1;
  154. flash(translate('Salesman Banned Successfully'))->success();
  155. }
  156. $user->save();
  157. return back();
  158. }
  159. }