/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/Services/OldCartService.php
250 строк
8 KB
vivanenko
fix taps and others structure
27 май 2025, 18:06
27 май 2025, 18:06
8cdc7d9
Код
Авторство
О чём код?
<?php namespace App\Services; use App\Contracts\CartRepositoryInterface; use App\DTOs\CartData; use App\Models\Tariff; use App\Models\User; use Binafy\LaravelCart\Models\CartItem; use Illuminate\Database\Eloquent\Collection; class CartService { public function __construct( private readonly CartRepositoryInterface $cartRepository ) {} /** * Add tariff to user's cart */ public function addTariffToCart(CartData $data): array { $data->validateForAdd(); // Check if user exists $user = User::find($data->user_id); if (!$user) { throw new \InvalidArgumentException('User not found'); } // Check if tariff exists and is active $tariff = Tariff::where('id', $data->tariff_id) ->where('is_active', true) ->first(); if (!$tariff) { throw new \InvalidArgumentException('Tariff not found or inactive'); } // Add to cart $cartItem = $this->cartRepository->addTariffToCart( $data->user_id, $tariff, $data->quantity ); return $this->formatCartItemResponse($cartItem); } /** * Remove tariff from user's cart */ public function removeTariffFromCart(int $userId, int $tariffId): bool { if (!User::find($userId)) { throw new \InvalidArgumentException('User not found'); } return $this->cartRepository->removeTariffFromCart($userId, $tariffId); } /** * Update tariff quantity in cart */ public function updateTariffQuantity(CartData $data): array { $data->validateForUpdate(); // Check if user exists if (!User::find($data->user_id)) { throw new \InvalidArgumentException('User not found'); } // Check if tariff exists in cart if (!$this->cartRepository->hasTariffInCart($data->user_id, $data->tariff_id)) { throw new \InvalidArgumentException('Tariff not found in cart'); } // Update quantity $updated = $this->cartRepository->updateTariffQuantity( $data->user_id, $data->tariff_id, $data->quantity ); if (!$updated) { throw new \RuntimeException('Failed to update cart item'); } // Get updated cart summary return $this->getCartSummary($data->user_id); } /** * Get user's cart items */ public function getCartItems(int $userId): array { if (!User::find($userId)) { throw new \InvalidArgumentException('User not found'); } $items = $this->cartRepository->getCartItems($userId); $subtotal = $this->cartRepository->calculateCartSubtotal($userId); $quantityDiscount = $this->calculateQuantityDiscount($userId); $total = $subtotal - $quantityDiscount; return [ 'items' => $items->map(fn($item) => $this->formatCartItemResponse($item)), 'subtotal' => $subtotal, 'quantity_discount' => $quantityDiscount, 'total' => $total, 'count' => $this->cartRepository->getCartItemsCount($userId), ]; } /** * Get cart summary (total, count, etc.) */ public function getCartSummary(int $userId): array { if (!User::find($userId)) { throw new \InvalidArgumentException('User not found'); } $subtotal = $this->cartRepository->calculateCartSubtotal($userId); $quantityDiscount = $this->calculateQuantityDiscount($userId); $total = $subtotal - $quantityDiscount; return [ 'subtotal' => $subtotal, 'quantity_discount' => $quantityDiscount, 'total' => $total, 'count' => $this->cartRepository->getCartItemsCount($userId), 'items_count' => $this->cartRepository->getCartItems($userId)->count(), ]; } /** * Clear user's cart */ public function clearCart(int $userId): bool { if (!User::find($userId)) { throw new \InvalidArgumentException('User not found'); } return $this->cartRepository->clearCart($userId); } /** * Check if tariff is in cart */ public function hasTariffInCart(int $userId, int $tariffId): bool { return $this->cartRepository->hasTariffInCart($userId, $tariffId); } /** * Get available tariffs for cart (active tariffs) */ public function getAvailableTariffs(): Collection { return Tariff::where('is_active', true) ->orderBy('price', 'asc') ->get(); } /** * Prepare cart for checkout * Returns cart data formatted for purchase creation */ public function prepareCartForCheckout(int $userId): array { $cartItems = $this->cartRepository->getCartItems($userId); if ($cartItems->isEmpty()) { throw new \InvalidArgumentException('Cart is empty'); } $subtotal = $this->cartRepository->calculateCartSubtotal($userId); $quantityDiscount = $this->calculateQuantityDiscount($userId); $total = $subtotal - $quantityDiscount; return [ 'user_id' => $userId, 'items' => $cartItems->map(function ($item) { $quantity = $item->quantity; $freeTariffs = $quantity >= 5 ? intval($quantity / 5) : 0; return [ 'tariff_id' => $item->itemable_id, 'tariff_name' => $item->itemable->name, 'quantity' => $quantity, 'free_quantity' => $freeTariffs, 'price' => $item->itemable->getPrice(), 'subtotal' => $quantity * $item->itemable->getPrice(), 'discount' => $freeTariffs * $item->itemable->getPrice(), ]; }), 'subtotal_amount' => $subtotal, 'quantity_discount' => $quantityDiscount, 'total_amount' => $total, 'items_count' => $cartItems->count(), ]; } /** * Calculate quantity discount (Buy 5 get 1 free) */ private function calculateQuantityDiscount(int $userId): float { $cartItems = $this->cartRepository->getCartItems($userId); $totalDiscount = 0.0; foreach ($cartItems as $item) { $quantity = $item->quantity; if ($quantity >= 5) { $freeTariffs = intval($quantity / 5); $tariffPrice = $item->itemable->getPrice(); $totalDiscount += $freeTariffs * $tariffPrice; } } return $totalDiscount; } /** * Format cart item response */ private function formatCartItemResponse(CartItem $cartItem): array { return [ 'id' => $cartItem->id, 'tariff_id' => $cartItem->itemable_id, 'tariff' => [ 'id' => $cartItem->itemable->id, 'name' => $cartItem->itemable->name, 'description' => $cartItem->itemable->description, 'duration_days' => $cartItem->itemable->duration_days, 'price' => $cartItem->itemable->getPrice(), 'is_active' => $cartItem->itemable->is_active, ], 'quantity' => $cartItem->quantity, 'subtotal' => $cartItem->quantity * $cartItem->itemable->getPrice(), 'created_at' => $cartItem->created_at, 'updated_at' => $cartItem->updated_at, ]; } }