diff --git a/source/packages/com_mokosuitefield/api/src/Controller/FieldSchedulingController.php b/source/packages/com_mokosuitefield/api/src/Controller/FieldSchedulingController.php new file mode 100644 index 0000000..75b125c --- /dev/null +++ b/source/packages/com_mokosuitefield/api/src/Controller/FieldSchedulingController.php @@ -0,0 +1,83 @@ +getIdentity(); + if (!$user || $user->guest || (!$user->authorise('core.admin') && !$user->authorise($action, 'com_mokosuitefield'))) { + http_response_code(403); + echo json_encode(['error' => 'Access denied']); + Factory::getApplication()->close(); + } + } + + public function availableSlots(): void + { + $this->requireAuth(); + $input = Factory::getApplication()->getInput(); + + $slots = \Moko\Plugin\System\MokoSuiteField\Helper\SchedulingHelper::getAvailableSlots( + $input->getString('date', date('Y-m-d')), + $input->getString('trade', 'general'), + $input->getInt('duration', 60) + ); + + $this->sendJson($slots); + } + + public function bookSlot(): void + { + $this->requireAuth('core.create'); + $input = Factory::getApplication()->getInput(); + + $result = \Moko\Plugin\System\MokoSuiteField\Helper\SchedulingHelper::scheduleWorkOrder( + $input->getInt('wo_id', 0), + $input->getString('date', ''), + $input->getString('time', ''), + $input->getInt('tech_id', 0) ?: null + ); + + $this->sendJson(['success' => $result]); + } + + public function todaySchedule(): void + { + $this->requireAuth(); + $schedule = \Moko\Plugin\System\MokoSuiteField\Helper\SchedulingHelper::getTodaySchedule(); + $this->sendJson($schedule); + } + + public function techRoute(): void + { + $this->requireAuth(); + $techId = Factory::getApplication()->getInput()->getInt('tech_id', 0); + $date = Factory::getApplication()->getInput()->getString('date', date('Y-m-d')); + + $route = \Moko\Plugin\System\MokoSuiteField\Helper\RouteHelper::getTechRoute($techId, $date); + $metrics = \Moko\Plugin\System\MokoSuiteField\Helper\RouteHelper::estimateRouteMetrics($techId, $date); + + $this->sendJson(['route' => $route, 'metrics' => $metrics]); + } + + private function sendJson(mixed $data): void + { + header('Content-Type: application/json; charset=utf-8'); + echo json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE); + Factory::getApplication()->close(); + } +}