添加单价和金额计算
This commit is contained in:
@@ -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(),
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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