Changes
Specific Folder FTP Upload / ftp-deploy (push) Successful in 37s Details

This commit is contained in:
Ravi Gulhane 2026-11-23 16:38:00 +05:30
parent dd8c9f625e
commit 46c6faed2a
2 changed files with 225 additions and 23 deletions

View File

@ -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).
*/

View File

@ -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
<div class="col-md-12 mb-2{{ $hideReason ? ' d-none doctor-reason' : '' }}" @if($reasonSlot) id="doctor-reason-{{ $reasonSlot }}" @endif>
<label for="field-{{$question->code}}" class="form-label">{{$question->question}} {!!$fieldRequired ? '<span class="text-danger">*</span>':''!!} @if($hideReason)<span class="text-danger reason-required d-none">*</span>@endif </label>
<label for="field-{{$question->code}}" class="form-label">{{$question->question}} {!!$fieldRequired ? '<span class="text-danger">*</span>':''!!}@if($showCondRequiredStar)<span class="text-danger slot-cond-required d-none">*</span>@endif @if($hideReason)<span class="text-danger reason-required d-none">*</span>@endif </label>
@switch($question->type)
@case('hidden')
<input type="{{$question->type}}" id="field-{{$question->code}}" name="{{$question->code}}" value="{{isset($question->value)?$question->value:''}}">
@ -244,10 +322,7 @@
@php
$isSubmitted = ($question->code == 'month' && in_array(trim($option->value), $submittedMonths, true));
$isSelected = (isset($question->value) && $question->value == $option->value);
// Create: block already-submitted months. Edit: block other submitted months.
$isDisabled = $question->code == 'month'
? (($isEditMode && $isSubmitted && !$isSelected) || (!$isEditMode && $isSubmitted))
: false;
$isDisabled = ($isEditMode && $isSubmitted && !$isSelected);
@endphp
@if($question->code == 'contact_id' && isset($option->contact->type) && $option->contact->type == 'doctor')
@ -261,7 +336,7 @@
<option value="{{ $option->value }}"
{{ $isSelected ? 'selected' : '' }}
{{ $isDisabled ? 'disabled' : '' }}>
{{ $option->display }}@if($isSubmitted) (Already Submitted)@endif
{{ $option->display }}@if($isDisabled) (Already Submitted)@elseif($isSubmitted && !$isEditMode) (I want to change)@endif
</option>
@else
<option value="{{ $option->value }}"
@ -362,12 +437,35 @@
var monthEntries = @json($monthEntries);
var loadedDoctors = {};
var applyingMonth = false;
var DOCTOR_SLOTS = 10;
var SLOT_COND_REQUIRED = ['city', 'pin_code'];
function doctorSlot(selectId) {
var match = String(selectId || '').match(/^field-contact_id_(\d+)$/);
return match ? match[1] : '';
}
function syncSlotRequiredFields(slot) {
var hasDoctor = String($('#field-contact_id_' + slot).val() || '').trim() !== '';
SLOT_COND_REQUIRED.forEach(function (prefix) {
var field = $('#field-' + prefix + '_' + slot);
if (!field.length) return;
field.prop('required', hasDoctor);
field.closest('.col-md-12').find('.slot-cond-required').toggleClass('d-none', !hasDoctor);
});
// Doctor 1 contact stays required; others optional.
var contactField = $('#field-contact_id_' + slot);
if (contactField.length) {
contactField.prop('required', String(slot) === '1');
}
}
function syncAllSlotRequiredFields() {
for (var n = 1; n <= DOCTOR_SLOTS; n++) {
syncSlotRequiredFields(n);
}
}
function setReasonVisible(slot, show) {
var wrap = $('#doctor-reason-' + slot);
var field = $('#field-reason_' + slot);
@ -386,6 +484,7 @@
$('#field-city_' + slot).val('');
$('#field-pin_code_' + slot).val('');
setReasonVisible(slot, false);
syncSlotRequiredFields(slot);
}
function fillLoadedDoctor(slot) {
@ -396,10 +495,11 @@
$('#field-city_' + slot).val(saved.city);
$('#field-pin_code_' + slot).val(saved.pincode);
setReasonVisible(slot, false);
syncSlotRequiredFields(slot);
}
function rememberLoadedDoctors() {
for (var n = 1; n <= 5; n++) {
for (var n = 1; n <= DOCTOR_SLOTS; n++) {
var doctor = $('#field-contact_id_' + n);
if (!doctor.length) continue;
var contact = doctor.attr('data-initial-value');
@ -425,6 +525,7 @@
$('#field-msl_code_' + slot).val(selected.attr('msl_code') || '');
$('#field-speciality_' + slot).val(selected.attr('speciality') || '');
setReasonVisible(slot, false);
syncSlotRequiredFields(slot);
return;
}
if (current === saved.contact) {
@ -436,6 +537,7 @@
$('#field-msl_code_' + slot).val(selected.attr('msl_code') || '');
$('#field-speciality_' + slot).val(selected.attr('speciality') || '');
setReasonVisible(slot, true);
syncSlotRequiredFields(slot);
}
function setDoctorSelect(select, contactId, label) {
@ -466,11 +568,12 @@
loadedDoctors = {};
if (!entry || !entry.formdata) {
replaceField.val('');
for (var slot = 1; slot <= 5; slot++) clearDoctorSlot(slot);
for (var slot = 1; slot <= DOCTOR_SLOTS; slot++) clearDoctorSlot(slot);
$("[id^='field-contact_id_']").each(function () {
$(this).trigger('change.select2');
});
applyingMonth = false;
syncAllSlotRequiredFields();
return;
}
replaceField.val(entry.id || '');
@ -482,7 +585,7 @@
if (!field.length || field.attr('id') === 'field-month') return;
field.val(data[key] == null ? '' : data[key]);
});
for (var n = 1; n <= 5; n++) {
for (var n = 1; n <= DOCTOR_SLOTS; n++) {
loadedDoctors[n] = {
contact: String(data['contact_id_' + n] || '').trim(),
msl: data['msl_code_' + n] || '',
@ -494,23 +597,17 @@
fillLoadedDoctor(n);
}
applyingMonth = false;
syncAllSlotRequiredFields();
}
$('#field-month').on('change', function () {
// Create flow: do not prefill/replace prior month submissions.
// True edit (/form/edit?id=...) keeps existing values from the server.
if ($('#report-form input[name="id"]').length) {
return;
}
var replaceField = $('#field-replace_submission_id');
if (replaceField.length) {
replaceField.val('');
}
applyMonth(String($(this).val() || ''));
});
if ($('#report-form input[name="id"]').length) {
rememberLoadedDoctors();
}
syncAllSlotRequiredFields();
$('[data-toggle="select2"]').select2();
var managerEntryMode = @json($form->manager_entry_mode ?? 'abm');
@ -597,6 +694,10 @@
select.val(String(existingVal)).trigger('change');
}
});
if ($('#field-replace_submission_id').length && $('#field-month').val()) {
applyMonth(String($('#field-month').val()));
}
syncAllSlotRequiredFields();
}
function loadDoctorsForHierarchies(hierarchyIds) {
@ -688,7 +789,20 @@
});
}
@if(!empty($form->manager_can_submit) || (($form->manager_entry_mode ?? '') === 'abm'))
@php
$abmTmHierarchyIds = [];
if (($form->manager_entry_mode ?? 'abm') !== 'rbm' && !empty($form->users) && is_array($form->users)) {
foreach ($form->users as $abmUserOption) {
if (!empty($abmUserOption->hierarchy_id)) {
$abmTmHierarchyIds[] = (string) $abmUserOption->hierarchy_id;
}
}
}
$shouldLoadManagerDoctors = !empty($form->manager_can_submit)
|| (($form->manager_entry_mode ?? '') === 'abm')
|| count($abmTmHierarchyIds) > 0;
@endphp
@if($shouldLoadManagerDoctors)
bindCityPinHandlers();
if (managerEntryMode === 'rbm') {
$('#field-user_id').on('change', function () {
@ -702,8 +816,13 @@
loadTmDoctorsForUser(abmUserId);
});
} else {
// ABM: load all TM doctors under the logged-in ABM (do not rely on $form->users).
loadTmDoctorsForUser(@json($form->user_id ?? ''));
// ABM: prefer server TM hierarchy ids; fall back to hierarchybelow for logged-in ABM.
var abmTmIds = @json(array_values(array_unique($abmTmHierarchyIds)));
if (abmTmIds.length) {
loadDoctorsForHierarchies(abmTmIds);
} else {
loadTmDoctorsForUser(@json($form->user_id ?? ''));
}
}
@endif