You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.1 KiB

5 days ago
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Activity extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'venue_id',
'title',
'summary',
'category',
'quota',
'registered_count',
'start_at',
'end_at',
'address',
'lat',
'lng',
'detail_html',
'cover_image',
'gallery_media',
'tags',
'reservation_notice',
'open_time',
'sort',
'is_active',
'booking_audience',
'total_quota',
'min_people_per_order',
'max_people_per_order',
];
protected $casts = [
'start_at' => 'datetime',
'end_at' => 'datetime',
'lat' => 'float',
'lng' => 'float',
'gallery_media' => 'array',
'tags' => 'array',
'sort' => 'integer',
'is_active' => 'boolean',
'total_quota' => 'integer',
'min_people_per_order' => 'integer',
'max_people_per_order' => 'integer',
];
public function venue(): BelongsTo
{
return $this->belongsTo(Venue::class);
}
public function reservations(): HasMany
{
return $this->hasMany(Reservation::class);
}
public function activityDays(): HasMany
{
return $this->hasMany(ActivityDay::class)->orderBy('activity_date');
}
/**
* 按未取消预约的票数合计,回写 activities.registered_count展示用「已预约总人数」
*/
public static function refreshRegisteredCountFromReservations(int $activityId): void
{
$total = (int) Reservation::query()
->where('activity_id', $activityId)
->where('status', '!=', 'cancelled')
->get()
->sum(fn (Reservation $r) => max(1, (int) ($r->ticket_count ?? 1)));
static::query()->where('id', $activityId)->update(['registered_count' => $total]);
}
}