364 lines
13 KiB
Kotlin
364 lines
13 KiB
Kotlin
package com.sw.inbound.dialog
|
|
|
|
import android.content.Context
|
|
import android.net.Uri
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.widget.EditText
|
|
import android.widget.FrameLayout
|
|
import android.widget.ImageView
|
|
import android.widget.TextView
|
|
import androidx.camera.view.PreviewView
|
|
import androidx.core.view.updateLayoutParams
|
|
import coil.load
|
|
import com.sw.inbound.GlobalData
|
|
import com.sw.inbound.R
|
|
import com.sw.inbound.activity.GoodsListActivity
|
|
import com.sw.inbound.databinding.DialogAddGoodsBinding
|
|
import com.sw.inbound.databinding.LayoutCameraPreviewBinding
|
|
import com.sw.inbound.ext.toSafeBigDecimal
|
|
import com.sw.inbound.ext.toSafeFloat
|
|
import com.sw.inbound.model.request.GoodsAddParam
|
|
import com.sw.inbound.model.response.DictType
|
|
import com.sw.inbound.utils.BitmapCropper
|
|
import com.sw.inbound.utils.BitmapSaver
|
|
import com.sw.inbound.utils.CameraUtils
|
|
import com.sw.inbound.utils.ImageUtil
|
|
import com.sw.inbound.utils.ext.dp
|
|
import com.sw.inbound.utils.ext.gone
|
|
import com.sw.inbound.utils.ext.hideKeyboard
|
|
import com.sw.inbound.utils.ext.toast
|
|
import com.sw.inbound.utils.ext.visible
|
|
import kotlin.math.min
|
|
|
|
class AddGoodsDialog(
|
|
context: Context,
|
|
var cancelBlock: () -> Unit = {},
|
|
var confirmBlock: (String) -> Unit = {},
|
|
) : BaseDialog(context) {
|
|
|
|
private lateinit var binding: DialogAddGoodsBinding
|
|
override fun getRootView(): View {
|
|
val inflater = LayoutInflater.from(context)
|
|
binding = DialogAddGoodsBinding.inflate(inflater)
|
|
return binding.root
|
|
}
|
|
|
|
private var dropdownPopup: DropdownPopup? = null
|
|
|
|
private val dropdownList: MutableList<DictType> = mutableListOf()
|
|
private var activity: GoodsListActivity? = null
|
|
private val cameraUtils: CameraUtils by lazy {
|
|
CameraUtils(activity!!)
|
|
}
|
|
|
|
override fun show() {
|
|
super.show()
|
|
cameraUtils.setPreviewController(previewView)
|
|
cameraUtils.bind()
|
|
window?.decorView?.postDelayed({
|
|
binding.llCameraFlag.gone()
|
|
}, 1000)
|
|
}
|
|
|
|
private val takePhotoCallback: (Uri) -> Unit = { uri ->
|
|
binding.flCameraPreview.gone()
|
|
imageUri = uri
|
|
binding.cvImageBg.visible()
|
|
binding.ivGoodsImage.load(uri)
|
|
}
|
|
|
|
private lateinit var previewView: PreviewView
|
|
override fun initView() {
|
|
activity = getReceiptActivity()
|
|
activity?.unBindCamera()
|
|
setOnDismissListener {
|
|
cameraUtils.unbind()
|
|
}
|
|
cameraUtils.initCamera()
|
|
val previewBinding =
|
|
LayoutCameraPreviewBinding.inflate(
|
|
LayoutInflater.from(context),
|
|
binding.flCameraPreview
|
|
)
|
|
previewBinding.previewView.let {
|
|
it.updateLayoutParams {
|
|
width = 428.dp
|
|
height = 320.dp
|
|
}
|
|
previewView = it
|
|
}
|
|
// binding.flCameraPreview.removeAllViews()
|
|
// binding.flCameraPreview.addView(previewView)
|
|
binding.flCameraPreview.visible()
|
|
binding.cvImageBg.gone()
|
|
binding.btnClose.setOnClickListener {
|
|
cancelBlock()
|
|
dismiss()
|
|
}
|
|
binding.btnConfirm.setOnClickListener {
|
|
submit()
|
|
}
|
|
binding.btnStartPreview.setOnClickListener {
|
|
binding.cvImageBg.gone()
|
|
binding.flCameraPreview.visible()
|
|
}
|
|
binding.btnTakePhoto.setOnClickListener {
|
|
cameraUtils.takePhoto(takePhotoCallback)
|
|
}
|
|
binding.btnUploadImg.gone()
|
|
// binding.btnUploadImg.setOnClickListener {
|
|
// uploadImage()
|
|
// }
|
|
binding.tvGoodsType.let { tv ->
|
|
tv.text = "原材料"
|
|
tv.tag = GlobalData.goodsTypeList.firstOrNull { it.value == "原材料" }?.id
|
|
?: "1980098911276191746"
|
|
}
|
|
showDropDownList(
|
|
binding.flProcurementUnit,
|
|
textView = binding.tvProcurementUnit,
|
|
arrowImage = binding.ivProcurementArrow,
|
|
list = GlobalData.unitTypeList,
|
|
height = 2 * 71.dp
|
|
) {
|
|
val warehouseUnit = binding.tvWarehouseUnit.text.toString().trim()
|
|
if (warehouseUnit.isBlank()) return@showDropDownList
|
|
when (it.value) {
|
|
"千克", "公斤" -> {
|
|
setGoodsCount(warehouseUnit, "1", "2", "1000")
|
|
}
|
|
"斤" -> {
|
|
setGoodsCount(warehouseUnit, "0.5", "1", "500")
|
|
}
|
|
"克" -> {
|
|
setGoodsCount(warehouseUnit, "0.001", "0.002", "1")
|
|
}
|
|
else -> {
|
|
val procurementUnit = binding.tvProcurementUnit.text.toString().trim()
|
|
val warehouseUnit = binding.tvWarehouseUnit.text.toString().trim()
|
|
binding.etGoodsCount.setText(
|
|
if (procurementUnit == warehouseUnit) "1" else ""
|
|
)
|
|
}
|
|
}
|
|
}
|
|
// showDropDownList(
|
|
// binding.flGoodsType,
|
|
// textView = binding.tvGoodsType,
|
|
// list = GlobalData.goodsTypeList,
|
|
// )
|
|
showDropDownList(
|
|
binding.flStorageType,
|
|
textView = binding.tvStorageType,
|
|
arrowImage = binding.ivStorageArrow,
|
|
list = GlobalData.storageTypeList,
|
|
)
|
|
showDropDownList(
|
|
dropdownView = binding.flWarehouseUnit,
|
|
textView = binding.tvWarehouseUnit,
|
|
arrowImage = binding.ivWarehouseArrow,
|
|
list = GlobalData.unitTypeList,
|
|
height = 3 * 71.dp
|
|
) {
|
|
binding.tvSelectUseUnit.text = it.value
|
|
binding.tvSelectProcurementUnit.text = it.value
|
|
val procurementUnit = binding.tvProcurementUnit.text.toString().trim()
|
|
when (it.value) {
|
|
"千克", "公斤" -> {
|
|
binding.etUseUnit.setText("1000")
|
|
setGoodsCount(procurementUnit, "1", "0.5", "0.001")
|
|
}
|
|
"斤" -> {
|
|
binding.etUseUnit.setText("500")
|
|
setGoodsCount(procurementUnit, "2", "1", "0.002")
|
|
}
|
|
"克" -> {
|
|
binding.etUseUnit.setText("1")
|
|
setGoodsCount(procurementUnit, "1000", "500", "1")
|
|
}
|
|
else -> {
|
|
binding.etUseUnit.setText("")
|
|
|
|
val procurementUnit = binding.tvProcurementUnit.text.toString().trim()
|
|
val warehouseUnit = binding.tvWarehouseUnit.text.toString().trim()
|
|
binding.etGoodsCount.setText(
|
|
if (procurementUnit == warehouseUnit) "1" else ""
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun setGoodsCount(unit:String, kgCount:String, jinCount:String, gCount:String) {
|
|
when (unit) {
|
|
"千克", "公斤" -> {
|
|
binding.etGoodsCount.setText(kgCount)
|
|
}
|
|
"斤" -> {
|
|
binding.etGoodsCount.setText(jinCount)
|
|
}
|
|
"克" -> {
|
|
binding.etGoodsCount.setText(gCount)
|
|
}
|
|
else -> {
|
|
val procurementUnit = binding.tvProcurementUnit.text.toString().trim()
|
|
val warehouseUnit = binding.tvWarehouseUnit.text.toString().trim()
|
|
binding.etGoodsCount.setText(
|
|
if (procurementUnit == warehouseUnit) "1" else ""
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun showDropDownList(
|
|
dropdownView: FrameLayout,
|
|
textView: TextView,
|
|
arrowImage: ImageView,
|
|
list: List<DictType>,
|
|
height: Int = 355.dp,
|
|
block: (DictType) -> Unit = {}
|
|
) {
|
|
dropdownView.setOnClickListener { v ->
|
|
v.hideKeyboard()
|
|
dropdownList.clear()
|
|
val tempList =
|
|
list.filter { it.id.isNullOrBlank().not() && it.value.isNullOrBlank().not() }
|
|
if (tempList.isEmpty()) {
|
|
context.toast("暂无下拉数据")
|
|
return@setOnClickListener
|
|
}
|
|
dropdownList.addAll(tempList)
|
|
val realHeight = min(height, dropdownList.size * 71.dp)
|
|
dropdownPopup = DropdownPopup(
|
|
context = context,
|
|
list = dropdownList,
|
|
popWidth = dropdownView.width,
|
|
popHeight = realHeight
|
|
) {
|
|
textView.run {
|
|
text = it.value
|
|
tag = it.id
|
|
}
|
|
block(it)
|
|
}.also {
|
|
it.arrowImage = arrowImage
|
|
it.bgLayout = dropdownView
|
|
}
|
|
dropdownPopup?.showAsDropDown(dropdownView)
|
|
dropdownPopup?.setOnDismissListener {
|
|
dropdownPopup = null
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun submit() {
|
|
if (imageUri == null) {
|
|
context.toast("请采集图片")
|
|
return
|
|
}
|
|
val goodsName = binding.etGoodsName.text.trim().toString()
|
|
if (goodsName.isBlank()) {
|
|
context.toast("请输入物品名称")
|
|
return
|
|
}
|
|
val netRateText = binding.etNetMaterialRate.text.trim().toString()
|
|
if (netRateText.isBlank()) {
|
|
context.toast("请输入净材率")
|
|
return
|
|
}
|
|
val netRate = (netRateText.toFloatOrNull() ?: 0f) / 100f
|
|
val goodsType = binding.tvGoodsType.tag
|
|
if (goodsType == null || goodsType.toString().isBlank()) {
|
|
context.toast("请录入物品类型")
|
|
return
|
|
}
|
|
val storageType = binding.tvStorageType.tag
|
|
if (storageType == null || storageType.toString().isBlank()) {
|
|
context.toast("请录入存储方式")
|
|
return
|
|
}
|
|
val warehouseUnit = binding.tvWarehouseUnit.tag
|
|
if (warehouseUnit == null || warehouseUnit.toString().isBlank()) {
|
|
context.toast("请录入库存单位")
|
|
return
|
|
}
|
|
val purchaseUnit = binding.tvProcurementUnit.tag
|
|
if (purchaseUnit == null || purchaseUnit.toString().isBlank()) {
|
|
context.toast("请录采购单位")
|
|
return
|
|
}
|
|
val costValue = binding.etUseUnit.text.trim().toString()
|
|
if (costValue.isBlank()) {
|
|
context.toast("请输入物品名称")
|
|
return
|
|
}
|
|
val goodsCount = binding.etGoodsCount.text.trim().toString()
|
|
if (goodsCount.isBlank()) {
|
|
context.toast("请输入采购数量")
|
|
return
|
|
}
|
|
val goodsPrice = binding.etGoodsPrice.text.trim().toString()
|
|
if (goodsPrice.isBlank()) {
|
|
context.toast("请输入采购单价")
|
|
return
|
|
}
|
|
val param = GoodsAddParam(
|
|
goodName = goodsName,
|
|
netRate = netRate,
|
|
goodType = goodsType.toString(),
|
|
storageType = storageType.toString(),
|
|
unitId = warehouseUnit.toString(),
|
|
consumeValue = costValue,
|
|
purchaseUnit = purchaseUnit.toString(),
|
|
purchaseValue = goodsCount.toSafeFloat(),
|
|
purchasePrice = goodsPrice.toSafeBigDecimal(),
|
|
)
|
|
activity?.let {
|
|
Loading.show(it)
|
|
}
|
|
ImageUtil.uriToBitmap(context, imageUri!!)?.let { bitmap ->
|
|
val newBmp = BitmapCropper.cropCenter(
|
|
original = bitmap,
|
|
targetWidth = 1000, targetHeight = 1300,
|
|
offsetX = 30, offsetY = 100
|
|
)
|
|
val file = BitmapSaver.saveToAppFilesDir(
|
|
newBmp,
|
|
context,
|
|
"IMG_CROP_${System.currentTimeMillis()}.jpg"
|
|
)
|
|
if(file == null) {
|
|
context.toast("请重新拍照后再试")
|
|
Loading.dismiss()
|
|
return@let
|
|
}
|
|
activity?.createNewGoods(file, param) {
|
|
Loading.dismiss()
|
|
if (it.not()) {
|
|
context.toast("新增物品失败")
|
|
return@createNewGoods
|
|
}
|
|
context.toast("新增物品成功")
|
|
confirmBlock(goodsName)
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
|
|
private var imageUri: Uri? = null
|
|
|
|
// private fun uploadImage() {
|
|
// if (imageUri == null) {
|
|
// context.toast("请采集图片")
|
|
// return
|
|
// }
|
|
// activity?.uploadImage(imageUri!!) { imgUrl ->
|
|
// if (imgUrl.isNullOrBlank()) {
|
|
// context.toast("图片上传失败")
|
|
// return@uploadImage
|
|
// }
|
|
// context.toast(imgUrl)
|
|
// }
|
|
// }
|
|
} |