clqms-be/app/Controllers/ZonesController.php

96 lines
3.2 KiB
PHP
Raw Normal View History

2025-10-22 13:40:27 +07:00
<?php
2025-12-01 16:47:52 +07:00
/*
2025-10-22 13:40:27 +07:00
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use App\Controllers\BaseController;
use App\Models\SyncCRM\ZonesModel;
2025-10-22 13:40:27 +07:00
class ZonesController extends BaseController {
2025-10-22 13:40:27 +07:00
use ResponseTrait;
protected $model;
2025-10-22 13:40:27 +07:00
public function __construct() {
$this->model = new ZonesModel();
2025-10-22 13:40:27 +07:00
}
public function index() {
try {
$rows = $this->model->getZones();
2025-10-22 13:40:27 +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);
}
}
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-11-05 15:03:55 +07:00
$client = \Config\Services::curlrequest([
'headers' => [
'User-Agent' => 'Mozilla/5.0 (CI4 cURL Request)',
'Accept' => 'application/json',
],
]);
2025-10-22 13:40:27 +07:00
try {
// Ambil data dari API pusat (CRM)
2025-11-20 09:45:16 +07:00
$response = $client->get('http://services-summit.my.id/api/zones'); // Prod
// $response = $client->get('https://services-summit.my.id/api/zones'); // Dev
2025-10-22 13:40:27 +07:00
$result = json_decode($response->getBody(), true);
2025-11-05 15:03:55 +07:00
2025-10-22 13:40:27 +07:00
if (!isset($result['data']) || !is_array($result['data'])) {
return $this->response->setJSON([
'status' => 'error',
'message' => 'Invalid or empty response from CRM API'
]);
}
$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' => [
'inserted' => $record['inserted'],
'updated' => $record['updated'],
'deleted' => $record['deleted'],
2025-11-04 09:42:03 +07:00
'skipped' => $record['skipped']
2025-10-22 13:40:27 +07:00
]
]);
} catch (\Exception $e) {
return $this->response->setJSON([
'status' => 'error',
'message' => $e->getMessage()
]);
}
}
2025-12-01 16:47:52 +07:00
}
*/