add dashboard, fix get data scraping, fix table
This commit is contained in:
278
app/ServiceSIMBG.php
Normal file
278
app/ServiceSIMBG.php
Normal file
@@ -0,0 +1,278 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Helpers\ApiResponse;
|
||||
use App\Models\PbgTaskIndexIntegrations;
|
||||
use App\Models\PbgTaskPrasarana;
|
||||
use App\Models\PbgTaskRetributions;
|
||||
use Exception;
|
||||
use App\Models\PbgTask;
|
||||
class ServiceSIMBG
|
||||
{
|
||||
private $email;
|
||||
private $password;
|
||||
/**
|
||||
* Create a new class instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->email = $_ENV['SIMBG_EMAIL'];
|
||||
$this->password = $_ENV['SIMBG_PASSWORD'];
|
||||
}
|
||||
|
||||
public function getToken(){
|
||||
$clientHelper = new ServiceClient($_ENV['SIMBG_HOST']);
|
||||
$url = "api/user/v1/auth/login/";
|
||||
$body = [
|
||||
'email' => $this->email,
|
||||
'password' => $this->password,
|
||||
];
|
||||
|
||||
$res = $clientHelper->post($url, $body);
|
||||
if(!$res->original['success']){
|
||||
return null;
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function syncIndexIntegration($uuid)
|
||||
{
|
||||
$clientHelper = new ServiceClient($_ENV['SIMBG_HOST']);
|
||||
$url = "api/pbg/v1/detail/" . $uuid . "/retribution/indeks-terintegrasi/";
|
||||
$resToken = $this->getToken();
|
||||
|
||||
if (!isset($resToken) || empty($resToken->original['data']['token']['access'])) {
|
||||
// Log error
|
||||
\Log::error("Token not retrieved for syncIndexIntegration", ['uuid' => $uuid]);
|
||||
return null;
|
||||
}
|
||||
|
||||
$apiToken = $resToken->original['data']['token']['access'];
|
||||
$headers = [
|
||||
'Authorization' => "Bearer " . $apiToken,
|
||||
];
|
||||
|
||||
$res = $clientHelper->get($url, $headers);
|
||||
|
||||
if (empty($res->original['success']) || !$res->original['success']) {
|
||||
// Log error
|
||||
\Log::error("API response indicates failure", ['url' => $url, 'uuid' => $uuid]);
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = $res->original['data']['data'] ?? null;
|
||||
if (!$data) {
|
||||
\Log::error("No valid data returned from API", ['url' => $url, 'uuid' => $uuid]);
|
||||
return null;
|
||||
}
|
||||
|
||||
PbgTaskIndexIntegrations::updateOrCreate(
|
||||
['pbg_task_uid' => $uuid],
|
||||
[
|
||||
'indeks_fungsi_bangunan' => $data['indeks_fungsi_bangunan'] ?? null,
|
||||
'indeks_parameter_kompleksitas' => $data['indeks_parameter_kompleksitas'] ?? null,
|
||||
'indeks_parameter_permanensi' => $data['indeks_parameter_permanensi'] ?? null,
|
||||
'indeks_parameter_ketinggian' => $data['indeks_parameter_ketinggian'] ?? null,
|
||||
'faktor_kepemilikan' => $data['faktor_kepemilikan'] ?? null,
|
||||
'indeks_terintegrasi' => $data['indeks_terintegrasi'] ?? null,
|
||||
'total' => $data['total'] ?? null,
|
||||
]
|
||||
);
|
||||
|
||||
// Log success
|
||||
\Log::info("syncIndexIntegration completed successfully", ['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
|
||||
public function syncTaskList()
|
||||
{
|
||||
$clientHelper = new ServiceClient($_ENV['SIMBG_HOST']);
|
||||
$resToken = $this->getToken();
|
||||
|
||||
if (!isset($resToken) || empty($resToken->original['data']['token']['access'])) {
|
||||
\Log::error("Token not retrieved for syncTaskList");
|
||||
return ApiResponse::errorResponse("Failed to retrieve token", 401);
|
||||
}
|
||||
|
||||
$apiToken = $resToken->original['data']['token']['access'];
|
||||
$queryParams = http_build_query([
|
||||
'page' => 1,
|
||||
'size' => 20,
|
||||
'sort' => 'ASC',
|
||||
'type' => 'task',
|
||||
]);
|
||||
|
||||
$url = "api/pbg/v1/list/?" . $queryParams;
|
||||
$headers = ['Authorization' => "Bearer " . $apiToken];
|
||||
|
||||
$initialResponse = $clientHelper->get($url, $headers);
|
||||
if (empty($initialResponse->original['data']['total_page'])) {
|
||||
\Log::error("Invalid response: no total_page", ['response' => $initialResponse->original]);
|
||||
return ApiResponse::errorResponse("Invalid response from API", 400);
|
||||
}
|
||||
|
||||
$totalPage = $initialResponse->original['data']['total_page'];
|
||||
$savedCount = 0;
|
||||
$failedCount = 0;
|
||||
|
||||
for ($currentPage = 1; $currentPage <= $totalPage; $currentPage++) {
|
||||
$queryParams = http_build_query([
|
||||
'page' => $currentPage,
|
||||
'size' => 20,
|
||||
'sort' => 'ASC',
|
||||
'type' => 'task',
|
||||
]);
|
||||
|
||||
$url = "api/pbg/v1/list/?" . $queryParams;
|
||||
$response = $clientHelper->get($url, $headers);
|
||||
|
||||
if (empty($response->original['data']['data'])) {
|
||||
\Log::warning("No data found on page", ['page' => $currentPage]);
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($response->original['data']['data'] as $item) {
|
||||
try {
|
||||
$taskCreatedAt = isset($item['created_at'])
|
||||
? \Carbon\Carbon::parse($item['created_at'])->format('Y-m-d H:i:s')
|
||||
: null;
|
||||
PbgTask::updateOrCreate(
|
||||
[
|
||||
'uuid' => $item['uid'],
|
||||
],
|
||||
[
|
||||
'name' => $item['name'],
|
||||
'owner_name' => $item['owner_name'],
|
||||
'application_type' => $item['application_type'],
|
||||
'application_type_name' => $item['application_type_name'],
|
||||
'condition' => $item['condition'],
|
||||
'registration_number' => $item['registration_number'],
|
||||
'document_number' => $item['document_number'],
|
||||
'address' => $item['address'],
|
||||
'status' => $item['status'],
|
||||
'status_name' => $item['status_name'],
|
||||
'slf_status' => $item['slf_status'] ?? null,
|
||||
'slf_status_name' => $item['slf_status_name'] ?? null,
|
||||
'function_type' => $item['function_type'],
|
||||
'consultation_type' => $item['consultation_type'],
|
||||
'due_date' => $item['due_date'],
|
||||
'land_certificate_phase' => $item['land_certificate_phase'],
|
||||
'task_created_at' => $taskCreatedAt,
|
||||
]
|
||||
);
|
||||
|
||||
// Synchronize additional details
|
||||
$this->syncIndexIntegration($item['uid']);
|
||||
$this->syncTaskDetailSubmit($item['uid']);
|
||||
$savedCount++;
|
||||
} catch (Exception $e) {
|
||||
\Log::error("Failed to process task", [
|
||||
'error' => $e->getMessage(),
|
||||
'task' => $item,
|
||||
]);
|
||||
$failedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$result = [
|
||||
"savedCount" => $savedCount,
|
||||
"failedCount" => $failedCount,
|
||||
];
|
||||
|
||||
\Log::info("syncTaskList completed", $result);
|
||||
|
||||
return ApiResponse::successResponse(json_encode($result), "Successfully saved");
|
||||
}
|
||||
|
||||
|
||||
public function syncTaskDetailSubmit($uuid)
|
||||
{
|
||||
$clientHelper = new ServiceClient($_ENV['SIMBG_HOST']);
|
||||
$resToken = $this->getToken();
|
||||
|
||||
if (!isset($resToken) || empty($resToken->original['data']['token']['access'])) {
|
||||
// Log error
|
||||
\Log::error("Token not retrieved for syncTaskDetailSubmit");
|
||||
return null;
|
||||
}
|
||||
|
||||
$apiToken = $resToken->original['data']['token']['access'];
|
||||
$url = "api/pbg/v1/detail/" . $uuid . "/retribution/submit/";
|
||||
$headers = [
|
||||
'Authorization' => "Bearer " . $apiToken,
|
||||
];
|
||||
|
||||
$res = $clientHelper->get($url, $headers);
|
||||
|
||||
if (empty($res->original['success']) || !$res->original['success']) {
|
||||
// Log error
|
||||
\Log::error("API response indicates failure", ['url' => $url, 'uuid' => $uuid]);
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = $res->original['data']['data'] ?? [];
|
||||
if (empty($data)) {
|
||||
\Log::error("No data returned from API", ['url' => $url, 'uuid' => $uuid]);
|
||||
return null;
|
||||
}
|
||||
|
||||
$detailCreatedAt = isset($data['created_at'])
|
||||
? \Carbon\Carbon::parse($data['created_at'])->format('Y-m-d H:i:s')
|
||||
: null;
|
||||
|
||||
$detailUpdatedAt = isset($data['updated_at'])
|
||||
? \Carbon\Carbon::parse($data['updated_at'])->format('Y-m-d H:i:s')
|
||||
: null;
|
||||
|
||||
PbgTaskRetributions::updateOrCreate(
|
||||
['detail_id' => $data['id']],
|
||||
[
|
||||
'detail_uid' => $data['uid'] ?? null,
|
||||
'detail_created_at' => $detailCreatedAt ?? null,
|
||||
'detail_updated_at' => $detailUpdatedAt ?? null,
|
||||
'luas_bangunan' => $data['luas_bangunan'] ?? null,
|
||||
'indeks_lokalitas' => $data['indeks_lokalitas'] ?? null,
|
||||
'wilayah_shst' => $data['wilayah_shst'] ?? null,
|
||||
'kegiatan_id' => $data['kegiatan']['id'] ?? null,
|
||||
'kegiatan_name' => $data['kegiatan']['name'] ?? null,
|
||||
'nilai_shst' => $data['nilai_shst'] ?? null,
|
||||
'indeks_terintegrasi' => $data['indeks_terintegrasi'] ?? null,
|
||||
'indeks_bg_terbangun' => $data['indeks_bg_terbangun'] ?? null,
|
||||
'nilai_retribusi_bangunan' => $data['nilai_retribusi_bangunan'] ?? null,
|
||||
'nilai_prasarana' => $data['nilai_prasarana'] ?? null,
|
||||
'created_by' => $data['created_by'] ?? null,
|
||||
'pbg_document' => $data['pbg_document'] ?? null,
|
||||
'underpayment' => $data['underpayment'] ?? null,
|
||||
'skrd_amount' => $data['skrd_amount'] ?? null,
|
||||
'pbg_task_uid' => $uuid,
|
||||
]
|
||||
);
|
||||
|
||||
$prasaranaData = $data['prasarana'] ?? [];
|
||||
if (is_array($prasaranaData) && count($prasaranaData) > 0) {
|
||||
foreach ($prasaranaData as $item) {
|
||||
PbgTaskPrasarana::updateOrCreate(
|
||||
[
|
||||
'pbg_task_uid' => $uuid,
|
||||
'prasarana_id' => $item['id'] ?? null,
|
||||
],
|
||||
[
|
||||
'pbg_task_uid' => $uuid,
|
||||
'prasarana_type' => $item['prasarana_type'] ?? null,
|
||||
'building_type' => $item['building_type'] ?? null,
|
||||
'total' => $item['total'] ?? null,
|
||||
'quantity' => $item['quantity'] ?? null,
|
||||
'unit' => $item['unit'] ?? null,
|
||||
'index_prasarana' => $item['index_prasarana'] ?? null,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
\Log::info("syncTaskDetailSubmit completed successfully", ['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user