128 lines
4.8 KiB
PHP
128 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\Maintenance\Http\Controllers\Api\V1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Modules\Maintenance\Models\PartCatalog;
|
|
use Modules\Maintenance\Traits\ResolvesBusiness;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PartCatalogController extends Controller
|
|
{
|
|
use ResolvesBusiness;
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$business = $this->resolveBusiness($request);
|
|
if (! $business) {
|
|
return $this->jsonError('کسبوکار یافت نشد.', 404);
|
|
}
|
|
|
|
$query = PartCatalog::where('business_id', $business->id)
|
|
->withCount('documents')
|
|
->orderBy('name');
|
|
|
|
if ($type = $request->get('part_type')) {
|
|
$query->where('part_type', $type);
|
|
}
|
|
if ($request->has('is_consumable')) {
|
|
$query->where('is_consumable', $request->boolean('is_consumable'));
|
|
}
|
|
if ($search = $request->get('q')) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'like', "%{$search}%")
|
|
->orWhere('catalog_code', 'like', "%{$search}%")
|
|
->orWhere('manufacturer_part_number', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
$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 show(Request $request, PartCatalog $partCatalog)
|
|
{
|
|
if (! $this->ensureBusinessOwnership($request, $partCatalog->business_id)) {
|
|
return $this->jsonError('دسترسی ندارید.', 403);
|
|
}
|
|
|
|
return $this->jsonSuccess($partCatalog->load('documents'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$business = $this->resolveBusiness($request);
|
|
if (! $business) {
|
|
return $this->jsonError('کسبوکار یافت نشد.', 404);
|
|
}
|
|
|
|
$data = $request->validate([
|
|
'catalog_code' => ['required', 'string', 'max:100'],
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'manufacturer' => ['nullable', 'string', 'max:255'],
|
|
'manufacturer_part_number' => ['nullable', 'string', 'max:100'],
|
|
'part_type' => ['sometimes', 'string', 'in:mechanical,electronic,consumable,hydraulic,pneumatic'],
|
|
'is_consumable' => ['sometimes', 'boolean'],
|
|
'description' => ['nullable', 'string'],
|
|
'specifications' => ['nullable', 'array'],
|
|
'default_replacement_interval_days' => ['nullable', 'integer', 'min:0'],
|
|
'is_oem' => ['sometimes', 'boolean'],
|
|
'equivalent_parts' => ['nullable', 'array'],
|
|
'status' => ['sometimes', 'string', 'in:active,inactive'],
|
|
]);
|
|
|
|
$catalog = PartCatalog::create(array_merge($data, [
|
|
'business_id' => $business->id,
|
|
'status' => $data['status'] ?? 'active',
|
|
]));
|
|
|
|
return $this->jsonSuccess($catalog, 'کاتالوگ قطعه ایجاد شد.', 201);
|
|
}
|
|
|
|
public function update(Request $request, PartCatalog $partCatalog)
|
|
{
|
|
if (! $this->ensureBusinessOwnership($request, $partCatalog->business_id)) {
|
|
return $this->jsonError('دسترسی ندارید.', 403);
|
|
}
|
|
|
|
$data = $request->validate([
|
|
'catalog_code' => ['sometimes', 'string', 'max:100'],
|
|
'name' => ['sometimes', 'string', 'max:255'],
|
|
'manufacturer' => ['nullable', 'string', 'max:255'],
|
|
'manufacturer_part_number' => ['nullable', 'string', 'max:100'],
|
|
'part_type' => ['sometimes', 'string', 'in:mechanical,electronic,consumable,hydraulic,pneumatic'],
|
|
'is_consumable' => ['sometimes', 'boolean'],
|
|
'description' => ['nullable', 'string'],
|
|
'specifications' => ['nullable', 'array'],
|
|
'default_replacement_interval_days' => ['nullable', 'integer', 'min:0'],
|
|
'is_oem' => ['sometimes', 'boolean'],
|
|
'equivalent_parts' => ['nullable', 'array'],
|
|
'status' => ['sometimes', 'string', 'in:active,inactive'],
|
|
]);
|
|
|
|
$partCatalog->update($data);
|
|
|
|
return $this->jsonSuccess($partCatalog->fresh(), 'کاتالوگ قطعه بهروزرسانی شد.');
|
|
}
|
|
|
|
public function destroy(Request $request, PartCatalog $partCatalog)
|
|
{
|
|
if (! $this->ensureBusinessOwnership($request, $partCatalog->business_id)) {
|
|
return $this->jsonError('دسترسی ندارید.', 403);
|
|
}
|
|
|
|
$partCatalog->delete();
|
|
|
|
return $this->jsonSuccess(null, 'کاتالوگ قطعه حذف شد.');
|
|
}
|
|
}
|