Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.83 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.  
  96. $createShopping = false;
  97. $cartEntries = [];
  98.  
  99. foreach(UserCart::getCartByUserId(Auth::user()->id) as $cartItem) {
  100. if($cartItem[0] == null) {
  101. return redirect()->route('checkout')->with([
  102. 'errorMessage' => __('frontend/v4.cart_error1')
  103. ]);
  104. }
  105.  
  106. if(!$cartItem[0]->isUnlimited() && !$cartItem[0]->isAvailableAmount($cartItem[1])) {
  107. return redirect()->route('checkout')->with([
  108. 'errorMessage' => __('frontend/v4.cart_error2')
  109. ]);
  110. }
  111.  
  112. // HISTORY
  113. $product = $cartItem[0];
  114.  
  115. $status = 'nothing';
  116. $dropInfo = '';
  117. $deliveryMethodNameX = '';
  118. $deliveryMethodPriceX = 0;
  119.  
  120. if($product->dropNeeded()) {
  121. $status = 'pending';
  122. $dropInfo = $dropInput;
  123.  
  124. $deliveryMethodNameX = $deliveryMethodName;
  125. $deliveryMethodPriceX = $deliveryMethodPrice;
  126. }
  127.  
  128. if($product->isUnlimited()) {
  129. $order = UserOrder::create([
  130. 'user_id' => Auth::user()->id,
  131. 'name' => $product->name,
  132. 'content' => $product->content,
  133. 'amount' => $cartItem[1],
  134. 'price_in_cent' => $product->price_in_cent,
  135. 'totalprice' => $cartItem[2],
  136. 'drop_info' => $dropInfo,
  137. 'delivery_price' => $deliveryMethodPriceX,
  138. 'delivery_method' => $deliveryMethodNameX,
  139. 'status' => $status,
  140. 'weight' => 0,
  141. 'weight_char' => ''
  142. ]);
  143.  
  144. if($product->dropNeeded()) {
  145. if($order != null) {
  146. $createShopping = true;
  147. $cartEntries[] = [
  148. 'product_id' => $product->id,
  149. 'order_id' => $order->id,
  150. 'user_id' => Auth::user()->id
  151. ];
  152. }
  153. }
  154.  
  155. $product->update([
  156. 'sells' => $product->sells + $cartItem[1]
  157. ]);
  158.  
  159. Setting::set('shop.total_sells', Setting::get('shop.total_sells', 0) + $cartItem[1]);
  160. } else if($product->asWeight()) {
  161. $order = UserOrder::create([
  162. 'user_id' => Auth::user()->id,
  163. 'name' => $product->name,
  164. 'amount' => 1,
  165. 'content' => $product->content,
  166. 'weight' => $cartItem[1],
  167. 'weight_char' => $product->getWeightChar(),
  168. 'price_in_cent' => $product->price_in_cent,
  169. 'totalprice' => $cartItem[2],
  170. 'drop_info' => $dropInfo,
  171. 'delivery_price' => $deliveryMethodPriceX,
  172. 'delivery_method' => $deliveryMethodNameX,
  173. 'status' => $status
  174. ]);
  175.  
  176. if($product->dropNeeded()) {
  177. if($order != null) {
  178. $createShopping = true;
  179. $cartEntries[] = [
  180. 'product_id' => $product->id,
  181. 'order_id' => $order->id,
  182. 'user_id' => Auth::user()->id
  183. ];
  184. }
  185. }
  186.  
  187. $product->update([
  188. 'sells' => $product->sells + $cartItem[1],
  189. 'weight_available' => $product->weight_available - $cartItem[1]
  190. ]);
  191.  
  192. Setting::set('shop.total_sells', Setting::get('shop.total_sells', 0) + 1);
  193. } else {
  194. for($i = 0; $i < $cartItem[1]; $i++) {
  195. $productItem = ProductItem::where('product_id', $product->id)->get()->first();
  196. $productContent = $productItem->content;
  197. $productItem->delete();
  198.  
  199. $order = UserOrder::create([
  200. 'user_id' => Auth::user()->id,
  201. 'name' => $product->name,
  202. 'amount' => 1,
  203. 'content' => $productContent,
  204. 'price_in_cent' => $product->price_in_cent,
  205. 'totalprice' => $product->price_in_cent,
  206. 'weight' => 0,
  207. 'weight_char' => '',
  208. 'status' => $status,
  209. 'delivery_price' => $deliveryMethodPriceX,
  210. 'delivery_method' => $deliveryMethodNameX,
  211. 'drop_info' => $dropInfo
  212. ]);
  213.  
  214. if($product->dropNeeded()) {
  215. if($order != null) {
  216. $createShopping = true;
  217. $cartEntries[] = [
  218. 'product_id' => $product->id,
  219. 'order_id' => $order->id,
  220. 'user_id' => Auth::user()->id
  221. ];
  222. }
  223. }
  224.  
  225. $product->update([
  226. 'sells' => $product->sells + 1
  227. ]);
  228.  
  229. Setting::set('shop.total_sells', Setting::get('shop.total_sells', 0) + 1);
  230. }
  231. }
  232. }
  233.  
  234. if($createShopping && count($cartEntries) > 1) {
  235. $shopping = UserCartShopping::create([
  236. 'user_id' => Auth::user()->id
  237. ]);
  238.  
  239. if($shopping != null) {
  240. foreach($cartEntries as $cartEntry) {
  241. UserCartEntry::create([
  242. 'user_id' => $cartEntry['user_id'],
  243. 'order_id' => $cartEntry['order_id'],
  244. 'product_id' => $cartEntry['product_id'],
  245. 'shopping_id' => $shopping->id
  246. ]);
  247. }
  248. }
  249. }
  250.  
  251. // CLEAR CART
  252.  
  253. UserCart::where([
  254. ['user_id', '=', Auth::user()->id]
  255. ])->delete();
  256.  
  257. // SUCCESS PART
  258.  
  259. $newBalance = Auth::user()->balance_in_cent - $total;
  260.  
  261. // COUPON
  262. if(count(Auth::user()->getCheckoutCoupons()) > 0) {
  263. $userCouponCheckout = UserCouponCheckout::where('user_id', Auth::user()->id)->get()->first();
  264.  
  265. if($userCouponCheckout != null) {
  266. $coupon = Coupon::where('code', $userCouponCheckout->id)->get()->first();
  267.  
  268. if($coupon != null) {
  269. $coupon->update([
  270. 'used' => $coupon->used + 1
  271. ]);
  272.  
  273. UserCoupon::create([
  274. 'user_id' => Auth::user()->id,
  275. 'coupon_id' => $coupon->id
  276. ]);
  277. }
  278.  
  279. $userCouponCheckout->delete();
  280. }
  281. }
  282.  
  283. Auth::user()->update([
  284. 'balance_in_cent' => $newBalance
  285. ]);
  286.  
  287. return redirect()->route('orders')->with([
  288. 'successMessage' => __('frontend/v4.thank_you')
  289. ]);
  290. } else {
  291. return redirect()->route('checkout')->with([
  292. 'errorMessage' => __('frontend/shop.not_enought_money')
  293. ]);
  294. }
  295. }
  296. }
  297.  
  298. return redirect()->route('cart');
  299. }
  300.  
  301. public function checkout() {
  302. if(UserCart::isEmpty(Auth::user()->id)) {
  303. return redirect()->route('cart');
  304. }
  305.  
  306. return view('frontend/shop.checkout', [
  307. 'cart' => UserCart::getCartByUserId(Auth::user()->id)
  308. ]);
  309. }
  310.  
  311. public function clear() {
  312. UserCart::where([
  313. ['user_id', '=', Auth::user()->id]
  314. ])->delete();
  315.  
  316. return redirect()->route('cart');
  317. }
  318.  
  319. public function delete($product_id) {
  320. UserCart::where([
  321. ['product_id', '=', $product_id],
  322. ['user_id', '=', Auth::user()->id]
  323. ])->delete();
  324.  
  325. return redirect()->route('cart');
  326. }
  327.  
  328. public function cart(Request $request) {
  329. echo __('frontend/v4.cart_widget', [
  330. 'count' => UserCart::getCartCountByUserId(Auth::user()->id),
  331. 'price' => UserCart::getCartSubPrice(Auth::user()->id)
  332. ]);
  333. }
  334.  
  335. public function ajaxAddItem(Request $request) {
  336. if($request->getMethod() == 'POST') {
  337. $productId = intval($request->get('product_id') ?? '0');
  338. $amount = intval($request->get('amount') ?? '0');
  339.  
  340. $product = Product::where('id', $productId)->get()->first();
  341.  
  342. if($product != null) {
  343. $userCartItem = UserCart::where('product_id', $productId)->get()->first();
  344.  
  345. if($userCartItem != null) {
  346. $newAmount = $userCartItem->amount + $amount;
  347.  
  348. $userCartItem->update([
  349. 'amount' => $newAmount
  350. ]);
  351. } else {
  352. UserCart::create([
  353. 'product_id' => $product->id,
  354. 'amount' => $amount,
  355. 'user_id' => Auth::user()->id
  356. ]);
  357. }
  358. }
  359. }
  360. }
  361.  
  362. public function show()
  363. {
  364. return view('frontend/shop.cart', [
  365. 'cart' => UserCart::getCartByUserId(Auth::user()->id)
  366. ]);
  367. }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement