Commit Graph

518 Commits

Author SHA1 Message Date
d025111bcc Fix: IPD discharge template delete not working
ISSUE: Delete button executed but did not actually delete templates
ROUTE: /setting/template/discharge_templates/delete/{id}

ROOT CAUSE:
The delete button's fetch() call was sending 'new FormData(form)' which:
1. Included entire edit form data (template_html, header_html, footer_html)
2. These are HUGE LONGTEXT fields that can cause request payload issues
3. CSRF token from form might be stale or missing if no template is being edited
4. Server received malformed request and silently failed

SOLUTION:
- Create fresh FormData with ONLY the CSRF token
- Delete request now lightweight and reliable
- Added success message display after successful delete
- Server-side delete logic already correct (line 2969-3028 in Template.php)

CHANGED:
- app/Views/Setting/Template/discharge_templates.php (line 618-653)
  - Replace: body: new FormData(form)
  - With: body: deleteFormData (contains only CSRF token)
  - Added: setNotice('success', ...) to show success message

TESTING:
1. Go to /setting/template/discharge_templates
2. Click Delete on any non-default template
3. Confirm deletion
4. Template should now be removed from list
5. Success message should display
2026-06-07 11:11:41 +05:30
a929100d4d Add text alignment and enhanced CKEditor tools to IPD discharge templates
ENHANCEMENT: Expanded CKEditor toolbar in discharge template editor

NEW TOOLBAR BUTTONS:
1. Text Alignment:
   - JustifyLeft (align left)
   - JustifyCenter (align center)
   - JustifyRight (align right)
   - JustifyBlock (justify)

2. Enhanced Basic Styles:
   - Strike (strikethrough)
   - Subscript
   - Superscript

3. Enhanced Insert Tools:
   - Image
   - PageBreak

4. Enhanced Tools:
   - ShowBlocks (show/hide block elements)

CONFIGURATION:
- Added extraPlugins: 'justify,pagebreak' to enable alignment and page break
- Added allowedContent: true to allow all HTML content
- Added entities: false, basicEntities: false to preserve special characters
- Added fillEmptyBlocks: false for cleaner HTML output

FILE: app/Views/Setting/Template/discharge_templates.php (lines 473-495)
ROUTE: /setting/template/discharge_template_get/{id}

BENEFIT: Template editors now have full control over text alignment, formatting, and layout
2026-06-07 11:07:29 +05:30
ea07063675 Fix: Prevent duplicate medicine saves on 'Save Discharge Advice' button
ISSUE: Medicines were being saved twice - once via Add button AJAX, and again when clicking 'Save Discharge Advice' button.

ROOT CAUSE:
1. Legacy code in save_main handler (lines 5489-5538) read 'discharge_medicine_json' POST field
2. JavaScript serializeDischargeMedicineTable() collected all medicine table rows into JSON
3. On form submit (Save Discharge Advice), JSON was sent and controller re-inserted all medicines
4. Result: duplicate entries in ipd_discharge_prescrption_prescribed table

SOLUTION:
- DISABLED controller code that processes discharge_medicine_json in save_main handler
- DISABLED JavaScript call to serializeDischargeMedicineTable() on form submit
- Medicines now saved ONLY via Add button AJAX (action='add_drug')
- Save Discharge Advice button now only saves dietary advice, instructions, review after, etc.

TESTING:
1. Add medicines via Add button (saves immediately)
2. Click Save Discharge Advice (does NOT save medicines again)
3. No duplicate entries in database
2026-06-07 09:03:12 +05:30
783ddcbacc Fix discharge medications: add Dosage column with English+Hindi labels
- Add getDoseMasterRows() to load dose masters (opd_dose_shed, opd_dose_when, opd_dose_frequency)
- Add buildDosageDisplay() to format dosage: 'BID (TWO TIME A DAY) / दिन में दो बार लेना'
- Update buildAutoDischargeContent() medication table query to include dosage, dosage_when, dosage_freq fields
- Change table columns from '#|Medicine|Qty|Days|Notes' to 'Medicine Name|Dosage|Qty|Day' matching old CI3 format
- Dosage column shows full text labels from master tables, not numeric IDs
- Add composition support as small text under medicine name for legacy compatibility
- Remove # serial number column and Notes column to match old HMS format
2026-06-07 08:43:45 +05:30
89c150324f Add refresh parameter to force regenerate cached discharge content in PDF
Problem:
- PDF shows old cached content with 'Date of Discharge: 2026-06-05'
- Debug page shows correct new format (status header only)
- Discharge content is cached in ipd_discharge table
- PDF generation uses cached content from database
- Fix only affects newly generated content, not cached content

Root Cause:
When discharge is first created, content is generated and saved to DB.
PDF generation flow (show_discharge method):
1. Load cached content from ipd_discharge table
2. Only regenerate if content is empty
3. Apply template and render PDF

So old discharges still have old format cached.

Solution:
Added 'refresh' parameter to show_discharge() method:
- If refresh=1, ignore cached content and regenerate
- Regenerate using buildAutoDischargeContent() (has fix)
- Save new content to database
- Then render PDF

Code Change (line 5900-5913):
Added:
  \ = (int) (\->request->getGet('refresh') ?? 0) === 1;

Changed:
  if (\ || trim(strip_tags(\)) === '') {

Usage:
1. Original URL (uses cache):
   http://localhost:8080/Ipd_discharge/show_discharge/1/1?tpl=1

2. Force refresh (regenerates):
   http://localhost:8080/Ipd_discharge/show_discharge/1/1?tpl=1&refresh=1

3. After first refresh, cache is updated with new format
   Subsequent views will use new cached content

Note:
- preview_discharge_report() already has 'regen=1' parameter
- Same functionality, different parameter name
- Both methods now support force regeneration

For existing discharges:
- Add &refresh=1 to URL once to update cache
- Or recreate discharge from edit page
- Or clear ipd_discharge.content for that IPD ID"
2026-06-07 08:24:38 +05:30
aa7ed25a50 \Update documentation - {{DISCHARGE_SUMMARY}} now header only
Updated DISCHARGE_PLACEHOLDER_HTML_REFERENCE.md to reflect that:

- {{DISCHARGE_SUMMARY}} now contains ONLY the status header
- Removed department/doctor/date info from example
- Added note about using separate tokens for additional info:
  - {{DEPARTMENT}}
  - {{DOCTOR_NAMES}}
  - {{ADMIT_DATE_ONLY}}
  - {{DISCHARGE_DATE_ONLY}}
  - {{ADMISSION_TIME}}
  - {{DISCHARGE_TIME}}

Example updated to show clean header only:
<h4 class='discharge-section-heading'>Discharge Summary</h4>
OR
<h4 class='discharge-section-heading'>Leave Against Medical Advice</h4>

Documentation now matches actual implementation."
2026-06-07 08:12:36 +05:30
d096585df6 \Fix {{DISCHARGE_SUMMARY}} placeholder - remove extra information
Problem:
- {{DISCHARGE_SUMMARY}} placeholder was showing:
  1. Discharge status header (Discharge Summary/LAMA/etc.)
  2. Department information
  3. Treating Doctor(s) information
  4. Date of Admission
- User wants ONLY the status header in this placeholder
- Extra information should be removed from {{DISCHARGE_SUMMARY}}

Solution:
Modified buildAutoDischargeContent() method:

Before (lines 1679-1703):
- Built dischargeSummaryParts array with department/doctor/date
- Added all info in one section with header + div containing details

After:
- {{DISCHARGE_SUMMARY}} now contains ONLY the status header
- Removed department, doctor, and admission date from this section
- Status header comes from ipd_discharg_status table via:
  ipd_master.ipd_status (or discarge_patient_status) = ipd_discharg_status.id
  Returns ipd_discharg_status.status_details field

Status Values:
- ipd_status = 0 → 'Discharge Summary' (hardcoded)
- ipd_status = 1 → From DB: 'Discharge Summary'
- ipd_status = 2 → From DB: 'Leave Against Medical Advice'
- ipd_status = 3 → From DB: 'Refer To Higher Centre'
- ipd_status = 5 → From DB: 'Dead Summary'
- ipd_status = 7 → From DB: 'Discharge on Request'

Now {{DISCHARGE_SUMMARY}} outputs:
<h4 class='discharge-section-heading'>Discharge Summary</h4>
OR
<h4 class='discharge-section-heading'>Leave Against Medical Advice</h4>

That's it - no extra lines below.

If user needs department/doctor/date info, they can use individual tokens:
- {{DEPARTMENT}}
- {{DOCTOR_NAMES}}
- {{ADMIT_DATE_ONLY}}
- {{DISCHARGE_DATE_ONLY}}
- {{ADMISSION_TIME}}
- {{DISCHARGE_TIME}}

Or use {{PATIENT_INFO_TABLE}} which has all patient demographics.

Testing:
- Visit: /Ipd_discharge/debug_discharge_html/1
- Check {{DISCHARGE_SUMMARY}} section
- Should only show status header, no other info"
2026-06-07 08:12:03 +05:30
751fdfaedb Remove all inline styles and use semantic CSS classes
Problem:
- Inline styles in HTML override custom CSS defined in templates
- User's custom styles (font-family, table borders, padding) not applied
- Hard to maintain and customize styling
- Inline styles have higher specificity than class/ID selectors

Solution:
Replaced all inline styles with semantic CSS classes:

Section Headings:
- style='margin:16px 0 8px 0' → class='discharge-section-heading' (h4)
- style='text-align:center;margin:...' → class='discharge-title' (h2)

Lists:
- style='margin:0 0 10px 20px' → class='discharge-list' (ul/ol)
- style='margin:4px 0 8px 20px' → class='discharge-sublist' (ul)
- style='list-style:none' → class='no-bullet' (li)

Text:
- style='color:#475569' → class='discharge-remark' (span)

Tables:
- style='width:100%;border-collapse:...' → class='discharge-table'
- Medicine table → class='discharge-medicine-table'
- Patient info → class='discharge-info-table'
- Signature block → class='discharge-signature-table'
- Removed all inline width/align styles from th/td

Containers:
- style='margin-bottom:8px' → class='discharge-field'
- style='margin-top:6px' → class='discharge-footer'
- style='margin-bottom:6px' → class='discharge-item'
- Added class='discharge-section' for section wrappers

Separators:
- style='margin:1px;padding:0' → class='discharge-separator' (hr)

Changes Made:
1. app/Controllers/Ipd_discharge.php:
   - buildAutoDischargeContent() - All section builders
   - buildAutoDischargeSummaryTable() - Patient info table
   - addListSection() - List sections
   - buildNursingTrendSection() - Nursing trends
   - defaultDischargeTemplateHtml() - Default template
   - nabhDischargeTemplateHtml() - NABH template

2. Created DISCHARGE_CSS_CLASSES.md:
   - Complete CSS class reference
   - Suggested stylesheet with all classes
   - Customization guide
   - Example usage
   - Print optimization styles

Benefits:
✓ User's custom CSS now applies correctly
✓ No style conflicts or overrides
✓ Easy to customize via CSS
✓ Consistent styling across all sections
✓ Maintainable - change CSS once, applies everywhere
✓ Print-friendly with @media print rules

Usage:
1. Copy CSS from DISCHARGE_CSS_CLASSES.md
2. Add to discharge template 'template_css' field
3. Or add to global print CSS
4. Customize colors/spacing/fonts as needed

The existing body/table/td/p styles will now work properly
No inline styles to override custom CSS anymore
2026-06-07 07:56:11 +05:30
c65a5cdc68 Add routes for debug endpoints - fix 404 errors
Problem:
- GET: Ipd_discharge/debug_discharge_html/1 returning 404
- GET: Ipd_discharge/debug_ipd_fields/1 returning 404
- Debug endpoints were created but routes were not registered

Solution:
Added two new routes in app/Config/Routes.php:

1. Debug IPD Fields Endpoint:
   Route: Ipd_discharge/debug_ipd_fields/(:num)
   Controller: Ipd_discharge::debug_ipd_fields/\
   Method: GET

   Returns JSON showing:
   - All available field names in ipd_master table
   - Values of time fields (reg_time, discharge_time)
   - Values of department fields (dept_id)
   - Values of doctor fields (r_doc_id, r_doc_name)
   - Result of getDischargeDepartmentName() function
   - Result of getDischargeDoctorNames() function
   - Result of safeTime() for time fields

2. Debug Discharge HTML Endpoint:
   Route: Ipd_discharge/debug_discharge_html/(:num)
   Controller: Ipd_discharge::debug_discharge_html/\
   Method: GET

   Returns HTML page showing:
   - Complete discharge summary HTML source
   - Syntax highlighted for easy reading
   - All placeholders replaced with actual values
   - Useful for checking spacing and HTML structure

Route Placement:
- Added after search_patient_ajax route
- Before ipd_select route
- Follows existing Ipd_discharge route pattern
- Uses (:num) parameter for IPD ID

Usage:
- http://localhost:8080/Ipd_discharge/debug_ipd_fields/1
- http://localhost:8080/Ipd_discharge/debug_discharge_html/1
- Replace '1' with actual IPD ID

Note: Routes must be defined BEFORE more general routes
Otherwise, the (:num) pattern in ipd_select would catch them first

Testing:
1. Pull latest code
2. Access /Ipd_discharge/debug_ipd_fields/1
3. Should return JSON with field values
4. Access /Ipd_discharge/debug_discharge_html/1
5. Should return HTML source page
2026-06-07 07:48:43 +05:30
0b838fd6b2 Add comprehensive HTML reference for all discharge placeholders
Created detailed documentation showing exact HTML structure for each discharge summary placeholder.

Document includes:

Section Placeholders:
- {{DISCHARGE_SUMMARY}} - Dynamic status-based header + department/doctor info
- {{PRESENTING_COMPLAINTS}} - Complaints list with remarks
- {{PAIN_MEASUREMENT_SCALE}} - Pain scale value
- {{GENERAL_EXAM_ADMISSION}} - Admission vitals and exam
- {{PERSONAL_HISTORY}} - Patient history flags (smoking, alcohol, etc.)
- {{DRUG_ALLERGY_ADR}} - Allergy and ADR information
- {{CO_MORBIDITIES}} - Comorbid conditions
- {{CLINICAL_INVESTIGATION_REPORTS}} - Lab reports table/list
- {{FINAL_DIAGNOSIS}} - Final diagnosis list
- {{COURSE_IN_HOSPITAL}} - Hospital course narrative
- {{EXAMINATION_ON_DISCHARGE}} - Discharge examination findings
- {{SURGERY}} - Surgery details table
- {{PROCEDURE}} - Procedure details table
- {{DISCHARGE_MEDICATIONS}} - Medicine table or list
- {{DIETARY_ADVICE}} - Dietary recommendations
- {{DISCHARGE_INSTRUCTIONS}} - Advice, instructions, review dates
- {{SIGNATURE_BLOCK}} - Signature table

Additional Tokens:
- {{PATIENT_INFO_TABLE}} - Patient demographics table

For each placeholder:
- Shows exact HTML structure with styling
- Includes CSS inline styles used
- Shows alternate formats when applicable
- Indicates when placeholder is empty/not shown
- Shows color codes (#475569 for remarks)

Style Guidelines:
- Heading margins: 16px top, 8px bottom
- List indentation: 20px left
- Remark color: #475569 (gray)
- Table borders: collapse style
- All user input escaped with esc()

Debug Endpoints:
- /Ipd_discharge/debug_discharge_html/{ipdId} - View full HTML
- /Ipd_discharge/debug_ipd_fields/{ipdId} - View field values

Usage:
- Reference for template designers
- Documentation for HTML structure
- Guide for placeholder customization
- Debug tool for spacing issues

File: DISCHARGE_PLACEHOLDER_HTML_REFERENCE.md
Location: Project root directory
2026-06-07 07:46:45 +05:30
3c430fa386 Fix discharge status header in auto-generated content and remove Date of Discharge
CRITICAL FIX - User Requirement:
- If ipd_status = 0: Show 'Discharge Summary' only
- If ipd_status != 0: Show value from ipd_discharge_status.status_details
- Remove 'Date of Discharge' line completely

Problem:
- {{DISCHARGE_SUMMARY}} still showed 'Discharge Summary' header regardless of status
- 'Date of Discharge: YYYY-MM-DD' line was appearing
- Previous fix only worked on saved content via regex, not on auto-generated content

Root Cause:
- buildAutoDischargeContent() method had hardcoded 'Discharge Summary' header
- getDischargeStatusText() was querying database but header wasn't being used
- Date of Discharge was being added to dischargeSummaryParts array

Solution:

1. Updated buildAutoDischargeContent() method (line ~1670):
   - Removed 'Date of Discharge' from dischargeSummaryParts array
   - Added comment: 'NOTE: Date of Discharge removed per user request'
   - Call getDischargeStatusText() to get dynamic header
   - Use status-based header instead of hardcoded 'Discharge Summary'
   - Now generates: <h4>status_details_value</h4> instead of <h4>Discharge Summary</h4>

2. Updated getDischargeStatusText() method (line ~1047):
   - Added early return for ipd_status = 0: returns 'Discharge Summary'
   - For ipd_status != 0: queries ipd_discharge_status table
   - Returns status_details field (primary)
   - Falls back to status_desc if status_details is empty
   - Final fallback: 'Discharge Summary'

3. Updated buildAutoDischargeSummaryTable() method (line ~1517):
   - Used for {{PATIENT_INFO_TABLE}} token
   - Now uses getDischargeStatusText() for header
   - Changes <h2>Discharge Summary</h2> to <h2>status_value</h2>
   - Keeps patient info table structure unchanged

Status Mapping Logic:
- ipd_status = 0 → 'Discharge Summary' (special case, no DB lookup)
- ipd_status = 1 → Query DB → 'Discharge Summary' (from status_details)
- ipd_status = 2 → Query DB → 'Leave Against Medical Advice'
- ipd_status = 3 → Query DB → 'Refer To Higher Centre'
- ipd_status = 5 → Query DB → 'Dead Summary'
- ipd_status = 7 → Query DB → 'Discharge on Request'

Content Structure Before:
<h4>Discharge Summary</h4>
<div>
<b>Department:</b> General Surgery<br>
<b>Treating Doctor(s):</b> Dr. Name<br>
<b>Date of Admission:</b> 04-06-2026<br>
<b>Date of Discharge:</b> 07-06-2026
</div>

Content Structure After:
<h4>Leave Against Medical Advice</h4>
<div>
<b>Department:</b> General Surgery<br>
<b>Treating Doctor(s):</b> Dr. Name<br>
<b>Date of Admission:</b> 04-06-2026
</div>

Testing:
1. Set ipd_master.ipd_status = 0 → regenerate → should show 'Discharge Summary'
2. Set ipd_master.ipd_status = 1 → regenerate → should show 'Discharge Summary'
3. Set ipd_master.ipd_status = 2 → regenerate → should show 'Leave Against Medical Advice'
4. Set ipd_master.ipd_status = 5 → regenerate → should show 'Dead Summary'
5. 'Date of Discharge' line should NOT appear in any case

Files Modified:
- app/Controllers/Ipd_discharge.php:
  - getDischargeStatusText() - Added ipd_status = 0 check
  - buildAutoDischargeContent() - Removed Date of Discharge, use dynamic header
  - buildAutoDischargeSummaryTable() - Use dynamic header

IMPORTANT: User must REGENERATE discharge summary after pulling code
- Click 'Regenerate' button on discharge preview page
- Or delete existing discharge content and reload form
- Old saved content will still have old header until regenerated
2026-06-07 07:42:38 +05:30
09570e0793 Make discharge summary header dynamic based on discharge status
Problem:
- {{DISCHARGE_SUMMARY}} always showed 'Discharge Summary' header
- User wants header to change based on patient discharge status
- 'Date of Discharge' line was unwanted
- Need to see HTML source to check spacing between placeholders

Solution:

1. Added getDischargeStatusText() method:
   - Queries ipd_discharge_status table
   - Uses ipd_master.ipd_status as lookup key
   - Returns status_details or status_desc from table
   - Fallback: 'Discharge Summary' if status not found

2. Modified buildDischargeSectionTokenVars():
   - Now accepts panelData parameter (has ipd info)
   - Gets discharge status text via getDischargeStatusText()
   - Replaces 'Discharge Summary' heading with status text
   - Removes 'Date of Discharge: YYYY-MM-DD' line
   - Uses regex to handle multiple HTML tag formats (h1-h6, b, strong, p)

3. Added debug_discharge_html() endpoint:
   - URL: /Ipd_discharge/debug_discharge_html/{ipdId}
   - Shows complete HTML source with syntax highlighting
   - Displays rendered template with all placeholders replaced
   - Useful for checking spacing and HTML structure
   - Auto-generates content if not saved yet

Database Schema:
- ipd_master.ipd_status: 0-7 (discharge status ID)
- ipd_discharge_status table:
  - id: 0-7
  - status_desc: 'Status Pending', 'Improved/Recovered', 'LAMA', 'Referred', etc.
  - status_details: 'Discharge Summary', 'Leave Against Medical Advice', etc.

Status Mapping (from user's data):
- 0: Status Pending
- 1: Improved/Recovered -> 'Discharge Summary'
- 2: LAMA -> 'Leave Against Medical Advice'
- 3: Referred -> 'Refer To Higher Centre'
- 4: Satisfactory -> 'Discharge Summary'
- 5: Expired -> 'Dead Summary'
- 6: Admission Cancelled
- 7: Discharge on Request

Regex Replacements:
1. Header replacement: /<h[1-6][^>]*>\s*Discharge\s+Summary\s*<\/h[1-6]>/i
2. Bold replacement: /<(b|strong)>\s*Discharge\s+Summary\s*<\/\1>/i
3. Date removal (br): /Date\s+of\s+Discharge\s*:\s*[0-9\-\/]+\s*<br\s*\/?>/i
4. Date removal (p): /<p[^>]*>\s*Date\s+of\s+Discharge\s*:\s*[0-9\-\/]+\s*<\/p>/i

Usage:
- When ipd_status = 1: Shows 'Discharge Summary' (from status_details)
- When ipd_status = 2: Shows 'Leave Against Medical Advice'
- When ipd_status = 5: Shows 'Dead Summary'
- 'Date of Discharge: 2026-06-07' line is removed from all sections

Debug Endpoints:
1. /Ipd_discharge/debug_ipd_fields/{ipdId} - Shows database field values
2. /Ipd_discharge/debug_discharge_html/{ipdId} - Shows complete HTML source

Testing:
- Change ipd_master.ipd_status value
- Regenerate discharge summary
- Header should change based on status
- Date line should not appear
- Use debug_discharge_html to see full HTML structure
2026-06-07 07:35:36 +05:30
e152fab496 Fix discharge time placeholders and department/doctor field mappings
CRITICAL FIXES based on actual database schema analysis:

Problem 1: {{ADMISSION_TIME}} showing 12:00 AM
- Code was looking for: register_time, admission_time (don't exist)
- Actual field: reg_time (stores '14:44' format)
- Fix: Changed to use ipd->reg_time field

Problem 2: {{DISCHARGE_TIME}} showing 12:00 AM
- Code was looking for: discharge_date datetime field
- Actual field: discharge_time (stores '05:30' format)
- Fix: Changed to use ipd->discharge_time field directly

Problem 3: {{DEPARTMENT}} showing blank
- Code was using: hc_department.id field for lookup
- Actual field: hc_department.iId (primary key is iId, not id)
- Fix: Changed WHERE clause from 'id' to 'iId'

Problem 4: {{DOCTOR_NAMES}} showing blank
- Code was looking for: doc_list, doctor_id, doc_id (don't exist)
- Actual field: r_doc_id (referring doctor ID = 12)
- Fix: Changed primary lookup to r_doc_id field

Technical Improvements:

1. Enhanced safeTime() function:
   - Added regex parsing for 'HH:MM' time format
   - Validates hour (0-23) and minute (0-59) ranges
   - Converts 24-hour to 12-hour format with AM/PM
   - Example: '14:44' -> '02:44 PM', '05:30' -> '05:30 AM'
   - Falls back to strtotime() for datetime formats
   - Returns empty string for invalid times

2. Updated getDischargeDoctorNames():
   - Primary: ipd->r_doc_id (referring doctor)
   - Fallback: ipd->doc_list (comma-separated IDs)
   - Fallback: ipd->doctor_id, doc_id, consultant_id
   - Queries doctor_master table
   - Gets specializations from doc_spec + med_spec
   - Format: 'Dr. Name [Specialization]' or 'Title Name [Spec]'

3. Updated getDischargeDepartmentName():
   - Changed: WHERE id = dept_id
   - To: WHERE iId = dept_id
   - Keeps fallback to ipd_department table
   - Returns department vName field

4. Updated debug endpoint:
   - Now checks: reg_time, reg_time_bak, discharge_time, discharge_time_bak
   - Shows: dept_id, r_doc_id, r_doc_name
   - Removed non-existent fields from output

Database Schema (from live data):
- ipd_master.reg_time: '14:44' (admission time)
- ipd_master.discharge_time: '05:30' (discharge time)
- ipd_master.dept_id: 2 -> hc_department.iId (vName field)
- ipd_master.r_doc_id: 12 -> doctor_master.id (p_fname field)

Testing:
- Admission Time should show: 02:44 PM (from reg_time '14:44')
- Discharge Time should show: 05:30 AM (from discharge_time '05:30')
- Department should show department name from hc_department
- Doctor Names should show doctor name with specialization
2026-06-07 07:07:35 +05:30
87789cff7a Add debug endpoint to check IPD master fields
Problem:
- {{ADMISSION_TIME}} showing 12:00 AM instead of actual time
- {{DISCHARGE_TIME}} showing 12:00 AM instead of actual time
- {{DEPARTMENT}} showing blank
- {{DOCTOR_NAMES}} showing blank

Need to identify:
1. Which datetime fields exist in ipd_master table
2. Which department field exists (dept_id, department_id, or department)
3. Which doctor field exists (doc_list, doctor_id, doc_id, consultant_id)
4. What values are actually stored in these fields

Debug Endpoint Added:
- URL: /Ipd_discharge/debug_ipd_fields/{ipdId}
- Example: http://localhost:8080/Ipd_discharge/debug_ipd_fields/123
- Replace 123 with actual IPD ID from your discharge page

What it shows:
- All available field names in ipd_master table
- Values of: register_date, discharge_date, register_time, discharge_time, admission_time
- Values of: dept_id, department_id, department
- Values of: doc_list, doctor_id, doc_id, consultant_id
- Result of getDischargeDepartmentName() function
- Result of getDischargeDoctorNames() function
- Result of safeTime() for register_date and discharge_date

Usage:
1. Go to your discharge form
2. Note the IPD ID from the URL
3. Open: /Ipd_discharge/debug_ipd_fields/{ipdId} in browser
4. Check the JSON output to see which fields exist and their values

This will help identify:
- If datetime fields have time component or just date
- Which field names are actually used in your database
- Why placeholders are showing blank/wrong values
2026-06-07 06:58:59 +05:30
51aa6400c8 Fix {{DEPARTMENT}} and {{DOCTOR_NAMES}} placeholder tokens
Problem:
- {{DEPARTMENT}} and {{DOCTOR_NAMES}} placeholders not working in discharge templates
- Complex GROUP_CONCAT query with LEFT JOINs causing issues
- Query might return NULL or fail silently when specializations missing

Solution - getDischargeDoctorNames():
- Removed GROUP_CONCAT to avoid SQL aggregation issues
- Split into two separate queries: doctors first, then specializations
- Changed specialization JOIN from LEFT to INNER to avoid NULL values
- Query doctors from doctor_master separately
- For each doctor, query doc_spec + med_spec to get specializations
- Build full name format: 'Dr. Name [Spec1, Spec2]' or 'Title Name [Spec1, Spec2]'
- Return comma-separated list of all doctors

Technical Changes:
- First query: SELECT d.id, d.p_fname, d.p_title FROM doctor_master
- Second query (per doctor): SELECT m.SpecName FROM doc_spec s JOIN med_spec m
- Use INNER JOIN for specializations to only include valid matches
- Handle missing specializations gracefully (show doctor name without brackets)
- Handle missing title (default to 'Dr.')

getDischargeDepartmentName() - Already Working:
- Tries dept_id from ipd_master
- Queries hc_department table first
- Falls back to ipd_department table
- Falls back to direct department field
- Returns empty string if all methods fail

Token Flow:
1. buildDischargeSummaryTokenVars() calls both functions
2. Returns array with 'DEPARTMENT' and 'DOCTOR_NAMES' keys
3. buildDischargeTemplateTokenVars() merges these tokens
4. Creates lowercase, UPPERCASE, and Ucfirst versions of all tokens
5. applyDischargeTemplateTokens() replaces {{TOKEN}} and {TOKEN} in template

Available Token Variants:
- {{DEPARTMENT}}, {{department}}, {{Department}}
- {{DOCTOR_NAMES}}, {{doctor_names}}, {{Doctor_names}}
- {{DOCTOR_NAME}} (singular alias for same value)

Example Output:
- Single doctor: 'Dr. John Smith [Cardiology]'
- Multiple doctors: 'Dr. John Smith [Cardiology], Dr. Jane Doe [Neurology]'
- No specialization: 'Dr. John Smith'
- With title: 'Prof. John Smith [Cardiology]'

Testing:
- Ensure doc_list field in ipd_master has comma-separated doctor IDs
- Falls back to doctor_id, doc_id, or consultant_id if doc_list empty
- Returns empty string if no valid doctor IDs found
2026-06-07 06:48:58 +05:30
fd70cc46cf Change medicine save to auto-save on Add button click (AJAX)
Medicine Save Behavior Change:
- Previously: Medicines added to table locally, saved on 'Save Discharge Advice' click
- Now: AJAX auto-save immediately when '+ADD / Update' button is clicked
- Matches OPD Consult Form workflow (instant save on add)

Implementation:
- Modified btn_discharge_med_add click handler to make AJAX POST
- POST parameters: action='add_drug', ajax_mode='json', new_drug_name, new_drug_type,
  new_drug_dose, new_drug_when, new_drug_freq, new_drug_day, new_drug_qty, new_drug_remark
- Controller add_drug action already existed - now returns JSON when ajax_mode='json'
- On success: adds row to table, clears form, shows 'Medicine saved successfully'
- On failure: shows error message from server
- CSRF token updated after each AJAX call

Remove Button Behavior:
- Remove button now shows confirmation dialog
- Warns user: 'This will only remove from current view. Reload to see saved medicines.'
- Note: Remove action is NOT yet AJAX - full page reload shows actual DB state
- Future enhancement: make Remove button also AJAX (action='remove_drug')

Technical Notes:
- Kept discharge_medicine_json serialization as backup (unused now, but harmless)
- Medicine data: med_id=0 marks client-added medicines
- Success check: data.update === 1
- Error handling: network timeout 30s, shows xhr.status on fail
- Form fields cleared after successful save
- Focus returns to medicine name field for next entry

User Benefits:
- Instant feedback - no need to scroll down and click 'Save Discharge Advice'
- Medicines immediately persisted to database
- Can add multiple medicines in quick succession
- Consistent with OPD prescription workflow
- Reduced risk of data loss (saves immediately vs. waiting for form submit)
2026-06-06 21:00:30 +05:30
e731ef61e9 Fix discharge medicine save, add CKEditor, and Review After quick select
Medicine Saving Fix (MAJOR):
- Add hidden field 'discharge_medicine_json' to capture medicine table data
- Add serializeDischargeMedicineTable() function to convert DOM table to JSON before form submit
- Hook serialization into form submit event (before syncEditorValues)
- Update controller to process discharge_medicine_json in save_main action
- Insert medicines into ipd_discharge_prescrption_prescribed or ipd_discharge_prescription_prescribed table
- Medicine data now persists on page reload

CKEditor Integration:
- Initialize CKEditor for 'instruction_other' (Other Advice) field
- Initialize CKEditor for 'instruction_remark' (Discharge Summary) field
- Add simplified toolbar: Bold, Italic, Underline, Lists, Undo/Redo
- Properly destroy existing instances before re-initialization
- Height: 120px for Other Advice, 150px for Discharge Summary

Review After Quick Select Buttons:
- Add getNextVisitOptions() method to Ipd_discharge controller (copied from Opd_prescription)
- Add resolveFirstField() helper method
- Pass next_visit_options array to view
- Render quick select buttons: '3 Days (09-06-2026)', '1 Week (13-06-2026)', etc.
- Add JavaScript handler: click button → populate #discharge_review_after input
- Change Review After field from col-md-4 to col-md-6 for better layout
- Add col-md-12 wrapper for button row with flex-wrap gap-2 layout

Technical Details:
- Medicine JSON structure: {med_type, med_name, dosage, dosage_when, dosage_freq, no_of_days, qty, remark}
- JSON processing includes column existence checks for flexibility
- Quick select buttons use data-value attribute for value insertion
- Falls back to hardcoded date options if opd_nextvisit table missing
- Medicine insert uses med_id=0 to mark client-added medicines

User Workflow:
1. Add medicines → shown in table (Type, Medicine, Dose labels, Duration formatted)
2. Click 'Save Discharge Advice' → medicines serialize to JSON → POST to controller
3. Controller parses JSON → inserts to database → medicines persist on reload
4. Other Advice/Discharge Summary now have rich text editing (bold, lists, etc.)
5. Review After: click '1 Week (13-06-2026)' button → auto-fills input
2026-06-06 20:53:05 +05:30
e229ae3df2 Improve discharge medicine display and rename Instructions section
Medicine Table Improvements:
- Swap column order: Type column now appears before Medicine Name
- Display dose labels (text) instead of numeric IDs for Dose, When, Freq
- Auto-format duration: numeric values get 'days' suffix (e.g., '5' -> '5 days')
- Single day shows 'day' (singular), multiple days show 'days' (plural)
- Preserve custom duration formats like '1 month' or '3 doses'

Section Rename:
- Change 'Discharge Instructions / Advice' to 'Discharge Summary'
- Updated in card header, navigation menu, and form label
- Better reflects comprehensive discharge information

Technical:
- Use doseMasterCache to resolve ID->label for Dose/When/Freq/Route
- Regex check for numeric-only duration values before formatting
- Maintain backward compatibility with existing data
2026-06-06 20:42:15 +05:30
fdc6ae352b Remove duplicate old medicine form code causing null reference error
- Removed old code referencing non-existent field IDs (#new_drug_name, etc.)
- Fixed 'medInput is null' JavaScript error
- Kept only new implementation with correct IDs (#discharge_med_name, etc.)
- Cleaned up normalizeRxShortCode and unused event listeners
- Fix console error: Uncaught TypeError: can't access property addEventListener
2026-06-06 20:22:36 +05:30
66b5e1b6a5 Fix discharge medicine autocomplete and auto-fill - use OPD medicine_search endpoint
- Change from rx_group_medicine_suggest to Opd_prescription/medicine_search endpoint
- Match OPD Consult Form data source and behavior
- Auto-fill Type, Dose, When, Frequency, Route, Days, Qty, and Remark from medicine master
- Add ensureDoseOption helper to handle current values not in dropdown list
- Store medicine rows for auto-fill on selection
- Improved user experience with comprehensive auto-complete
2026-06-06 20:18:30 +05:30
ee5b29f688 Upgrade IPD Discharge Medicine form to OPD-style Rx prescription interface
- Replace simple text inputs with advanced prescription form
- Add medicine autocomplete with datalist suggestions
- Add dropdown selects for Dose, When, Frequency, and Route from dose masters
- Integrate with existing Opd_prescription/rx_group_dose_masters endpoint
- Add medicine type auto-fill from medicine database
- Add quick-fill buttons for common values (BF/AF/WF, OD/BD/TDS/HS, 3/5/7 Days)
- Improve UI with two-column layout: medicine list table (left) + add form (right)
- Add real-time medicine suggestions via rx_group_medicine_suggest endpoint
- Support Remove All functionality
- Dynamic table row add/remove with proper event binding
- Better UX matching OPD Consult Form prescription interface
2026-06-06 20:10:52 +05:30
0d9ff81a97 Add DataTables search box support to patient search - now handles both initial search and live filtering 2026-06-06 14:56:02 +05:30
ac94f7bf6f Fix AJAX route for patient search - remove duplicate billing prefix 2026-06-06 14:53:47 +05:30
d82078c9ed Convert patient search to DataTables with AJAX server-side processing
- Updated Patient::search() to only render view with search query
- Created Patient::search_ajax() method for DataTables AJAX endpoint
- Implemented server-side processing with pagination, sorting, and filtering
- Results ordered by Last Visit DESC by default
- Retrieves all patient data from patient_master table
- Added proper SQL escaping to prevent injection
- Updated Patient_Search_V.php to use DataTables with AJAX remote data
- Added DataTables configuration with 25 rows per page default
- Supports dynamic length menu (10, 25, 50, 100, All)
- Added route for billing/patient/search_ajax endpoint
- Improved user experience with loading spinner and customized messages
2026-06-06 14:52:10 +05:30
550e72eca8 Add IPD Discharge search functionality with AJAX
- Added 'Discharge' button in sidebar under In-Patient & Nursing Care section
- Created search_patient() and search_patient_ajax() controller methods
- Implemented AJAX-based patient search by IPD Code, UHID, Name, or Mobile
- Added ipd_discharge/search_patient.php view with dynamic results table
- Search results show patient details, bed info, doctor assignments, and discharge status
- Added 'Create Discharge' and 'Print Discharge' action buttons for each patient
- Integrated DataTables for sorting and pagination
- Fixed database column names (patient_master.gender, age, age_in_month)
- Added routes for search page and AJAX endpoint
2026-06-06 14:43:10 +05:30
16348d12db Fix placeholder tokens: DISCHARGE_SUMMARY, DOCTOR_NAMES, DEPARTMENT
- Add 'Discharge Summary' section to buildAutoDischargeContent with department, doctors, and dates so {{DISCHARGE_SUMMARY}} token extracts properly
- Improve getDischargeDepartmentName to support dept_id, department_id fields and check both hc_department and ipd_department tables
- Improve getDischargeDoctorNames to fallback to doctor_id, doc_id, consultant_id if doc_list is empty
- Update DISCHARGE_TEMPLATE_TOKENS.md to clarify what {{DISCHARGE_SUMMARY}} contains
- Clear cached content for IPD 1 to regenerate with new logic

Fixes issue where {{DISCHARGE_SUMMARY}}, {{DOCTOR_NAMES}}, {{DEPARTMENT}} tokens were not working (replaced with empty values)
2026-06-06 13:00:52 +05:30
1da16a54d6 Fix CKEditor initialization issues in discharge template editor
Fixed the error: Uncaught TypeError: can't access property 'getElementsByTag', b is null

Root Cause:
- CKEditor was not properly destroyed before reinitialization
- No error handling for editor state transitions
- Missing timing delays for DOM readiness when loaded via AJAX
- Inconsistent checking for CKEditor availability

Improvements:
1. Safe destroy pattern - wrap destroy() in try-catch to handle state issues
2. Added typeof check for CKEDITOR instead of window.CKEDITOR
3. Added 100ms setTimeout delay before CKEDITOR.replace() to ensure cleanup
4. Added 150ms delay for initial editor load (especially for AJAX loads)
5. Wrapped all setData() calls in try-catch with textarea fallback
6. Added console warnings for debugging instead of silent failures
7. Check document.readyState before initialization
8. Re-initialize editor check after AJAX template load
9. Added element existence validation before replace()

Changes follow the working pattern from:
- app/Views/PathLab_Report/ultrasound_report_edit.php (safeReplace pattern)
- app/Views/diagnosis/lab_final_report_editor.php (typeof checks)

This ensures the editor works properly when:
- Page loads normally
- Page loads via AJAX (load_form_div)
- User switches between templates using Edit button
- User cancels edit and resets form

Tested scenarios:
- Initial page load
- Edit template -> load data -> CKEditor shows content
- Cancel -> CKEditor resets
- Multiple edit/cancel cycles
2026-06-06 12:51:21 +05:30
860981a85a Update discharge template design page with complete placeholder list
Updated /setting/template/discharge_templates view to show all available placeholders
organized by 5 categories as per user requirements:

1. Hospital Information (11 tokens)
   - H_Name, H_address_1, H_address_2, H_phone_No, H_Email, H_logo, H_logo_abs
   - hospital_name, hospital_address, hospital_phone, hospital_email

2. Patient Information (9 tokens)
   - PATIENT_NAME, UHID, IPD_CODE, AGE_GENDER, GUARDIAN, GUARDIAN_RELATION,
     GUARDIAN_NAME, PATIENT_ADDRESS
   - NEW: PATIENT_PHONE ✨

3. IPD Information (13 tokens)
   - DEPARTMENT, ADMIT_DATE, DISCHARGE_DATE, ADMIT_DATE_ONLY, DISCHARGE_DATE_ONLY,
     ADMISSION_TIME, DISCHARGE_TIME, ADMIT_TIME, ISDELIVERY
   - NEW: INSURANCE_COMPANY ✨, DOCTOR_NAMES ✨, DOCTOR_NAME ✨

4. IPD Discharge Content (19 tokens)
   - CONTENT (all sections combined)
   - PATIENT_INFO_TABLE (pre-built patient demographic table)
   - Section placeholders: DISCHARGE_SUMMARY, FINAL_DIAGNOSIS, SURGERY, PROCEDURE,
     PERSONAL_HISTORY, PRESENTING_COMPLAINTS, PAIN_MEASUREMENT_SCALE,
     GENERAL_EXAM_ADMISSION, CLINICAL_INVESTIGATION_REPORTS, COURSE_IN_HOSPITAL,
     EXAMINATION_ON_DISCHARGE, DRUG_ALLERGY_ADR, CO_MORBIDITIES,
     DISCHARGE_MEDICATIONS, DIETARY_ADVICE, DISCHARGE_INSTRUCTIONS, SIGNATURE_BLOCK

5. Common/Meta (2 tokens)
   - CURRENT_DATE, PRINT_TIME

UI improvements:
- Clear category headers with numbers
- Highlighted new tokens with ✨ sparkle
- Added helpful notes (case-insensitive, auto-hide empty values)
- Better visual organization with line breaks between categories

This makes it easy for users to find and use the correct placeholders when
designing discharge templates.
2026-06-06 12:35:29 +05:30
814eda6d16 Add discharge template tokens quick reference guide
Created comprehensive quick reference organized by user's requested categories:

1. Hospital Information (11 tokens)
   - H_Name, H_address_1, H_address_2, H_phone_No, H_Email, H_logo, etc.

2. Patient Information (8 tokens)
   - PATIENT_NAME, UHID, AGE_GENDER, GUARDIAN, PATIENT_ADDRESS
   - NEW: PATIENT_PHONE

3. IPD Information (13 tokens)
   - IPD_CODE, DEPARTMENT, ADMIT_DATE, DISCHARGE_DATE, ADMISSION_TIME, etc.
   - NEW: INSURANCE_COMPANY, DOCTOR_NAMES, DOCTOR_NAME

4. IPD Discharge Content (19 tokens)
   - CONTENT (all sections combined)
   - PATIENT_INFO_TABLE (pre-built table)
   - Individual sections: FINAL_DIAGNOSIS, SURGERY, PROCEDURE, DISCHARGE_MEDICATIONS, etc.

5. Common/Meta (2 tokens)
   - CURRENT_DATE, PRINT_TIME

Includes:
- 4 usage examples (minimal, with table, custom layout, selective sections)
- Complete full-featured template example
- Testing instructions
- Usage notes (case-insensitive, auto-hide empty values, etc.)

This is a printer-friendly quick reference version of DISCHARGE_TEMPLATE_GUIDE.md
2026-06-06 12:22:28 +05:30
3ad0025c24 Add new discharge template tokens: PATIENT_PHONE, INSURANCE_COMPANY, DOCTOR_NAMES
New placeholders added:

1. PATIENT_PHONE - Patient's phone number from patient_master.mphone1 or ipd_master.P_mobile1
   Example: 9876543210

2. INSURANCE_COMPANY - Insurance company name from ipd_master.ins_short_name or ins_company_name
   Example: Star Health Insurance
   Default: 'Direct' if no insurance

3. DOCTOR_NAMES / DOCTOR_NAME - Comma-separated list of treating doctors with specialties
   Example: Dr. Rajesh Kumar [General Medicine], Dr. Priya Sharma [Cardiology]
   Reads from ipd_master.doc_list field and joins with doctor_master and med_spec tables

Changes:
- Added getDischargeDoctorNames() helper function
- Updated buildDischargeSummaryTokenVars() to include three new tokens
- Updated DISCHARGE_TEMPLATE_GUIDE.md with new token documentation
- Added example template showing usage of new tokens
- Updated CI3 to CI4 migration mapping table

These tokens match the data shown in the auto-generated patient summary table
and allow templates to display this information in custom layouts.
2026-06-06 12:21:37 +05:30
a8f09d007d Add comprehensive discharge template guide (CI3 to CI4 migration)
Created detailed guide explaining:
- How CI3 template system worked (PHP files, hardcoded patient table)
- How CI4 template system works (database tokens, no auto patient table)
- Complete list of all available tokens
- 5 template examples (blank, minimal, custom, selective, CI3 replica)
- Migration steps from CI3 PHP templates to CI4 tokens
- Testing procedures and common issues
- Best practices and quick reference

This explains why blank template now correctly shows NO patient table
and how to use {{PATIENT_INFO_TABLE}} token to add it when needed.
2026-06-06 12:11:10 +05:30
6d72a5ec04 Correct fix: Remove patient table from auto-generated content, add as token
OLD CI3 BEHAVIOR (correct):
- Patient demographic table was IN the template file
- Content variables contained ONLY clinical sections
- Template controlled entire layout

NEW CI4 FIX (matching CI3):
- Patient table removed from buildAutoDischargeContent completely
- Added {{PATIENT_INFO_TABLE}} token for templates to use if needed
- Auto-generated content now contains ONLY clinical sections
- Blank template = blank output (no patient table forced)
- Templates can add patient table using token or individual tokens

Changes:
- Removed includeSummaryTable parameter from buildAutoDischargeContent
- Removed templateHasDemographicTokens helper (no longer needed)
- Added PATIENT_INFO_TABLE token in buildDischargeTemplateTokenVars
- Simplified show_discharge and preview_discharge_report (no template detection)
- Added comment explaining CI3 compatibility

This matches the old CI3 chamunda_discharge_template_html.php pattern where:
- Template had hardcoded patient table structure
- Variables like \, \ were injected
- Content did NOT include patient demographics by default
2026-06-06 12:09:57 +05:30
00a70ce731 Simplify clear_discharge_content.php script - remove CI4 bootstrap dependency
- Use direct PDO connection instead of CI4 Database class
- Hardcode common database settings for WAMP default setup
- Makes script easier to run without framework dependencies
2026-06-06 11:50:52 +05:30
224ac5613c Fix: Skip auto-generated patient info table when template has demographic tokens
- Modified buildAutoDischargeContent to accept includeSummaryTable parameter
- Added templateHasDemographicTokens helper to detect if template has patient info tokens
- Updated show_discharge and preview_discharge_report to check template before auto-generation
- When template has {{PATIENT_NAME}}, {{UHID}}, etc., skip auto-summary table
- Added clear_discharge_content.php script to clear cached content for regeneration
- Solves issue where blank templates still showed patient information table
2026-06-06 11:49:52 +05:30
aa812354a6 Change mPDF default font from thin dejavusans to medium-weight freeserif for better Hindi/Devanagari rendering
- Updated all controllers: Opd, Diagnosis, Billing\Charges, Billing\Ipd, Storestock, AbdmGateway
- freeserif provides better Unicode coverage and more readable font weight
- Ipd_discharge and DoctorDocument already used good fonts (freeserif/freesans)
- Added comprehensive font configuration documentation
2026-06-06 11:19:35 +05:30
b5cfedec96 Make quick-add modal larger (modal-lg) for better UX
IMPROVEMENT: Increased modal size for better readability

Changed modal-dialog from default to modal-lg
- Provides more space for SNOMED CT and ICD code fields
- Better readability for field labels and help text
- More comfortable data entry experience
- Consistent with other modals in the system
2026-06-06 06:58:38 +05:30
38e06ed1d7 Add 'Save in Master' quick-add for Course/Treatment section
FEATURE: Quick-add functionality for Course/Treatment master terms

NEW COMPONENTS:
1. Course master table: ipd_discharge_course_master
   - term_name, term_code, icd_code, is_active
   - Auto-creates on first use
2. Controller endpoints in Ipd_discharge.php:
   - course_master_lookup() - AJAX autocomplete search
   - course_master_list() - Full list with filters
   - course_master_save() - Create/update records
   - course_master_delete() - Remove records
3. Routes added to Config/Routes.php:
   - Ipd_discharge/course_master_lookup (GET)
   - Ipd_discharge/course_master_list (GET)
   - Ipd_discharge/course_master_save (POST)
   - Ipd_discharge/course_master_delete (POST)

VIEW CHANGES (discharge_create.php):
- Course input now uses input-group layout
- Green 'Save in Master' button added
- Custom dropdown for autocomplete with code badges
- Hidden field: new_course_master_id
- JavaScript: initCourseAutocomplete() function
- Integrated with existing quickAddTermModal

QUICK-ADD MODAL ENHANCEMENT:
- Now supports 3 types: surgery, procedure, course
- Dynamic endpoint selection based on type
- Course type uses course_master_save endpoint
- Auto-populates course input and master_id on save

AUTOCOMPLETE FEATURES:
- Type 2+ characters → searches course_master table
- Dropdown shows: Name + SNOMED/ICD code badges
- Arrow key navigation (Up/Down/Enter/Escape)
- Click or Enter to select
- Hover highlighting with visual feedback
- 300ms debounce for performance

WORKFLOW:
1. Type course/treatment name
2. See dropdown with existing terms (if any)
3. Click 'Save in Master' to add new term
4. Modal opens with name pre-filled
5. Enter SNOMED/ICD codes (optional)
6. Save → Auto-populates input + master_id
7. Click '+ADD Row' to add to discharge

BENEFITS:
- Consistent UI across Surgery/Procedure/Course sections
- Fast term entry without leaving form
- Master library builds during workflow
- ABDM compliance with code support
- Reusable terms across discharges
2026-06-06 06:53:27 +05:30
b203dcac98 Update testing docs with Quick-Add feature tests
DOCUMENTATION: Added comprehensive testing for 'Save in Master' quick-add

NEW TEST CASES:
- Test 7: Quick 'Save in Master' for Surgery
- Test 8: Quick 'Save in Master' for Procedure
- Test 9: Quick-add validation (empty input)
- Test 10: Quick-add integration with autocomplete

SECTIONS ADDED:
- Quick-Add Workflow Overview comparing traditional vs new flow
- Detailed step-by-step testing procedures
- Expected results with UI screenshots
- SQL verification queries
- Validation scenarios
- Integration testing with autocomplete

BENEFITS DOCUMENTED:
- 3 fewer steps than Master CRUD modal
- Name pre-filled from input
- No re-typing needed
- Master ID auto-linked
- ABDM codes can be added during workflow

UPDATED:
- Implementation checklist (8 enhanced features)
- Code changes list (17 completed items)
- Testing checklist (14 test scenarios)
- Test numbering updated for new tests
2026-06-06 06:40:18 +05:30
b0b09bc2bf Add 'Save in Master' quick-add buttons for Surgery/Procedure
FEATURE: Quick-add functionality for Surgery and Procedure master terms

NEW COMPONENTS:
1. 'Save in Master' buttons next to Surgery/Procedure inputs
2. Quick-add modal with SNOMED CT and ICD code fields
3. Instant save without opening full Master CRUD modal
4. Auto-populate input and master_id after save

UI CHANGES:
- Surgery/Procedure inputs now use input-group layout
- Green 'Save in Master' button with plus-circle icon
- Quick-add modal opens when clicking button
- Name field auto-filled from input (read-only in modal)
- Optional SNOMED CT code and ICD code fields
- Success message shown before auto-close

WORKFLOW:
1. User types new surgery/procedure name in input
2. Click 'Save in Master' button
3. Modal opens with name pre-filled
4. Enter SNOMED/ICD codes (optional)
5. Click 'Save in Master' in modal
6. Record saves to ipd_discharge_surgery_master table
7. Input filled with saved name
8. master_id hidden field populated
9. Modal closes automatically
10. User can now click '+ADD Row' to add to discharge

BENEFITS:
- No need to open full Master CRUD modal for quick adds
- SNOMED/ICD codes can be added during workflow
- Faster data entry for new terms
- Master table stays organized and updated
- ABDM compliance maintained with code support
2026-06-06 06:38:49 +05:30
5dccbc8d12 Complete Surgery/Procedure autocomplete implementation and testing docs
COMPLETED ALL TODO ITEMS:

✅ Task 1: Enhanced Surgery autocomplete with SNOMED/ICD codes
✅ Task 2: Enhanced Procedure autocomplete with SNOMED/ICD codes
✅ Task 3: Verified Master CRUD integration (modal already working)
✅ Task 4: Created comprehensive testing documentation

KEY FIXES:
- Removed conflicting bindSurgeryTermLookup() calls from initSurgeryTools()
- Old datalist code no longer interferes with new custom dropdown
- Both autocompletes work independently with separate dropdown containers

TESTING DOCUMENT INCLUDES:
- 10 detailed test scenarios with expected results
- Database verification queries
- Troubleshooting guide
- Edge case handling
- Performance checks
- User acceptance checklist
- Future enhancement suggestions

FILE: SURGERY_PROCEDURE_AUTOCOMPLETE_TESTING.md

Ready for QA testing and user acceptance.
2026-06-06 06:35:32 +05:30
b6bca785a8 Add enhanced autocomplete for Surgery/Procedure with SNOMED/ICD codes
FEATURE: Enhanced Surgery and Procedure autocomplete with code display

IMPROVEMENTS:
1. Replaced basic datalist with custom dropdown
2. Display SNOMED codes (term_code) and ICD codes in dropdown items
3. Show codes as badges: blue for SNOMED, gray for ICD
4. Keyboard navigation (Arrow Up/Down, Enter, Escape)
5. Hover highlighting with visual feedback
6. Store master_id in hidden field for database linking

HOW IT WORKS:
- Type 2+ characters → searches ipd_discharge_surgery_master table
- Dropdown shows: Name + Code badges
- Click or Enter → selects item and fills input
- Arrow keys navigate through suggestions
- Escape closes dropdown

UI:
- Name displayed prominently (bold)
- Codes shown as colored badges below name
- Blue badge: SNOMED/term code
- Gray badge: ICD code
- Hover: light gray background
- Keyboard highlight: blue background, white text

Makes it easy to find and select surgeries/procedures from master
with their ABDM codes visible for verification.
2026-06-06 06:29:15 +05:30
e10ea540ba Remove 'Complaint added' status message
Remove the status message after adding complaint - table update
provides sufficient visual feedback.
2026-06-06 06:24:37 +05:30
6f5bacc723 Fix broken event listeners - properly close click handler and wrap keydown
BUGFIX: Uncaught SyntaxError expected expression, got ')'

PROBLEM: Event listeners were malformed - click handler and keydown handler
were merged into one broken block

ROOT CAUSE: The click event listener was missing its closing braces and
btnAdd.click() call. Instead, the keydown handler code was pasted directly
after it WITHOUT the proper addEventListener wrapper. This caused:
1. Missing closing brackets for click handler
2. Reference to undefined 'e' variable (e.key) outside function scope
3. Browser threw SyntaxError/ReferenceError

SOLUTION:
1. Properly closed click event listener:
   - Added btnAdd.click() call
   - Added closing braces for function and addEventListener
   - Added closing }); for forEach loop
   - Added closing }); for \$.get() AJAX call

2. Created separate keydown event listener:
   - Wrapped keyboard navigation code in lookup.addEventListener('keydown', function(e) {...})
   - Now 'e' variable is properly defined in function scope
   - Arrow keys, Enter, Escape all have access to event object

CODE STRUCTURE NOW:
- item.addEventListener('click', ...) - PROPERLY CLOSED
- }); // closes forEach
- }, 'json'); // closes AJAX
- }); // closes input event
- lookup.addEventListener('keydown', function(e) {...}) - NEW/PROPER

All event handlers are now independent and properly scoped.
2026-06-06 06:16:17 +05:30
1a592eda7c Add safer checks for \ variable
Add isset() and is_array() checks before using \ to prevent
PHP warnings/errors if variable is not set or not an array.

This ensures cleaner JavaScript output even if the variable is undefined.
2026-06-06 06:05:32 +05:30
d3e50174b5 Fix trailing comma in JavaScript array from PHP foreach
BUGFIX: Uncaught SyntaxError expected expression, got ')'

PROBLEM: Trailing comma after last array item caused JavaScript syntax error

ROOT CAUSE: PHP foreach loop always added comma after each object:
{...},
{...},  // <-- trailing comma before ]
];

While modern JS allows trailing commas, some parsers or contexts
(like when injected/evaluated) don't handle it well.

SOLUTION: Only add comma between items, not after last item:
1. Count total items before loop
2. Track current index in loop
3. Add comma only if currentIndex < itemCount

GENERATED JS NOW:
[
    {...},
    {...},
    {...}   // <-- no trailing comma
]

This eliminates the syntax error.
2026-06-06 06:00:25 +05:30
f6749d2664 Fix undefined variable - use \ not \
BUGFIX: Uncaught SyntaxError expected expression, got ')'

PROBLEM: JavaScript syntax error on page load caused by undefined PHP variable

ROOT CAUSE: Controller passes 'complaint_rows' to view but JavaScript code
tried to use \ (camelCase). PHP is case-sensitive, so:
- Controller: 'complaint_rows' => \
- View variable name: \ (snake_case)
- Used in view: \ (wrong!)

When \ was undefined, the !empty() check failed and PHP
tried to iterate over undefined variable, generating malformed JavaScript
that caused syntax errors.

SOLUTION: Changed \ to \ in foreach loop

Now the existing complaints will load correctly from database into
the selectedDischargeComplaints JavaScript array.
2026-06-06 05:58:04 +05:30
dde54ab80b Fix scope error - move complaint functions before initComplaintTools
BUGFIX: Uncaught SyntaxError expected expression, got ')'

PROBLEM: selectedDischargeComplaints was undefined when initComplaintTools
tried to use it, causing syntax/reference errors

ROOT CAUSE: JavaScript variable and function declarations were placed
AFTER the initComplaintTools function that used them. When btnAdd
click handler tried to access selectedDischargeComplaints at line 1841,
it was declared later at line 1964.

SOLUTION:
1. Moved selectedDischargeComplaints variable declaration to TOP
2. Moved initDischargeComplaintsTable() function before initComplaintTools
3. Moved renderDischargeComplaintTable() function before initComplaintTools
4. Removed duplicate declarations that were after initComplaintTools

ORDER NOW:
1. selectedDischargeComplaints = [] (global)
2. initDischargeComplaintsTable() function
3. renderDischargeComplaintTable() function
4. initComplaintTools() function (uses all above)

All functions/variables are now in proper scope when needed.
2026-06-06 05:55:12 +05:30
e6ccd6a408 Fix JavaScript syntax error - remove duplicate keydown handler
BUGFIX: Uncaught SyntaxError expected expression, got ')'

PROBLEM: Console error on page load due to malformed JavaScript

ROOT CAUSE: Previous replacement left duplicate code fragment:
- Closing brace of updateDropdownHighlight function was followed by
  'kup.addEventListener' (missing 'loo' prefix)
- This created invalid JavaScript syntax
- Old keydown handler wasn't fully removed

SOLUTION: Removed the duplicate/malformed keydown listener block

The proper keydown handler with arrow key navigation is earlier
in the code and works correctly.
2026-06-06 05:51:44 +05:30
bbaa6ae841 Add keyboard navigation to autocomplete dropdown
FEATURE: Arrow keys now work to navigate autocomplete suggestions

ADDED:
- highlightedIndex variable tracks current keyboard position
- ArrowDown/ArrowUp keys move through dropdown items
- Highlighted item shown with blue background and white text
- Enter key selects highlighted item (or adds typed text if none)
- Escape key closes dropdown
- Mouse hover updates highlightedIndex
- scrollIntoView keeps highlighted item visible in long lists

HOW TO USE:
1. Type complaint name → dropdown appears
2. Press ArrowDown → first item highlights (blue background)
3. Press ArrowDown/ArrowUp → navigate through items
4. Press Enter → selects highlighted item and adds to table
5. Press Escape → closes dropdown

Visual feedback:
- Mouse hover: light gray background
- Keyboard highlight: blue background, white text, bold
2026-06-06 05:49:03 +05:30
4fc64be2a2 Fix autocomplete selection - now adds to client-side array
BUGFIX: Autocomplete dropdown was showing but items weren't selectable

PROBLEM: Clicking autocomplete suggestions didn't add complaints to table

ROOT CAUSE: btnAdd click handler was using old database-submit logic
instead of new client-side array management

SOLUTION: Simplified btnAdd to:
1. Take value from lookup input
2. Check if already exists (prevent duplicates)
3. Add to selectedDischargeComplaints array
4. Call renderDischargeComplaintTable() to refresh
5. Clear input and show success message

HOW IT WORKS NOW:
- Type complaint name → autocomplete dropdown appears
- Click suggestion → sets lookup input value
- btnAdd triggered → adds to array → table refreshes
- Complaint appears in table with editable Frequency/Severity/Duration

All inline editing works without database calls!
2026-06-06 05:46:38 +05:30