add siteid to contact index
This commit is contained in:
parent
dd999aaf7b
commit
98acaff93b
@ -95,7 +95,7 @@ class Contact extends Controller {
|
|||||||
if ($result) {
|
if ($result) {
|
||||||
return $this->respondCreated([
|
return $this->respondCreated([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'message' => 'Contact created successfully',
|
'message' => 'Contact updated successfully',
|
||||||
'data' => $dataContact,
|
'data' => $dataContact,
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
@ -160,6 +160,7 @@ class Contact extends Controller {
|
|||||||
private function prepareContactDetailData(array $input): array {
|
private function prepareContactDetailData(array $input): array {
|
||||||
foreach($input['ContactDetail'] as $detail) {
|
foreach($input['ContactDetail'] as $detail) {
|
||||||
$data[] = [
|
$data[] = [
|
||||||
|
"SiteID" => $detail['SiteID'] ?? null,
|
||||||
"ContactCode" => $detail['ContactCode'] ?? null,
|
"ContactCode" => $detail['ContactCode'] ?? null,
|
||||||
"ContactEmail" => $detail['ContactEmail'] ?? null,
|
"ContactEmail" => $detail['ContactEmail'] ?? null,
|
||||||
"OccupationID" => $detail['OccupationID'] ?? null,
|
"OccupationID" => $detail['OccupationID'] ?? null,
|
||||||
|
|||||||
@ -8,4 +8,30 @@ class ContactDetailModel extends Model {
|
|||||||
protected $table = 'contactdetail';
|
protected $table = 'contactdetail';
|
||||||
protected $primaryKey = 'ContactDetID';
|
protected $primaryKey = 'ContactDetID';
|
||||||
protected $allowedFields = ['ContactID', 'SiteID', 'ContactCode', 'ContactEmail', 'OccupationID', 'JobTitle', 'Department', 'ContactStartDate', 'ContactEndDate'];
|
protected $allowedFields = ['ContactID', 'SiteID', 'ContactCode', 'ContactEmail', 'OccupationID', 'JobTitle', 'Department', 'ContactStartDate', 'ContactEndDate'];
|
||||||
|
|
||||||
|
public function syncDetails(int $contactId, array $details) {
|
||||||
|
$kept = [];
|
||||||
|
|
||||||
|
foreach ($details as $detail) {
|
||||||
|
$detail['ContactID'] = $contactId;
|
||||||
|
|
||||||
|
$existing = $this->where('SiteID', $detail['SiteID'])
|
||||||
|
->where('ContactID', $contactId)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($existing) {
|
||||||
|
$this->update($existing[$this->primaryKey], $detail);
|
||||||
|
$kept[] = $existing[$this->primaryKey];
|
||||||
|
} else {
|
||||||
|
$newId = $this->insert($detail);
|
||||||
|
$kept[] = $newId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($kept)) {
|
||||||
|
$this->where('ContactID', $contactId)
|
||||||
|
->whereNotIn($this->primaryKey, $kept)
|
||||||
|
->delete();
|
||||||
|
} else { $this->where('ContactID', $contactId)->delete(); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@ class ContactModel extends Model {
|
|||||||
protected $allowedFields = ['NameFirst', 'NameLast', 'Title', 'Initial', 'Birthdate', 'EmailAddress1', 'EmailAddress2', 'Phone', 'MobilePhone1', 'MobilePhone2', 'Specialty', 'SubSpecialty'];
|
protected $allowedFields = ['NameFirst', 'NameLast', 'Title', 'Initial', 'Birthdate', 'EmailAddress1', 'EmailAddress2', 'Phone', 'MobilePhone1', 'MobilePhone2', 'Specialty', 'SubSpecialty'];
|
||||||
|
|
||||||
public function getContactsWithDetail() {
|
public function getContactsWithDetail() {
|
||||||
$rows = $this->select("c.ContactID, cd.ContactCode, c.NameFirst, c.NameLast, c.Specialty")
|
$rows = $this->select("c.ContactID, cd.SiteID, cd.ContactCode, c.NameFirst, c.NameLast, c.Specialty")
|
||||||
->join("contactdetail cd", "c.ContactID=cd.ContactID", "left")
|
->join("contactdetail cd", "c.ContactID=cd.ContactID", "left")
|
||||||
->join("occupation o","cd.OccupationID=o.OccupationID", "left")
|
->join("occupation o","cd.OccupationID=o.OccupationID", "left")
|
||||||
->get()->getResultArray();
|
->get()->getResultArray();
|
||||||
@ -40,7 +40,7 @@ class ContactModel extends Model {
|
|||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
if (!empty($row['ContactDetID'])) {
|
if (!empty($row['ContactDetID'])) {
|
||||||
$contact['Details'][] = [
|
$contact['Details'][] = [
|
||||||
'SiteID' => $row['SiteID'] ?? null,
|
'SiteID' => $row['SiteID'] ?? null,
|
||||||
'ContactDetID' => $row['ContactDetID'],
|
'ContactDetID' => $row['ContactDetID'],
|
||||||
'ContactCode' => $row['ContactCode'] ?? null,
|
'ContactCode' => $row['ContactCode'] ?? null,
|
||||||
'ContactEmail' => $row['DetailPhone'] ?? null,
|
'ContactEmail' => $row['DetailPhone'] ?? null,
|
||||||
@ -82,23 +82,17 @@ class ContactModel extends Model {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateContact(array $contactData, array $contactDetails) {
|
public function updateContact(int $ContactID, array $contactData, array $contactDetails) {
|
||||||
$db = \Config\Database::connect();
|
$db = \Config\Database::connect();
|
||||||
$db->transStart();
|
$db->transStart();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!$this->update($contactData)) {
|
if (!$this->update($ContactID, $contactData)) {
|
||||||
throw new \Exception('Failed to insert contact');
|
throw new \Exception('Failed to update contact');
|
||||||
}
|
}
|
||||||
$ContactID = $this->getInsertID();
|
|
||||||
|
|
||||||
$detailModel = new \App\Models\ContactDetailModel();
|
$detailModel = new \App\Models\ContactDetailModel();
|
||||||
foreach ($contactDetails as $detail) {
|
$detailModel->syncDetails($ContactID, $contactDetails);
|
||||||
$detail['ContactID'] = $ContactID;
|
|
||||||
if (!$detailModel->insert($detail)) {
|
|
||||||
throw new \Exception('Failed to insert contact details');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$db->transComplete();
|
$db->transComplete();
|
||||||
return $db->transStatus();
|
return $db->transStatus();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user