Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.94 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers\Shop;
  4.  
  5. use Illuminate\Http\Request;
  6.  
  7. use App\Http\Controllers\Controller;
  8.  
  9. use Illuminate\Support\Facades\Auth;
  10.  
  11. use App\Models\UserCart;
  12. use App\Models\UserCoupon;
  13. use App\Models\UserCartEntry;
  14. use App\Models\UserCouponCheckout;
  15. use App\Models\UserCartShopping;
  16. use App\Models\Product;
  17. use App\Models\UserOrder;
  18. use App\Models\Coupon;
  19. use App\Models\Setting;
  20. use App\Models\ProductItem;
  21. use App\Models\DeliveryMethod;
  22.  
  23. class CartController extends Controller
  24. {
  25. public function __construct() {
  26. $this->middleware('auth');
  27. }
  28.  
  29. public function checkoutSubmit(Request $request) {
  30. if($request->getMethod() == 'POST') {
  31. if(!UserCart::isEmpty(Auth::user()->id)) {
  32. $extraCosts = 0;
  33. $deliveryMethodName = '';
  34. $deliveryMethodPrice = 0;
  35.  
  36. // DELIVERY METHOD
  37. if(UserCart::hasDroplestProducts(Auth::user()->id)) {
  38. $deliveryMethodId = $request->get('product_delivery_method') ?? 0;
  39. $deliveryMethod = DeliveryMethod::where('id', $deliveryMethodId)->get()->first();
  40.  
  41. if($deliveryMethod == null || !$deliveryMethod->isAvailableForUsersCart()) {
  42. return redirect()->route('checkout')->with([
  43. 'errorMessage' => __('frontend/shop.delivery_method_needed')
  44. ]);
  45. } else {
  46. $extraCosts += $deliveryMethod->price;
  47. $deliveryMethodName = $deliveryMethod->name;
  48. $deliveryMethodPrice = $deliveryMethod->price;
  49. }
  50. }
  51.  
  52. // DROP
  53. $dropInput = $request->get('product_drop') ?? '';
  54. if(UserCart::hasDroplestProducts(Auth::user()->id)) {
  55. if(strlen($dropInput) > 500) {
  56. return redirect()->route('checkout')->with([
  57. 'errorMessage' => __('frontend/shop.order_note_long', [
  58. 'charallowed' => 500
  59. ])
  60. ]);
  61. }
  62.  
  63. if(strlen($dropInput) <= 0) {
  64. return redirect()->route('checkout')->with([
  65. 'errorMessage' => __('frontend/shop.order_note_needed')
  66. ]);
  67. }
  68.  
  69. $dropInput = encrypt($request->get('product_drop'));
  70. }
  71.  
  72. $cartTotal = UserCart::getCartSubInCent(Auth::user()->id);
  73. $total = $cartTotal;
  74.  
  75. if(count(Auth::user()->getCheckoutCoupons()) > 0) {
  76. $userCouponCheckout = UserCouponCheckout::where('user_id', Auth::user()->id)->get()->first();
  77.  
  78. if($userCouponCheckout != null) {
  79. $coupon = Coupon::where('code', $userCouponCheckout->coupon_code)->get()->first();
  80.  
  81. if($coupon != null) {
  82. $total = $coupon->toPay($total);
  83. }
  84. }
  85.  
  86. $total = $total + $extraCosts;
  87. }
  88.  
  89. if(Auth::user()->balance_in_cent >= $total) {
  90. if($coupon != null) {
  91. $coupon->update([
  92. 'used' => $coupon->used + 1
  93. ]);
  94.  
  95. UserCoupon::create([
  96. 'user_id' => Auth::user()->id,
  97. 'counpon_id' => $coupon->id
  98. ]);
  99. }
  100.  
  101. $createShopping = false;
  102. $cartEntries = [];
  103.  
  104. foreach(UserCart::getCartByUserId(Auth::user()->id) as $cartItem) {
  105. if($cartItem[0] == null) {
  106. return redirect()->route('checkout')->with([
  107. 'errorMessage' => __('frontend/v4.cart_error1')
  108. ]);
  109. }
  110.  
  111. if(!$cartItem[0]->isUnlimited() && !$cartItem[0]->isAvailableAmount($cartItem[1])) {
  112. return redirect()->route('checkout')->with([
  113. 'errorMessage' => __('frontend/v4.cart_error2')
  114. ]);
  115. }
  116.  
  117. // HISTORY
  118. $product = $cartItem[0];
  119.  
  120. $status = 'nothing';
  121. $dropInfo = '';
  122. $deliveryMethodNameX = '';
  123. $deliveryMethodPriceX = 0;
  124.  
  125. if($product->dropNeeded()) {
  126. $status = 'pending';
  127. $dropInfo = $dropInput;
  128.  
  129. $deliveryMethodNameX = $deliveryMethodName;
  130. $deliveryMethodPriceX = $deliveryMethodPrice;
  131. }
  132.  
  133. if($product->isUnlimited()) {
  134. $order = UserOrder::create([
  135. 'user_id' => Auth::user()->id,
  136. 'name' => $product->name,
  137. 'content' => $product->content,
  138. 'amount' => $cartItem[1],
  139. 'price_in_cent' => $product->price_in_cent,
  140. 'totalprice' => $cartItem[2],
  141. 'drop_info' => $dropInfo,
  142. 'delivery_price' => $deliveryMethodPriceX,
  143. 'delivery_method' => $deliveryMethodNameX,
  144. 'status' => $status,
  145. 'weight' => 0,
  146. 'weight_char' => ''
  147. ]);
  148.  
  149. if($product->dropNeeded()) {
  150. if($order != null) {
  151. $createShopping = true;
  152. $cartEntries[] = [
  153. 'product_id' => $product->id,
  154. 'order_id' => $order->id,
  155. 'user_id' => Auth::user()->id
  156. ];
  157. }
  158. }
  159.  
  160. $product->update([
  161. 'sells' => $product->sells + $cartItem[1]
  162. ]);
  163.  
  164. Setting::set('shop.total_sells', Setting::get('shop.total_sells', 0) + $cartItem[1]);
  165. } else if($product->asWeight()) {
  166. $order = UserOrder::create([
  167. 'user_id' => Auth::user()->id,
  168. 'name' => $product->name,
  169. 'amount' => 1,
  170. 'content' => $product->content,
  171. 'weight' => $cartItem[1],
  172. 'weight_char' => $product->getWeightChar(),
  173. 'price_in_cent' => $product->price_in_cent,
  174. 'totalprice' => $cartItem[2],
  175. 'drop_info' => $dropInfo,
  176. 'delivery_price' => $deliveryMethodPriceX,
  177. 'delivery_method' => $deliveryMethodNameX,
  178. 'status' => $status
  179. ]);
  180.  
  181. if($product->dropNeeded()) {
  182. if($order != null) {
  183. $createShopping = true;
  184. $cartEntries[] = [
  185. 'product_id' => $product->id,
  186. 'order_id' => $order->id,
  187. 'user_id' => Auth::user()->id
  188. ];
  189. }
  190. }
  191.  
  192. $product->update([
  193. 'sells' => $product->sells + $cartItem[1],
  194. 'weight_available' => $product->weight_available - $cartItem[1]
  195. ]);
  196.  
  197. Setting::set('shop.total_sells', Setting::get('shop.total_sells', 0) + 1);
  198. } else {
  199. for($i = 0; $i < $cartItem[1]; $i++) {
  200. $productItem = ProductItem::where('product_id', $product->id)->get()->first();
  201. $productContent = $productItem->content;
  202. $productItem->delete();
  203.  
  204. $order = UserOrder::create([
  205. 'user_id' => Auth::user()->id,
  206. 'name' => $product->name,
  207. 'amount' => 1,
  208. 'content' => $productContent,
  209. 'price_in_cent' => $product->price_in_cent,
  210. 'totalprice' => $product->price_in_cent,
  211. 'weight' => 0,
  212. 'weight_char' => '',
  213. 'status' => $status,
  214. 'delivery_price' => $deliveryMethodPriceX,
  215. 'delivery_method' => $deliveryMethodNameX,
  216. 'drop_info' => $dropInfo
  217. ]);
  218.  
  219. if($product->dropNeeded()) {
  220. if($order != null) {
  221. $createShopping = true;
  222. $cartEntries[] = [
  223. 'product_id' => $product->id,
  224. 'order_id' => $order->id,
  225. 'user_id' => Auth::user()->id
  226. ];
  227. }
  228. }
  229.  
  230. $product->update([
  231. 'sells' => $product->sells + 1
  232. ]);
  233.  
  234. Setting::set('shop.total_sells', Setting::get('shop.total_sells', 0) + 1);
  235. }
  236. }
  237. }
  238.  
  239. if($createShopping && count($cartEntries) > 1) {
  240. $shopping = UserCartShopping::create([
  241. 'user_id' => Auth::user()->id
  242. ]);
  243.  
  244. if($shopping != null) {
  245. foreach($cartEntries as $cartEntry) {
  246. UserCartEntry::create([
  247. 'user_id' => $cartEntry['user_id'],
  248. 'order_id' => $cartEntry['order_id'],
  249. 'product_id' => $cartEntry['product_id'],
  250. 'shopping_id' => $shopping->id
  251. ]);
  252. }
  253. }
  254. }
  255.  
  256. // CLEAR CART
  257.  
  258. UserCart::where([
  259. ['user_id', '=', Auth::user()->id]
  260. ])->delete();
  261.  
  262. // SUCCESS PART
  263.  
  264. $newBalance = Auth::user()->balance_in_cent - $total;
  265.  
  266. // COUPON
  267. if(count(Auth::user()->getCheckoutCoupons()) > 0) {
  268. $userCouponCheckout = UserCouponCheckout::where('user_id', Auth::user()->id)->get()->first();
  269.  
  270. if($userCouponCheckout != null) {
  271. $coupon = Coupon::where('code', $userCouponCheckout->id)->get()->first();
  272.  
  273. if($coupon != null) {
  274. $coupon->update([
  275. 'used' => $coupon->used + 1
  276. ]);
  277.  
  278. UserCoupon::create([
  279. 'user_id' => Auth::user()->id,
  280. 'coupon_id' => $coupon->id
  281. ]);
  282. }
  283.  
  284. $userCouponCheckout->delete();
  285. }
  286. }
  287.  
  288. Auth::user()->update([
  289. 'balance_in_cent' => $newBalance
  290. ]);
  291.  
  292. return redirect()->route('orders')->with([
  293. 'successMessage' => __('frontend/v4.thank_you')
  294. ]);
  295. } else {
  296. return redirect()->route('checkout')->with([
  297. 'errorMessage' => __('frontend/shop.not_enought_money')
  298. ]);
  299. }
  300. }
  301. }
  302.  
  303. return redirect()->route('cart');
  304. }
  305.  
  306. public function checkout() {
  307. if(UserCart::isEmpty(Auth::user()->id)) {
  308. return redirect()->route('cart');
  309. }
  310.  
  311. return view('frontend/shop.checkout', [
  312. 'cart' => UserCart::getCartByUserId(Auth::user()->id)
  313. ]);
  314. }
  315.  
  316. public function clear() {
  317. UserCart::where([
  318. ['user_id', '=', Auth::user()->id]
  319. ])->delete();
  320.  
  321. return redirect()->route('cart');
  322. }
  323.  
  324. public function delete($product_id) {
  325. UserCart::where([
  326. ['product_id', '=', $product_id],
  327. ['user_id', '=', Auth::user()->id]
  328. ])->delete();
  329.  
  330. return redirect()->route('cart');
  331. }
  332.  
  333. public function cart(Request $request) {
  334. echo __('frontend/v4.cart_widget', [
  335. 'count' => UserCart::getCartCountByUserId(Auth::user()->id),
  336. 'price' => UserCart::getCartSubPrice(Auth::user()->id)
  337. ]);
  338. }
  339.  
  340. public function ajaxAddItem(Request $request) {
  341. if($request->getMethod() == 'POST') {
  342. $productId = intval($request->get('product_id') ?? '0');
  343. $amount = intval($request->get('amount') ?? '0');
  344.  
  345. $product = Product::where('id', $productId)->get()->first();
  346.  
  347. if($product != null) {
  348. $userCartItem = UserCart::where('product_id', $productId)->get()->first();
  349.  
  350. if($userCartItem != null) {
  351. $newAmount = $userCartItem->amount + $amount;
  352.  
  353. $userCartItem->update([
  354. 'amount' => $newAmount
  355. ]);
  356. } else {
  357. UserCart::create([
  358. 'product_id' => $product->id,
  359. 'amount' => $amount,
  360. 'user_id' => Auth::user()->id
  361. ]);
  362. }
  363. }
  364. }
  365. }
  366.  
  367. public function show()
  368. {
  369. return view('frontend/shop.cart', [
  370. 'cart' => UserCart::getCartByUserId(Auth::user()->id)
  371. ]);
  372. }
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement