forked from admin/services_core
sample about material
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Material;
|
||||
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Http\Requests\Material\AssignMaterialRequest;
|
||||
use App\Http\Requests\Material\TransferMaterialRequest;
|
||||
use App\Http\Requests\Material\ReturnMaterialRequest;
|
||||
use App\Http\Resources\Material\MaterialResource;
|
||||
use App\Services\Material\MaterialService;
|
||||
|
||||
class MaterialController extends ApiController
|
||||
{
|
||||
protected $service;
|
||||
|
||||
public function __construct(MaterialService $service)
|
||||
{
|
||||
$this->service = $service;
|
||||
}
|
||||
|
||||
public function assign(AssignMaterialRequest $request)
|
||||
{
|
||||
$result = $this->service->assign($request->validated());
|
||||
|
||||
return $this->created(
|
||||
new MaterialResource($result),
|
||||
'Material berhasil di-assign'
|
||||
);
|
||||
}
|
||||
|
||||
public function transfer(TransferMaterialRequest $request)
|
||||
{
|
||||
$result = $this->service->transfer($request->validated());
|
||||
|
||||
return $this->updated(
|
||||
new MaterialResource($result),
|
||||
'Material berhasil di-transfer'
|
||||
);
|
||||
}
|
||||
|
||||
public function return(ReturnMaterialRequest $request)
|
||||
{
|
||||
$result = $this->service->return($request->validated());
|
||||
|
||||
return $this->updated(
|
||||
new MaterialResource($result),
|
||||
'Material berhasil direturn'
|
||||
);
|
||||
}
|
||||
|
||||
public function listByUser($user_id)
|
||||
{
|
||||
$data = $this->service->listByUser($user_id);
|
||||
|
||||
return $this->success(
|
||||
\App\Http\Resources\Material\MaterialResource::collection($data),
|
||||
'List material user berhasil diambil'
|
||||
);
|
||||
}
|
||||
|
||||
public function history($barcode_id)
|
||||
{
|
||||
$data = $this->service->history($barcode_id);
|
||||
|
||||
return $this->success(
|
||||
$data,
|
||||
'History material berhasil diambil'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Material;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class AssignMaterialRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'user_id' => 'required|integer',
|
||||
'barcode_id' => 'required|string',
|
||||
'material_name' => 'required|string',
|
||||
|
||||
'type' => 'required|in:serialized,consumable',
|
||||
|
||||
'serial_number' => 'nullable|string',
|
||||
'qty' => 'nullable|numeric',
|
||||
'unit' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Material;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ReturnMaterialRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'barcode_id' => 'required|string',
|
||||
'user_id' => 'required|integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Material;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class TransferMaterialRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'barcode_id' => 'required|string',
|
||||
'from_user_id' => 'required|integer',
|
||||
'to_user_id' => 'required|integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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