select("contact.ContactID, cd.SiteID, cd.ContactCode, NameFirst, NameLast, Specialty") ->join("contactdetail cd", "contact.ContactID=cd.ContactID", "left") ->join("occupation o","cd.OccupationID=o.OccupationID", "left") ->get()->getResultArray(); return $rows; } public function getContacts($ContactName, $Specialty) { $sql = $this->select("ContactID, NameFirst, NameLast, Title, Initial, Specialty"); if($ContactName !='') { $sql->like('NameFirst',$ContactName,'both')->orLike('NameLast',$ContactName,'both'); } if($Specialty != '') { $sql->like('Specialty',$Specialty); } $rows = $sql->get()->getResultArray(); return $rows; } public function getContactWithDetail($ContactID) { $rows = $this->join('contactdetail cd', 'contact.ContactID=cd.ContactID','left')->where('contact.ContactID', $ContactID)->findAll(); $contact = []; foreach ($rows as $row) { if(empty($contact['NameFirst'])) { $contact = [ 'ContactID' => $ContactID, 'NameFirst' => $row['NameFirst'] ?? null, 'NameLast' => $row['NameLast'] ?? null, 'Title' => $row['Title'] ?? null, 'Initial' => $row['Initial'] ?? null, 'Birthdate' => $row['Birthdate'] ?? null, 'EmailAddress1' => $row['EmailAddress1'] ?? null, 'EmailAddress2' => $row['EmailAddress2'] ?? null, 'Phone' => $row['Phone'] ?? null, 'MobilePhone1' => $row['MobilePhone1'] ?? null, 'MobilePhone2' => $row['MobilePhone2'] ?? null, 'Specialty' => $row['Specialty'] ?? null, 'SubSpecialty' => $row['SubSpecialty'] ?? null, 'Details' => [] ]; } if (!empty($row['ContactDetID'])) { if (!empty($row['ContactEndDate'])) { continue; } $contact['Details'][] = [ 'SiteID' => $row['SiteID'] ?? null, 'ContactDetID' => $row['ContactDetID'], 'ContactCode' => $row['ContactCode'] ?? null, 'ContactEmail' => $row['ContactEmail'] ?? null, 'OccupationID' => $row['OccupationID'] ?? null, 'JobTitle' => $row['JobTitle'] ?? null, 'Department' => $row['Department'] ?? null, 'ContactStartDate' => $row['ContactStartDate'] ?? null, 'ContactEndDate' => $row['ContactEndDate'] ?? null ]; } } return $contact; } public function saveContact(array $data): array { $db = \Config\Database::connect(); $db->transStart(); try { $details = $data['Details'] ?? []; unset($data['Details']); if (!empty($data['ContactID'])) { $contactId = $data['ContactID']; $this->update($contactId, $data); } else { $contactId = $this->insert($data, true); } if (!$contactId) { throw new \RuntimeException('Failed to save contact'); } if (!empty($details)) { $modelDetail = new \App\Models\Contact\ContactDetailModel(); if ($this->isDetailOperationsPayload($details)) { $result = $modelDetail->applyDetailOperations($contactId, $details); } else { $result = $modelDetail->syncDetails($contactId, $details); } if ($result['status'] !== 'success') { throw new \RuntimeException('SyncDetails failed: ' . $result['message']); } } $db->transComplete(); return [ 'status' => 'success', 'ContactID' => $contactId, 'DetailsCount' => count($details), ]; } catch (\Throwable $e) { $db->transRollback(); return [ 'status' => 'error', 'message' => $e->getMessage(), ]; } } private function isDetailOperationsPayload(array $details): bool { return (bool) array_intersect(array_keys($details), ['created', 'edited', 'deleted']); } }