添加单价和金额计算

This commit is contained in:
zxj
2025-07-10 11:23:11 +08:00
parent 6cd5069aac
commit b657821b23
8 changed files with 254 additions and 27 deletions
@@ -1,9 +1,9 @@
package com.sw.inbound.viewmodel
import androidx.lifecycle.viewModelScope
import com.sw.inbound.ext.toFormattedString
import com.sw.inbound.ext.toSafeBigDecimal
import com.sw.inbound.ext.toSafeFloat
import com.sw.inbound.ext.toSafeString
import com.sw.inbound.model.request.UploadInfo
import com.sw.inbound.model.response.DictType
import com.sw.inbound.model.response.GoodsInfo
@@ -72,6 +72,8 @@ class ReceiptViewModel @Inject constructor(
// 物品数量 是否由用户输入
private val _countUserInput = MutableStateFlow<Boolean>(false)
private val _priceUserInput = MutableStateFlow<Boolean>(false)
private val _amountUserInput = MutableStateFlow<Boolean>(false)
/**
* 更新调整状态
@@ -84,6 +86,7 @@ class ReceiptViewModel @Inject constructor(
fun StateFlow<List<GoodsInfo>>.getValidOrders(): List<GoodsInfo> {
return this.value.filter { it.goodId != null }
}
/**
* 更新收货提示弹窗
*/
@@ -103,6 +106,28 @@ class ReceiptViewModel @Inject constructor(
* 更新选中的item
*/
fun updateSelectedItem(purchaseOrder: GoodsInfo?) {
if (purchaseOrder != null) {
purchaseOrder.receivedNum.takeIf { it != 0f }?.let { receivedNum ->
// 计算金额 单价不为空,并且金额没有手动输入
if (purchaseOrder.recUnitPriceTaxIn != null && purchaseOrder.recUnitPriceTaxIn == 0f && !_amountUserInput.value) {
val result =
receivedNum.times(purchaseOrder.recUnitPriceTaxIn!!)
purchaseOrder.recPriceExItem = result.toSafeFloat()
purchaseOrder.recPriceExItemTemp = result.toFormattedString()
}
// 计算单价 金额不为空,并且单价没有手动输入
if (purchaseOrder.recPriceExItem != null && purchaseOrder.recPriceExItem == 0f && !_priceUserInput.value) {
val result =
purchaseOrder.recPriceExItem!!.div(receivedNum)
purchaseOrder.recUnitPriceTaxIn = result.toSafeFloat()
purchaseOrder.recUnitPriceTaxInTemp = result.toFormattedString()
}
}
} else {
_countUserInput.value = false
_priceUserInput.value = false
_amountUserInput.value = false
}
_selectedItem.value = purchaseOrder
}
@@ -179,31 +204,70 @@ class ReceiptViewModel @Inject constructor(
_orders.value = emptyList()
}
/**
* 更新数量输入状态
*/
fun updateCountInputState(boolean: Boolean) {
_countUserInput.value = boolean
}
/**
* 更新单价输入状态
*/
fun updatePriceInputState(boolean: Boolean) {
_priceUserInput.value = boolean
}
/**
* 更新金额输入状态
*/
fun updateAmountInputState(boolean: Boolean) {
_amountUserInput.value = boolean
}
fun startSensorScale() {
Timber.d("开始称重")
SensorScaleUtils.startScale(callback = { weight ->
val currentItem = _selectedItem.value
currentItem?.let { info ->
// 用户输入数量后不再通过重量反算
if (_countUserInput.value) {
_selectedItem.value = info.copy(
goodsWeight = weight.toSafeBigDecimal(),
)
return@startScale
}
val consumeValue = currentItem.consumeValue?.toDouble() ?: 1.0
val purchaseValue = currentItem.purchaseValue?.toDouble() ?: 1.0
val count =
weight * 1000 / consumeValue / purchaseValue
val finalCount = count.toSafeFloat(currentItem.unitName)
Timber.d("startSensorScale weight = $weight, count = $count, finalCount = $finalCount, consumeValue = $consumeValue, purchaseValue = $purchaseValue")
if (info.receivedNumTemp.isEmpty()) {
_countUserInput.value = false
}
// 计算单价
val finalPrice = if (count == 0.0) {
0f
} else if (info.recPriceExItem != null && info.recPriceExItem != 0f && !_countUserInput.value && !_priceUserInput.value) {
info.recPriceExItem!!.div(count).toSafeFloat()
} else info.recUnitPriceTaxIn ?: 0f
// 计算金额
val finalAmount = if (count == 0.0) {
0f
} else if (info.recUnitPriceTaxIn != null && info.recUnitPriceTaxIn != 0f && !_countUserInput.value && !_amountUserInput.value) {
count.times(info.recUnitPriceTaxIn!!).toSafeFloat()
} else info.recPriceExItem ?: 0f
_selectedItem.value = info.copy(
goodsWeight = weight.toSafeBigDecimal(),
receivedNumTemp = if (_countUserInput.value) info.receivedNumTemp else count.toSafeString(
currentItem.unitName
),
receivedNum = if (_countUserInput.value) info.receivedNum else finalCount
// 收货数量
receivedNumTemp = if (_countUserInput.value) info.receivedNumTemp else count.toFormattedString(),
receivedNum = if (_countUserInput.value) info.receivedNum else finalCount,
// 单价
recUnitPriceTaxIn = if (_priceUserInput.value) info.recUnitPriceTaxIn else finalPrice,
recUnitPriceTaxInTemp = if (_priceUserInput.value) info.recUnitPriceTaxInTemp else finalPrice.toFormattedString(),
// 金额
recPriceExItem = if (_amountUserInput.value) info.recPriceExItem else finalAmount,
recPriceExItemTemp = if (_amountUserInput.value) info.recPriceExItemTemp else finalAmount.toFormattedString(),
)
}
})