feat(webstatement): enhance statement generation and add narrative formatting logic

- Menambahkan logika baru untuk menghasilkan data statement dalam format JSON dan CSV.
- Memuat data transaksi statement menggunakan model `StmtEntry` dengan relasi `ft` dan `transaction`.
- Mengimplementasikan proses mapping data untuk:
  - Penambahan urutan nomor (sequential numbering).
  - Format tanggal transaksi menggunakan properti `booking_date` dan `date_time`.
  - Klasifikasi jenis transaksi (debit atau kredit).
  - Perhitungan running balance.
  - Pembangkitan narrative deskripsi transaksi.
- Menambahkan fungsi `generateNarrative` untuk menghasilkan deskripsi transaksi berdasarkan logika parameter dinamis.
- Menggunakan model `TempStmtNarrFormat` dan `TempStmtNarrParam` untuk format dan parameter narrative.
- Menambahkan fungsi `getFormatNarrative` untuk mem-parsing format narrative dan memasukkan placeholder value sesuai data transaksi.
- Fungsi `getTransaction` ditambahkan sebagai fallback untuk mengambil field secara spesifik dari transaksi.
- Menyediakan opsi data dalam format CSV menggunakan stream response dengan pemisah data berupa pipe (`|`).
- Menambahkan route baru pada `/` untuk mengakses controller `WebstatementController` dan fungsionalitas ini di route `webstatement.index`.
This commit is contained in:
daengdeni
2025-05-21 21:18:40 +07:00
parent def0b037a8
commit 3c061f40f7
2 changed files with 163 additions and 1 deletions

View File

@@ -4,6 +4,10 @@ namespace Modules\Webstatement\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Webstatement\Models\StmtEntry;
use Modules\Webstatement\Models\TempFundsTransfer;
use Modules\Webstatement\Models\TempStmtNarrFormat;
use Modules\Webstatement\Models\TempStmtNarrParam;
class WebstatementController extends Controller
{
@@ -12,7 +16,164 @@ class WebstatementController extends Controller
*/
public function index()
{
return view('webstatement::index');
$account_number = '1080425781';
$period = '20250512';
$stmt = StmtEntry::with(['ft','transaction'])->where('account_number',$account_number)->where('booking_date',$period)->get();
$saldo = '23984352604';
$runningBalance = (float) $saldo;
// Map the data to transform or format specific fields
$mappedData = $stmt->sortBy(['ACTUAL.DATE', 'REFERENCE.NUMBER'])->map(function ($item, $index) use (&$runningBalance) {
$runningBalance += (float) $item->amount_lcy;
return [
'NO' => 0, // Use $index instead of $item->count()
'TRANSACTION.DATE' => \Carbon\Carbon::createFromFormat('YmdHi', $item->booking_date . substr($item->ft?->date_time ?? '0000000000', 6, 4))->format('d/m/Y H:i'),
'REFERENCE.NUMBER' => $item->trans_reference,
'TRANSACTION.AMOUNT' => $item->amount_lcy,
'TRANSACTION.TYPE' => $item->amount_lcy < 0 ? 'D' : 'C',
'DESCRIPTION' => $this->generateNarative($item),
'END.BALANCE' => $runningBalance,
'ACTUAL.DATE' => \Carbon\Carbon::createFromFormat('ymdHi', $item->ft?->date_time ?? '2505120000')
->format('d/m/Y H:i'),
];
})->values();
// Then apply the sequential numbers
$mappedData = $mappedData->map(function ($item, $index) {
$item['NO'] = $index + 1;
return $item;
});
return response()->json($mappedData);
$csvFileName = $account_number."_".$period.".csv";
$headers = [
"Content-Type" => "text/csv",
"Content-Disposition" => "attachment; filename={$csvFileName}"
];
$callback = function () use ($mappedData) {
$file = fopen('php://output', 'w');
// Write headers without quotes, using pipe separator
fputs($file, implode('|', array_keys($mappedData[0])) . "\n");
// Write data rows without quotes, using pipe separator
foreach ($mappedData as $row) {
fputs($file, implode('|', $row) . "\n");
}
fclose($file);
};
return response()->stream($callback, 200, $headers);
}
function generateNarative($item){
$narr = '';
if($item->transaction->narr_type){
$narr .= $item->transaction->stmt_narr.' ';
$narr .= $this->getFormatNarrative($item->transaction->narr_type,$item);
} else {
$narr .= $item->transaction->stmt_narr.' ';
}
if($item->ft?->recipt_no) {
$narr .= 'Receipt No: ' . $item->ft->recipt_no;
}
return $narr;
}
function getFormatNarrative($narr,$item){
$narrParam = TempStmtNarrParam::where('_id', $narr)->first();
if (!$narrParam) {
return '';
}
$fmt = '';
if($narrParam->_id=='FTIN'){
$fmt = 'FT.IN';
}elseif($narrParam->_id=='FTOUT'){
$fmt = 'FT.IN';
} else {
$fmt = $narrParam->_id;
}
$narrFormat = TempStmtNarrFormat::where('_id', $fmt)->first();
if (!$narrFormat) {
return '';
}
// Get the format string from the database
$formatString = $narrFormat->text_data ?? '';
// Parse the format string
// Split by the separator ']'
$parts = explode(']', $formatString);
$result = '';
foreach ($parts as $index => $part) {
if (empty($part)) {
continue;
}
if ($index === 0) {
// For the first part, take only what's before the '!'
$splitPart = explode('!', $part);
if (count($splitPart) > 0) {
// Remove quotes, backslashes, and other escape characters
$cleanPart = trim($splitPart[0]);
// Remove quotes at the beginning and end
$cleanPart = preg_replace('/^["\'\\\\]+|["\'\\\\]+$/', '', $cleanPart);
// Remove any remaining backslashes
$cleanPart = str_replace('\\', '', $cleanPart);
// Remove any remaining quotes
$cleanPart = str_replace('"', '', $cleanPart);
$result .= $cleanPart;
}
} else {
// For other parts, these are field placeholders
$fieldName = strtolower(str_replace('.', '_', $part));
// Get the corresponding parameter value from narrParam
$paramValue = null;
// Check if the field exists as a property in narrParam
if (property_exists($narrParam, $fieldName)) {
$paramValue = $narrParam->$fieldName;
} elseif (isset($narrParam->$fieldName)) {
$paramValue = $narrParam->$fieldName;
}
// If we found a value, add it to the result
if ($paramValue !== null) {
$result .= $paramValue;
} else {
// If no value found, try to use the original field name as a fallback
if($fieldName!='recipt_no') {
$result .= $this->getTransaction($item->trans_reference, $fieldName).' ';
// $result .= "[$fieldName]";
}
}
}
}
return $result;
}
public function getTransaction($ref, $field){
$trans = TempFundsTransfer::where('ref_no', $ref)->first();
return $trans->$field ?? "";
}
/**