File size: 1,551 Bytes
10dc6f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Attendee extends Model
{
    protected $fillable = ['order_item_id', 'full_name', 'email', 'phone', 'ticket_code', 'checkin_at'];

    protected function casts(): array
    {
        return [
            'checkin_at' => 'datetime',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (Attendee $attendee) {
            if (empty($attendee->ticket_code)) {
                do {
                    $code = 'TKT-' . strtoupper(Str::random(8));
                } while (static::where('ticket_code', $code)->exists());
                $attendee->ticket_code = $code;
            }
        });
    }

    // ── Relationships ────────────────────────────────────

    public function orderItem()
    {
        return $this->belongsTo(OrderItem::class);
    }

    public function order()
    {
        return $this->hasOneThrough(Order::class, OrderItem::class, 'id', 'id', 'order_item_id', 'order_id');
    }

    // ── Helpers ──────────────────────────────────────────

    public function isCheckedIn(): bool
    {
        return $this->checkin_at !== null;
    }

    public function checkIn(): bool
    {
        if ($this->isCheckedIn()) return false;

        $this->update(['checkin_at' => now()]);
        return true;
    }
}