RefundRequestController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\BusinessSetting;
  5. use App\Models\RefundRequest;
  6. use App\Models\OrderDetail;
  7. use App\Models\Shop;
  8. use App\Models\Wallet;
  9. use App\Models\User;
  10. use Artisan;
  11. use Auth;
  12. class RefundRequestController extends Controller
  13. {
  14. /**
  15. * @param \Illuminate\Http\Request $request
  16. * @param int $id
  17. * @return \Illuminate\Http\Response
  18. */
  19. //Store Customer Refund Request
  20. public function request_store(Request $request, $id)
  21. {
  22. $order_detail = OrderDetail::where('id', $id)->first();
  23. $refund = new RefundRequest;
  24. $refund->user_id = Auth::user()->id;
  25. $refund->order_id = $order_detail->order_id;
  26. $refund->order_detail_id = $order_detail->id;
  27. $refund->seller_id = $order_detail->seller_id;
  28. $refund->seller_approval = 0;
  29. $refund->reason = $request->reason;
  30. $refund->admin_approval = 0;
  31. $refund->admin_seen = 0;
  32. $refund->refund_amount = $order_detail->price + $order_detail->tax;
  33. $refund->refund_status = 0;
  34. if ($refund->save()) {
  35. flash( translate("Refund Request has been sent successfully") )->success();
  36. return redirect()->route('purchase_history.index');
  37. }
  38. else {
  39. flash( translate("Something went wrong") )->error();
  40. return back();
  41. }
  42. }
  43. /**
  44. * Display a listing of the resource.
  45. *
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function vendor_index()
  49. {
  50. $refunds = RefundRequest::where('seller_id', Auth::user()->id)->latest()->paginate(10);
  51. return view('refund_request.frontend.recieved_refund_request.index', compact('refunds'));
  52. }
  53. /**
  54. * Display a listing of the resource.
  55. *
  56. * @return \Illuminate\Http\Response
  57. */
  58. public function customer_index()
  59. {
  60. $refunds = RefundRequest::where('user_id', Auth::user()->id)->latest()->paginate(10);
  61. return view('refund_request.frontend.refund_request.index', compact('refunds'));
  62. }
  63. //Set the Refund configuration
  64. public function refund_config()
  65. {
  66. return view('refund_request.config');
  67. }
  68. /**
  69. * Update the specified resource in storage.
  70. *
  71. * @param \Illuminate\Http\Request $request
  72. * @return \Illuminate\Http\Response
  73. */
  74. public function refund_time_update(Request $request)
  75. {
  76. $business_settings = BusinessSetting::where('type', $request->type)->first();
  77. if ($business_settings != null) {
  78. $business_settings->value = $request->value;
  79. $business_settings->save();
  80. }
  81. else {
  82. $business_settings = new BusinessSetting;
  83. $business_settings->type = $request->type;
  84. $business_settings->value = $request->value;
  85. $business_settings->save();
  86. }
  87. Artisan::call('cache:clear');
  88. flash( translate("Refund Request sending time has been updated successfully") )->success();
  89. return back();
  90. }
  91. /**
  92. * Update the specified resource in storage.
  93. *
  94. * @param \Illuminate\Http\Request $request
  95. * @return \Illuminate\Http\Response
  96. */
  97. public function refund_sticker_update(Request $request)
  98. {
  99. $business_settings = BusinessSetting::where('type', $request->type)->first();
  100. if ($business_settings != null) {
  101. $business_settings->value = $request->logo;
  102. $business_settings->save();
  103. }
  104. else {
  105. $business_settings = new BusinessSetting;
  106. $business_settings->type = $request->type;
  107. $business_settings->value = $request->logo;
  108. $business_settings->save();
  109. }
  110. Artisan::call('cache:clear');
  111. flash( translate("Refund Sticker has been updated successfully"))->success();
  112. return back();
  113. }
  114. /**
  115. * Display a listing of the resource.
  116. *
  117. * @return \Illuminate\Http\Response
  118. */
  119. public function admin_index()
  120. {
  121. $refunds = RefundRequest::where('refund_status', 0)->latest()->paginate(15);
  122. return view('refund_request.index', compact('refunds'));
  123. }
  124. /**
  125. * Display a listing of the resource.
  126. *
  127. * @return \Illuminate\Http\Response
  128. */
  129. public function paid_index()
  130. {
  131. $refunds = RefundRequest::where('refund_status', 1)->latest()->paginate(15);
  132. return view('refund_request.paid_refund', compact('refunds'));
  133. }
  134. public function rejected_index()
  135. {
  136. $refunds = RefundRequest::where('refund_status', 2)->latest()->paginate(15);
  137. return view('refund_request.rejected_refund', compact('refunds'));
  138. }
  139. /**
  140. * Update the specified resource in storage.
  141. *
  142. * @param \Illuminate\Http\Request $request
  143. * @param int $id
  144. * @return \Illuminate\Http\Response
  145. */
  146. public function request_approval_vendor(Request $request)
  147. {
  148. $refund = RefundRequest::findOrFail($request->el);
  149. if (Auth::user()->user_type == 'admin' || Auth::user()->user_type == 'staff') {
  150. $refund->seller_approval = 1;
  151. $refund->admin_approval = 1;
  152. }
  153. else {
  154. $refund->seller_approval = 1;
  155. }
  156. if ($refund->save()) {
  157. return 1;
  158. }
  159. else {
  160. return 0;
  161. }
  162. }
  163. /**
  164. * @param \Illuminate\Http\Request $request
  165. * @return \Illuminate\Http\Response
  166. */
  167. public function refund_pay(Request $request)
  168. {
  169. $refund = RefundRequest::findOrFail($request->el);
  170. if ($refund->seller_approval == 1) {
  171. $seller = Shop::where('user_id', $refund->seller_id)->first();
  172. if ($seller != null) {
  173. $seller->admin_to_pay -= $refund->refund_amount;
  174. }
  175. $seller->save();
  176. }
  177. $wallet = new Wallet;
  178. $wallet->user_id = $refund->user_id;
  179. $wallet->amount = $refund->refund_amount;
  180. $wallet->payment_method = 'Refund';
  181. $wallet->payment_details = 'Product Money Refund';
  182. $wallet->save();
  183. $user = User::findOrFail($refund->user_id);
  184. $user->balance += $refund->refund_amount;
  185. $user->save();
  186. if (Auth::user()->user_type == 'admin' || Auth::user()->user_type == 'staff') {
  187. $refund->admin_approval = 1;
  188. $refund->refund_status = 1;
  189. }
  190. if ($refund->save()) {
  191. return 1;
  192. }
  193. else {
  194. return 0;
  195. }
  196. }
  197. public function reject_refund_request(Request $request){
  198. $refund = RefundRequest::findOrFail($request->refund_id);
  199. if (Auth::user()->user_type == 'admin' || Auth::user()->user_type == 'staff') {
  200. $refund->admin_approval = 2;
  201. $refund->refund_status = 2;
  202. $refund->reject_reason = $request->reject_reason;
  203. }
  204. else{
  205. $refund->seller_approval = 2;
  206. $refund->reject_reason = $request->reject_reason;
  207. }
  208. if ($refund->save()) {
  209. flash(translate('Refund request rejected successfully.'))->success();
  210. return back();
  211. }
  212. else {
  213. return back();
  214. }
  215. }
  216. /**
  217. * Show the form for creating a new resource.
  218. *
  219. * @param int $id
  220. * @return \Illuminate\Http\Response
  221. */
  222. public function refund_request_send_page($id)
  223. {
  224. $order_detail = OrderDetail::findOrFail($id);
  225. if ($order_detail->product != null && $order_detail->product->refundable == 1) {
  226. return view('refund_request.frontend.refund_request.create', compact('order_detail'));
  227. }
  228. else {
  229. return back();
  230. }
  231. }
  232. /**
  233. * Show the form for view the specified resource.
  234. *
  235. * @param int $id
  236. * @return \Illuminate\Http\Response
  237. */
  238. //Shows the refund reason
  239. public function reason_view($id)
  240. {
  241. $refund = RefundRequest::findOrFail($id);
  242. if (Auth::user()->user_type == 'admin' || Auth::user()->user_type == 'staff') {
  243. if ($refund->orderDetail != null) {
  244. $refund->admin_seen = 1;
  245. $refund->save();
  246. return view('refund_request.reason', compact('refund'));
  247. }
  248. }
  249. else {
  250. return view('refund_request.frontend.refund_request.reason', compact('refund'));
  251. }
  252. }
  253. public function reject_reason_view($id)
  254. {
  255. $refund = RefundRequest::findOrFail($id);
  256. return $refund->reject_reason;
  257. }
  258. }