SellerSpreadPackageController.php 13 KB

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