From bd6184fddfbf0c6256095883fae6438e5c4735a3 Mon Sep 17 00:00:00 2001 From: mikael-zakaria Date: Wed, 22 Oct 2025 13:40:27 +0700 Subject: [PATCH] Update Zones Sync v1 --- app/Config/Routes.php | 7 +- app/Controllers/Zones.php | 135 ++++++++++++++++++++++++++++++ app/Models/SyncCRM/ZonesModel.php | 22 +++++ 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 app/Controllers/Zones.php create mode 100644 app/Models/SyncCRM/ZonesModel.php diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 8908f7d..7cd7baa 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -90,4 +90,9 @@ $routes->get('/api/containerdef/', 'Specimen\ContainerDef::index'); $routes->get('/api/containerdef/(:num)', 'Specimen\ContainerDef::show/$1'); $routes->post('/api/containerdef', 'Specimen\ContainerDef::create'); $routes->patch('/api/containerdef', 'Specimen\ContainerDef::update'); -$routes->delete('/api/containerdef', 'Specimen\ContainerDef::delete'); \ No newline at end of file +$routes->delete('/api/containerdef', 'Specimen\ContainerDef::delete'); + +$routes->get('/api/zones', 'Zones::index'); +$routes->get('/api/zones/syncronize', 'Zones::syncronize'); +// $routes->get('/api/provinces', 'Api\ZonesApi::getProvinces'); +// $routes->get('/api/cities', 'Api\ZonesApi::getCities'); \ No newline at end of file diff --git a/app/Controllers/Zones.php b/app/Controllers/Zones.php new file mode 100644 index 0000000..a40f49f --- /dev/null +++ b/app/Controllers/Zones.php @@ -0,0 +1,135 @@ +model = new PatVisitModel(); + } + + // public function __construct() { + // $this->db = \Config\Database::connect(); + // $this->model = new PatientModel(); + // $this->rules = [ + // 'PatientID' => 'required|max_length[50]', + // 'AlternatePID' => 'permit_empty|max_length[50]' + // ]; + // } + + public function index() { + + // Buat instance HTTP client + $client = \Config\Services::curlrequest(); + + try { + // $response = $client->get('https://services-summit.my.id/api/provinces'); + $response = $client->get('http://crmcomposer.local/api/zones'); + + // Ambil body responsenya + $body = $response->getBody(); + + // Decode JSON ke array PHP + $data = json_decode($body, true); + + return $this->respond(['status'=>'success', 'message'=>"data fetched successfully", 'data'=>$data['data']], 200); + } catch (\Exception $e) { + + return $this->respond([ 'status' => 'error', 'message' => $e->getMessage() ], 200); + } + + } + + public function synchronize() + { + $client = \Config\Services::curlrequest(); + $zonesModel = new ZonesModel(); + + try { + // 1️⃣ Ambil data dari API pusat (CRM) + $response = $client->get('https://services-summit.my.id/api/zones'); + $result = json_decode($response->getBody(), true); + + if (!isset($result['data']) || !is_array($result['data'])) { + return $this->response->setJSON([ + 'status' => 'error', + 'message' => 'Invalid or empty response from CRM API' + ]); + } + + $crmData = $result['data']; + + // 2️⃣ Ambil semua data zoneid di database lokal + $localData = $zonesModel->findAll(); + $localIds = array_column($localData, 'zoneid'); + + // 3️⃣ Siapkan ID dari data CRM + $crmIds = array_column($crmData, 'zoneid'); + + // Hitung statistik + $inserted = 0; + $updated = 0; + $deleted = 0; + + // 4️⃣ Mulai transaksi agar aman + $db = \Config\Database::connect(); + $db->transStart(); + + // 5️⃣ Insert atau Update data + foreach ($crmData as $zone) { + if (!isset($zone['zoneid'])) continue; + + $exists = $zonesModel->find($zone['zoneid']); + + if ($exists) { + // Update jika sudah ada + $zonesModel->update($zone['zoneid'], [ + 'parentzoneid' => $zone['parentzoneid'], + 'zonecode' => $zone['zonecode'], + 'zoneclass' => $zone['zoneclass'], + 'zonename' => $zone['zonename'] + ]); + $updated++; + } else { + // Insert jika belum ada + $zonesModel->insert($zone); + $inserted++; + } + } + + // 6️⃣ Hapus data yang sudah tidak ada di CRM + foreach ($localIds as $localId) { + if (!in_array($localId, $crmIds)) { + $zonesModel->delete($localId); + $deleted++; + } + } + + // 7️⃣ Commit transaksi + $db->transComplete(); + + return $this->response->setJSON([ + 'status' => 'success', + 'message' => 'Zones synchronized successfully', + 'summary' => [ + 'inserted' => $inserted, + 'updated' => $updated, + 'deleted' => $deleted, + 'total' => count($crmData) + ] + ]); + } catch (\Exception $e) { + return $this->response->setJSON([ + 'status' => 'error', + 'message' => $e->getMessage() + ]); + } + } + +} \ No newline at end of file diff --git a/app/Models/SyncCRM/ZonesModel.php b/app/Models/SyncCRM/ZonesModel.php new file mode 100644 index 0000000..3efe4f0 --- /dev/null +++ b/app/Models/SyncCRM/ZonesModel.php @@ -0,0 +1,22 @@ +error(); + if (!empty($error['code'])) { + throw new \Exception( + "{$context} failed: {$error['code']} - {$error['message']}" + ); + } + } + + +} \ No newline at end of file