From d4c92af9e44537dfdde5f8c8f1f1c0fc808ff815 Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Mon, 27 Oct 2025 10:37:44 +0700 Subject: [PATCH] fix pvadt --- app/Models/PatVisit/PatVisitModel.php | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/app/Models/PatVisit/PatVisitModel.php b/app/Models/PatVisit/PatVisitModel.php index 1f57dfc..90d0ec8 100644 --- a/app/Models/PatVisit/PatVisitModel.php +++ b/app/Models/PatVisit/PatVisitModel.php @@ -104,21 +104,17 @@ class PatVisitModel extends BaseModel { throw new \Exception("Failed to update PatDiag record. ". $error['message']); } - // patvisitadt - $exist = $modelPVA->where('InternalPVID',$InternalPVID)->find(); - if($exist) { - if(!empty($input['PatVisitADT'])) { - $tmp = $modelPVA->where('InternalPVID',$InternalPVID)->set($input['PatVisitADT'])->update(); - } else { $tmp = $modelPVA->where('InternalPVID',$InternalPVID)->delete(); } - } else { - if(!empty($input['PatVisitADT'])) { - $input['PatVisitADT']['InternalPVID'] = $InternalPVID; - $tmp = $modelPVA->insert($input['PatVisitADT']); - } - } - if ($tmp === false) { - $error = $db->error(); - throw new \Exception("Failed to update PatVisitADT record. ". $error['message']); + if(!empty($input['PatVisitADT'])) { + $adtList = $input['PatVisitADT']; + usort($adtList, fn($a, $b) => $a['sequence'] <=> $b['sequence']); + foreach ($adtList as $adt) { + $adt['InternalPVID'] = $InternalPVID; + $tmp = $modelPVA->insert($adt); + if ($tmp === false) { + $error = $db->error(); + throw new \Exception("Failed to update PatVisitADT record. ". $error['message']); + } + } } if ($db->transStatus() === FALSE) {