diff --git a/app/Http/Controllers/FormController.php b/app/Http/Controllers/FormController.php index 85c9b599..709263ea 100644 --- a/app/Http/Controllers/FormController.php +++ b/app/Http/Controllers/FormController.php @@ -288,6 +288,10 @@ class FormController extends Controller $formdata->manager_can_submit = true; } + // Mother Form: ABM may get empty users when admin child-type is Area. + // Ensure ABM can still submit and has Territory TM hierarchies for doctor load. + $this->ensureMotherFormAbmDoctorHierarchies($formdata); + $request->session()->put('formdata', $formdata); if ($formdata->link_type == 'form' && isset($finfo->data->personalizations) && sizeof($finfo->data->personalizations) > 0) { @@ -11578,6 +11582,85 @@ class FormController extends Controller return response()->json($docs); } + /** + * Mother Form (ABM): if users list is empty, load Territory nodes under the ABM + * so the blade can auto-populate doctor dropdowns without relying on admin child-type. + */ + protected function ensureMotherFormAbmDoctorHierarchies($formdata) + { + $motherFormId = '1791539482-b06c2f9a-fe45-4b16-843a-224f07ed0d7b'; + if (!is_object($formdata) || ($formdata->form_id ?? '') !== $motherFormId) { + return; + } + + $mode = (string) ($formdata->manager_entry_mode ?? ''); + if ($mode === 'rbm') { + return; + } + + // Prefer existing non-empty users (already Territory TMs from Form Lambda). + $existingUsers = (isset($formdata->users) && is_array($formdata->users)) ? $formdata->users : []; + if (count($existingUsers) > 0) { + if ($mode === '' || $mode === 'abm') { + $formdata->manager_entry_mode = $mode !== '' ? $mode : 'abm'; + $formdata->manager_can_submit = true; + } + return; + } + + $partition = (string) ($formdata->partition ?? ''); + $userId = (string) ($formdata->user_id ?? ''); + if ($partition === '' || $userId === '') { + return; + } + + try { + $response = LambdaRequest::getrequest('ums', 'hierarchy/hierarchy_below_me', [ + 'partition' => $partition, + 'user_id' => $userId, + ]); + } catch (\Throwable $e) { + Log::debug('ensureMotherFormAbmDoctorHierarchies hierarchy_below_me failed: ' . $e->getMessage()); + return; + } + + $hierarchies = []; + if (isset($response->data->hierarchies) && is_array($response->data->hierarchies)) { + $hierarchies = $response->data->hierarchies; + } elseif (isset($response->data) && is_array($response->data)) { + $hierarchies = $response->data; + } + + $tmUsers = []; + foreach ($hierarchies as $h) { + $type = is_object($h) ? ($h->type ?? '') : ($h['type'] ?? ''); + $id = is_object($h) ? ($h->id ?? '') : ($h['id'] ?? ''); + if ($type !== 'Territory' || $id === '') { + continue; + } + $owner = is_object($h) ? ($h->owner ?? '') : ($h['owner'] ?? ''); + $name = is_object($h) ? ($h->name ?? '') : ($h['name'] ?? ''); + $isVacant = is_object($h) ? (!empty($h->is_vacant)) : (!empty($h['is_vacant'])); + $ownerInfo = is_object($h) ? ($h->owner_info ?? (object) []) : ($h['owner_info'] ?? (object) []); + $ownerName = is_object($ownerInfo) ? ($ownerInfo->name ?? '') : ''; + $tmUsers[] = (object) [ + 'value' => $owner, + 'hierarchy_id' => $id, + 'display' => $name . ' - Territory -' . ($isVacant ? ' (Vacant)' : ' (' . $ownerName . ')'), + 'user_info' => $isVacant ? (object) [] : $ownerInfo, + ]; + } + + if (count($tmUsers) > 0) { + $formdata->users = $tmUsers; + $formdata->manager_entry_mode = 'abm'; + $formdata->manager_can_submit = true; + } elseif ($mode === 'abm') { + // Keep ABM submit path even if no TMs found (doctor list will stay empty). + $formdata->manager_can_submit = true; + } + } + /** * Hierarchies below a given user (for RBM → ABM → TM doctor loading). */ diff --git a/resources/views/form/rapport-team4/1791539482-b06c2f9a-fe45-4b16-843a-224f07ed0d7b.blade.php b/resources/views/form/rapport-team4/1791539482-b06c2f9a-fe45-4b16-843a-224f07ed0d7b.blade.php index 10d2f557..56c67768 100644 --- a/resources/views/form/rapport-team4/1791539482-b06c2f9a-fe45-4b16-843a-224f07ed0d7b.blade.php +++ b/resources/views/form/rapport-team4/1791539482-b06c2f9a-fe45-4b16-843a-224f07ed0d7b.blade.php @@ -86,7 +86,7 @@ } } $doctorLabels = []; - for ($slot = 1; $slot <= 5; $slot++) { + for ($slot = 1; $slot <= 10; $slot++) { $contactKey = 'contact_id_'.$slot; $current = trim((string) ($payload[$contactKey] ?? '')); if ($current === '') { @@ -114,6 +114,68 @@ $isEditMode = !empty($id); + // Ensure doctor slots 6-10 exist in $questions (Form Designer may still only have 1-5). + $doctorFieldKinds = ['contact_id', 'msl_code', 'speciality', 'city', 'pin_code', 'reason']; + $questionsByCode = []; + foreach ($questions as $q) { + if (!empty($q->code)) { + $questionsByCode[$q->code] = $q; + } + } + $doctorTemplates = []; + for ($templateSlot = 5; $templateSlot >= 1; $templateSlot--) { + foreach ($doctorFieldKinds as $kind) { + $templateCode = $kind.'_'.$templateSlot; + if (!isset($doctorTemplates[$kind]) && isset($questionsByCode[$templateCode])) { + $doctorTemplates[$kind] = $questionsByCode[$templateCode]; + } + } + } + $syntheticDoctorQuestions = []; + for ($slot = 6; $slot <= 10; $slot++) { + foreach ($doctorFieldKinds as $kind) { + $code = $kind.'_'.$slot; + if (isset($questionsByCode[$code]) || !isset($doctorTemplates[$kind])) { + continue; + } + $src = $doctorTemplates[$kind]; + $clone = json_decode(json_encode($src)); + if (!is_object($clone)) { + continue; + } + $srcSlot = 1; + if (preg_match('/_(\d+)$/', (string) ($src->code ?? ''), $srcMatch)) { + $srcSlot = (int) $srcMatch[1]; + } + $clone->id = 'synthetic-'.$code; + $clone->code = $code; + $clone->required = 'no'; + $label = (string) ($clone->question ?? $code); + $label = preg_replace('/\b'.$srcSlot.'\b/', (string) $slot, $label); + $clone->question = $label; + if ($isEditMode && isset($form->formdata) && is_object($form->formdata) && isset($form->formdata->{$code})) { + $clone->value = $form->formdata->{$code}; + } else { + unset($clone->value); + } + $syntheticDoctorQuestions[] = $clone; + $questionsByCode[$code] = $clone; + } + } + if (count($syntheticDoctorQuestions) > 0) { + $insertAfter = -1; + foreach ($questions as $idx => $q) { + if (preg_match('/^(contact_id|msl_code|speciality|city|pin_code|reason)_(\d+)$/', (string) ($q->code ?? ''), $m) && (int) $m[2] <= 5) { + $insertAfter = $idx; + } + } + if ($insertAfter >= 0) { + array_splice($questions, $insertAfter + 1, 0, $syntheticDoctorQuestions); + } else { + $questions = array_merge($questions, $syntheticDoctorQuestions); + } + } + $reasonQuestionCode = ''; foreach ($questions as $question) { $label = trim(preg_replace('/\s+/', ' ', strip_tags($question->question ?? ''))); @@ -184,11 +246,27 @@ } $isReasonField = $reasonSlot !== null; $hideReason = $isReasonField; - $fieldRequired = $hideReason ? false : ($isReasonField ? true : ($question->required == 'yes')); + $doctorFieldKind = null; + $doctorSlotNum = null; + if (preg_match('/^(contact_id|msl_code|speciality|city|pin_code)_(\d+)$/', (string) $question->code, $doctorMatch)) { + $doctorFieldKind = $doctorMatch[1]; + $doctorSlotNum = (int) $doctorMatch[2]; + } + // Doctor 1 required; doctors 2-10 optional. Sibling fields become required via JS when a doctor is selected. + if ($hideReason) { + $fieldRequired = false; + } elseif ($doctorFieldKind === 'contact_id') { + $fieldRequired = ($doctorSlotNum === 1); + } elseif (in_array($doctorFieldKind, ['msl_code', 'speciality', 'city', 'pin_code'], true)) { + $fieldRequired = false; + } else { + $fieldRequired = ($question->required == 'yes'); + } $fieldDisabled = $hideReason; + $showCondRequiredStar = in_array($doctorFieldKind, ['city', 'pin_code'], true); @endphp