/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/Models/Tariff.php
135 строк
3 KB
vivanenko
fix subsriptions
29 май 2025, 14:09
29 май 2025, 14:09
f726f40
Код
Авторство
О чём код?
<?php namespace App\Models; use DateTime; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\SoftDeletes; /** * @property string $name The unique name/identifier of the tariff * @property string|null $description The description of what the tariff includes * @property int $duration_days The duration of the tariff in days * @property float $price The price of the tariff * @property bool $is_active Whether the tariff is currently available for purchase * @property DateTime $created_at When the tariff was created * @property DateTime $updated_at When the tariff was last updated * @property DateTime|null $deleted_at When the tariff was soft deleted */ class Tariff extends Model { use HasFactory, SoftDeletes; protected $fillable = [ 'name', 'description', 'duration_days', 'price', 'is_active', 'profitability', 'parent_id', 'type', ]; protected $casts = [ 'price' => 'decimal:2', 'profitability' => 'decimal:2', 'is_active' => 'boolean', 'duration_days' => 'integer', 'parent_id' => 'integer', 'type' => 'string', ]; public function tapApps(): BelongsToMany { return $this->belongsToMany(TapApp::class, 'tariff_tap_apps') ->withTimestamps(); } public function subscriptions(): HasMany { return $this->hasMany(UserTariffSubscription::class); } public function getName(): string { return $this->name; } public function getDescription(): ?string { return $this->description; } public function getDurationDays(): int { return $this->duration_days; } /** * Implementation of Cartable interface * Required by Laravel Cart package */ public function getPrice(): float { return (float) $this->price; } public function getIsActive(): bool { return $this->is_active; } public function setName(string $name): self { if (empty(trim($name))) { throw new \InvalidArgumentException('Tariff name cannot be empty'); } $this->name = $name; return $this; } public function setDescription(?string $description): self { $this->description = $description; return $this; } public function setDurationDays(int $days): self { if ($days < 1) { throw new \InvalidArgumentException('Duration must be at least 1 day'); } $this->duration_days = $days; return $this; } public function setPrice(float $price): self { if ($price < 0) { throw new \InvalidArgumentException('Price cannot be negative'); } $this->price = $price; return $this; } public function setIsActive(bool $isActive): self { $this->is_active = $isActive; return $this; } public function parent() { return $this->belongsTo(self::class, 'parent_id'); } public function children() { return $this->hasMany(self::class, 'parent_id'); } }