qr_code_batches + qr_codes tables. State machine via
spatie/laravel-model-states (unassigned → allocated → active → used,
plus expired/voided). Serial format PH-{area}-{YYMM}-{batch}-{code}-
{checksum} with 2-char SHA-256-derived checksum. BatchGenerator
bulk-inserts in chunks. QrAllocator allocates free codes to verified
households (prefers area-targeted batch, falls back to any). Real
HouseholdVerified listener replaces the placeholder. QrBalanceLow
event for Module 11.
Admin: batch CRUD, mark-printed, void code, lifecycle search, PDF
print sheet (24/A4 via dompdf + endroid/qr-code + picqer/barcode).
Resident: list own codes, balance with low_balance flag, activate
allocated codes.
Removed explicit Event::listen for HouseholdVerified — Laravel 11
auto-discovers it via handle() type-hint, and double-registering was
causing double-dispatch.
130 feature tests passing.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
68 lines
1.5 KiB
PHP
68 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\States\QrCode\QrCodeState;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Spatie\ModelStates\HasStates;
|
|
|
|
class QrCode extends Model
|
|
{
|
|
use HasFactory, HasStates;
|
|
|
|
protected $fillable = [
|
|
'serial',
|
|
'barcode_value',
|
|
'batch_id',
|
|
'status',
|
|
'assigned_to_household_id',
|
|
'assigned_to_store_id',
|
|
'allocated_at',
|
|
'activated_at',
|
|
'used_at',
|
|
'used_at_drop_off_id',
|
|
'scanned_by_user_id',
|
|
'expires_at',
|
|
'metadata',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'status' => QrCodeState::class,
|
|
'metadata' => 'array',
|
|
'allocated_at' => 'datetime',
|
|
'activated_at' => 'datetime',
|
|
'used_at' => 'datetime',
|
|
'expires_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function getRouteKeyName(): string
|
|
{
|
|
return 'serial';
|
|
}
|
|
|
|
public function batch(): BelongsTo
|
|
{
|
|
return $this->belongsTo(QrCodeBatch::class, 'batch_id');
|
|
}
|
|
|
|
public function household(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Household::class, 'assigned_to_household_id');
|
|
}
|
|
|
|
public function usedAtDropOff(): BelongsTo
|
|
{
|
|
return $this->belongsTo(DropOffPoint::class, 'used_at_drop_off_id');
|
|
}
|
|
|
|
public function scannedBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'scanned_by_user_id');
|
|
}
|
|
}
|