From 2b95bb8b7c772698d0336802d66f7f7bd270ae61 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Sun, 7 Jun 2026 20:00:59 +0530 Subject: [PATCH] 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 --- app/Controllers/Ipd_discharge.php | 2 +- check_instruction_db.php | 69 +++++++++++++++++++++++++++++++ check_legacy_food.php | 68 ++++++++++++++++++++++++++++++ clear_legacy_food.php | 59 ++++++++++++++++++++++++++ list_instruction_records.php | 40 ++++++++++++++++++ 5 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 check_instruction_db.php create mode 100644 check_legacy_food.php create mode 100644 clear_legacy_food.php create mode 100644 list_instruction_records.php diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index c4b2ba3..d6cf14c 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -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; diff --git a/check_instruction_db.php b/check_instruction_db.php new file mode 100644 index 0000000..380e76c --- /dev/null +++ b/check_instruction_db.php @@ -0,0 +1,69 @@ + +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"; diff --git a/check_legacy_food.php b/check_legacy_food.php new file mode 100644 index 0000000..b5e3628 --- /dev/null +++ b/check_legacy_food.php @@ -0,0 +1,68 @@ +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"; diff --git a/clear_legacy_food.php b/clear_legacy_food.php new file mode 100644 index 0000000..8298851 --- /dev/null +++ b/clear_legacy_food.php @@ -0,0 +1,59 @@ +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"; diff --git a/list_instruction_records.php b/list_instruction_records.php new file mode 100644 index 0000000..8835d2e --- /dev/null +++ b/list_instruction_records.php @@ -0,0 +1,40 @@ + +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();