ultimatepos/Modules/Maintenance/Http/Controllers/Api/V1/SparePartController.php

111 lines
3.9 KiB
PHP

<?php
namespace Modules\Maintenance\Http\Controllers\Api\V1;
use App\Http\Controllers\Controller;
use Modules\Maintenance\Models\SparePart;
use Modules\Maintenance\Traits\ResolvesBusiness;
use Illuminate\Http\Request;
class SparePartController extends Controller
{
use ResolvesBusiness;
public function index(Request $request)
{
$business = $this->resolveBusiness($request);
if (! $business) {
return $this->jsonError('کسب‌وکار یافت نشد.', 404);
}
$query = SparePart::where('business_id', $business->id)
->with(['warehouse:id,name', 'supplier:id,name'])
->orderBy('name');
if ($search = $request->get('q')) {
$query->where(function ($q) use ($search) {
$q->where('name', 'like', "%{$search}%")
->orWhere('part_number', 'like', "%{$search}%");
});
}
if ($request->boolean('low_stock')) {
$query->whereColumn('quantity', '<=', 'min_stock');
}
$paginator = $query->paginate((int) $request->get('per_page', 15));
return $this->jsonSuccess([
'items' => $paginator->items(),
'pagination' => [
'total' => $paginator->total(),
'per_page' => $paginator->perPage(),
'current_page' => $paginator->currentPage(),
'last_page' => $paginator->lastPage(),
],
]);
}
public function store(Request $request)
{
$business = $this->resolveBusiness($request);
if (! $business) {
return $this->jsonError('کسب‌وکار یافت نشد.', 404);
}
$data = $request->validate([
'part_number' => ['required', 'string', 'max:100'],
'name' => ['required', 'string', 'max:255'],
'warehouse_id' => ['nullable', 'exists:warehouses,id'],
'supplier_id' => ['nullable', 'exists:suppliers,id'],
'product_id' => ['nullable', 'exists:products,id'],
'quantity' => ['sometimes', 'numeric', 'min:0'],
'min_stock' => ['sometimes', 'numeric', 'min:0'],
'max_stock' => ['nullable', 'numeric', 'min:0'],
'purchase_price' => ['sometimes', 'integer', 'min:0'],
'unit' => ['nullable', 'string'],
'notes' => ['nullable', 'string'],
]);
$part = SparePart::create(array_merge($data, [
'business_id' => $business->id,
]));
return $this->jsonSuccess($part, 'قطعه یدکی ایجاد شد.', 201);
}
public function update(Request $request, SparePart $sparePart)
{
if (! $this->ensureBusinessOwnership($request, $sparePart->business_id)) {
return $this->jsonError('دسترسی ندارید.', 403);
}
$data = $request->validate([
'part_number' => ['sometimes', 'string', 'max:100'],
'name' => ['sometimes', 'string', 'max:255'],
'warehouse_id' => ['nullable', 'exists:warehouses,id'],
'supplier_id' => ['nullable', 'exists:suppliers,id'],
'quantity' => ['sometimes', 'numeric', 'min:0'],
'min_stock' => ['sometimes', 'numeric', 'min:0'],
'max_stock' => ['nullable', 'numeric', 'min:0'],
'purchase_price' => ['sometimes', 'integer', 'min:0'],
'unit' => ['nullable', 'string'],
'notes' => ['nullable', 'string'],
]);
$sparePart->update($data);
return $this->jsonSuccess($sparePart, 'قطعه یدکی به‌روزرسانی شد.');
}
public function destroy(Request $request, SparePart $sparePart)
{
if (! $this->ensureBusinessOwnership($request, $sparePart->business_id)) {
return $this->jsonError('دسترسی ندارید.', 403);
}
$sparePart->delete();
return $this->jsonSuccess(null, 'قطعه یدکی حذف شد.');
}
}