110 lines
4.1 KiB
PHP
110 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Maintenance\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\Maintenance\Http\Controllers\Concerns\AuthorizesMaintenance;
|
|
use Modules\Maintenance\Models\DispatchBoardEntry;
|
|
use Modules\Maintenance\Models\WorkOrder;
|
|
use Modules\Maintenance\Utils\MaintenanceUtil;
|
|
|
|
class DispatchBoardController extends Controller
|
|
{
|
|
use AuthorizesMaintenance;
|
|
|
|
public function __construct(
|
|
protected MaintenanceUtil $maintenanceUtil
|
|
) {
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.mission.view');
|
|
|
|
if (! Schema::hasTable('maintenance_dispatch_board')) {
|
|
return view('maintenance::dispatch_board.index', [
|
|
'entries' => collect(),
|
|
'users' => [],
|
|
'work_orders' => [],
|
|
'schema_missing' => true,
|
|
]);
|
|
}
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
|
|
$entries = DispatchBoardEntry::where('business_id', $business_id)
|
|
->with(['workOrder.equipment', 'technician:id,first_name,last_name'])
|
|
->orderBy('scheduled_start')
|
|
->get();
|
|
|
|
return view('maintenance::dispatch_board.index', [
|
|
'entries' => $entries,
|
|
'users' => $this->maintenanceUtil->getUsersDropdown($business_id),
|
|
'work_orders' => WorkOrder::where('business_id', $business_id)
|
|
->whereIn('status', ['open', 'in_progress'])
|
|
->orderByDesc('created_at')
|
|
->limit(100)
|
|
->pluck('work_order_number', 'id'),
|
|
'schema_missing' => false,
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.mission.manage');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
|
|
$data = $request->validate([
|
|
'work_order_id' => 'required|exists:maintenance_work_orders,id',
|
|
'technician_id' => 'nullable|exists:users,id',
|
|
'scheduled_start' => 'nullable|string',
|
|
'scheduled_end' => 'nullable|string',
|
|
'priority' => 'nullable|string|in:low,medium,high,critical',
|
|
'notes' => 'nullable|string',
|
|
]);
|
|
|
|
DispatchBoardEntry::create([
|
|
'business_id' => $business_id,
|
|
'work_order_id' => $data['work_order_id'],
|
|
'technician_id' => $data['technician_id'] ?? null,
|
|
'scheduled_start' => $this->maintenanceUtil->parseInputDateTime($data['scheduled_start'] ?? null),
|
|
'scheduled_end' => $this->maintenanceUtil->parseInputDateTime($data['scheduled_end'] ?? null),
|
|
'priority' => $data['priority'] ?? 'medium',
|
|
'dispatch_status' => 'planned',
|
|
'notes' => $data['notes'] ?? null,
|
|
]);
|
|
|
|
return $this->maintenanceUtil->jsonOrRedirect($request, true, 'اعزام ثبت شد.', action([self::class, 'index']));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$this->authorizeMaintenanceAccess('maintenance.mission.manage');
|
|
|
|
$business_id = $this->maintenanceUtil->getBusinessId();
|
|
$entry = DispatchBoardEntry::where('business_id', $business_id)->findOrFail($id);
|
|
|
|
$data = $request->validate([
|
|
'technician_id' => 'nullable|exists:users,id',
|
|
'dispatch_status' => 'sometimes|string|in:planned,dispatched,en_route,on_site,done',
|
|
'scheduled_start' => 'nullable|string',
|
|
'scheduled_end' => 'nullable|string',
|
|
'notes' => 'nullable|string',
|
|
]);
|
|
|
|
if (isset($data['scheduled_start'])) {
|
|
$data['scheduled_start'] = $this->maintenanceUtil->parseInputDateTime($data['scheduled_start']);
|
|
}
|
|
if (isset($data['scheduled_end'])) {
|
|
$data['scheduled_end'] = $this->maintenanceUtil->parseInputDateTime($data['scheduled_end']);
|
|
}
|
|
|
|
$entry->update($data);
|
|
|
|
return $this->maintenanceUtil->jsonOrRedirect($request, true, 'بهروزرسانی شد.', action([self::class, 'index']));
|
|
}
|
|
}
|