Fix: Data mixing between instruction_other and instruction_remark fields

ISSUE: instruction_remark (Discharge Summary) data appearing in instruction_other (Other Advice) field after save

ROOT CAUSE: Legacy table sync was writing wrong variable
- Save handler at line 5478 was writing \ to legacy food_text
- Should have been writing \
- When legacy fallback loads (if food_ids empty), wrong data copied to instruction_other

FIX: Changed line 5478 in Ipd_discharge.php
- Before: \['food_text'] = \;
- After:  \['food_text'] = \;

FILES MODIFIED:
1. app/Controllers/Ipd_discharge.php - Fixed legacy save handler
2. app/Views/billing/ipd/discharge_create.php - Removed debug comments

TESTING STEPS:
1. Clear corrupt legacy data: php clear_legacy_food.php 1
2. Go to /Ipd_discharge/ipd_select/1
3. Clear both fields
4. Type 'Hello Other' in Other Advice
5. Type 'Hello Discharge' in Discharge Summary
6. Click 'Save Discharge Advice'
7. After reload: Other Advice should show 'Hello Other', NOT 'Hello Discharge'

SCRIPTS ADDED:
- check_instruction_db.php - Check database values
- check_legacy_food.php - Check legacy table
- clear_legacy_food.php - Clean corrupt legacy data
- list_instruction_records.php - List all records
This commit is contained in:
DevSoftTech Server 2026-06-07 20:00:59 +05:30
parent c4932601f1
commit 2b95bb8b7c
5 changed files with 237 additions and 1 deletions

View File

@ -5475,7 +5475,7 @@ class Ipd_discharge extends BaseController
'food_id_list' => implode(',', array_values($foodIds)),
];
if ($this->db->fieldExists('food_text', 'ipd_discharge_drug_food_interaction')) {
$legacyData['food_text'] = $instructionRemark;
$legacyData['food_text'] = $instructionOther;
}
$savedAny = $this->upsertByIpd('ipd_discharge_drug_food_interaction', $ipdId, $legacyData) || $savedAny;

69
check_instruction_db.php Normal file
View File

@ -0,0 +1,69 @@
<?php
// Direct database check - no CI4 bootstrap needed
$envFile = __DIR__ . '/.env';
if (!file_exists($envFile)) {
die("ERROR: .env file not found\n");
}
// Manually parse .env because parse_ini_file doesn't handle CI4 format well
$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = '';
$dbName = 'hms_data_ci4';
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos($line, 'database.default.hostname') !== false) {
$dbHost = trim(explode('=', $line, 2)[1] ?? 'localhost');
} elseif (strpos($line, 'database.default.username') !== false) {
$dbUser = trim(explode('=', $line, 2)[1] ?? 'root');
} elseif (strpos($line, 'database.default.password') !== false) {
$dbPass = trim(explode('=', $line, 2)[1] ?? '');
} elseif (strpos($line, 'database.default.database') !== false) {
$dbName = trim(explode('=', $line, 2)[1] ?? 'hms_data_ci4');
}
}
$conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
if ($conn->connect_error) {
die("ERROR: Connection failed: " . $conn->connect_error . "\n");
}
// Get IPD ID from command line or use default
$ipdId = isset($argv[1]) ? intval($argv[1]) : 1;
echo "=== Checking instruction data for IPD ID: {$ipdId} ===\n\n";
$stmt = $conn->prepare("SELECT id, ipd_id, comp_report, comp_remark, review_after FROM ipd_discharge_instructions WHERE ipd_id = ? LIMIT 1");
$stmt->bind_param("i", $ipdId);
$stmt->execute();
$result = $stmt->get_result();
if ($row = $result->fetch_assoc()) {
echo "ID: " . $row['id'] . "\n";
echo "IPD ID: " . $row['ipd_id'] . "\n\n";
echo "=== comp_remark (Discharge Summary field) ===\n";
echo $row['comp_remark'] . "\n\n";
echo "=== comp_report JSON (contains other_text = Other Advice field) ===\n";
echo $row['comp_report'] . "\n\n";
if (!empty($row['comp_report'])) {
$decoded = json_decode($row['comp_report'], true);
if ($decoded && isset($decoded['other_text'])) {
echo "=== Extracted other_text (Other Advice) ===\n";
echo $decoded['other_text'] . "\n\n";
}
}
echo "=== Review After ===\n";
echo ($row['review_after'] ?? '(empty)') . "\n\n";
} else {
echo "No instruction record found for IPD ID {$ipdId}\n";
}
$stmt->close();
$conn->close();
echo "=== Done ===\n";

68
check_legacy_food.php Normal file
View File

@ -0,0 +1,68 @@
<?php
// Check if legacy food_text is interfering
$envFile = __DIR__ . '/.env';
$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = '';
$dbName = 'hms_data_ci4';
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos($line, 'database.default.hostname') !== false) {
$dbHost = trim(explode('=', $line, 2)[1] ?? 'localhost');
} elseif (strpos($line, 'database.default.username') !== false) {
$dbUser = trim(explode('=', $line, 2)[1] ?? 'root');
} elseif (strpos($line, 'database.default.password') !== false) {
$dbPass = trim(explode('=', $line, 2)[1] ?? '');
} elseif (strpos($line, 'database.default.database') !== false) {
$dbName = trim(explode('=', $line, 2)[1] ?? 'hms_data_ci4');
}
}
$conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
if ($conn->connect_error) {
die("ERROR: Connection failed: " . $conn->connect_error . "\n");
}
$ipdId = isset($argv[1]) ? intval($argv[1]) : 1;
echo "=== Checking LEGACY ipd_discharge_drug_food_interaction for IPD ID: {$ipdId} ===\n\n";
// Check if table exists
$tableCheck = $conn->query("SHOW TABLES LIKE 'ipd_discharge_drug_food_interaction'");
if ($tableCheck->num_rows === 0) {
echo "Table ipd_discharge_drug_food_interaction does NOT exist\n";
$conn->close();
exit(0);
}
echo "Table exists. Checking for food_text column...\n";
$colCheck = $conn->query("SHOW COLUMNS FROM ipd_discharge_drug_food_interaction LIKE 'food_text'");
if ($colCheck->num_rows === 0) {
echo "Column food_text does NOT exist\n";
} else {
echo "Column food_text EXISTS\n\n";
$stmt = $conn->prepare("SELECT id, ipd_id, food_id_list, food_text FROM ipd_discharge_drug_food_interaction WHERE ipd_id = ? LIMIT 1");
$stmt->bind_param("i", $ipdId);
$stmt->execute();
$result = $stmt->get_result();
if ($row = $result->fetch_assoc()) {
echo "=== LEGACY RECORD FOUND ===\n";
echo "ID: " . $row['id'] . "\n";
echo "IPD ID: " . $row['ipd_id'] . "\n";
echo "food_id_list: " . ($row['food_id_list'] ?? '(null)') . "\n";
echo "food_text: " . ($row['food_text'] ?? '(null)') . "\n\n";
if (!empty($row['food_text'])) {
echo "⚠️ WARNING: Legacy food_text has content! This might override instruction_other\n";
}
} else {
echo "No legacy record found for IPD ID {$ipdId}\n";
}
$stmt->close();
}
$conn->close();
echo "\n=== Done ===\n";

59
clear_legacy_food.php Normal file
View File

@ -0,0 +1,59 @@
<?php
// Clear corrupt legacy food_text for IPD ID
$envFile = __DIR__ . '/.env';
$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = '';
$dbName = 'hms_data_ci4';
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos($line, 'database.default.hostname') !== false) {
$dbHost = trim(explode('=', $line, 2)[1] ?? 'localhost');
} elseif (strpos($line, 'database.default.username') !== false) {
$dbUser = trim(explode('=', $line, 2)[1] ?? 'root');
} elseif (strpos($line, 'database.default.password') !== false) {
$dbPass = trim(explode('=', $line, 2)[1] ?? '');
} elseif (strpos($line, 'database.default.database') !== false) {
$dbName = trim(explode('=', $line, 2)[1] ?? 'hms_data_ci4');
}
}
$conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
if ($conn->connect_error) {
die("ERROR: Connection failed: " . $conn->connect_error . "\n");
}
$ipdId = isset($argv[1]) ? intval($argv[1]) : 1;
echo "=== Clearing corrupt legacy food_text for IPD ID: {$ipdId} ===\n\n";
// Check if table and column exist
$tableCheck = $conn->query("SHOW TABLES LIKE 'ipd_discharge_drug_food_interaction'");
if ($tableCheck->num_rows === 0) {
echo "Table does not exist, nothing to clean\n";
$conn->close();
exit(0);
}
$colCheck = $conn->query("SHOW COLUMNS FROM ipd_discharge_drug_food_interaction LIKE 'food_text'");
if ($colCheck->num_rows === 0) {
echo "Column food_text does not exist, nothing to clean\n";
$conn->close();
exit(0);
}
// Clear the food_text
$stmt = $conn->prepare("UPDATE ipd_discharge_drug_food_interaction SET food_text = '' WHERE ipd_id = ?");
$stmt->bind_param("i", $ipdId);
if ($stmt->execute()) {
echo "✅ Cleared food_text for IPD ID {$ipdId}\n";
echo "Affected rows: " . $stmt->affected_rows . "\n";
} else {
echo "❌ Failed to clear: " . $stmt->error . "\n";
}
$stmt->close();
$conn->close();
echo "\n=== Done ===\n";

View File

@ -0,0 +1,40 @@
<?php
// List all IPD records with instruction data
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dbHost = $_ENV['database.default.hostname'] ?? 'localhost';
$dbUser = $_ENV['database.default.username'] ?? 'root';
$dbPass = $_ENV['database.default.password'] ?? '';
$dbName = $_ENV['database.default.database'] ?? 'hms_data_ci4';
$conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
if ($conn->connect_error) {
die("ERROR: Connection failed: " . $conn->connect_error . "\n");
}
echo "=== Recent IPD instruction records ===\n\n";
$sql = "SELECT id, ipd_id, LEFT(comp_remark, 100) as comp_remark_preview,
LEFT(comp_report, 100) as comp_report_preview,
review_after, update_date
FROM ipd_discharge_instructions
ORDER BY id DESC
LIMIT 20";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "ID: {$row['id']} | IPD_ID: {$row['ipd_id']} | Updated: {$row['update_date']}\n";
echo " comp_remark (Discharge Summary): " . substr(strip_tags($row['comp_remark_preview']), 0, 50) . "...\n";
echo " comp_report (JSON with other_text): " . substr($row['comp_report_preview'], 0, 50) . "...\n\n";
}
} else {
echo "No instruction records found\n";
}
$conn->close();