Edit File: CartService.php
<?php namespace App\Services; use App\Http\Resources\CartDetailsResource; use App\Models\Cart; use App\Models\ProductAdditive; use App\Models\ProductAdditiveCategory; use App\Models\Setting; use App\Traits\ApiTrait; use App\Traits\GeneralTrait; use App\Models\ProductGroup; class CartService { use ApiTrait, GeneralTrait; public function detailCarts($id) { $count = Cart::where('user_id', auth('api')->id())->where('store_id', $id)->count(); $ids = Cart::where('user_id', auth('api')->id())->where('store_id', $id)->get(); $price = 0; foreach ($ids as $id) { $price += ($id->group->price() * $id->qty); if ($id->additives) { foreach (json_decode($id->additives) as $ad_id) { $additive = ProductAdditiveCategory::find($ad_id); $price += $additive->price * $id->qty; } } } $added_value = $this->addedValue($price); return [ 'count' => $count, 'price' => number_format($price , 2), 'added_value' => number_format($added_value , 2 , '.' , ''), 'total_price' => number_format(($price + $added_value) , 2), 'num_total' => $price + $added_value, ]; } public function addedValue($price) { $added_value = Setting::where('key', 'added_value')->first()->value; return $price * ($added_value / 100) ; } public function getSingleCart($store_id) { $cart = Cart::where('user_id', auth()->id())->where('store_id', $store_id)->get(); $price = $this->detailCarts($store_id); $data['store_id'] = $store_id; $data['count'] = $cart->count(); $data['cart'] = CartDetailsResource::collection($cart); $data['price'] = [ 'price' => $price['price'], 'added_value' => $price['added_value'], 'total_price' => $price['total_price'], ]; return $data; } public function deleteCart($id) { $cart = Cart::where('user_id', auth()->id())->where('group_id', $id)->first(); $store_id = $cart->store_id; if (!$cart) { return $this->failMsg(__('order.cart_not_found')); } $cart->delete(); $cart = Cart::where('user_id', auth()->id())->where('store_id', $store_id)->get(); $price = $this->detailCarts($store_id); $data['count'] = $cart->count(); $data['store_id'] = $store_id; $data['cart'] = CartDetailsResource::collection($cart); $data['price'] = [ 'price' => $price['price'], 'added_value' => $price['added_value'], 'total_price' => $price['total_price'], ]; return $this->dataReturn($data); } public function updateCart($request) { $cart = Cart::where('user_id', auth()->id())->where('group_id', $request->group_id)->first(); if (!$cart && $request->store_id) { $cart = Cart::where('user_id', auth()->id())->where('store_id', $request->store_id)->first(); } if (!$cart){ return $this->failMsg(__('order.cart_not_found')); } $store_id = $cart->store_id; $cart->update($request->validated()); $cart = Cart::where('user_id', auth()->id())->where('store_id', $store_id)->get(); $price = $this->detailCarts($store_id); $data['count'] = $cart->count(); $data['store_id'] = $store_id; $data['cart'] = CartDetailsResource::collection($cart); $data['price'] = [ 'price' => $price['price'], 'added_value' => $price['added_value'], 'discount' => '0.0', 'total_price' => $price['total_price'] , ]; if (isset($request['coupon'])) { $coupon_service = new CouponService(); $coupon_service_data = ['coupon' => isset($request['coupon']) ? $request['coupon'] : '', 'delivery_price' => $price['total_price']]; $discount = $coupon_service->enquiryCoupon($coupon_service_data); if ($discount) { $data['price'] = [ 'price' => $price['price'], 'added_value' => $price['added_value'], 'discount' => number_format($discount , 2), 'total_price' => number_format(($price['num_total'] - $discount) ,2), ]; } } return $this->dataReturn($data); } public function checkUserCartOrder($carts , $payment_type) { $price = 0; $msg = ''; $key = 'success'; if ($carts->count() == 0){ $msg = __('order.cart_not_found'); $data['fail_msg'] = $msg; return $data; } foreach($carts as $cart){ $product = ProductGroup::find($cart->group_id); if(!$product){ $msg = trans('order.products_not_available'); $data['fail_msg'] = $msg; return $data; } if ($cart->additives){ foreach(json_decode($cart->additives) as $id){ $additive = ProductAdditiveCategory::find($id); if(!$additive){ $msg = trans('order.products_not_available'); $data['fail_msg'] = $msg; return $data; } $price += $additive->price * $cart->qty; } } $price += $product->price() * $cart->qty; } if($payment_type == 'wallet' && auth('api')->user()->wallet < $price){ $msg = trans('order.walletHasNoEnouphCredit'); $key = 'fail_msg'; } return [ 'key' => $key, 'msg' => $msg, 'price' => $price, ]; } }
Back to File Manager