Advertisement
Guest User

Untitled

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