104 lines
3.6 KiB
PHP
104 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Maintenance\Http\Controllers\Api\V1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Modules\Maintenance\Models\ProductionLine;
|
|
use Modules\Maintenance\Traits\ResolvesBusiness;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ProductionLineController extends Controller
|
|
{
|
|
use ResolvesBusiness;
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$business = $this->resolveBusiness($request);
|
|
if (! $business) {
|
|
return $this->jsonError('کسبوکار یافت نشد.', 404);
|
|
}
|
|
|
|
$query = ProductionLine::where('business_id', $business->id)
|
|
->with(['project:id,name'])
|
|
->withCount('equipment')
|
|
->orderBy('name');
|
|
|
|
if ($projectId = $request->get('project_id')) {
|
|
$query->where('project_id', $projectId);
|
|
}
|
|
if ($search = $request->get('q')) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'like', "%{$search}%")
|
|
->orWhere('code', '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 store(Request $request)
|
|
{
|
|
$business = $this->resolveBusiness($request);
|
|
if (! $business) {
|
|
return $this->jsonError('کسبوکار یافت نشد.', 404);
|
|
}
|
|
|
|
$data = $request->validate([
|
|
'project_id' => ['nullable', 'exists:pjt_projects,id'],
|
|
'code' => ['required', 'string', 'max:50'],
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'description' => ['nullable', 'string'],
|
|
'location' => ['nullable', 'string', 'max:255'],
|
|
'status' => ['sometimes', 'string', 'in:active,inactive'],
|
|
]);
|
|
|
|
$line = ProductionLine::create(array_merge($data, [
|
|
'business_id' => $business->id,
|
|
'status' => $data['status'] ?? 'active',
|
|
]));
|
|
|
|
return $this->jsonSuccess($line->load('project:id,name'), 'خط تولید ایجاد شد.', 201);
|
|
}
|
|
|
|
public function update(Request $request, ProductionLine $productionLine)
|
|
{
|
|
if (! $this->ensureBusinessOwnership($request, $productionLine->business_id)) {
|
|
return $this->jsonError('دسترسی ندارید.', 403);
|
|
}
|
|
|
|
$data = $request->validate([
|
|
'project_id' => ['nullable', 'exists:pjt_projects,id'],
|
|
'code' => ['sometimes', 'string', 'max:50'],
|
|
'name' => ['sometimes', 'string', 'max:255'],
|
|
'description' => ['nullable', 'string'],
|
|
'location' => ['nullable', 'string', 'max:255'],
|
|
'status' => ['sometimes', 'string', 'in:active,inactive'],
|
|
]);
|
|
|
|
$productionLine->update($data);
|
|
|
|
return $this->jsonSuccess($productionLine->fresh()->load('project:id,name'), 'خط تولید بهروزرسانی شد.');
|
|
}
|
|
|
|
public function destroy(Request $request, ProductionLine $productionLine)
|
|
{
|
|
if (! $this->ensureBusinessOwnership($request, $productionLine->business_id)) {
|
|
return $this->jsonError('دسترسی ندارید.', 403);
|
|
}
|
|
|
|
$productionLine->delete();
|
|
|
|
return $this->jsonSuccess(null, 'خط تولید حذف شد.');
|
|
}
|
|
}
|