146 lines
5.6 KiB
PHP
146 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Maintenance\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\Maintenance\Http\Controllers\Concerns\AuthorizesMaintenance;
|
|
use Modules\Maintenance\Models\ProductionLine;
|
|
use Modules\Maintenance\Utils\MaintenanceUtil;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class ProductionLineController extends Controller
|
|
{
|
|
use AuthorizesMaintenance;
|
|
|
|
public function __construct(
|
|
protected MaintenanceUtil $maintenanceUtil
|
|
) {}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.lines.view');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
|
|
if ($request->ajax()) {
|
|
$lines = ProductionLine::where('business_id', $business_id)
|
|
->with('project:id,name')
|
|
->withCount('equipment')
|
|
->orderBy('name');
|
|
|
|
if ($project_id = $request->get('project_id')) {
|
|
$lines->where('project_id', $project_id);
|
|
}
|
|
|
|
return DataTables::of($lines)
|
|
->addColumn('project_name', fn ($row) => $row->project?->name ?? '—')
|
|
->addColumn('status_label', fn ($row) => $this->maintenanceUtil->statusLabel($row->status))
|
|
->addColumn('action', function ($row) {
|
|
$html = '<div class="btn-group">';
|
|
if (auth()->user()->can('maintenance.lines.update')) {
|
|
$html .= '<button type="button" data-href="'.action([self::class, 'edit'], [$row->id]).'" class="btn btn-xs btn-primary btn-modal" data-container=".maintenance_production_line_modal"><i class="glyphicon glyphicon-edit"></i></button>';
|
|
}
|
|
if (auth()->user()->can('maintenance.lines.delete')) {
|
|
$html .= ' <button type="button" data-href="'.action([self::class, 'destroy'], [$row->id]).'" class="btn btn-xs btn-danger delete_maintenance_production_line"><i class="glyphicon glyphicon-trash"></i></button>';
|
|
}
|
|
$html .= '</div>';
|
|
|
|
return $html;
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
|
|
return view('maintenance::production_lines.index', [
|
|
'projects' => $this->maintenanceUtil->getProjectsDropdown($business_id),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.lines.create');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
|
|
return view('maintenance::production_lines.create', [
|
|
'projects' => $this->maintenanceUtil->getProjectsDropdown($business_id),
|
|
'statuses' => ['active' => 'فعال', 'inactive' => 'غیرفعال'],
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.lines.create');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
|
|
$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|in:active,inactive',
|
|
]);
|
|
|
|
ProductionLine::create(array_merge($data, [
|
|
'business_id' => $business_id,
|
|
'status' => $data['status'] ?? 'active',
|
|
]));
|
|
|
|
return $this->maintenanceUtil->jsonOrRedirect($request, true, 'خط تولید ایجاد شد.');
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.lines.update');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
$production_line = ProductionLine::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
return view('maintenance::production_lines.edit', [
|
|
'production_line' => $production_line,
|
|
'projects' => $this->maintenanceUtil->getProjectsDropdown($business_id),
|
|
'statuses' => ['active' => 'فعال', 'inactive' => 'غیرفعال'],
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.lines.update');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
$production_line = ProductionLine::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
$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|in:active,inactive',
|
|
]);
|
|
|
|
$production_line->update($data);
|
|
|
|
return $this->maintenanceUtil->jsonOrRedirect($request, true, 'خط تولید بهروزرسانی شد.');
|
|
}
|
|
|
|
public function destroy(Request $request, $id)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.lines.delete');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
$production_line = ProductionLine::where('business_id', $business_id)->withCount('equipment')->findOrFail($id);
|
|
|
|
if ($production_line->equipment_count > 0) {
|
|
return $this->maintenanceUtil->jsonOrRedirect($request, false, 'این خط تولید دارای تجهیز است و قابل حذف نیست.');
|
|
}
|
|
|
|
$production_line->delete();
|
|
|
|
return $this->maintenanceUtil->jsonOrRedirect($request, true, 'خط تولید حذف شد.');
|
|
}
|
|
}
|