SellerPackageController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\SellerSpreadPackage;
  4. use App\Models\SellerSpreadPackagePayment;
  5. use Illuminate\Http\Request;
  6. use App\Models\SellerPackage;
  7. use App\Models\Payment;
  8. use App\Models\SellerPackageTranslation;
  9. use App\Models\SellerPackagePayment;
  10. use App\Models\Shop;
  11. use Auth;
  12. use DB;
  13. use Session;
  14. use Carbon\Carbon;
  15. use function back;
  16. use function class_exists;
  17. use function date;
  18. use function flash;
  19. use function redirect;
  20. use function str_replace;
  21. use function strtotime;
  22. use function translate;
  23. use function ucwords;
  24. class SellerPackageController extends Controller
  25. {
  26. /**
  27. * Display a listing of the resource.
  28. *
  29. * @return \Illuminate\Http\Response
  30. */
  31. public function buy_package_by_cash(Request $request)
  32. {
  33. $package_id = $request->package_id;
  34. $seller_packages = SellerPackage::findOrFail( $package_id );
  35. $name = $seller_packages->name;
  36. $user = Auth::user();
  37. $shop_id = $user->shop->id;
  38. #print_r($seller_packages->toArray());
  39. #exit;
  40. DB::beginTransaction();
  41. $amount = $seller_packages->amount;
  42. if ($user->balance >= $seller_packages->amount)
  43. {
  44. $user->balance -= $seller_packages->amount;
  45. $user->save();
  46. $shop = Shop::findOrFail($shop_id );
  47. $shop->seller_package_id = $package_id;
  48. $seller_package = SellerPackage::findOrFail( $package_id );
  49. $seller = Auth::user()->shop;
  50. $shop->product_upload_limit = $seller_package->product_upload_limit;
  51. $shop->package_invalid_at = date('Y-m-d', strtotime($seller->package_invalid_at . ' +' . $seller_package->duration . 'days'));
  52. $res = $shop->save();
  53. $seller_package2 = new SellerPackagePayment;
  54. $seller_package2->user_id = $shop->user_id;
  55. $seller_package2->seller_package_id = $package_id;
  56. $seller_package2->payment_method = 'wallet';
  57. $seller_package2->payment_details = '';
  58. $seller_package2->approval = 1;
  59. $seller_package2->offline_payment = 0;
  60. $seller_package2->save();
  61. $payment = new Payment;
  62. $payment->seller_id = $user->id;
  63. $payment->amount = -1* abs($amount);
  64. $payment->payment_method = 'wallet';
  65. $payment->txn_code = date("YmdHis");
  66. $payment->payment_details = $name;
  67. $payment->save();
  68. DB::commit();
  69. return response()->json(['success' => 1, 'message' => translate('Payment completed')]);
  70. }
  71. DB::rollBack();
  72. return response()->json(['success' => 0, 'message' => translate('Insufficient balance')]);
  73. }
  74. public function index()
  75. {
  76. $seller_packages = SellerPackage::all();
  77. return view('seller_packages.index', compact('seller_packages'));
  78. }
  79. /**
  80. * Show the form for creating a new resource.
  81. *
  82. * @return \Illuminate\Http\Response
  83. */
  84. public function create()
  85. {
  86. return view('seller_packages.create');
  87. }
  88. /**
  89. * Store a newly created resource in storage.
  90. *
  91. * @param \Illuminate\Http\Request $request
  92. * @return \Illuminate\Http\Response
  93. */
  94. public function set_default(Request $request, $id)
  95. {
  96. DB::table('seller_packages')->update(['is_default'=>0]);
  97. $seller_package = SellerPackage::findOrFail($id);
  98. $seller_package->is_default = 1;
  99. $seller_package->save();
  100. flash(translate('successfully'))->success();
  101. return redirect()->route('seller_packages.index');
  102. }
  103. public function store(Request $request)
  104. {
  105. $seller_package = new SellerPackage;
  106. $seller_package->name = $request->name;
  107. $seller_package->amount = $request->amount;
  108. $seller_package->product_upload_limit = $request->product_upload_limit;
  109. $seller_package->duration = $request->duration;
  110. $seller_package->logo = $request->logo;
  111. $seller_package->max_profit = min(255, (int)$request->max_profit);
  112. if ($seller_package->save()) {
  113. $seller_package_translation = SellerPackageTranslation::firstOrNew(['lang' => env('DEFAULT_LANGUAGE'), 'seller_package_id' => $seller_package->id]);
  114. $seller_package_translation->name = $request->name;
  115. $seller_package_translation->save();
  116. flash(translate('Package has been inserted successfully'))->success();
  117. return redirect()->route('seller_packages.index');
  118. } else {
  119. flash(translate('Something went wrong'))->error();
  120. return back();
  121. }
  122. }
  123. /**
  124. * Display the specified resource.
  125. *
  126. * @param int $id
  127. * @return \Illuminate\Http\Response
  128. */
  129. public function show($id)
  130. {
  131. //
  132. }
  133. /**
  134. * Show the form for editing the specified resource.
  135. *
  136. * @param int $id
  137. * @return \Illuminate\Http\Response
  138. */
  139. public function edit(Request $request, $id)
  140. {
  141. $lang = $request->lang;
  142. $seller_package = SellerPackage::findOrFail($id);
  143. return view('seller_packages.edit', compact('seller_package', 'lang'));
  144. }
  145. /**
  146. * Update the specified resource in storage.
  147. *
  148. * @param \Illuminate\Http\Request $request
  149. * @param int $id
  150. * @return \Illuminate\Http\Response
  151. */
  152. public function update(Request $request, $id)
  153. {
  154. $seller_package = SellerPackage::findOrFail($id);
  155. if ($request->lang == env("DEFAULT_LANGUAGE")) {
  156. $seller_package->name = $request->name;
  157. }
  158. $seller_package->amount = $request->amount;
  159. $seller_package->product_upload_limit = $request->product_upload_limit;
  160. $seller_package->duration = $request->duration;
  161. $seller_package->max_profit = min(255, (int)$request->max_profit);
  162. $seller_package->logo = $request->logo;
  163. if ($seller_package->save()) {
  164. $seller_package_translation = SellerPackageTranslation::firstOrNew(['lang' => $request->lang, 'seller_package_id' => $seller_package->id]);
  165. $seller_package_translation->name = $request->name;
  166. $seller_package_translation->save();
  167. flash(translate('Package has been inserted successfully'))->success();
  168. return redirect()->route('seller_packages.index');
  169. } else {
  170. flash(translate('Something went wrong'))->error();
  171. return back();
  172. }
  173. }
  174. /**
  175. * Remove the specified resource from storage.
  176. *
  177. * @param int $id
  178. * @return \Illuminate\Http\Response
  179. */
  180. public function destroy($id)
  181. {
  182. $seller_package = SellerPackage::findOrFail($id);
  183. foreach ($seller_package->seller_package_translations as $key => $seller_package_translation) {
  184. $seller_package_translation->delete();
  185. }
  186. SellerPackage::destroy($id);
  187. flash(translate('Package has been deleted successfully'))->success();
  188. return redirect()->route('seller_packages.index');
  189. }
  190. //FrontEnd
  191. //@index
  192. public function packages_payment_list()
  193. {
  194. $seller_packages_payment = SellerPackagePayment::with('seller_package')->where('user_id', Auth::user()->id)->latest()->paginate(10);
  195. return view('seller_packages.frontend.packages_payment_list', compact('seller_packages_payment'));
  196. }
  197. public function seller_packages_list()
  198. {
  199. $seller_packages = SellerPackage::all();
  200. return view('seller_packages.frontend.seller_packages_list', compact('seller_packages'));
  201. }
  202. public function purchase_package(Request $request)
  203. {
  204. $data['seller_package_id'] = $request->seller_package_id;
  205. $data['payment_method'] = $request->payment_option;
  206. $request->session()->put('payment_type', 'seller_package_payment');
  207. $request->session()->put('payment_data', $data);
  208. $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
  209. if ($seller_package->amount == 0) {
  210. return $this->purchase_payment_done(Session::get('payment_data'), null);
  211. } elseif (Auth::user()->shop->seller_package != null && $seller_package->product_upload_limit < Auth::user()->shop->seller_package->product_upload_limit) {
  212. flash(translate('You have more uploaded products than this package limit. You need to remove excessive products to downgrade.'))->warning();
  213. return back();
  214. }
  215. $decorator = __NAMESPACE__ . '\\Payment\\' . str_replace(' ', '', ucwords(str_replace('_', ' ', $request->payment_option))) . "Controller";
  216. if (class_exists($decorator)) {
  217. return (new $decorator)->pay($request);
  218. }
  219. }
  220. public function purchase_payment_done($payment_data, $payment)
  221. {
  222. $seller = Auth::user()->shop;
  223. $seller->seller_package_id = Session::get('payment_data')['seller_package_id'];
  224. $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
  225. $seller->product_upload_limit = $seller_package->product_upload_limit;
  226. $seller->package_invalid_at = date('Y-m-d', strtotime($seller->package_invalid_at . ' +' . $seller_package->duration . 'days'));
  227. $seller->save();
  228. $seller_package = new SellerPackagePayment;
  229. $seller_package->user_id = Auth::user()->id;
  230. $seller_package->seller_package_id = Session::get('payment_data')['seller_package_id'];
  231. $seller_package->payment_method = Session::get('payment_data')['payment_method'];
  232. $seller_package->payment_details = $payment;
  233. $seller_package->approval = 1;
  234. $seller_package->offline_payment = 2;
  235. $seller_package->save();
  236. flash(translate('Package purchasing successful'))->success();
  237. return redirect()->route('seller.dashboard');
  238. }
  239. public function unpublish_products(Request $request)
  240. {
  241. foreach (Shop::all() as $shop) {
  242. if ($shop->package_invalid_at != null && Carbon::now()->diffInDays(Carbon::parse($shop->package_invalid_at), false) <= 0) {
  243. foreach ($shop->user->products as $product) {
  244. $product->published = 0;
  245. $product->save();
  246. }
  247. $shop->seller_package_id = null;
  248. $shop->save();
  249. }
  250. }
  251. }
  252. public function purchase_package_offline(Request $request)
  253. {
  254. $seller_package = SellerPackage::findOrFail($request->package_id);
  255. if (Auth::user()->shop->seller_package != null && $seller_package->product_upload_limit < Auth::user()->shop->seller_package->product_upload_limit) {
  256. flash(translate('You have more uploaded products than this package limit. You need to remove excessive products to downgrade.'))->warning();
  257. return redirect()->route('seller.seller_packages_list');
  258. }
  259. $seller_package = new SellerPackagePayment;
  260. $seller_package->user_id = Auth::user()->id;
  261. $seller_package->seller_package_id = $request->package_id;
  262. $seller_package->payment_method = $request->payment_option;
  263. $seller_package->payment_details = $request->trx_id;
  264. $seller_package->approval = 0;
  265. $seller_package->offline_payment = 1;
  266. $seller_package->reciept = $request->photo;
  267. $seller_package->save();
  268. flash(translate('Offline payment has been done. Please wait for response.'))->success();
  269. return redirect()->route('seller.products');
  270. }
  271. }