2025-10-22 13:40:27 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
use App\Controllers\BaseController;
|
2025-10-23 09:41:49 +07:00
|
|
|
use App\Models\SyncCRM\ZonesModel;
|
2025-10-22 13:40:27 +07:00
|
|
|
|
|
|
|
|
class Zones extends BaseController {
|
|
|
|
|
use ResponseTrait;
|
|
|
|
|
|
2025-10-23 09:41:49 +07:00
|
|
|
protected $model;
|
2025-10-22 13:40:27 +07:00
|
|
|
|
|
|
|
|
public function __construct() {
|
2025-10-23 09:41:49 +07:00
|
|
|
$this->model = new ZonesModel();
|
2025-10-22 13:40:27 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
|
|
|
|
try {
|
2025-10-23 09:41:49 +07:00
|
|
|
$rows = $this->model->getZones();
|
2025-10-22 13:40:27 +07:00
|
|
|
|
2025-10-23 09:41:49 +07:00
|
|
|
if(empty($rows)){return $this->respond(['status'=>'success', 'message'=>"no data found.", 'data'=>$rows], 200);}
|
|
|
|
|
return $this->respond(['status'=>'success', 'message'=>"data fetched successfully", 'data'=>$rows], 200);
|
2025-10-22 13:40:27 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->respond([ 'status' => 'error', 'message' => $e->getMessage() ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-23 09:41:49 +07:00
|
|
|
public function getProvinces() {
|
|
|
|
|
$filters = [
|
|
|
|
|
'zoneid' => $this->request->getVar('zoneid') ?? null,
|
|
|
|
|
'zonename' => $this->request->getVar('zonename') ?? null
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$rows = $this->model->getProvinces();
|
|
|
|
|
|
|
|
|
|
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); }
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCities() {
|
|
|
|
|
$filter = [
|
|
|
|
|
'zoneid' => $this->request->getVar('zoneid') ?? null
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$rows = $this->model->getCities($filter);
|
|
|
|
|
|
|
|
|
|
if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "data not found", 'data' => [] ], 200); }
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function synchronize() {
|
2025-10-22 13:40:27 +07:00
|
|
|
$client = \Config\Services::curlrequest();
|
|
|
|
|
|
|
|
|
|
try {
|
2025-10-23 09:41:49 +07:00
|
|
|
// Ambil data dari API pusat (CRM)
|
2025-10-22 13:40:27 +07:00
|
|
|
$response = $client->get('https://services-summit.my.id/api/zones');
|
2025-10-23 09:41:49 +07:00
|
|
|
// $response = $client->get('http://crmcomposer.local/api/zones');
|
2025-10-22 13:40:27 +07:00
|
|
|
$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'
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-23 09:41:49 +07:00
|
|
|
$record = $this->model->synchronize($result['data']);
|
2025-10-22 13:40:27 +07:00
|
|
|
|
|
|
|
|
return $this->response->setJSON([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Zones synchronized successfully',
|
|
|
|
|
'summary' => [
|
2025-10-23 09:41:49 +07:00
|
|
|
'inserted' => $record['inserted'],
|
|
|
|
|
'updated' => $record['updated'],
|
|
|
|
|
'deleted' => $record['deleted'],
|
|
|
|
|
// 'total' => count($crmData)
|
2025-10-22 13:40:27 +07:00
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->response->setJSON([
|
|
|
|
|
'status' => 'error',
|
|
|
|
|
'message' => $e->getMessage()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|