SellerWithdrawRequestController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Http\Controllers\Seller;
  3. use App\Models\Order;
  4. use App\Models\Payment;
  5. use App\Models\Wallet;
  6. use Illuminate\Http\Request;
  7. use App\Models\SellerWithdrawRequest;
  8. use App\Models\User;
  9. use App\Models\Shop;
  10. use Auth;
  11. class SellerWithdrawRequestController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return \Illuminate\Http\Response
  17. */
  18. public function index()
  19. {
  20. $user = Auth::user();
  21. $balance = $user->balance;
  22. $userId = $user->id;
  23. $seller_withdraw_requests = SellerWithdrawRequest::where('user_id', $userId)->latest()->paginate(9)
  24. ->appends(['opage' => \request()->opage, 'rpage' => \request()->rpage]);
  25. $freezeOrders = Order::query()->where('seller_id', $userId)
  26. ->where(function ($query) {
  27. $query->whereNotNull('freeze_expired_at')
  28. ->orWhere(function ($subQuery) {
  29. $subQuery->whereNull('freeze_expired_at')->where('product_storehouse_status', 0);
  30. });
  31. })
  32. ->latest()
  33. ->paginate(9, ['*'], 'opage')
  34. ->appends(['page' => \request()->page, 'rpage' => \request()->rpage]);
  35. $rechargeList = Wallet::query()->where('user_id', $userId)->latest()->paginate(9, ['*'], 'rpage')
  36. ->appends(['page' => \request()->page, 'opage' => \request()->opage]);
  37. $shop = $user->shop;
  38. $paymentList = Payment::orderBy('created_at', 'desc')->where('t_type',1)->where('seller_id',Auth::user()->id)->paginate(15);
  39. return view('seller.money_withdraw_requests.index', compact('paymentList','seller_withdraw_requests', 'freezeOrders', 'rechargeList', 'balance', 'shop'));
  40. }
  41. /**
  42. * Store a newly created resource in storage.
  43. *
  44. * @param \Illuminate\Http\Request $request
  45. * @return \Illuminate\Http\Response
  46. */
  47. public function store(Request $request)
  48. {
  49. $user = Auth::user();
  50. $type = $request->type;
  51. if( $type == 1 )
  52. {
  53. if ($request->amount > $user->balance) {
  54. flash(translate('You do not have enough balance to send withdraw request'))->error();
  55. return back();
  56. }
  57. $exits = SellerWithdrawRequest::where('status', '0')->where('type',1)->where('user_id', $user->id)->count();
  58. if ($exits !== 0) {
  59. flash(translate('withdraw exited'))->error();
  60. return back();
  61. }
  62. $seller_withdraw_request = new SellerWithdrawRequest;
  63. $seller_withdraw_request->user_id = $user->id;
  64. $seller_withdraw_request->amount = $request->amount;
  65. $seller_withdraw_request->message = $request->message;
  66. $seller_withdraw_request->status = '0';
  67. $seller_withdraw_request->viewed = '0';
  68. $seller_withdraw_request->w_type = $request->w_type;
  69. if ($seller_withdraw_request->save()) {//扣除余额
  70. $userModel = User::find($user->id);
  71. $userModel->balance = $user->balance-$request->amount;
  72. $userModel->save();
  73. flash(translate('Request has been sent successfully'))->success();
  74. return redirect()->route('seller.money_withdraw_requests.index');
  75. } else {
  76. flash(translate('Something went wrong'))->error();
  77. return back();
  78. }
  79. }
  80. elseif ( $type == 2 )
  81. {
  82. if ($request->amount > $user->shop->bzj_money) {
  83. flash(translate('You do not have enough guarantee balance to send withdraw request'))->error();
  84. return back();
  85. }
  86. $exits = SellerWithdrawRequest::where('status', '0')->where('type',2)->where('user_id', $user->id)->count();
  87. if ($exits !== 0)
  88. {
  89. flash(translate('withdraw exited'))->error();
  90. return back();
  91. }
  92. $seller_withdraw_request = new SellerWithdrawRequest;
  93. $seller_withdraw_request->user_id = $user->id;
  94. $seller_withdraw_request->amount = $request->amount;
  95. $seller_withdraw_request->message = $request->message;
  96. $seller_withdraw_request->status = '0';
  97. $seller_withdraw_request->viewed = '0';
  98. $seller_withdraw_request->type = 2;
  99. $seller_withdraw_request->w_type = $request->w_type;
  100. if ($seller_withdraw_request->save()) {//扣除余额
  101. $userModel = Shop::find($user->shop->id);
  102. $userModel->bzj_money = $userModel->bzj_money-$request->amount;
  103. $userModel->save();
  104. flash(translate('Request has been sent successfully'))->success();
  105. return redirect()->route('seller.money_withdraw_requests.index');
  106. } else {
  107. flash(translate('Something went wrong'))->error();
  108. return back();
  109. }
  110. }
  111. }
  112. public function store234(Request $request)
  113. {
  114. $user = Auth::user();
  115. $type = $request->type;
  116. if( $type == 1 )
  117. {
  118. if ($request->amount > $user->balance) {
  119. flash(translate('You do not have enough balance to send withdraw request'))->error();
  120. return back();
  121. }
  122. $exits = SellerWithdrawRequest::where('status', '0')->where('type',1)->where('user_id', $user->id)->count();
  123. if ($exits !== 0) {
  124. flash(translate('withdraw exited'))->error();
  125. return back();
  126. }
  127. $seller_withdraw_request = new SellerWithdrawRequest;
  128. $seller_withdraw_request->user_id = $user->id;
  129. $seller_withdraw_request->amount = $request->amount;
  130. $seller_withdraw_request->message = $request->message;
  131. $seller_withdraw_request->status = '0';
  132. $seller_withdraw_request->viewed = '0';
  133. if ($seller_withdraw_request->save()) {//扣除余额
  134. $userModel = User::find($user->id);
  135. $userModel->balance = $user->balance-$request->amount;
  136. $userModel->save();
  137. flash(translate('Request has been sent successfully'))->success();
  138. return redirect()->route('seller.money_withdraw_requests.index');
  139. } else {
  140. flash(translate('Something went wrong'))->error();
  141. return back();
  142. }
  143. }
  144. elseif ( $type == 2 )
  145. {
  146. if ($request->amount > $user->shop->bzj_money) {
  147. flash(translate('You do not have enough guarantee balance to send withdraw request'))->error();
  148. return back();
  149. }
  150. $exits = SellerWithdrawRequest::where('status', '0')->where('type',2)->where('user_id', $user->id)->count();
  151. if ($exits !== 0)
  152. {
  153. flash(translate('withdraw exited'))->error();
  154. return back();
  155. }
  156. $seller_withdraw_request = new SellerWithdrawRequest;
  157. $seller_withdraw_request->user_id = $user->id;
  158. $seller_withdraw_request->amount = $request->amount;
  159. $seller_withdraw_request->message = $request->message;
  160. $seller_withdraw_request->status = '0';
  161. $seller_withdraw_request->viewed = '0';
  162. $seller_withdraw_request->type = 2;
  163. if ($seller_withdraw_request->save()) {//扣除余额
  164. $userModel = Shop::find($user->shop->id);
  165. $userModel->bzj_money = $userModel->bzj_money-$request->amount;
  166. $userModel->save();
  167. flash(translate('Request has been sent successfully'))->success();
  168. return redirect()->route('seller.money_withdraw_requests.index');
  169. } else {
  170. flash(translate('Something went wrong'))->error();
  171. return back();
  172. }
  173. }
  174. }
  175. }