39 lines
1.4 KiB
PHP
39 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use App\Models\QrCode;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class QrCodeBatchResource extends JsonResource
|
|
{
|
|
public function toArray(Request $request): array
|
|
{
|
|
$statusCounts = $this->whenLoaded('codes', function () {
|
|
return $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,
|
|
'created_at' => $this->created_at?->toIso8601String(),
|
|
];
|
|
}
|
|
}
|