mein-sterntours/app/Models/OfferTemplate.php

87 lines
2.4 KiB
PHP

<?php
namespace App\Models;
use App\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Vorlage (Blueprint) für neue Angebote — stellt Default-Texte und eine
* Default-Positionsliste (`default_items`, JSON) bereit. Beim Anlegen
* eines neuen Angebots über eine Vorlage werden diese Defaults in die
* erste OfferVersion kopiert.
*
* @property int $id
* @property int|null $branch_id
* @property string $name
* @property string|null $description
* @property string|null $default_headline
* @property string|null $default_intro
* @property string|null $default_itinerary
* @property string|null $default_closing
* @property array|null $default_items
* @property bool $is_active
* @property int $created_by
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Carbon|null $deleted_at
* @property-read Branch|null $branch
* @property-read User $createdBy
*/
class OfferTemplate extends Model
{
use HasFactory, SoftDeletes;
protected $connection = 'mysql';
protected $table = 'offer_templates';
protected $fillable = [
'branch_id',
'name',
'description',
'default_headline',
'default_intro',
'default_itinerary',
'default_closing',
'default_items',
'is_active',
'created_by',
];
protected $casts = [
'branch_id' => 'int',
'created_by' => 'int',
'is_active' => 'bool',
'default_items' => 'array',
];
public function branch(): BelongsTo
{
return $this->belongsTo(Branch::class, 'branch_id');
}
public function createdBy(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
public function scopeActive(Builder $query): Builder
{
return $query->where('is_active', true);
}
public function scopeForBranch(Builder $query, ?int $branchId): Builder
{
if ($branchId === null) {
return $query;
}
return $query->where(function (Builder $q) use ($branchId) {
$q->whereNull('branch_id')->orWhere('branch_id', $branchId);
});
}
}