135 lines
4.2 KiB
PHP
135 lines
4.2 KiB
PHP
<?php
|
||
namespace App\Controllers;
|
||
|
||
use CodeIgniter\API\ResponseTrait;
|
||
use App\Controllers\BaseController;
|
||
// use App\Models\PatVisit\PatVisitModel;
|
||
|
||
class Zones extends BaseController {
|
||
use ResponseTrait;
|
||
|
||
// protected $model;
|
||
|
||
public function __construct() {
|
||
// $this->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()
|
||
]);
|
||
}
|
||
}
|
||
|
||
} |