236 lines
10 KiB
PHP
236 lines
10 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\Equipment;
|
|
use Modules\Maintenance\Models\ServiceRecord;
|
|
use Modules\Maintenance\Traits\AppliesMaintenanceScope;
|
|
use Modules\Maintenance\Utils\MaintenanceUtil;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class ServiceRecordController extends Controller
|
|
{
|
|
use AuthorizesMaintenance, AppliesMaintenanceScope;
|
|
|
|
public function __construct(
|
|
protected MaintenanceUtil $maintenanceUtil
|
|
) {}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.work_orders.view');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
|
|
if ($request->ajax()) {
|
|
$query = ServiceRecord::where('business_id', $business_id)
|
|
->with(['equipment:id,name,code', 'performer:id,first_name,last_name'])
|
|
->orderByDesc('started_at');
|
|
|
|
$this->applyMaintenanceScopeFilters($query, $request);
|
|
|
|
if ($equipment_id = $request->get('equipment_id')) {
|
|
$query->where('equipment_id', $equipment_id);
|
|
}
|
|
|
|
return DataTables::of($query)
|
|
->addColumn('equipment_name', fn ($row) => $row->equipment?->name ?? '—')
|
|
->addColumn('performer_name', function ($row) {
|
|
if (! $row->performer) {
|
|
return '—';
|
|
}
|
|
|
|
return trim($row->performer->first_name.' '.($row->performer->last_name ?? ''));
|
|
})
|
|
->editColumn('started_at', fn ($row) => $this->maintenanceUtil->formatDateTime($row->started_at))
|
|
->editColumn('completed_at', fn ($row) => $this->maintenanceUtil->formatDateTime($row->completed_at))
|
|
->editColumn('cost', fn ($row) => $this->maintenanceUtil->formatMoney($row->cost))
|
|
->addColumn('action', function ($row) {
|
|
$html = '<a href="'.action([self::class, 'show'], [$row->id]).'" class="btn btn-xs btn-info"><i class="fa fa-eye"></i></a>';
|
|
if (auth()->user()->can('maintenance.work_orders.update')) {
|
|
$html .= ' <a href="'.action([self::class, 'edit'], [$row->id]).'" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i></a>';
|
|
}
|
|
if (auth()->user()->can('maintenance.work_orders.delete')) {
|
|
$html .= ' <button type="button" data-href="'.action([self::class, 'destroy'], [$row->id]).'" class="btn btn-xs btn-danger delete_mm_service_record"><i class="glyphicon glyphicon-trash"></i></button>';
|
|
}
|
|
|
|
return $html;
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
|
|
return view('maintenance::service_records.index', [
|
|
'equipment' => $this->maintenanceUtil->getEquipmentDropdown($business_id),
|
|
'projects' => $this->maintenanceUtil->getProjectsDropdown($business_id),
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.work_orders.create');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
|
|
return view('maintenance::service_records.create', [
|
|
'equipment' => $this->maintenanceUtil->getEquipmentDropdown($business_id),
|
|
'projects' => $this->maintenanceUtil->getProjectsDropdown($business_id),
|
|
'production_lines' => $this->maintenanceUtil->getProductionLinesDropdown($business_id),
|
|
'users' => $this->maintenanceUtil->getUsersDropdown($business_id),
|
|
'providers' => $this->maintenanceUtil->getServiceProvidersDropdown($business_id),
|
|
'service_types' => $this->serviceTypeLabels(),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.work_orders.create');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
|
|
$data = $request->validate([
|
|
'equipment_id' => ['required', 'exists:maintenance_equipment,id'],
|
|
'service_type' => ['required', 'string', 'in:minor,major,overhaul'],
|
|
'title' => ['required', 'string', 'max:255'],
|
|
'description' => ['nullable', 'string'],
|
|
'started_at' => ['nullable', 'string'],
|
|
'completed_at' => ['nullable', 'string'],
|
|
'labor_hours' => ['nullable', 'numeric', 'min:0'],
|
|
'cost' => ['nullable', 'integer', 'min:0'],
|
|
'performed_by' => ['nullable', 'exists:users,id'],
|
|
'repair_provider_id' => ['nullable', 'exists:maintenance_service_providers,id'],
|
|
'health_before' => ['nullable', 'numeric', 'min:0', 'max:100'],
|
|
'health_after' => ['nullable', 'numeric', 'min:0', 'max:100'],
|
|
'status' => ['sometimes', 'string', 'in:draft,completed,approved'],
|
|
]);
|
|
|
|
$equipment = Equipment::where('business_id', $business_id)->findOrFail($data['equipment_id']);
|
|
|
|
$record = ServiceRecord::create([
|
|
'business_id' => $business_id,
|
|
'equipment_id' => $equipment->id,
|
|
'project_id' => $data['project_id'] ?? $equipment->project_id,
|
|
'production_line_id' => $data['production_line_id'] ?? $equipment->production_line_id,
|
|
'service_type' => $data['service_type'],
|
|
'title' => $data['title'],
|
|
'description' => $data['description'] ?? null,
|
|
'started_at' => $this->maintenanceUtil->parseInputDateTime($data['started_at'] ?? null) ?? now(),
|
|
'completed_at' => $this->maintenanceUtil->parseInputDateTime($data['completed_at'] ?? null),
|
|
'labor_hours' => $data['labor_hours'] ?? null,
|
|
'cost' => $data['cost'] ?? 0,
|
|
'performed_by' => $data['performed_by'] ?? auth()->id(),
|
|
'repair_provider_id' => $data['repair_provider_id'] ?? null,
|
|
'health_before' => $data['health_before'] ?? $equipment->health_percentage,
|
|
'health_after' => $data['health_after'] ?? null,
|
|
'status' => $data['status'] ?? 'completed',
|
|
]);
|
|
|
|
if (isset($data['health_after'])) {
|
|
$equipment->update(['health_percentage' => $data['health_after']]);
|
|
}
|
|
|
|
return $this->maintenanceUtil->jsonOrRedirect(
|
|
$request,
|
|
true,
|
|
'سرویس ثبت شد.',
|
|
action([self::class, 'show'], [$record->id])
|
|
);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.work_orders.view');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
$record = ServiceRecord::where('business_id', $business_id)
|
|
->with(['equipment', 'performer', 'repairProvider', 'items.equipmentPart'])
|
|
->findOrFail($id);
|
|
|
|
return view('maintenance::service_records.show', compact('record'));
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.work_orders.update');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
$record = ServiceRecord::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
return view('maintenance::service_records.edit', [
|
|
'record' => $record,
|
|
'equipment' => $this->maintenanceUtil->getEquipmentDropdown($business_id),
|
|
'users' => $this->maintenanceUtil->getUsersDropdown($business_id),
|
|
'providers' => $this->maintenanceUtil->getServiceProvidersDropdown($business_id),
|
|
'service_types' => $this->serviceTypeLabels(),
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.work_orders.update');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
$record = ServiceRecord::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
$data = $request->validate([
|
|
'equipment_id' => ['sometimes', 'exists:maintenance_equipment,id'],
|
|
'service_type' => ['sometimes', 'string', 'in:minor,major,overhaul'],
|
|
'title' => ['sometimes', 'string', 'max:255'],
|
|
'description' => ['nullable', 'string'],
|
|
'started_at' => ['nullable', 'string'],
|
|
'completed_at' => ['nullable', 'string'],
|
|
'labor_hours' => ['nullable', 'numeric', 'min:0'],
|
|
'cost' => ['nullable', 'integer', 'min:0'],
|
|
'performed_by' => ['nullable', 'exists:users,id'],
|
|
'repair_provider_id' => ['nullable', 'exists:maintenance_service_providers,id'],
|
|
'health_before' => ['nullable', 'numeric', 'min:0', 'max:100'],
|
|
'health_after' => ['nullable', 'numeric', 'min:0', 'max:100'],
|
|
'status' => ['sometimes', 'string', 'in:draft,completed,approved'],
|
|
]);
|
|
|
|
if (isset($data['started_at'])) {
|
|
$data['started_at'] = $this->maintenanceUtil->parseInputDateTime($data['started_at']);
|
|
}
|
|
if (isset($data['completed_at'])) {
|
|
$data['completed_at'] = $this->maintenanceUtil->parseInputDateTime($data['completed_at']);
|
|
}
|
|
|
|
$record->update($data);
|
|
|
|
if (isset($data['health_after'])) {
|
|
$record->equipment?->update(['health_percentage' => $data['health_after']]);
|
|
}
|
|
|
|
return $this->maintenanceUtil->jsonOrRedirect(
|
|
$request,
|
|
true,
|
|
'سرویس بهروزرسانی شد.',
|
|
action([self::class, 'show'], [$record->id])
|
|
);
|
|
}
|
|
|
|
public function destroy(Request $request, $id)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.work_orders.delete');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
$record = ServiceRecord::where('business_id', $business_id)->findOrFail($id);
|
|
$record->items()->delete();
|
|
$record->delete();
|
|
|
|
return $this->maintenanceUtil->jsonOrRedirect($request, true, 'سرویس حذف شد.', action([self::class, 'index']));
|
|
}
|
|
|
|
protected function serviceTypeLabels(): array
|
|
{
|
|
return [
|
|
'minor' => __('maintenance::lang.service_type_minor'),
|
|
'major' => __('maintenance::lang.service_type_major'),
|
|
'overhaul' => __('maintenance::lang.service_type_overhaul'),
|
|
];
|
|
}
|
|
}
|