fix pbg payments
This commit is contained in:
@@ -427,6 +427,203 @@ class ServiceGoogleSheet
|
||||
}
|
||||
}
|
||||
|
||||
public function get_realisasi_pad_data(){
|
||||
try {
|
||||
// Get data from "REALISASI PAD" sheet
|
||||
$sheet_data = $this->get_data_by_sheet_name("REALISASI PAD");
|
||||
|
||||
if (empty($sheet_data)) {
|
||||
Log::warning("No data found in REALISASI PAD sheet");
|
||||
return [];
|
||||
}
|
||||
|
||||
// Column indices: C=2, AK=36, AL=37, AW=48 (0-based)
|
||||
$columns = [
|
||||
'C' => 2,
|
||||
'AK' => 36,
|
||||
'AL' => 37,
|
||||
'AW' => 48
|
||||
];
|
||||
|
||||
$result = [
|
||||
'headers' => [],
|
||||
'data' => []
|
||||
];
|
||||
|
||||
foreach ($sheet_data as $row_index => $row) {
|
||||
if (!is_array($row)) continue;
|
||||
|
||||
if ($row_index === 0) {
|
||||
// First row contains headers
|
||||
foreach ($columns as $column_name => $column_index) {
|
||||
$result['headers'][$column_name] = isset($row[$column_index]) ? trim($row[$column_index]) : '';
|
||||
}
|
||||
} else {
|
||||
// Data rows
|
||||
$row_data = [
|
||||
'row' => $row_index + 1 // 1-based row number
|
||||
];
|
||||
|
||||
$has_data = false;
|
||||
foreach ($columns as $column_name => $column_index) {
|
||||
$value = isset($row[$column_index]) ? trim($row[$column_index]) : '';
|
||||
$row_data[$column_name] = $value;
|
||||
if ($value !== '') {
|
||||
$has_data = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Only add row if it has at least one non-empty value
|
||||
if ($has_data) {
|
||||
$result['data'][] = $row_data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log::info("REALISASI PAD Multiple Columns Data", [
|
||||
'sheet_name' => 'REALISASI PAD',
|
||||
'columns' => array_keys($columns),
|
||||
'headers' => $result['headers'],
|
||||
'total_data_rows' => count($result['data']),
|
||||
'sample_data' => array_slice($result['data'], 0, 5), // Show first 5 rows as sample
|
||||
'column_indices' => $columns
|
||||
]);
|
||||
|
||||
return $result;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Error getting REALISASI PAD data", ['error' => $e->getMessage()]);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function sync_pbg_task_payments(){
|
||||
try {
|
||||
// Get payment data from REALISASI PAD sheet
|
||||
$payment_data = $this->get_realisasi_pad_data();
|
||||
|
||||
if (empty($payment_data['data'])) {
|
||||
Log::warning("No payment data found to sync");
|
||||
return ['success' => false, 'message' => 'No payment data found'];
|
||||
}
|
||||
|
||||
$successful_syncs = 0;
|
||||
$failed_syncs = 0;
|
||||
$not_found_tasks = 0;
|
||||
$not_found_tasks_registration_number = [];
|
||||
$failed_sync_registration_numbers = [];
|
||||
$errors = [];
|
||||
|
||||
foreach ($payment_data['data'] as $row) {
|
||||
try {
|
||||
// Clean registration number from column C
|
||||
$registration_number = \App\Models\PbgTaskPayment::cleanRegistrationNumber($row['C']);
|
||||
|
||||
if (empty($registration_number)) {
|
||||
$failed_syncs++;
|
||||
$failed_sync_registration_numbers[] = [
|
||||
'row' => $row['row'],
|
||||
'registration_number' => $row['C'] ?? 'EMPTY',
|
||||
'reason' => 'Empty registration number'
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find PBG task by registration number
|
||||
$pbg_task = \App\Models\PbgTask::where('registration_number', $registration_number)->first();
|
||||
|
||||
if (!$pbg_task) {
|
||||
$not_found_tasks_registration_number[] = [
|
||||
'row' => $row['row'],
|
||||
'registration_number' => $registration_number
|
||||
];
|
||||
$not_found_tasks++;
|
||||
Log::warning("PBG Task not found for registration number", [
|
||||
'registration_number' => $registration_number,
|
||||
'row' => $row['row']
|
||||
]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Convert data types
|
||||
$payment_date = \App\Models\PbgTaskPayment::convertPaymentDate($row['AK']);
|
||||
$pad_amount = \App\Models\PbgTaskPayment::convertPadAmount($row['AW']);
|
||||
$sts_form_number = !empty($row['AL']) ? trim($row['AL']) : null;
|
||||
|
||||
// Create or update payment record
|
||||
\App\Models\PbgTaskPayment::updateOrCreate(
|
||||
[
|
||||
'pbg_task_id' => $pbg_task->id,
|
||||
'registration_number' => $registration_number
|
||||
],
|
||||
[
|
||||
'pbg_task_uid' => $pbg_task->uuid,
|
||||
'sts_form_number' => $sts_form_number,
|
||||
'payment_date' => $payment_date,
|
||||
'pad_amount' => $pad_amount
|
||||
]
|
||||
);
|
||||
|
||||
$successful_syncs++;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$failed_syncs++;
|
||||
$registration_number = $row['C'] ?? 'N/A';
|
||||
$failed_sync_registration_numbers[] = [
|
||||
'row' => $row['row'],
|
||||
'registration_number' => $registration_number,
|
||||
'reason' => $e->getMessage()
|
||||
];
|
||||
$errors[] = [
|
||||
'row' => $row['row'],
|
||||
'registration_number' => $registration_number,
|
||||
'error' => $e->getMessage()
|
||||
];
|
||||
|
||||
Log::error("Error syncing payment data for row", [
|
||||
'row' => $row['row'],
|
||||
'registration_number' => $registration_number,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$result = [
|
||||
'success' => true,
|
||||
'total_rows' => count($payment_data['data']),
|
||||
'successful_syncs' => $successful_syncs,
|
||||
'failed_syncs' => $failed_syncs,
|
||||
'not_found_tasks' => $not_found_tasks,
|
||||
'not_found_tasks_registration_number' => $not_found_tasks_registration_number,
|
||||
'failed_sync_registration_numbers' => $failed_sync_registration_numbers,
|
||||
'errors' => $errors
|
||||
];
|
||||
|
||||
Log::info("PBG Task Payments sync completed", $result);
|
||||
|
||||
// Log detailed arrays for failed syncs and not found registration numbers
|
||||
if (!empty($failed_sync_registration_numbers)) {
|
||||
Log::warning("Failed Sync Registration Numbers", [
|
||||
'count' => count($failed_sync_registration_numbers),
|
||||
'details' => $failed_sync_registration_numbers
|
||||
]);
|
||||
}
|
||||
|
||||
if (!empty($not_found_tasks_registration_number)) {
|
||||
Log::warning("Not Found Registration Numbers", [
|
||||
'count' => count($not_found_tasks_registration_number),
|
||||
'details' => $not_found_tasks_registration_number
|
||||
]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Error syncing PBG task payments", ['error' => $e->getMessage()]);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
private function get_data_by_sheet($no_sheet = 8){
|
||||
$spreadsheet = $this->service->spreadsheets->get($this->spreadsheetID);
|
||||
$sheets = $spreadsheet->getSheets();
|
||||
@@ -437,6 +634,45 @@ class ServiceGoogleSheet
|
||||
return!empty($values)? $values : [];
|
||||
}
|
||||
|
||||
private function get_data_by_sheet_name($sheet_name){
|
||||
try {
|
||||
$spreadsheet = $this->service->spreadsheets->get($this->spreadsheetID);
|
||||
$sheets = $spreadsheet->getSheets();
|
||||
|
||||
// Find sheet by name
|
||||
$targetSheet = null;
|
||||
foreach ($sheets as $sheet) {
|
||||
if ($sheet->getProperties()->getTitle() === $sheet_name) {
|
||||
$targetSheet = $sheet;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$targetSheet) {
|
||||
Log::warning("Sheet not found", ['sheet_name' => $sheet_name]);
|
||||
return [];
|
||||
}
|
||||
|
||||
$range = "{$sheet_name}";
|
||||
$response = $this->service->spreadsheets_values->get($this->spreadsheetID, $range);
|
||||
$values = $response->getValues();
|
||||
|
||||
Log::info("Sheet data retrieved", [
|
||||
'sheet_name' => $sheet_name,
|
||||
'total_rows' => count($values ?? [])
|
||||
]);
|
||||
|
||||
return !empty($values) ? $values : [];
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Error getting data by sheet name", [
|
||||
'sheet_name' => $sheet_name,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get specific values from a row that contains a specific text/section identifier
|
||||
* @param string $section_identifier Text to search for in the row
|
||||
|
||||
Reference in New Issue
Block a user