Advertisement
Guest User

Untitled

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