Update Zones (Skipped)
This commit is contained in:
parent
5d80b09e96
commit
f41fcad526
@ -74,7 +74,7 @@ class Zones extends BaseController {
|
|||||||
'inserted' => $record['inserted'],
|
'inserted' => $record['inserted'],
|
||||||
'updated' => $record['updated'],
|
'updated' => $record['updated'],
|
||||||
'deleted' => $record['deleted'],
|
'deleted' => $record['deleted'],
|
||||||
// 'total' => count($crmData)
|
'skipped' => $record['skipped']
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
|||||||
@ -37,41 +37,50 @@ class ZonesModel extends BaseModel {
|
|||||||
|
|
||||||
// synchronize with crm.zones
|
// synchronize with crm.zones
|
||||||
public function synchronize($crmData) {
|
public function synchronize($crmData) {
|
||||||
|
|
||||||
$db = \Config\Database::connect();
|
$db = \Config\Database::connect();
|
||||||
$db->transBegin();
|
$db->transBegin();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Ambil semua data zoneid di database lokal
|
|
||||||
$localData = $this->findAll();
|
$localData = $this->findAll();
|
||||||
$localIds = array_column($localData, 'zoneid');
|
$localMap = array_column($localData, null, 'zoneid');
|
||||||
|
$crmIds = array_column($crmData, 'zoneid');
|
||||||
|
|
||||||
// Siapkan ID dari data CRM
|
$record = ['inserted' => 0, 'updated' => 0, 'deleted' => 0, 'skipped' => 0];
|
||||||
$crmIds = array_column($crmData, 'zoneid');
|
|
||||||
|
|
||||||
// Hitung statistik
|
|
||||||
$record = [
|
|
||||||
'inserted' => 0,
|
|
||||||
'updated' => 0,
|
|
||||||
'deleted' => 0,
|
|
||||||
];
|
|
||||||
|
|
||||||
// Insert atau Update data
|
|
||||||
foreach ($crmData as $zone) {
|
foreach ($crmData as $zone) {
|
||||||
if (!isset($zone['zoneid'])) continue;
|
if (!isset($zone['zoneid'])) continue;
|
||||||
|
|
||||||
$exists = $this->find($zone['zoneid']);
|
if (isset($localMap[$zone['zoneid']])) {
|
||||||
|
// Ambil data lokal untuk perbandingan
|
||||||
|
$localRow = $localMap[$zone['zoneid']];
|
||||||
|
|
||||||
|
// Buat array subset dari kolom yang relevan untuk dibandingkan
|
||||||
|
$fieldsToCompare = ['parentzoneid', 'zonecode', 'zoneclass', 'zonename'];
|
||||||
|
|
||||||
|
$isDifferent = false;
|
||||||
|
foreach ($fieldsToCompare as $field) {
|
||||||
|
$crmVal = $zone[$field] ?? null;
|
||||||
|
$localVal = $localRow[$field] ?? null;
|
||||||
|
|
||||||
|
if ($crmVal != $localVal) { // gunakan != agar beda tipe tetap terdeteksi
|
||||||
|
$isDifferent = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($isDifferent) {
|
||||||
|
$this->update($zone['zoneid'], [
|
||||||
|
'parentzoneid' => $zone['parentzoneid'],
|
||||||
|
'zonecode' => $zone['zonecode'],
|
||||||
|
'zoneclass' => $zone['zoneclass'],
|
||||||
|
'zonename' => $zone['zonename']
|
||||||
|
]);
|
||||||
|
$this->checkDbError($db, 'Update Zones');
|
||||||
|
$record['updated']++;
|
||||||
|
} else {
|
||||||
|
$record['skipped']++; // tandai bahwa data identik, tidak diupdate
|
||||||
|
}
|
||||||
|
|
||||||
if ($exists) {
|
|
||||||
// Update jika sudah ada
|
|
||||||
$this->update($zone['zoneid'], [
|
|
||||||
'parentzoneid' => $zone['parentzoneid'],
|
|
||||||
'zonecode' => $zone['zonecode'],
|
|
||||||
'zoneclass' => $zone['zoneclass'],
|
|
||||||
'zonename' => $zone['zonename']
|
|
||||||
]);
|
|
||||||
$this->checkDbError($db, 'Update Zones');
|
|
||||||
$record['updated']++;
|
|
||||||
} else {
|
} else {
|
||||||
// Insert jika belum ada
|
// Insert jika belum ada
|
||||||
$this->insert($zone);
|
$this->insert($zone);
|
||||||
@ -80,27 +89,26 @@ class ZonesModel extends BaseModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hapus data yang sudah tidak ada di CRM
|
// Hapus data yang tidak ada di CRM
|
||||||
foreach ($localIds as $localId) {
|
foreach ($localMap as $localId => $row) {
|
||||||
if (!in_array($localId, $crmIds)) {
|
if (!in_array($localId, $crmIds)) {
|
||||||
$this->delete($localId);
|
$this->delete($localId);
|
||||||
$this->checkDbError($db, 'Delete Zones');
|
$this->checkDbError($db, 'Delete Zones');
|
||||||
$record['deleted']++;;
|
$record['deleted']++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$db->transCommit();
|
$db->transCommit();
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$db->transRollback();
|
$db->transRollback();
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
$db->transComplete();
|
|
||||||
|
|
||||||
return $record;
|
return $record;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Check Error and Send Spesific Messages
|
// Check Error and Send Spesific Messages
|
||||||
private function checkDbError($db, string $context) {
|
private function checkDbError($db, string $context) {
|
||||||
$error = $db->error();
|
$error = $db->error();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user