添加单价和金额计算
This commit is contained in:
@@ -2,9 +2,9 @@ package com.sw.inbound.viewmodel
|
||||
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.sw.inbound.GlobalData
|
||||
import com.sw.inbound.ext.toFormattedString
|
||||
import com.sw.inbound.ext.toSafeBigDecimal
|
||||
import com.sw.inbound.ext.toSafeDouble
|
||||
import com.sw.inbound.ext.toSafeString
|
||||
import com.sw.inbound.model.request.GoodsAddParam
|
||||
import com.sw.inbound.model.request.PurchaseWarehouseParam
|
||||
import com.sw.inbound.model.response.DictType
|
||||
@@ -67,6 +67,16 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
*/
|
||||
private val _countUserInput = MutableStateFlow<Boolean>(false)
|
||||
|
||||
/**
|
||||
* 单价是否是用户输入的值
|
||||
*/
|
||||
private val _priceUserInput = MutableStateFlow<Boolean>(false)
|
||||
|
||||
/**
|
||||
* 金额输入状态
|
||||
*/
|
||||
private val _amountUserInput = MutableStateFlow<Boolean>(false)
|
||||
|
||||
/**
|
||||
* 更新全局仓库
|
||||
*/
|
||||
@@ -80,6 +90,14 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
val currentItem = _selectedItem.value
|
||||
|
||||
currentItem?.let { info ->
|
||||
// 用户输入数量后不再通过重量反算
|
||||
if (_countUserInput.value) {
|
||||
_selectedItem.value = info.copy(
|
||||
goodsWeight = weight.toSafeBigDecimal(),
|
||||
)
|
||||
return@startScale
|
||||
}
|
||||
|
||||
val selectUnitType = currentItem.selectUnitType
|
||||
if (selectUnitType == null) return@startScale
|
||||
|
||||
@@ -89,15 +107,35 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
weight * 1000 / consumeValue / purchaseValue
|
||||
val finalCount = count.toSafeDouble(currentItem.unitName)
|
||||
Timber.d("startSensorScale weight = $weight, count = $count, finalCount = $finalCount, consumeValue = $consumeValue, purchaseValue = $purchaseValue")
|
||||
if (info.goodsCountTemp.isEmpty()) {
|
||||
_countUserInput.value = false
|
||||
}
|
||||
|
||||
Timber.d("startSensorScale goodsUnitPrice = ${info.goodsUnitPrice}, goodsPrice = ${info.goodsPrice} ")
|
||||
// 计算单价 金额不为空,数量也不为空, 不是数量输入 也不是单价输入
|
||||
val finalPrice =
|
||||
if (count == 0.0) {
|
||||
0.0
|
||||
} else if (info.goodsPrice != 0.0 && !_countUserInput.value && !_priceUserInput.value) {
|
||||
info.goodsPrice.div(count).toSafeDouble()
|
||||
} else info.goodsUnitPrice
|
||||
|
||||
// 计算金额 单价不为空,不是数量输入 也不是金额输入
|
||||
val finalAmount =
|
||||
if (count == 0.0) {
|
||||
0.0
|
||||
} else if (info.goodsUnitPrice != 0.0 && !_countUserInput.value && !_amountUserInput.value) {
|
||||
count.times(info.goodsUnitPrice).toSafeDouble()
|
||||
} else info.goodsPrice
|
||||
Timber.d("startSensorScale finalPrice = $finalPrice, finalAmount = $finalAmount ")
|
||||
_selectedItem.value = info.copy(
|
||||
goodsWeight = weight.toSafeBigDecimal(),
|
||||
goodsCountTemp = if (_countUserInput.value) info.goodsCountTemp else count.toSafeString(
|
||||
currentItem.unitName
|
||||
),
|
||||
goodsCount = if (_countUserInput.value) info.goodsCount else finalCount
|
||||
// 数量
|
||||
goodsCountTemp = if (_countUserInput.value) info.goodsCountTemp else finalCount.toFormattedString(),
|
||||
goodsCount = if (_countUserInput.value) info.goodsCount else finalCount,
|
||||
// 单价
|
||||
goodsUnitPrice = if (_priceUserInput.value) info.goodsUnitPrice else finalPrice,
|
||||
goodsUnitPriceTemp = if (_priceUserInput.value) info.goodsUnitPriceTemp else finalPrice.toFormattedString(),
|
||||
// 金额
|
||||
goodsPrice = if (_amountUserInput.value) info.goodsPrice else finalAmount,
|
||||
goodsPriceTemp = if (_amountUserInput.value) info.goodsPriceTemp else finalAmount.toFormattedString()
|
||||
)
|
||||
}
|
||||
})
|
||||
@@ -113,7 +151,34 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
fun updateSelectedItem(purchaseOrder: PurchaseWarehouseParam?) {
|
||||
if (purchaseOrder != null) {
|
||||
purchaseOrder.goodsCount.takeIf { it != 0.0 }?.let { receivedNum ->
|
||||
// 计算金额 单价不为空,并且金额没有手动输入
|
||||
if (purchaseOrder.goodsUnitPrice != 0.0 && !_amountUserInput.value) {
|
||||
Timber.d("updateSelectedItem 计算金额")
|
||||
val result =
|
||||
receivedNum.times(purchaseOrder.goodsUnitPrice)
|
||||
purchaseOrder.goodsPrice = result.toSafeDouble()
|
||||
purchaseOrder.goodsPriceTemp = result.toFormattedString()
|
||||
}
|
||||
// 计算单价 金额不为空,并且单价没有手动输入
|
||||
if (purchaseOrder.goodsPrice != 0.0 && !_priceUserInput.value) {
|
||||
Timber.d("updateSelectedItem 计算单价")
|
||||
val result =
|
||||
purchaseOrder.goodsPrice.div(receivedNum)
|
||||
purchaseOrder.goodsUnitPrice = result.toSafeDouble()
|
||||
purchaseOrder.goodsUnitPriceTemp = result.toFormattedString()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 重置输入状态
|
||||
Timber.d("重置输入状态")
|
||||
_countUserInput.value = false
|
||||
_priceUserInput.value = false
|
||||
_amountUserInput.value = false
|
||||
}
|
||||
_selectedItem.value = purchaseOrder
|
||||
Timber.d("updateSelectedItem 更新,goodsUnitPrice = ${_selectedItem.value?.goodsUnitPrice}, ${_selectedItem.value?.goodsUnitPriceTemp}")
|
||||
}
|
||||
|
||||
fun updateGoodsAddParam(newState: GoodsAddParam) {
|
||||
@@ -195,10 +260,27 @@ class SelfProcurementViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数量输入状态
|
||||
*/
|
||||
fun updateCountInputState(boolean: Boolean) {
|
||||
_countUserInput.value = boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 单价输入状态
|
||||
*/
|
||||
fun updatePriceInputState(boolean: Boolean) {
|
||||
_priceUserInput.value = boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 金额输入状态
|
||||
*/
|
||||
fun updateAmountInputState(boolean: Boolean) {
|
||||
_amountUserInput.value = boolean
|
||||
}
|
||||
|
||||
fun addGoodsInfo() {
|
||||
val imageUri = GlobalData.imageUri
|
||||
if (imageUri == null) {
|
||||
|
||||
Reference in New Issue
Block a user