fix data and dashboard count and sum

This commit is contained in:
arifal hidayat
2025-08-08 03:17:04 +07:00
parent 588e3ad5e2
commit 1288ab509d
19 changed files with 182 additions and 78 deletions

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Services\ServiceGoogleSheet;
use App\Models\BigdataResume;
use App\Models\ImportDatasource;
use Illuminate\Support\Facades\Log;
class SyncDashboardPbg extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:sync-dashboard-pbg';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$import_datasource = ImportDatasource::create([
'message' => 'Initiating sync dashboard pbg...',
'response_body' => null,
'status' => 'processing',
'start_time' => now(),
'failed_uuid' => null
]);
try {
$service = new ServiceGoogleSheet();
$data_setting_result = $service->get_big_resume_data();
Log::info('Data setting result: ' . json_encode($data_setting_result));
BigdataResume::generateResumeData($import_datasource->id, "simbg", $data_setting_result);
$import_datasource->update([
'status' => 'success',
'message' => 'Sync dashboard pbg completed successfully.',
'finish_time' => now()
]);
} catch (\Exception $e) {
Log::error('Sync dashboard pbg failed: ' . $e->getMessage(), ['trace' => $e->getTraceAsString()]);
// Update status to failed
if (isset($import_datasource)) {
$import_datasource->update([
'status' => 'failed',
'message' => 'Sync dashboard pbg failed.',
'finish_time' => now()
]);
}
}
}
}

View File

@@ -23,14 +23,15 @@ class BigDataResumeController extends Controller
{
try{
$filterDate = $request->get("filterByDate");
$type = $request->get("type");
if (!$filterDate || $filterDate === "latest") {
$big_data_resume = BigdataResume::where('year', 'leader')->latest()->first();
$big_data_resume = BigdataResume::where('year', $type)->latest()->first();
if (!$big_data_resume) {
return $this->response_empty_resume();
}
} else {
$big_data_resume = BigdataResume::where('year', 'leader')
$big_data_resume = BigdataResume::where('year', $type)
->whereDate('created_at', $filterDate)
->orderBy('id', 'desc')
->first();

View File

@@ -24,7 +24,8 @@ class RequestAssignmentController extends Controller
$query = PbgTask::with([
'attachments' => function ($q) {
$q->whereIn('pbg_type', ['berita_acara', 'bukti_bayar']);
}
},
'pbg_task_retributions'
])->orderBy('id', 'desc');
if ($request->has('filter') && !empty($request->get('filter'))) {
@@ -34,7 +35,8 @@ class RequestAssignmentController extends Controller
case 'non-business':
$query->where(function ($q) {
$q->where(function ($q2) {
$q2->whereRaw("LOWER(function_type) != ?", ['sebagai tempat usaha'])
$q2->whereRaw("LOWER(function_type) NOT LIKE ?", ['%fungsi usaha%'])
->whereRaw("LOWER(function_type) NOT LIKE ?", ['%sebagai tempat usaha%'])
->whereRaw("status != ?", 20)
->orWhereNull('function_type');
});
@@ -43,8 +45,11 @@ class RequestAssignmentController extends Controller
case 'business':
$query->where(function ($q) {
$q->whereRaw("LOWER(function_type) = ?", ['sebagai tempat usaha'])
->whereRaw("status != ?", 20);
$q->where(function ($q2) {
$q2->whereRaw("LOWER(function_type) LIKE ?", ['%fungsi usaha%'])
->orWhereRaw("LOWER(function_type) LIKE ?", ['%sebagai tempat usaha%'])
->whereRaw("status != ?", 20);
});
});
break;

View File

@@ -42,6 +42,7 @@ class RequestAssignmentResouce extends JsonResource
->where('pbg_type', 'bukti_bayar')
->sortByDesc('created_at')
->first(),
'pbg_task_retributions' => $this->pbg_task_retributions,
];
}
}

View File

@@ -50,8 +50,8 @@ class ScrapingDataJob implements ShouldQueue
]);
// Run the scraping services
// $service_google_sheet->run_service();
// $service_pbg_task->run_service();
$service_google_sheet->run_service();
$service_pbg_task->run_service();
try{
$service_tab_pbg_task->run_service();
}catch(\Exception $e){

View File

@@ -38,28 +38,30 @@ class BigdataResume extends Model
}
public static function generateResumeData($import_datasource_id, $year, $data_setting){
$stats = PbgTask::with(['googleSheet', 'pbg_task_retributions'])
$stats = PbgTask::with(['pbg_task_retributions'])
->leftJoin('pbg_task_retributions as ptr', 'pbg_task.uuid', '=', 'ptr.pbg_task_uid')
->leftJoin('pbg_task_google_sheet as ptgs', 'pbg_task.registration_number', '=', 'ptgs.no_registrasi')
->when($year !== 'all', function ($query) use ($year) {
$query->whereYear('pbg_task.task_created_at', (int) $year);
})
->selectRaw("
COUNT(CASE WHEN LOWER(TRIM(ptgs.status_verifikasi)) = 'selesai verifikasi' THEN 1 END) AS verified_count,
SUM(CASE WHEN LOWER(TRIM(ptgs.status_verifikasi)) = 'selesai verifikasi' THEN ptr.nilai_retribusi_bangunan ELSE 0 END) AS verified_total,
COUNT(CASE WHEN pbg_task.status = 20 THEN 1 END) AS verified_count,
SUM(CASE WHEN pbg_task.status = 20 THEN ptr.nilai_retribusi_bangunan ELSE 0 END) AS verified_total,
COUNT(CASE WHEN LOWER(TRIM(ptgs.status_verifikasi)) != 'selesai verifikasi' OR ptgs.status_verifikasi IS NULL THEN 1 END) AS non_verified_count,
SUM(CASE WHEN LOWER(TRIM(ptgs.status_verifikasi)) != 'selesai verifikasi' OR ptgs.status_verifikasi IS NULL THEN ptr.nilai_retribusi_bangunan ELSE 0 END) AS non_verified_total,
COUNT(CASE WHEN pbg_task.status != 20 OR pbg_task.status IS NULL THEN 1 END) AS non_verified_count,
SUM(CASE WHEN pbg_task.status != 20 OR pbg_task.status IS NULL THEN ptr.nilai_retribusi_bangunan ELSE 0 END) AS non_verified_total,
COUNT(CASE WHEN (LOWER(TRIM(ptgs.status_verifikasi)) != 'selesai verifikasi' OR ptgs.status_verifikasi IS NULL)
AND LOWER(TRIM(pbg_task.function_type)) = 'sebagai tempat usaha' THEN 1 END) AS business_count,
SUM(CASE WHEN (LOWER(TRIM(ptgs.status_verifikasi)) != 'selesai verifikasi' OR ptgs.status_verifikasi IS NULL)
AND LOWER(TRIM(pbg_task.function_type)) = 'sebagai tempat usaha' THEN ptr.nilai_retribusi_bangunan ELSE 0 END) AS business_total,
COUNT(CASE WHEN (LOWER(TRIM(pbg_task.function_type)) LIKE '%fungsi usaha%'
OR LOWER(TRIM(pbg_task.function_type)) LIKE '%sebagai tempat usaha%')
AND pbg_task.status != 20 THEN 1 END) AS business_count,
SUM(CASE WHEN (LOWER(TRIM(pbg_task.function_type)) LIKE '%fungsi usaha%'
OR LOWER(TRIM(pbg_task.function_type)) LIKE '%sebagai tempat usaha%')
AND pbg_task.status != 20 THEN ptr.nilai_retribusi_bangunan ELSE 0 END) AS business_total,
COUNT(CASE WHEN (LOWER(TRIM(ptgs.status_verifikasi)) != 'selesai verifikasi' OR ptgs.status_verifikasi IS NULL)
AND (LOWER(TRIM(pbg_task.function_type)) != 'sebagai tempat usaha' OR pbg_task.function_type IS NULL) THEN 1 END) AS non_business_count,
SUM(CASE WHEN (LOWER(TRIM(ptgs.status_verifikasi)) != 'selesai verifikasi' OR ptgs.status_verifikasi IS NULL)
AND (LOWER(TRIM(pbg_task.function_type)) != 'sebagai tempat usaha' OR pbg_task.function_type IS NULL) THEN ptr.nilai_retribusi_bangunan ELSE 0 END) AS non_business_total
COUNT(CASE WHEN ((LOWER(TRIM(pbg_task.function_type)) NOT LIKE '%fungsi usaha%'
AND LOWER(TRIM(pbg_task.function_type)) NOT LIKE '%sebagai tempat usaha%')
OR pbg_task.function_type IS NULL)
AND pbg_task.status != 20 THEN 1 END) AS non_business_count,
SUM(CASE WHEN ((LOWER(TRIM(pbg_task.function_type)) NOT LIKE '%fungsi usaha%'
AND LOWER(TRIM(pbg_task.function_type)) NOT LIKE '%sebagai tempat usaha%')
OR pbg_task.function_type IS NULL)
AND pbg_task.status != 20 THEN ptr.nilai_retribusi_bangunan ELSE 0 END) AS non_business_total
")
->first();
@@ -77,10 +79,6 @@ class BigdataResume extends Model
$query = PbgTask::leftJoin('pbg_task_retributions as ptr', 'pbg_task.uuid', '=', 'ptr.pbg_task_uid')
->selectRaw('COUNT(DISTINCT pbg_task.id) as task_count, SUM(ptr.nilai_retribusi_bangunan) as total_retribution');
if ($year !== 'all') {
$query->whereYear('pbg_task.task_created_at', (int) $year);
}
return $query->first();
});
@@ -98,10 +96,6 @@ class BigdataResume extends Model
SUM(CASE WHEN sp.id IS NOT NULL AND ptr.id IS NOT NULL THEN ptr.nilai_retribusi_bangunan ELSE 0 END) as total_retribution
');
if ($year !== 'all') {
$query->whereYear('pbg_task.task_created_at', (int) $year);
}
return $query->first();
});

View File

@@ -356,39 +356,63 @@ class ServiceGoogleSheet
public function get_big_resume_data(){
try {
$sheet_big_data = $this->get_data_by_sheet();
$data_setting_result = []; // Initialize result storage
$sections = [
'TARGET_PAD' => "TARGET PAD 2024",
'KEKURANGAN_POTENSI' => "DEVIASI TARGET DENGAN POTENSI TOTAL BERKAS",
'TOTAL_POTENSI_BERKAS' => "•TOTAL BERKAS 2025",
'BELUM_TERVERIFIKASI' => "•BERKAS AKTUAL BELUM TERVERIFIKASI (POTENSI):",
'TERVERIFIKASI' => "•BERKAS AKTUAL TERVERIFIKASI DINAS TEKNIS 2025:",
'NON_USAHA' => "•NON USAHA: HUNIAN, SOSBUD, KEAGAMAAN",
'USAHA' => "•USAHA: USAHA, CAMPURAN, KOLEKTIF, PRASARANA",
'PROSES_DINAS_TEKNIS' => "•TERPROSES DI DPUTR: belum selesai rekomtek'",
'WAITING_KLIK_DPMPTSP' => "•TERPROSES DI PTSP: Pengiriman SKRD/ Validasi di PTSP",
'REALISASI_TERBIT_PBG' => "•BERKAS YANG TERBIT PBG 2025:"
];
$found_section = null; // Track which section is found
$result = [];
foreach ($sheet_big_data as $row) {
// Check for section headers
if (in_array("•PROSES PENERBITAN:", $row)) {
$found_section = "MENUNGGU_KLIK_DPMPTSP";
} elseif (in_array("•BERKAS AKTUAL TERVERIFIKASI DINAS TEKNIS 2024:", $row)) {
$found_section = "REALISASI_TERBIT_PBG";
} elseif (in_array("•TERPROSES DI DPUTR: belum selesai rekomtek'", $row)) {
$found_section = "PROSES_DINAS_TEKNIS";
}
// If a section is found and we reach "Grand Total", save the corresponding values
if ($found_section && isset($row[0]) && trim($row[0]) === "Grand Total") {
if ($found_section === "MENUNGGU_KLIK_DPMPTSP") {
$data_setting_result["MENUNGGU_KLIK_DPMPTSP_COUNT"] = $this->convertToInteger($row[2]) ?? null;
$data_setting_result["MENUNGGU_KLIK_DPMPTSP_SUM"] = $this->convertToDecimal($row[3]) ?? null;
} elseif ($found_section === "REALISASI_TERBIT_PBG") {
$data_setting_result["REALISASI_TERBIT_PBG_COUNT"] = $this->convertToInteger($row[2]) ?? null;
$data_setting_result["REALISASI_TERBIT_PBG_SUM"] = $this->convertToDecimal($row[4]) ?? null;
} elseif ($found_section === "PROSES_DINAS_TEKNIS") {
$data_setting_result["PROSES_DINAS_TEKNIS_COUNT"] = $this->convertToInteger($row[2]) ?? null;
$data_setting_result["PROSES_DINAS_TEKNIS_SUM"] = $this->convertToDecimal($row[3]) ?? null;
}
// Reset section tracking after capturing "Grand Total"
$found_section = null;
foreach ($sections as $key => $identifier) {
$values = $this->get_values_from_section($identifier, [10, 11], 8);
if (!empty($values)) {
$result[$key] = [
'identifier' => $identifier,
'total' => $values[0] ?? null, // index 0 untuk total/jumlah
'nominal' => $values[1] ?? null // index 1 untuk nominal
];
}
}
return $data_setting_result;
// Save data settings
$dataSettings = [
'TARGET_PAD' => $this->convertToDecimal($result['TARGET_PAD']['nominal']) ?? 0,
'KEKURANGAN_POTENSI' => $this->convertToDecimal($result['KEKURANGAN_POTENSI']['nominal']) ?? 0,
'REALISASI_TERBIT_PBG_COUNT' => $this->convertToInteger($result['REALISASI_TERBIT_PBG']['total']) ?? 0,
'REALISASI_TERBIT_PBG_SUM' => $this->convertToDecimal($result['REALISASI_TERBIT_PBG']['nominal']) ?? 0,
'MENUNGGU_KLIK_DPMPTSP_COUNT' => $this->convertToInteger($result['WAITING_KLIK_DPMPTSP']['total']) ?? 0,
'MENUNGGU_KLIK_DPMPTSP_SUM' => $this->convertToDecimal($result['WAITING_KLIK_DPMPTSP']['nominal']) ?? 0,
'PROSES_DINAS_TEKNIS_COUNT' => $this->convertToInteger($result['PROSES_DINAS_TEKNIS']['total']) ?? 0,
'PROSES_DINAS_TEKNIS_SUM' => $this->convertToDecimal($result['PROSES_DINAS_TEKNIS']['nominal']) ?? 0,
];
foreach ($dataSettings as $key => $value) {
// Ensure value is not null before saving to database
$processedValue = 0; // Default to 0 instead of null
if ($value !== null && $value !== '') {
// Try to convert to appropriate type based on key name
if (strpos($key, '_COUNT') !== false) {
$processedValue = $this->convertToInteger($value) ?? 0;
} else {
$processedValue = $this->convertToDecimal($value) ?? 0;
}
}
DataSetting::updateOrCreate(
['key' => $key],
['value' => $processedValue]
);
}
return $dataSettings;
}catch(Exception $exception){
Log::error("Error getting big resume data", ['error' => $exception->getMessage()]);
throw $exception;