881 lines
36 KiB
PHP
881 lines
36 KiB
PHP
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
@include('layouts.shared/title-meta', ['title' => "Doctor & Chemist Survey"])
|
|
@include('layouts.shared/head-css', ["mode" => $mode ?? '', "demo" => $demo ?? ''])
|
|
<link href="{{asset('assets/libs/sweetalert2/sweetalert2.min.css')}}" rel="stylesheet" type="text/css" />
|
|
<link href="{{ asset('assets/libs/select2/select2.min.css') }}" rel="stylesheet" type="text/css" />
|
|
<style>
|
|
.quick-action-form-card .form-control { font-size: 16px; }
|
|
.survey-section { border: 1px solid #d9d9d9; border-radius: 0.25rem; margin-bottom: 0.75rem; }
|
|
.survey-section > .section-head {
|
|
display: flex; width: 100%; text-align: left; background: #e5eef7; color: var(--bs-secondary);
|
|
border: 0; padding: 0.85rem 1rem; font-weight: 600;
|
|
}
|
|
.survey-section.locked > .section-head { opacity: 0.55; }
|
|
.survey-section .section-body { padding: 0.75rem 1rem 0.25rem; }
|
|
.rcpa-doctors { width: 100%; table-layout: fixed; border: 0; }
|
|
.table.rcpa-doctors > :not(caption) > * > * { vertical-align: middle; padding: 0.15rem; border: 0; box-shadow: none; }
|
|
.table.rcpa-doctors > :not(caption) > * > *:first-child { width: 75%; }
|
|
.table.rcpa-doctors > :not(caption) > * > *:last-child { width: 25%; }
|
|
.rcpa-doctors .form-control { font-size: 16px; min-width: 0; padding: 0.2rem 0.3rem; }
|
|
.survey-section.locked .section-body { display: none; }
|
|
#pageloader {
|
|
display: none;
|
|
position: fixed;
|
|
inset: 0;
|
|
z-index: 9999;
|
|
background: rgba(255, 255, 255, 0.8);
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body class="loading">
|
|
<div id="pageloader">Loading</div>
|
|
<div class="account-pages">
|
|
<div class="container">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8 col-lg-6 col-xl-5 p-0">
|
|
<div class="card bg-pattern quick-action-form-card">
|
|
<div class="card-body p-3">
|
|
<div class="d-flex justify-content-between align-items-center mb-2">
|
|
<h4 class="page-title mb-0">Doctor & Chemist Survey</h4>
|
|
<a href="/form/history" class="pt-1">View History</a>
|
|
</div>
|
|
<form method="POST" action="/form/submit" id="report-form" enctype="multipart/form-data">
|
|
@csrf
|
|
<input type="hidden" name="form_id" id="field-form_id" value="{{ $form_id ?? $form->form_id }}">
|
|
<input type="hidden" name="id" id="field-submission_id" value="{{ $id ?: '' }}">
|
|
|
|
<div class="mb-2">
|
|
<label class="form-label" for="field-record_date">Date <span class="text-danger">*</span></label>
|
|
<input type="date" class="form-control" id="field-record_date" name="record_date" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label" for="field-visit_type">Survey for <span class="text-danger">*</span></label>
|
|
<select class="form-control" id="field-visit_type" required>
|
|
<option value="doctor">Doctor</option>
|
|
<option value="chemist">Chemist</option>
|
|
</select>
|
|
</div>
|
|
|
|
<fieldset id="doctor-fields">
|
|
@include('form.rapport-team3.partials.doctor-chemist-doctor')
|
|
</fieldset>
|
|
<fieldset id="chemist-fields" disabled style="display:none">
|
|
@include('form.rapport-team3.partials.doctor-chemist-chemist')
|
|
</fieldset>
|
|
|
|
<button type="submit" class="btn btn-primary w-100 mt-2">Submit</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
@include('layouts.shared/footer-script')
|
|
<script src="{{asset('assets/libs/sweetalert2/sweetalert2.min.js')}}"></script>
|
|
<script src="{{ asset('assets/libs/select2/select2.min.js') }}"></script>
|
|
@php
|
|
$savedAnswers = [];
|
|
$savedSource = null;
|
|
if (!empty($id)) {
|
|
if (isset($formdata) && (is_object($formdata) || is_array($formdata))) {
|
|
$savedSource = $formdata;
|
|
} elseif (isset($form->formdata)) {
|
|
$savedSource = $form->formdata;
|
|
}
|
|
}
|
|
if (is_object($savedSource) || is_array($savedSource)) {
|
|
foreach ((array) $savedSource as $savedKey => $savedValue) {
|
|
if ($savedKey === 'title' || is_array($savedValue) || is_object($savedValue) || $savedValue === null || $savedValue === '') {
|
|
continue;
|
|
}
|
|
$savedAnswers[$savedKey] = $savedValue;
|
|
}
|
|
}
|
|
foreach (($questions ?? []) as $savedQuestion) {
|
|
if (!is_object($savedQuestion) || empty($savedQuestion->code)) {
|
|
continue;
|
|
}
|
|
if (!isset($savedAnswers[$savedQuestion->code]) && isset($savedQuestion->value) && $savedQuestion->value !== '' && !is_array($savedQuestion->value) && !is_object($savedQuestion->value)) {
|
|
$savedAnswers[$savedQuestion->code] = $savedQuestion->value;
|
|
}
|
|
}
|
|
@endphp
|
|
<script>
|
|
$(function () {
|
|
var doctorFormId = '1790230856-a5ff58a6-d509-4ac0-9b18-6d366a74965e';
|
|
var chemistFormId = '1790230856-704c97b6-61f7-4eda-901a-2cb4ff6f293d';
|
|
var surveyContacts = [];
|
|
var submittedIdentities = [];
|
|
var contactsReady = false;
|
|
var unlockAllSections = false;
|
|
var savedAnswers = @json($savedAnswers);
|
|
var photoFields = ['visiting_card', 'rx_pad', 'doctor_board', 'chemist_board'];
|
|
|
|
function activeRoot() {
|
|
return $('#field-visit_type').val() === 'chemist' ? '#chemist-fields' : '#doctor-fields';
|
|
}
|
|
|
|
function contactPhone(contact) {
|
|
var digits = String((contact && contact.phone) || '').replace(/\D/g, '');
|
|
return digits.length > 10 ? digits.slice(-10) : digits;
|
|
}
|
|
|
|
function refreshSections() {
|
|
var root = $(activeRoot());
|
|
var open = true;
|
|
root.find('.survey-section').each(function () {
|
|
var section = $(this);
|
|
section.toggleClass('locked', !open);
|
|
section.find(':input').prop('disabled', !open);
|
|
if (!open || unlockAllSections) {
|
|
return;
|
|
}
|
|
var gates = section.find('[data-gate]');
|
|
if (gates.length && gates.filter(function () { return !$(this).val(); }).length) {
|
|
open = false;
|
|
}
|
|
});
|
|
syncOtherFields();
|
|
syncServiceFields();
|
|
syncProductUnits();
|
|
}
|
|
|
|
function syncOtherFields() {
|
|
var pairs = [
|
|
['#field-degree', '#wrap-other_degree', '#field-doctor_other_degree'],
|
|
['#field-specialty', '#wrap-other_specialty', '#field-doctor_other_speciality']
|
|
];
|
|
pairs.forEach(function (pair) {
|
|
var show = /^others?$/i.test($(pair[0]).val() || '');
|
|
$(pair[1]).toggle(show);
|
|
var input = $(pair[2]);
|
|
input.prop('required', show);
|
|
if (!show) {
|
|
input.val('').prop('disabled', true);
|
|
} else if (!$('#doctor-fields').prop('disabled')) {
|
|
input.prop('disabled', false);
|
|
}
|
|
});
|
|
}
|
|
|
|
function syncServiceFields() {
|
|
$('#chemist-fields .service-row').each(function () {
|
|
var row = $(this);
|
|
var show = row.find('.service-choice').val() === 'Yes' && !row.closest('.survey-section').hasClass('locked') && !$('#chemist-fields').prop('disabled');
|
|
var charge = row.find('.service-charge');
|
|
charge.toggle(show);
|
|
var select = charge.find('select');
|
|
select.prop('required', show).prop('disabled', !show);
|
|
if (!show) {
|
|
select.val('');
|
|
}
|
|
});
|
|
}
|
|
|
|
function syncProductUnits() {
|
|
$('#doctor-fields .product-row').each(function () {
|
|
var row = $(this);
|
|
var show = row.find('.product-choice').val() === 'Yes' && !$('#doctor-fields').prop('disabled');
|
|
var units = row.find('.product-units');
|
|
units.toggle(show);
|
|
var input = units.find('input');
|
|
input.prop('required', show).prop('disabled', !show);
|
|
if (!show) {
|
|
input.val('');
|
|
}
|
|
});
|
|
}
|
|
|
|
function fillSelect(select, contacts) {
|
|
var current = select.value;
|
|
select.innerHTML = select.classList.contains('rcpa-doctor')
|
|
? '<option value=""></option>'
|
|
: '<option value="">-- Select --</option>';
|
|
contacts.forEach(function (item) {
|
|
var contact = item.contact || {};
|
|
var option = document.createElement('option');
|
|
option.value = item.value;
|
|
option.textContent = item.display || contact.name || '';
|
|
option.setAttribute('data-phone', contact.phone || '');
|
|
option.setAttribute('data-speciality', contact.speciality || '');
|
|
option.setAttribute('data-code', contact.msl_code || '');
|
|
option.setAttribute('data-location', contact.stockist_location || contact.location || '');
|
|
option.setAttribute('data-stockist-id', contact.stockist_id || '');
|
|
select.appendChild(option);
|
|
});
|
|
if (select.classList.contains('stockist-pick')) {
|
|
var other = document.createElement('option');
|
|
other.value = 'others';
|
|
other.textContent = 'Others';
|
|
select.appendChild(other);
|
|
}
|
|
if (current) {
|
|
select.value = current;
|
|
}
|
|
}
|
|
|
|
function loadContacts() {
|
|
contactsReady = false;
|
|
$.ajax({
|
|
type: 'GET',
|
|
url: '/form/getdoctorlist?partition={{ $form->partition ?? '' }}&hierarchy={{ $form->hierarchy_id ?? ($form->hierarchy ?? '') }}&dedupe=0',
|
|
success: function (data) {
|
|
surveyContacts = Array.isArray(data) ? data : [];
|
|
contactsReady = true;
|
|
var doctors = surveyContacts.filter(function (item) {
|
|
return item.contact && String(item.contact.type || '').toLowerCase() === 'ebt_doctor';
|
|
});
|
|
var stockists = surveyContacts.filter(function (item) {
|
|
return item.contact && String(item.contact.type || '').toLowerCase() === 'ebt_stockist';
|
|
});
|
|
document.querySelectorAll('#chemist-fields .rcpa-doctor').forEach(function (select) {
|
|
if ($(select).data('select2')) {
|
|
$(select).select2('destroy');
|
|
}
|
|
fillSelect(select, doctors);
|
|
});
|
|
document.querySelectorAll('#chemist-fields .stockist-pick').forEach(function (select) {
|
|
fillSelect(select, stockists);
|
|
});
|
|
matchSavedContacts();
|
|
initDoctorSearch();
|
|
},
|
|
error: function () {
|
|
contactsReady = true;
|
|
}
|
|
});
|
|
}
|
|
|
|
var doctorLock = false;
|
|
function applyDoctor(select) {
|
|
if (doctorLock) {
|
|
return;
|
|
}
|
|
var row = $(select).closest('tr');
|
|
var option = select.options[select.selectedIndex];
|
|
var chosen = select.value;
|
|
if (chosen) {
|
|
var duplicate = false;
|
|
$('#chemist-fields .rcpa-doctor[data-product="' + select.getAttribute('data-product') + '"]').each(function () {
|
|
if (this !== select && this.value === chosen) {
|
|
duplicate = true;
|
|
}
|
|
});
|
|
if (duplicate) {
|
|
doctorLock = true;
|
|
$(select).val('').trigger('change');
|
|
doctorLock = false;
|
|
row.find('.rcpa-name, .rcpa-code').val('');
|
|
fail('This doctor is already selected for this product.');
|
|
return;
|
|
}
|
|
}
|
|
if (!chosen || !option) {
|
|
row.find('.rcpa-name, .rcpa-code').val('');
|
|
return;
|
|
}
|
|
row.find('.rcpa-name').val(option.textContent || '');
|
|
row.find('.rcpa-code').val(option.getAttribute('data-code') || '');
|
|
}
|
|
|
|
function applyStockist(select, keepAddress) {
|
|
var block = $(select).closest('.stockist-block');
|
|
var option = select.options[select.selectedIndex];
|
|
var chosen = select.value;
|
|
var isOther = chosen === 'others';
|
|
if (chosen && !isOther) {
|
|
var duplicate = false;
|
|
$('#chemist-fields .stockist-pick').each(function () {
|
|
if (this !== select && this.value === chosen) {
|
|
duplicate = true;
|
|
}
|
|
});
|
|
if (duplicate) {
|
|
select.value = '';
|
|
chosen = '';
|
|
isOther = false;
|
|
option = select.options[0];
|
|
fail('This stockist is already selected.');
|
|
}
|
|
}
|
|
var location = block.find('.stockist-location');
|
|
var otherName = block.find('.stockist-other-name');
|
|
block.find('.stockist-other').toggle(isOther);
|
|
block.find('.stockist-id-wrap').toggle(!isOther);
|
|
location.prop('readonly', !isOther).prop('required', isOther).attr('tabindex', isOther ? null : '-1');
|
|
otherName.prop('required', isOther);
|
|
block.find('.stockist-address-label').html(isOther
|
|
? 'Other Stockist Address <span class="text-danger">*</span>'
|
|
: 'Stockist ' + (select.getAttribute('data-slot') || '') + ' Location');
|
|
if (isOther) {
|
|
block.find('.stockist-name').val(otherName.val());
|
|
block.find('.stockist-id').val('');
|
|
if (!keepAddress) {
|
|
location.val('');
|
|
}
|
|
return;
|
|
}
|
|
otherName.val('').prop('required', false);
|
|
block.find('.stockist-name').val(chosen ? option.textContent : '');
|
|
location.val(chosen ? (option.getAttribute('data-location') || '') : '');
|
|
block.find('.stockist-id').val(chosen ? (option.getAttribute('data-stockist-id') || '') : '');
|
|
}
|
|
|
|
function setVisit(type) {
|
|
var doctor = type !== 'chemist';
|
|
$('#doctor-fields').prop('disabled', !doctor).toggle(doctor);
|
|
$('#chemist-fields').prop('disabled', doctor).toggle(!doctor);
|
|
$('#field-form_id').val(doctor ? doctorFormId : chemistFormId);
|
|
if (doctor && !$('#field-doctor_name').val()) {
|
|
$('#field-doctor_name').val('Dr. ');
|
|
}
|
|
if (doctor) {
|
|
lockDoctorPrefix();
|
|
}
|
|
refreshSections();
|
|
if (doctor) {
|
|
$('#chemist-fields .rcpa-doctor').each(function () {
|
|
if ($(this).data('select2')) {
|
|
$(this).select2('destroy');
|
|
}
|
|
});
|
|
} else {
|
|
initDoctorSearch();
|
|
}
|
|
}
|
|
|
|
function initDoctorSearch() {
|
|
if ($('#field-visit_type').val() !== 'chemist') {
|
|
return;
|
|
}
|
|
$('#chemist-fields .rcpa-doctor').each(function () {
|
|
var field = $(this);
|
|
if (field.data('select2')) {
|
|
field.select2('destroy');
|
|
}
|
|
field.select2({
|
|
width: '100%',
|
|
dropdownParent: $('.quick-action-form-card'),
|
|
placeholder: 'Search',
|
|
allowClear: true
|
|
});
|
|
field.off('select2:open.rcpa').on('select2:open.rcpa', function () {
|
|
var search = document.querySelector('.select2-container--open .select2-search__field');
|
|
if (search) {
|
|
search.placeholder = 'Search';
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function lockDoctorPrefix() {
|
|
var input = document.getElementById('field-doctor_name');
|
|
if (!input || input.value.indexOf('Dr. ') === 0) {
|
|
return;
|
|
}
|
|
var rest = input.value.replace(/^Dr\.\s*/i, '');
|
|
var caret = input.selectionStart || 0;
|
|
input.value = 'Dr. ' + rest;
|
|
var next = Math.max(4, caret);
|
|
if (document.activeElement === input) {
|
|
input.setSelectionRange(next, next);
|
|
}
|
|
}
|
|
|
|
function assignPhoto(targetId, file) {
|
|
var input = document.getElementById(targetId);
|
|
if (!input || !file) {
|
|
return;
|
|
}
|
|
var transfer = new DataTransfer();
|
|
transfer.items.add(file);
|
|
input.files = transfer.files;
|
|
var chosen = input.parentNode.querySelector('.photo-chosen');
|
|
if (chosen) {
|
|
chosen.textContent = file.name;
|
|
}
|
|
$(input).trigger('change');
|
|
}
|
|
|
|
$('#report-form').on('click', '[data-photo-take], [data-photo-gallery]', function () {
|
|
var target = this.getAttribute('data-photo-take') || this.getAttribute('data-photo-gallery');
|
|
var mode = this.getAttribute('data-photo-take') ? 'camera' : 'gallery';
|
|
var picker = this.parentNode.parentNode.querySelector('[data-photo-picker="' + target + '"][data-photo-mode="' + mode + '"]');
|
|
if (picker) {
|
|
picker.click();
|
|
}
|
|
});
|
|
|
|
$('#report-form').on('change', '[data-photo-picker]', function () {
|
|
var file = this.files && this.files[0];
|
|
if (file) {
|
|
assignPhoto(this.getAttribute('data-photo-picker'), file);
|
|
}
|
|
this.value = '';
|
|
});
|
|
|
|
function showSavedPhoto(field, url) {
|
|
var link = document.createElement('a');
|
|
link.href = url;
|
|
link.target = '_blank';
|
|
link.rel = 'noopener';
|
|
link.className = 'd-block mt-1';
|
|
link.textContent = 'View current photo';
|
|
field.parentNode.appendChild(link);
|
|
field.removeAttribute('required');
|
|
if (!field.parentNode.querySelector('input.saved-file[name="' + field.name + '"]')) {
|
|
var hidden = document.createElement('input');
|
|
hidden.type = 'hidden';
|
|
hidden.name = field.name;
|
|
hidden.value = url;
|
|
hidden.className = 'saved-file';
|
|
field.parentNode.appendChild(hidden);
|
|
}
|
|
}
|
|
|
|
function applySaved() {
|
|
if (!$('#field-submission_id').val()) {
|
|
return;
|
|
}
|
|
var root = $(activeRoot());
|
|
Object.keys(savedAnswers).forEach(function (code) {
|
|
if (photoFields.indexOf(code) !== -1 || /_charge$/.test(code)) {
|
|
return;
|
|
}
|
|
var fields = root.find('[name="' + code + '"]');
|
|
if (!fields.length && code === 'record_date') {
|
|
fields = $('#field-record_date');
|
|
}
|
|
fields.each(function () {
|
|
if (this.type === 'file') {
|
|
return;
|
|
}
|
|
if (this.type === 'radio' || this.type === 'checkbox') {
|
|
this.checked = this.value === String(savedAnswers[code]);
|
|
} else {
|
|
var savedValue = savedAnswers[code];
|
|
if ((code === 'total_patients' || code === 'respiratory_patients') && typeof savedValue === 'string') {
|
|
savedValue = savedValue.replace(/^(\d+)-(\d+)$/, '$1 to $2');
|
|
}
|
|
this.value = savedValue;
|
|
}
|
|
});
|
|
});
|
|
if (!savedAnswers.doctor_other_degree && savedAnswers.other_degree) {
|
|
$('#field-doctor_other_degree').val(savedAnswers.other_degree);
|
|
}
|
|
if (!savedAnswers.doctor_other_speciality && (savedAnswers.other_specialty || savedAnswers.other_speciality)) {
|
|
$('#field-doctor_other_speciality').val(savedAnswers.other_specialty || savedAnswers.other_speciality);
|
|
}
|
|
if ($('#field-visit_type').val() !== 'chemist') {
|
|
lockDoctorPrefix();
|
|
}
|
|
refreshSections();
|
|
Object.keys(savedAnswers).forEach(function (code) {
|
|
if (!/_charge$/.test(code)) {
|
|
return;
|
|
}
|
|
root.find('[name="' + code + '"]').val(savedAnswers[code]);
|
|
});
|
|
photoFields.forEach(function (code) {
|
|
if (!savedAnswers[code]) {
|
|
return;
|
|
}
|
|
var field = document.getElementById('field-' + code);
|
|
if (!field || !root.find(field).length) {
|
|
return;
|
|
}
|
|
showSavedPhoto(field, savedAnswers[code]);
|
|
});
|
|
syncStatusFields(root);
|
|
}
|
|
|
|
function selectSavedOption(select, label) {
|
|
if (!label) {
|
|
return;
|
|
}
|
|
for (var i = 0; i < select.options.length; i++) {
|
|
if (select.options[i].textContent === label) {
|
|
select.value = select.options[i].value;
|
|
return;
|
|
}
|
|
}
|
|
var option = document.createElement('option');
|
|
option.value = label;
|
|
option.textContent = label;
|
|
select.appendChild(option);
|
|
select.value = label;
|
|
}
|
|
|
|
function matchSavedContacts() {
|
|
if (!$('#field-submission_id').val() || $('#field-visit_type').val() !== 'chemist') {
|
|
return;
|
|
}
|
|
document.querySelectorAll('#chemist-fields .rcpa-doctor').forEach(function (select) {
|
|
selectSavedOption(select, $(select).closest('tr').find('.rcpa-name').val());
|
|
});
|
|
document.querySelectorAll('#chemist-fields .stockist-pick').forEach(function (select) {
|
|
var block = $(select).closest('.stockist-block');
|
|
var name = block.find('.stockist-name').val();
|
|
if (!name) {
|
|
return;
|
|
}
|
|
var matched = false;
|
|
for (var i = 0; i < select.options.length; i++) {
|
|
if (select.options[i].value !== 'others' && select.options[i].textContent === name) {
|
|
select.value = select.options[i].value;
|
|
matched = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!matched) {
|
|
select.value = 'others';
|
|
block.find('.stockist-other-name').val(name);
|
|
applyStockist(select, true);
|
|
return;
|
|
}
|
|
applyStockist(select, true);
|
|
});
|
|
}
|
|
|
|
function syncStatusFields(root) {
|
|
$(root || '#report-form').find('select.status-source').each(function () {
|
|
$(this).siblings('input.status-value').val(this.value === 'Yes' ? '1' : '0');
|
|
});
|
|
}
|
|
|
|
$('#field-doctor_name').on('input', lockDoctorPrefix);
|
|
$('#doctor-fields').on('change', '.product-choice', function () {
|
|
syncProductUnits();
|
|
syncStatusFields(this.parentNode);
|
|
});
|
|
$('#chemist-fields').on('input', '.stockist-other-name', function () {
|
|
$(this).closest('.stockist-block').find('.stockist-name').val(this.value);
|
|
});
|
|
$('#field-visit_type').on('change', function () { setVisit(this.value); });
|
|
$('#report-form').on('input change', '.survey-section :input', refreshSections);
|
|
$('#chemist-fields').on('change', '.rcpa-doctor', function () { applyDoctor(this); refreshSections(); });
|
|
$('#chemist-fields').on('change', '.stockist-pick', function () { applyStockist(this); });
|
|
$('#chemist-fields').on('change', '.service-choice', syncServiceFields);
|
|
|
|
$('#field-degree, #field-specialty').on('change', syncOtherFields);
|
|
$('#report-form').on('change', 'select.status-source', function () {
|
|
syncStatusFields(this.parentNode);
|
|
});
|
|
|
|
function fail(text) {
|
|
Swal.fire({ icon: 'error', title: 'Please check the form', text: text });
|
|
}
|
|
|
|
function alphaOk(value) {
|
|
return /^[A-Za-z][A-Za-z .]*$/.test(value);
|
|
}
|
|
|
|
function alnumOk(value) {
|
|
return /^[A-Za-z0-9]+$/.test(value);
|
|
}
|
|
|
|
function addressOk(value) {
|
|
return /^[A-Za-z0-9]+(?:[ ]+[A-Za-z0-9]+)*$/.test(value);
|
|
}
|
|
|
|
function stockistAddressOk(value) {
|
|
return /^(?=.*[A-Za-z0-9])[A-Za-z0-9 ,.\-\/#&()']+$/.test(value);
|
|
}
|
|
|
|
function doctorAddressOk(value) {
|
|
return /^[A-Za-z0-9]+(?:[ ,]+[A-Za-z0-9]+)*$/.test(value);
|
|
}
|
|
|
|
function therapiesFilled(root) {
|
|
var filled = false;
|
|
$(root).find('.therapy-units').each(function () {
|
|
if (this.value !== '') {
|
|
filled = true;
|
|
}
|
|
});
|
|
return filled;
|
|
}
|
|
|
|
function normalizedName(value) {
|
|
return String(value || '').trim().replace(/\s+/g, ' ').toLowerCase();
|
|
}
|
|
|
|
function contactPincode(contact) {
|
|
return String((contact && (contact.pincode || contact.chemist_pincode || contact.doctor_pincode)) || '').trim();
|
|
}
|
|
|
|
function identityTaken(type, name, mobile, pincode) {
|
|
var wantedName = normalizedName(name);
|
|
var wantedPhone = contactPhone({ phone: mobile });
|
|
var editingId = $('#field-submission_id').val() ? String(savedAnswers.contact_id || '') : '';
|
|
return surveyContacts.some(function (item) {
|
|
var contact = item.contact || {};
|
|
if (String(contact.type || '').toLowerCase() !== type) {
|
|
return false;
|
|
}
|
|
if (editingId && String(item.value || contact.id || '') === editingId) {
|
|
return false;
|
|
}
|
|
if (normalizedName(contact.name) !== wantedName || contactPhone(contact) !== wantedPhone) {
|
|
return false;
|
|
}
|
|
if (type !== 'ebt_chemist') {
|
|
return true;
|
|
}
|
|
var existingPin = contactPincode(contact);
|
|
if (!existingPin) {
|
|
return true;
|
|
}
|
|
return existingPin === String(pincode || '').trim();
|
|
}) || submittedIdentities.some(function (item) {
|
|
if (item.type !== type) {
|
|
return false;
|
|
}
|
|
if (normalizedName(item.name) !== wantedName || contactPhone({ phone: item.phone }) !== wantedPhone) {
|
|
return false;
|
|
}
|
|
if (type !== 'ebt_chemist') {
|
|
return true;
|
|
}
|
|
var savedPin = String(item.pincode || '').trim();
|
|
if (!savedPin) {
|
|
return true;
|
|
}
|
|
return savedPin === String(pincode || '').trim();
|
|
});
|
|
}
|
|
|
|
function rememberSubmission(type) {
|
|
if (type === 'doctor') {
|
|
submittedIdentities.push({
|
|
type: 'ebt_doctor',
|
|
name: $('#field-doctor_name').val(),
|
|
phone: $('#field-mobile_number').val(),
|
|
pincode: ''
|
|
});
|
|
return;
|
|
}
|
|
submittedIdentities.push({
|
|
type: 'ebt_chemist',
|
|
name: $('#field-chemist_name').val(),
|
|
phone: $('#field-chemist_mobile').val(),
|
|
pincode: $('#field-chemist_pincode').val()
|
|
});
|
|
}
|
|
|
|
$('#report-form').on('submit', function (event) {
|
|
event.preventDefault();
|
|
var type = $('#field-visit_type').val();
|
|
var recordDate = $('#field-record_date').val();
|
|
if (!recordDate) {
|
|
fail('Date is required.');
|
|
return;
|
|
}
|
|
if (type === 'doctor') {
|
|
var doctorName = $('#field-doctor_name').val().trim();
|
|
if (!alphaOk(doctorName)) {
|
|
fail('Doctor name can contain letters, spaces and periods only.');
|
|
return;
|
|
}
|
|
if (!/^[6-9][0-9]{9}$/.test($('#field-mobile_number').val())) {
|
|
fail('Mobile number must be 10 digits and start with 6, 7, 8 or 9.');
|
|
return;
|
|
}
|
|
var registration = $('#field-doctor_registration_number').val().trim();
|
|
if (registration && !alnumOk(registration)) {
|
|
fail('Registration number must be alphanumeric with no special characters.');
|
|
return;
|
|
}
|
|
var legacy = $('#field-legacy_code').val().trim();
|
|
if (legacy && !alnumOk(legacy)) {
|
|
fail('Legacy code must be alphanumeric with no special characters.');
|
|
return;
|
|
}
|
|
if (!contactsReady) {
|
|
fail('Contact list is still loading. Please try again.');
|
|
return;
|
|
}
|
|
if (identityTaken('ebt_doctor', doctorName, $('#field-mobile_number').val())) {
|
|
fail('A doctor with this mobile number already exists.');
|
|
return;
|
|
}
|
|
if (!doctorAddressOk($('#field-doctor_location').val().trim())) {
|
|
fail('Doctor address can contain letters, numbers, spaces and commas only.');
|
|
return;
|
|
}
|
|
if (!/^[1-9][0-9]{5}$/.test($('#field-doctor_pincode').val())) {
|
|
fail('Doctor pincode must be 6 digits and must not start with 0.');
|
|
return;
|
|
}
|
|
syncOtherFields();
|
|
if ($('#field-degree').val() === 'Other' && !alphaOk($('#field-doctor_other_degree').val().trim())) {
|
|
fail('Enter the other degree in letters only.');
|
|
return;
|
|
}
|
|
if ($('#field-specialty').val() === 'Other' && !alphaOk($('#field-doctor_other_speciality').val().trim())) {
|
|
fail('Enter the other specialty in letters only.');
|
|
return;
|
|
}
|
|
var missingUnits = false;
|
|
$('#doctor-fields .product-choice').each(function () {
|
|
if ($(this).val() !== 'Yes') {
|
|
return;
|
|
}
|
|
var units = $(this).closest('.product-row').find('.product-units input').val();
|
|
if (units === '' || !/^\d+$/.test(units)) {
|
|
missingUnits = true;
|
|
}
|
|
});
|
|
if (missingUnits) {
|
|
fail('Enter the number of units in a month for each product marked Yes. 0 is allowed.');
|
|
return;
|
|
}
|
|
if (!therapiesFilled('#doctor-fields')) {
|
|
fail('Enter at least one other therapy. 0 is allowed.');
|
|
return;
|
|
}
|
|
var photos = ['visiting_card', 'rx_pad', 'doctor_board'];
|
|
var hasPhoto = false;
|
|
for (var i = 0; i < photos.length; i++) {
|
|
var file = document.getElementById('field-' + photos[i]).files[0];
|
|
if (!file) continue;
|
|
hasPhoto = true;
|
|
if (!/\.(jpe?g|png)$/i.test(file.name) || file.size > 10 * 1024 * 1024) {
|
|
fail('Photos must be JPG, JPEG or PNG and no larger than 10 MB.');
|
|
return;
|
|
}
|
|
}
|
|
var savedDoctorPhoto = $('#field-submission_id').val() && photoFields.slice(0, 3).some(function (code) { return savedAnswers[code]; });
|
|
if (!hasPhoto && !savedDoctorPhoto) {
|
|
fail('Upload at least one of visiting card, Rx pad or doctor board.');
|
|
return;
|
|
}
|
|
} else {
|
|
if (!alphaOk($('#field-chemist_name').val().trim())) {
|
|
fail('Chemist name can contain letters, spaces and periods only.');
|
|
return;
|
|
}
|
|
if (!doctorAddressOk($('#field-chemist_location').val().trim())) {
|
|
fail('Chemist address can contain letters, numbers, spaces and commas only.');
|
|
return;
|
|
}
|
|
if (!/^[6-9][0-9]{9}$/.test($('#field-chemist_mobile').val())) {
|
|
fail('Mobile number must be 10 digits and start with 6, 7, 8 or 9.');
|
|
return;
|
|
}
|
|
if (!contactsReady) {
|
|
fail('Contact list is still loading. Please try again.');
|
|
return;
|
|
}
|
|
if (identityTaken('ebt_chemist', $('#field-chemist_name').val(), $('#field-chemist_mobile').val(), $('#field-chemist_pincode').val())) {
|
|
fail('A chemist with this mobile number already exists.');
|
|
return;
|
|
}
|
|
var badStockistName = false;
|
|
var badStockistAddress = false;
|
|
$('#chemist-fields .stockist-pick').each(function () {
|
|
if (this.value !== 'others') {
|
|
return;
|
|
}
|
|
var block = $(this).closest('.stockist-block');
|
|
var otherName = block.find('.stockist-other-name').val().trim();
|
|
var otherAddress = block.find('.stockist-location').val().trim();
|
|
block.find('.stockist-name').val(otherName);
|
|
if (!addressOk(otherName)) {
|
|
badStockistName = true;
|
|
}
|
|
if (!stockistAddressOk(otherAddress)) {
|
|
badStockistAddress = true;
|
|
}
|
|
});
|
|
if (badStockistName) {
|
|
fail('Other stockist name can contain letters, numbers and spaces only.');
|
|
return;
|
|
}
|
|
if (badStockistAddress) {
|
|
fail('Other stockist address can contain letters, numbers, spaces and symbols such as comma, period, hyphen, slash, hash, ampersand, parentheses and apostrophe.');
|
|
return;
|
|
}
|
|
if (!therapiesFilled('#chemist-fields')) {
|
|
fail('Enter at least one other therapy. 0 is allowed.');
|
|
return;
|
|
}
|
|
var missingCharge = false;
|
|
$('#chemist-fields .service-choice').each(function () {
|
|
if ($(this).val() === 'Yes' && !$(this).closest('.service-row').find('.service-charge select').val()) {
|
|
missingCharge = true;
|
|
}
|
|
});
|
|
if (missingCharge) {
|
|
fail('Select Free or Chargeable for each service marked Yes.');
|
|
return;
|
|
}
|
|
if (!/^[1-9][0-9]{5}$/.test($('#field-chemist_pincode').val())) {
|
|
fail('Chemist pincode must be 6 digits and must not start with 0.');
|
|
return;
|
|
}
|
|
var board = document.getElementById('field-chemist_board').files[0];
|
|
if (board && (!/\.(jpe?g|png)$/i.test(board.name) || board.size > 10 * 1024 * 1024)) {
|
|
fail('Chemist board must be a JPG, JPEG or PNG no larger than 10 MB.');
|
|
return;
|
|
}
|
|
if (!board && !($('#field-submission_id').val() && savedAnswers.chemist_board)) {
|
|
fail('Chemist board must be a JPG, JPEG or PNG no larger than 10 MB.');
|
|
return;
|
|
}
|
|
}
|
|
|
|
var emptyFiles = $('#report-form input[type="file"]').filter(function () {
|
|
return !this.files || !this.files.length;
|
|
});
|
|
emptyFiles.prop('disabled', true);
|
|
syncStatusFields(activeRoot());
|
|
$('#pageloader').css('display', 'flex').hide().fadeIn();
|
|
$.ajax({
|
|
type: 'POST',
|
|
url: '/form/ajaxsubmit',
|
|
data: new FormData($('#report-form')[0]),
|
|
processData: false,
|
|
contentType: false,
|
|
success: function (data) {
|
|
emptyFiles.prop('disabled', false);
|
|
$('#pageloader').fadeOut();
|
|
if (data.status == 'success') {
|
|
rememberSubmission(type);
|
|
Swal.fire({ title: 'Great!', text: 'Your data is successfully submitted!', icon: 'success' });
|
|
$('#report-form .saved-file').remove();
|
|
$('#report-form .photo-chosen').text('');
|
|
$('#report-form a').filter(function () { return $(this).text() === 'View current photo'; }).remove();
|
|
$('#report-form')[0].reset();
|
|
$('#field-submission_id').val('').attr('value', '');
|
|
$('#field-visit_type').val('doctor');
|
|
setVisit('doctor');
|
|
loadContacts();
|
|
} else {
|
|
Swal.fire({ icon: 'error', title: 'Data not submitted', text: 'Please try again.' });
|
|
}
|
|
},
|
|
error: function () {
|
|
emptyFiles.prop('disabled', false);
|
|
$('#pageloader').fadeOut();
|
|
Swal.fire({ icon: 'error', title: 'Data not submitted', text: 'Please try again.' });
|
|
}
|
|
});
|
|
});
|
|
|
|
var openingType = ($('#field-form_id').val() === chemistFormId || @json($form->form_id ?? '') === chemistFormId) ? 'chemist' : 'doctor';
|
|
$('#field-visit_type').val(openingType);
|
|
setVisit(openingType);
|
|
applySaved();
|
|
loadContacts();
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|