add dashboard, fix get data scraping, fix table

This commit is contained in:
arifal
2025-01-24 01:58:06 +07:00
parent 4de7067487
commit 5f93a18f60
53 changed files with 2671 additions and 58 deletions

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Helpers;
class ApiResponse
{
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
public static function successResponse($data, $message = null){
return response()->json([
'success' => true,
'message' => $message,
'data' => $data
]);
}
public static function errorResponse($message, $statusCode = 400){
return response()->json([
'success' => false,
'message' => $message,
], $statusCode);
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace App\Http\Controllers\Api;
use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\PbgTask;
use App\Models\PbgTaskRetributions;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller
{
public function businnessDocument(Request $request){
$businessData = DB::table('pbg_task')
->leftJoin('pbg_task_retributions', 'pbg_task.uuid', '=', 'pbg_task_retributions.pbg_task_uid')
->select(
DB::raw('COUNT(DISTINCT pbg_task.id) as task_count'),
DB::raw('SUM(pbg_task_retributions.nilai_retribusi_bangunan) as total_retribution')
)
->where(function ($query) {
$query->where("pbg_task.function_type", "NOT LIKE", "sebagai tempat usaha%")
->orWhereNull("pbg_task.function_type");
})
->first();
$taskCount = $businessData->task_count;
$taskTotal = $businessData->total_retribution;
$result = [
"count" => $taskCount,
"series" => [$taskCount],
"total" => $taskTotal
];
return ApiResponse::successResponse($result, "Successfully count businness doucument");
}
public function nonBusinnessDocument(Request $request){
$businessData = DB::table('pbg_task')
->leftJoin('pbg_task_retributions', 'pbg_task.uuid', '=', 'pbg_task_retributions.pbg_task_uid')
->select(
DB::raw('COUNT(DISTINCT pbg_task.id) as task_count'),
DB::raw('SUM(pbg_task_retributions.nilai_retribusi_bangunan) as total_retribution')
)
->where(function ($query) {
$query->where("pbg_task.function_type", "LIKE", "sebagai tempat usaha%");
})
->first();
$taskCount = $businessData->task_count;
$taskTotal = $businessData->total_retribution;
$result = [
"count" => $taskCount,
"series" => [$taskCount],
"total" => $taskTotal
];
return ApiResponse::successResponse($result, "Successfully count not businness doucument");
}
public function allTaskDocuments(){
$query = DB::table('pbg_task')
->leftJoin('pbg_task_retributions', 'pbg_task.uuid', '=', 'pbg_task_retributions.pbg_task_uid')
->select(
DB::raw('COUNT(DISTINCT pbg_task.id) as task_count'),
DB::raw('SUM(pbg_task_retributions.nilai_retribusi_bangunan) as total_retribution')
)
->first();
$taskCount = $query->task_count;
$taskTotal = $query->total_retribution;
$result = [
"count" => $taskCount,
"series" => [$taskCount],
"total" => $taskTotal
];
return ApiResponse::successResponse($result, "Successfully count all tasks documents");
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use App\Models\User;
use Hash;
use Illuminate\Http\Request;
class UsersController extends Controller
{
public function login(LoginRequest $request){
$user = User::where('email', $request->email)->first();
if(!$user || !Hash::check($request->password, $user->password)){
return response(['message' => 'Invalid credentials'], 401);
}
$token = $user->createToken($_ENV['APP_KEY'])->plainTextToken;
return response(['user' => $user, 'token' => $token], 200);
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Http\Controllers\Dashboards;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class BigDataController extends Controller
{
public function index(){
return view('dashboards.bigdata');
}
}

View File

@@ -10,7 +10,7 @@ class HomeController extends Controller
{
public function index(Request $request){
if(Auth::check()){
return view('index');
return view('home.index');
}else{
return view('auth.signin');
}

View File

@@ -0,0 +1,70 @@
<?php
namespace App\Http\Controllers\Master;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Validation\Rules;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
class UsersController extends Controller
{
public function index(){
$users = User::paginate(20);
return view('master.users.index', compact('users'));
}
public function create(){
return view('master.users.create');
}
public function store(Request $request){
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
'firstname' => ['required', 'string', 'max:255'],
'lastname' => ['required', 'string', 'max:255'],
'position' => ['required', 'string', 'max:255']
]);
dd($request);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
'firstname' => $request->firstname,
'lastname' => $request->lastname,
'position' => $request->position
]);
return redirect()->route('master.users')->with('success','Successfully registered');
}
public function show($id){
$user = User::find($id);
return view('master.users.show', compact('user'));
}
public function edit($id){
$user = User::find($id);
return view('master.users.edit', compact('user'));
}
public function update(Request $request, $id){
$user = User::find($id);
$validate = $request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
'firstname' => ['required', 'string', 'max:255'],
'lastname' => ['required', 'string', 'max:255'],
'position' => ['required', 'string', 'max:255']
]);
$user->update($validate);
return redirect()->route('master.users')->with('success', 'Successfully');
}
public function destroy($id){
$user = User::find($id);
$user->delete();
return redirect()->route('master.users')->with('success','Successfully deleted');
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Http\Controllers\Settings;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class SettingsController extends Controller
{
public function index(){
return view('settings.general.index');
}
public function create(){
return view('settings.general.create');
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Http\Controllers\Settings;
use App\Http\Controllers\Controller;
use App\ServiceSIMBG;
use Illuminate\Http\Request;
use Exception;
class SyncronizeController extends Controller
{
public function index(Request $request){
return view('settings.syncronize.index');
}
public function syncPbgTask(){
$res = (new ServiceSIMBG())->syncTaskList();
return $res;
}
public function syncronizeTask(Request $request){
$res = (new ServiceSIMBG())->syncTaskList();
return redirect()->back()->with('success', 'Processing completed successfully');
}
public function getUserToken(){
$res = (new ServiceSIMBG())->getToken();
return $res;
}
public function syncIndexIntegration($uuid){
$res = (new ServiceSIMBG())->syncIndexIntegration($uuid);
return $res;
}
public function syncTaskDetailSubmit($uuid){
$res = (new ServiceSIMBG())->syncTaskDetailSubmit($uuid);
return $res;
}
}

36
app/Models/PbgTask.php Normal file
View File

@@ -0,0 +1,36 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PbgTask extends Model
{
use HasFactory;
protected $table = 'pbg_task';
protected $fillable = [
'uuid',
'name',
'owner_name',
'application_type',
'application_type_name',
'condition',
'registration_number',
'document_number',
'address',
'status',
'status_name',
'slf_status',
'slf_status_name',
'function_type',
'consultation_type',
'due_date',
'land_certificate_phase',
'task_created_at'
];
public function retributions(){
return $this->hasOne(PbgTaskRetributions::class, 'pbg_task_uid', 'uuid');
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PbgTaskIndexIntegrations extends Model
{
use HasFactory;
protected $table = "pbg_task_index_integrations";
protected $fillable = [
'pbg_task_uid',
'indeks_fungsi_bangunan',
'indeks_parameter_kompleksitas',
'indeks_parameter_permanensi',
'indeks_parameter_ketinggian',
'faktor_kepemilikan',
'indeks_terintegrasi',
'total',
];
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PbgTaskPrasarana extends Model
{
use HasFactory;
protected $table = "pbg_task_prasarana";
protected $fillable = [
'pbg_task_uid',
'prasarana_id',
'prasarana_type',
'building_type',
'total',
'quantity',
'unit',
'index_prasarana',
];
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PbgTaskRetributions extends Model
{
use HasFactory;
protected $table = "pbg_task_retributions";
protected $fillable = [
'detail_id',
'detail_created_at',
'detail_updated_at',
'detail_uid',
'luas_bangunan',
'indeks_lokalitas',
'wilayah_shst',
'kegiatan_id',
'kegiatan_name',
'nilai_shst',
'indeks_terintegrasi',
'indeks_bg_terbangun',
'nilai_retribusi_bangunan',
'nilai_prasarana',
'created_by',
'pbg_document',
'underpayment',
'skrd_amount',
'pbg_task_uid'
];
public function task(){
return $this->belongsTo(PbgTask::class, 'pbg_task_uid', 'uuid');
}
}

View File

@@ -6,11 +6,12 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
use HasFactory, Notifiable, HasApiTokens;
/**
* The attributes that are mass assignable.
@@ -21,6 +22,9 @@ class User extends Authenticatable
'name',
'email',
'password',
'firstname',
'lastname',
'position'
];
/**

73
app/ServiceClient.php Normal file
View File

@@ -0,0 +1,73 @@
<?php
namespace App;
use App\Helpers\ApiResponse;
use GuzzleHttp\Client;
use Exception;
class ServiceClient
{
private $client;
private $baseUrl;
private $headers;
/**
* Create a new class instance.
*/
public function __construct($baseUrl = '', $headers = [])
{
$this->client = new Client();
$this->baseUrl = $baseUrl;
$this->headers = array_merge(
[
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
$headers
);
}
public function makeRequest($url, $method = 'GET', $body = null, $headers = []){
try {
$headers = array_merge($this->headers, $headers);
$options = [
'headers' => $headers,
];
if ($body) {
$options['json'] = $body; // Guzzle akan mengonversi array ke JSON
}
$response = $this->client->request($method, $this->baseUrl . $url, $options);
$resultResponse = json_decode($response->getBody(), true);
return ApiResponse::successResponse($resultResponse, "Successfully fetched data");
} catch (Exception $e) {
return ApiResponse::errorResponse($e->getMessage(), $e->getCode());
}
}
// Fungsi untuk melakukan permintaan GET
public function get($url, $headers = [])
{
return $this->makeRequest($url, 'GET', null, $headers);
}
// Fungsi untuk melakukan permintaan POST
public function post($url, $body, $headers = [])
{
return $this->makeRequest($url, 'POST', $body, $headers);
}
// Fungsi untuk melakukan permintaan PUT
public function put($url, $body, $headers = [])
{
return $this->makeRequest($url, 'PUT', $body, $headers);
}
// Fungsi untuk melakukan permintaan DELETE
public function delete($url, $headers = [])
{
return $this->makeRequest($url, 'DELETE', null, $headers);
}
}

278
app/ServiceSIMBG.php Normal file
View 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]);
}
}