sample about material
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Material;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class MaterialResource extends JsonResource
|
||||
{
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
// universal
|
||||
'barcode_id' => $this->barcode_id,
|
||||
'material_name' => $this->material_name,
|
||||
'user_id' => $this->user_id,
|
||||
|
||||
// detect type model
|
||||
'type' => $this->detectType(),
|
||||
|
||||
// serialized only
|
||||
'serial_number' => $this->when(
|
||||
$this->isSerialized(),
|
||||
$this->serial_number
|
||||
),
|
||||
|
||||
// consumable only
|
||||
'qty' => $this->when(
|
||||
$this->isConsumable(),
|
||||
[
|
||||
'received' => $this->qty_received ?? null,
|
||||
'used' => $this->qty_used ?? null,
|
||||
'remaining' => $this->qty_remaining ?? null,
|
||||
'unit' => $this->unit ?? null,
|
||||
]
|
||||
),
|
||||
|
||||
// status (beda logic tapi tetap dipakai keduanya)
|
||||
'status' => $this->status ?? null,
|
||||
|
||||
// optional tracking info
|
||||
'assigned_at' => $this->assigned_at ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
private function detectType(): string
|
||||
{
|
||||
// kalau punya qty field → consumable
|
||||
if (isset($this->qty_received) || isset($this->qty_remaining)) {
|
||||
return 'consumable';
|
||||
}
|
||||
|
||||
return 'serialized';
|
||||
}
|
||||
|
||||
private function isSerialized(): bool
|
||||
{
|
||||
return !isset($this->qty_received);
|
||||
}
|
||||
|
||||
private function isConsumable(): bool
|
||||
{
|
||||
return isset($this->qty_received);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user