Files
Verde-Web/app/Models/QrPurchaseOrder.php

72 lines
1.7 KiB
PHP

<?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\SoftDeletes;
use Illuminate\Support\Str;
class QrPurchaseOrder extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'uuid',
'resident_id',
'store_id',
'tenant_id',
'reservation_code',
'status',
'quantity',
'payment_method',
'amount',
'completed_at',
'qr_code_id',
];
protected function casts(): array
{
return [
'amount' => 'decimal:2',
'completed_at' => 'datetime',
'quantity' => 'integer',
];
}
protected static function booted(): void
{
static::creating(function (self $order): void {
if (empty($order->uuid)) {
$order->uuid = (string) Str::uuid();
}
if (empty($order->reservation_code)) {
// Generate a 6-character alphanumeric code
$order->reservation_code = strtoupper(Str::random(6));
}
});
}
public function resident(): BelongsTo
{
return $this->belongsTo(User::class, 'resident_id');
}
public function store(): BelongsTo
{
return $this->belongsTo(User::class, 'store_id');
}
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class);
}
// Optional: relation to QrCode if the QrCode model exists
// public function qrCode(): BelongsTo
// {
// return $this->belongsTo(QrCode::class);
// }
}