/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/Models/CartItem.php
156 строк
3 KB
vivanenko
fix taps and others structure
27 май 2025, 18:06
27 май 2025, 18:06
8cdc7d9
Код
Авторство
О чём код?
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\Factories\HasFactory; /** * @property int $id * @property int $user_id * @property int|null $cart_id * @property int|null $order_id * @property string $sku * @property float $price * @property float $tax * @property float $shipping * @property string $currency * @property int $quantity * @property string $itemable_type * @property int $itemable_id * @property \DateTime $created_at * @property \DateTime $updated_at */ class CartItem extends Model { use HasFactory; protected $fillable = [ 'user_id', 'cart_id', 'order_id', 'sku', 'price', 'tax', 'shipping', 'currency', 'quantity', 'itemable_type', 'itemable_id', ]; protected $casts = [ 'user_id' => 'integer', 'cart_id' => 'integer', 'order_id' => 'integer', 'price' => 'decimal:2', 'tax' => 'decimal:2', 'shipping' => 'decimal:2', 'quantity' => 'integer', 'itemable_id' => 'integer', ]; public function user(): BelongsTo { return $this->belongsTo(User::class); } public function cart(): BelongsTo { return $this->belongsTo(Cart::class); } public function order(): BelongsTo { return $this->belongsTo(Order::class); } public function itemable(): MorphTo { return $this->morphTo(); } /** * Get item subtotal (price * quantity) */ public function getSubtotal(): float { return $this->price * $this->quantity; } /** * Get item total with tax and shipping */ public function getTotal(): float { return $this->getSubtotal() + $this->tax + $this->shipping; } /** * Generate SKU for tariff */ public static function generateSku(string $itemableType, int $itemableId): string { $prefix = match ($itemableType) { Tariff::class => 'TARIFF', default => 'ITEM', }; return sprintf('%s_%d_%s', $prefix, $itemableId, time()); } /** * Check if item is for tariff */ public function isTariff(): bool { return $this->itemable_type === Tariff::class; } /** * Get quantity discount for this item */ public function getQuantityDiscount(): float { if ($this->quantity >= 5) { $freeTariffs = intval($this->quantity / 5); return $freeTariffs * $this->price; } return 0.0; } /** * Get effective quantity (paid quantity after discount) */ public function getEffectiveQuantity(): int { if ($this->quantity >= 5) { $freeTariffs = intval($this->quantity / 5); return $this->quantity - $freeTariffs; } return $this->quantity; } /** * Get free quantity from discount */ public function getFreeQuantity(): int { return $this->quantity >= 5 ? intval($this->quantity / 5) : 0; } /** * Move item from cart to order */ public function moveToOrder(int $orderId): bool { $this->order_id = $orderId; $this->cart_id = null; return $this->save(); } }