From 144f0a20d56b40dc4bce84c0235aa11b7cc060e0 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Tue, 1 Sep 2026 18:15:46 +0530 Subject: [PATCH] fix(ipd_discharge): sync full medicine list on main form submit to replace stale DB entries --- app/Controllers/Ipd_discharge.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/Controllers/Ipd_discharge.php b/app/Controllers/Ipd_discharge.php index a1e8a8e..faea8b5 100644 --- a/app/Controllers/Ipd_discharge.php +++ b/app/Controllers/Ipd_discharge.php @@ -6765,25 +6765,26 @@ class Ipd_discharge extends BaseController $medicineJson = trim((string) ($this->request->getPost('discharge_medicine_json') ?? '')); if ($medicineJson !== '') { $medicines = json_decode($medicineJson, true); - if (is_array($medicines) && ! empty($medicines)) { + if (is_array($medicines)) { $legacyTable = $this->findFirstExistingTable([ 'ipd_discharge_prescrption_prescribed', 'ipd_discharge_prescription_prescribed', ]); if ($legacyTable !== null) { + $validMeds = []; foreach ($medicines as $med) { $medName = trim((string) ($med['med_name'] ?? '')); - if ($medName === '' || $medName === 'No medicine added') { - continue; + if ($medName !== '' && $medName !== 'No medicine added') { + $validMeds[] = $med; } + } - $exists = $this->db->table($legacyTable) - ->where('ipd_id', $ipdId) - ->where('med_name', $medName) - ->countAllResults(); + if (! empty($validMeds)) { + $this->db->table($legacyTable)->where('ipd_id', $ipdId)->delete(); - if ($exists === 0) { + foreach ($validMeds as $med) { + $medName = trim((string) ($med['med_name'] ?? '')); $insert = [ 'ipd_id' => $ipdId, 'med_id' => 0,