Recent transaction settlement amount is showing correctly now, Settlement transactions are correct now.

This commit is contained in:
kizzy 2026-08-19 23:54:44 +07:00
parent 145b3ea898
commit f8066d50fb
4 changed files with 99 additions and 38 deletions

View File

@ -89,7 +89,8 @@ class DashboardViewModel @Inject constructor(
private fun PayDetail.toRecord(): TrnxRecord { private fun PayDetail.toRecord(): TrnxRecord {
val baseType = transType?.takeIf { it.isNotBlank() }?.let { formatTypeLabel(it) } ?: "Transaction" val baseType = transType?.takeIf { it.isNotBlank() }?.let { formatTypeLabel(it) } ?: "Transaction"
val type = if (isCanceled) "Void" else baseType val type = if (isCanceled) "Void" else baseType
val amountText = "MMK ${formatAmount(amount)}" val displayAmount = displayAmount()
val amountText = "MMK ${formatAmount(displayAmount)}"
val card = CardNo?.takeIf { it.length >= 4 }?.let { "**** ${it.takeLast(4)}" } ?: "----" val card = CardNo?.takeIf { it.length >= 4 }?.let { "**** ${it.takeLast(4)}" } ?: "----"
val dt = buildDateTime(TradeDate, TradeTime) val dt = buildDateTime(TradeDate, TradeTime)
val approved = tradeAnswerCode == "00" || approvalCode?.isNotBlank() == true val approved = tradeAnswerCode == "00" || approvalCode?.isNotBlank() == true
@ -107,7 +108,31 @@ private fun PayDetail.toRecord(): TrnxRecord {
fun formatAmount(amount: Long): String { fun formatAmount(amount: Long): String {
val scaled = BigDecimal(amount).movePointLeft(2) val scaled = BigDecimal(amount).movePointLeft(2)
return DecimalFormat("#,###.00").format(scaled) return DecimalFormat("#,##0.00").format(scaled)
}
private fun PayDetail.displayAmount(): Long {
if (amount != 0L) return amount
if (transactionType != TransactionsType.SETTLEMENT.value) return 0L
settleDataObj?.let { data ->
return data.saleAmount +
data.preAuthCompAmount +
data.cashAdvanceAmount -
data.refundAmount
}
val parts = settleList.orEmpty().split("-")
if (parts.size >= 4) {
fun partAmount(raw: String): Long = raw.substringAfter(":", "0").toLongOrNull() ?: 0L
val sale = partAmount(parts[0])
val cashAdvance = partAmount(parts[1])
val refund = partAmount(parts[2])
val preAuth = partAmount(parts[3])
return sale + cashAdvance + preAuth - refund
}
return 0L
} }
private fun List<PayDetail>.totalSaleCount(): Int = count { it.isSaleTransaction() } private fun List<PayDetail>.totalSaleCount(): Int = count { it.isSaleTransaction() }

View File

@ -74,7 +74,10 @@ fun SettlementScreen(
Spacer(modifier = Modifier.height(12.dp)) Spacer(modifier = Modifier.height(12.dp))
SummaryRow("Record Count", records.size.toString()) SummaryRow("Record Count", records.size.toString())
SummaryRow("Total Amount", POSUtil.getInstance().formatAmount(totalAmount)) SummaryRow(
"Total Amount",
POSUtil.getInstance().getDecimalAmountSeparatorFormat(totalAmount)
)
} }
} }
@ -167,7 +170,7 @@ private fun SettlementRecordRow(record: PayDetail) {
style = MaterialTheme.typography.titleSmall style = MaterialTheme.typography.titleSmall
) )
Text( Text(
text = POSUtil.getInstance().formatAmount(record.amount), text = POSUtil.getInstance().getDecimalAmountSeparatorFormat(record.amount),
color = Color.Black, color = Color.Black,
style = MaterialTheme.typography.titleSmall style = MaterialTheme.typography.titleSmall
) )
@ -181,7 +184,7 @@ private fun SettlementRecordRow(record: PayDetail) {
style = MaterialTheme.typography.bodySmall style = MaterialTheme.typography.bodySmall
) )
Text( Text(
text = "Card ${record.cardNo.ifBlank { "-" }}", text = "Card ${maskCardNumber(record.cardNo)}",
color = Color.Gray, color = Color.Gray,
style = MaterialTheme.typography.bodySmall style = MaterialTheme.typography.bodySmall
) )
@ -190,3 +193,11 @@ private fun SettlementRecordRow(record: PayDetail) {
HorizontalDivider(color = Color.Gray.copy(alpha = 0.25f)) HorizontalDivider(color = Color.Gray.copy(alpha = 0.25f))
} }
} }
private fun maskCardNumber(cardNo: String?): String {
val pan = cardNo.orEmpty().filter { it.isDigit() }
if (pan.isBlank()) return "-"
if (pan.length <= 8) return pan
val stars = "*".repeat(pan.length - 8)
return pan.take(4) + stars + pan.takeLast(4)
}

View File

@ -137,48 +137,62 @@ class SettlementViewModel @Inject constructor(
} }
fun setSettlementData(list: List<PayDetail>) { fun setSettlementData(list: List<PayDetail>) {
val newState = list.fold(_uiState.value) { state, payDetail -> var saleCount = 0
var saleAmount = 0L
var preCount = 0
var preAmount = 0L
var refundCount = 0
var refundAmount = 0L
var caCount = 0
var caAmount = 0L
list.forEach { payDetail ->
val transType = payDetail.transactionType val transType = payDetail.transactionType
val transName = payDetail.transType val transName = payDetail.transType
when { when {
(transType == TransactionType.SALE || transName.equals("SALE", ignoreCase = true)) (transType == TransactionType.SALE || transName.equals("SALE", ignoreCase = true))
&& !payDetail.isCanceled -> && !payDetail.isCanceled -> {
state.copy( saleCount += 1
saleCount = state.saleCount + 1, saleAmount += payDetail.amount
saleAmount = state.saleAmount + payDetail.amount }
)
(transType == TransactionType.PRE_SALE_COMPLETE (transType == TransactionType.PRE_SALE_COMPLETE
|| transName.equals("PREAUTH COMPLETION", ignoreCase = true) || transName.equals("PREAUTH COMPLETION", ignoreCase = true)
|| transName.equals("PRE_AUTH_COMPLETE", ignoreCase = true)) || transName.equals("PRE_AUTH_COMPLETE", ignoreCase = true))
&& !payDetail.isCanceled -> && !payDetail.isCanceled -> {
state.copy( preCount += 1
preCount = state.preCount + 1, preAmount += payDetail.amount
preAmount = state.preAmount + payDetail.amount }
)
transType == TransactionType.REFUND transType == TransactionType.REFUND
|| transName.equals("REFUND", ignoreCase = true) -> || transName.equals("REFUND", ignoreCase = true) -> {
state.copy( refundCount += 1
refundCount = state.refundCount + 1, refundAmount += payDetail.amount
refundAmount = state.refundAmount + payDetail.amount }
)
transType == TransactionType.CASH_ADVANCE transType == TransactionType.CASH_ADVANCE
|| transName.equals("CASH_OUT", ignoreCase = true) || transName.equals("CASH_OUT", ignoreCase = true)
|| transName.equals("CASH_ADVANCE", ignoreCase = true) -> || transName.equals("CASH_ADVANCE", ignoreCase = true) -> {
state.copy( caCount += 1
caCount = state.caCount + 1, caAmount += payDetail.amount
caAmount = state.caAmount + payDetail.amount }
)
else -> state
} }
} }
LogUtil.d(TAG,"sale count ${newState.saleCount} and sale amount ${newState.saleAmount}") LogUtil.d(TAG, "sale count $saleCount and sale amount $saleAmount")
_uiState.update { newState } _uiState.update {
it.copy(
saleCount = saleCount,
saleAmount = saleAmount,
preCount = preCount,
preAmount = preAmount,
refundCount = refundCount,
refundAmount = refundAmount,
caCount = caCount,
caAmount = caAmount
)
}
} }
fun setSettlementSummary( fun setSettlementSummary(
@ -264,24 +278,24 @@ class SettlementViewModel @Inject constructor(
LogUtil.d(TAG,"Sale count :$sale2Count and Sale amount :$sale2Amount") LogUtil.d(TAG,"Sale count :$sale2Count and Sale amount :$sale2Amount")
val totalSaleCount = String.format(Locale.getDefault(), "%03d", sale2Count) val totalSaleCount = String.format(Locale.getDefault(), "%03d", sale2Count)
val totalSaleAmount = String.format(Locale.getDefault(), "%010d00", sale2Amount) val totalSaleAmount = String.format(Locale.getDefault(), "%012d", sale2Amount)
val totalRefundCount = String.format(Locale.getDefault(), "%03d", state.refundCount) val totalRefundCount = String.format(Locale.getDefault(), "%03d", state.refundCount)
val totalRefundAmount = String.format( val totalRefundAmount = String.format(
Locale.getDefault(), Locale.getDefault(),
"%010d00", "%012d",
if (state.refundAmount == 0L) 0 else state.refundAmount if (state.refundAmount == 0L) 0 else state.refundAmount
) )
val totalDebitSaleCount = String.format(Locale.getDefault(), "%03d", state.caCount) val totalDebitSaleCount = String.format(Locale.getDefault(), "%03d", state.caCount)
val totalDebitSaleAmount = String.format( val totalDebitSaleAmount = String.format(
Locale.getDefault(), Locale.getDefault(),
"%010d00", "%012d",
if (state.caAmount == 0L) 0 else state.caAmount if (state.caAmount == 0L) 0 else state.caAmount
) )
val totalERefundCount = String.format(Locale.getDefault(), "%03d", 0) val totalERefundCount = String.format(Locale.getDefault(), "%03d", 0)
val totalERefundAmount = String.format(Locale.getDefault(), "%010d00", 0) val totalERefundAmount = String.format(Locale.getDefault(), "%012d", 0)
val tradeData = Params.newTrade(true) val tradeData = Params.newTrade(true)
val currentPayDetail = tradeData.payDetail val currentPayDetail = tradeData.payDetail
@ -321,10 +335,11 @@ class SettlementViewModel @Inject constructor(
currentPayDetail.settleDataObj = settleData currentPayDetail.settleDataObj = settleData
if (hostName == HostName.BPC) { val totalAmount =
val totalAmount = state.saleAmount + state.preAmount + state.caAmount - state.refundAmount
state.saleAmount + state.preAmount + state.refundAmount + state.caAmount currentPayDetail.amount = totalAmount
if (hostName == HostName.BPC) {
val settlementData = if (state.refundAmount != 0L) { val settlementData = if (state.refundAmount != 0L) {
val creditTotal = state.saleAmount + state.preAmount + state.caAmount val creditTotal = state.saleAmount + state.preAmount + state.caAmount
val subTotal = creditTotal - state.refundAmount val subTotal = creditTotal - state.refundAmount
@ -339,7 +354,6 @@ class SettlementViewModel @Inject constructor(
} }
currentPayDetail.settleData = settlementData currentPayDetail.settleData = settlementData
currentPayDetail.amount = totalAmount
} else { } else {
LogUtil.d(TAG,"sale count :$totalSaleCount") LogUtil.d(TAG,"sale count :$totalSaleCount")

View File

@ -3,6 +3,7 @@ package com.mob.utsmyanmar
import org.junit.Test import org.junit.Test
import org.junit.Assert.* import org.junit.Assert.*
import java.util.Locale
/** /**
* Example local unit test, which will execute on the development machine (host). * Example local unit test, which will execute on the development machine (host).
@ -14,4 +15,14 @@ class ExampleUnitTest {
fun addition_isCorrect() { fun addition_isCorrect() {
assertEquals(4, 2 + 2) assertEquals(4, 2 + 2)
} }
@Test
// 002 000057400000 000 000000000000 000 000000000000 000 000000000000
// 013 000206000000 000 000000000000 000 000000000000 000 000000000000
// 001 000006000000 000 000000000000 000 000000000000 000 000000000000
fun `test settlement data `() {
val res = String.format(Locale.getDefault(), "%010d00", 600)
print("Result : $res")
}
} }