52 lines
2.0 KiB
PHP
52 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use App\Models\QrCode;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
use Illuminate\Support\Facades\URL;
|
|
|
|
class QrCodeBatchResource extends JsonResource
|
|
{
|
|
public function toArray(Request $request): array
|
|
{
|
|
$statusCounts = [
|
|
'active' => $this->active_codes_count ?? 0,
|
|
'used' => $this->used_codes_count ?? 0,
|
|
'voided' => $this->voided_codes_count ?? 0,
|
|
];
|
|
|
|
// If the relation is loaded (like on the show endpoint), compute it accurately just in case
|
|
if ($this->relationLoaded('codes') && ! isset($this->active_codes_count)) {
|
|
$statusCounts = $this->codes->groupBy(fn (QrCode $c) => (string) $c->status)
|
|
->map(fn ($group) => $group->count())
|
|
->all();
|
|
}
|
|
|
|
return [
|
|
'batch_number' => $this->batch_number,
|
|
'quantity' => $this->quantity,
|
|
'purpose' => $this->purpose,
|
|
'target_area' => $this->whenLoaded('targetArea', fn () => $this->targetArea?->code),
|
|
'target_store_id' => $this->target_store_id,
|
|
'household' => $this->whenLoaded('household', fn () => $this->household ? [
|
|
'id' => $this->household->id,
|
|
'address' => $this->household->address_line,
|
|
'head_name' => $this->household->head?->full_name,
|
|
] : null),
|
|
'created_by' => $this->whenLoaded('createdBy', fn () => $this->createdBy?->full_name),
|
|
'printed_at' => $this->printed_at?->toIso8601String(),
|
|
'expires_at' => $this->expires_at?->toIso8601String(),
|
|
'notes' => $this->notes,
|
|
'status_counts' => $statusCounts,
|
|
'print_url' => URL::temporarySignedRoute(
|
|
'api.v1.admin.qr-batches.print-pdf',
|
|
now()->addHours(24),
|
|
['qr_code_batch' => $this->batch_number]
|
|
),
|
|
'created_at' => $this->created_at?->toIso8601String(),
|
|
];
|
|
}
|
|
}
|