mein-sterntours/app/Models/OfferItem.php

99 lines
3.1 KiB
PHP

<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* Eine einzelne Positionszeile einer Angebotsversion (Leistung, Option,
* Rabatt, Versicherung, …). Positionen sind versionsgebunden — eine neue
* Version hat ihre eigenen Zeilen.
*
* `travel_program_id` und `fewo_lodging_id` bleiben aktuell OHNE
* Foreign-Key, solange die v2-Reiseverwaltung noch nicht nach Laravel
* migriert ist (siehe Risiko R4 im Entwicklungsplan). Ein Snapshot
* relevanter Quelldaten wird zusätzlich in `metadata` gespeichert,
* damit Angebote auch nach einem v2-Schema-Wechsel lesbar bleiben.
*
* @property int $id
* @property int $offer_version_id
* @property int $position
* @property string $type travel|service|option|discount|insurance|custom
* @property string $title
* @property string|null $description
* @property int $quantity
* @property float $price_per_unit
* @property float $total_price
* @property int|null $travel_program_id
* @property int|null $fewo_lodging_id
* @property array|null $metadata
* @property Carbon $created_at
* @property Carbon $updated_at
* @property-read OfferVersion $offerVersion
*/
class OfferItem extends Model
{
use HasFactory;
public const TYPE_TRAVEL = 'travel';
public const TYPE_SERVICE = 'service';
public const TYPE_OPTION = 'option';
public const TYPE_DISCOUNT = 'discount';
public const TYPE_INSURANCE = 'insurance';
public const TYPE_CUSTOM = 'custom';
public const TYPES = [
self::TYPE_TRAVEL,
self::TYPE_SERVICE,
self::TYPE_OPTION,
self::TYPE_DISCOUNT,
self::TYPE_INSURANCE,
self::TYPE_CUSTOM,
];
protected $connection = 'mysql';
protected $table = 'offer_items';
protected $fillable = [
'offer_version_id',
'position',
'type',
'title',
'description',
'quantity',
'price_per_unit',
'total_price',
'travel_program_id',
'fewo_lodging_id',
'metadata',
];
protected $casts = [
'offer_version_id' => 'int',
'position' => 'int',
'quantity' => 'int',
'travel_program_id' => 'int',
'fewo_lodging_id' => 'int',
'price_per_unit' => 'decimal:2',
'total_price' => 'decimal:2',
'metadata' => 'array',
];
public function offerVersion(): BelongsTo
{
return $this->belongsTo(OfferVersion::class, 'offer_version_id');
}
/**
* Berechnet `total_price` aus `quantity * price_per_unit` — wird vom
* OfferService vor dem Speichern aufgerufen, damit UI-Aggregate
* und DB-Werte konsistent bleiben.
*/
public function recomputeTotal(): void
{
$this->total_price = round($this->quantity * (float) $this->price_per_unit, 2);
}
}