inventory-form.vue 7.58 KB
<template>
	<el-dialog title="添加库存" :close-on-click-modal="false" :visible.sync="visible" class="NCC-dialog NCC-dialog_center"
		lock-scroll width="800px">
		<el-form ref="elForm" :model="dataForm" size="small" label-width="120px" label-position="right" :rules="rules">
			<el-row :gutter="15">
				<el-col :span="12">
					<el-form-item label="产品" prop="productId">
						<el-select v-model="dataForm.productId" placeholder="请选择产品" clearable filterable
							:style='{"width":"100%"}' :disabled="!!defaultProductId" @change="onProductChange">
							<el-option v-for="item in productOptions" :key="item.id" :label="item.productName"
								:value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-show="false">
					<el-form-item label="入库类型" prop="stockInType">
						<el-select v-model="dataForm.stockInType" placeholder="请选择入库类型" clearable
							:style='{"width":"100%"}' @change="onStockInTypeChange">
							<el-option label="普通入库" :value="1" />
							<el-option label="采购入库" :value="2" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="数量" prop="quantity">
						<el-input-number v-model="dataForm.quantity" placeholder="请输入数量" :min="1" :precision="0"
							:style='{"width":"100%"}' @change="calculatePurchaseAmount" />
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="dataForm.stockInType === 2">
					<el-form-item label="采购单价" prop="purchaseUnitPrice">
						<el-input-number v-model="dataForm.purchaseUnitPrice" placeholder="请输入采购单价" :min="0"
							:precision="2" :style='{"width":"100%"}' @change="calculatePurchaseAmount" />
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="dataForm.stockInType === 2">
					<el-form-item label="采购总金额">
						<el-input v-model="purchaseAmountDisplay" placeholder="系统自动计算" readonly
							:style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="入库时间" prop="stockInTime">
						<el-date-picker v-model="dataForm.stockInTime" type="date" placeholder="请选择入库时间"
							value-format="yyyy-MM-dd" format="yyyy-MM-dd" :style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="生产日期" prop="productionDate">
						<el-date-picker v-model="dataForm.productionDate" type="date" placeholder="请选择生产日期"
							value-format="yyyy-MM-dd" format="yyyy-MM-dd" :style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="保质期(天)" prop="shelfLife">
						<el-input-number v-model="dataForm.shelfLife" placeholder="请输入保质期" :min="1" :precision="0"
							:style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="批次号" prop="batchNumber">
						<el-input v-model="dataForm.batchNumber" placeholder="请输入批次号" clearable
							:style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>

<script>
import request from '@/utils/request'

export default {
		data() {
		return {
			visible: false,
			btnLoading: false,
			defaultProductId: '',
			dataForm: {
				productId: '',
				stockInType: 2,
				quantity: 1,
				stockInTime: '',
				productionDate: '',
				shelfLife: 365,
				batchNumber: '',
				purchaseUnitPrice: null
			},
			productOptions: [],
			rules: {
				productId: [
					{ required: true, message: '请选择产品', trigger: 'change' }
				],
				quantity: [
					{ required: true, message: '请输入数量', trigger: 'blur' }
				],
				stockInTime: [
					{ required: true, message: '请选择入库时间', trigger: 'change' }
				],
				// purchaseUnitPrice: [
				// 	{ 
				// 		validator: (rule, value, callback) => {
				// 			if (this.dataForm.stockInType === 2) {
				// 				if (!value || value <= 0) {
				// 					callback(new Error('采购入库时,采购单价必须大于0'))
				// 				} else {
				// 					callback()
				// 				}
				// 			} else {
				// 				callback()
				// 			}
				// 		}, 
				// 		trigger: 'blur' 
				// 	}
				// ]
			}
		}
	},
	computed: {
		purchaseAmountDisplay() {
			if (this.dataForm.stockInType === 2 && this.dataForm.purchaseUnitPrice && this.dataForm.quantity) {
				const amount = this.dataForm.purchaseUnitPrice * this.dataForm.quantity
				return '¥' + amount.toFixed(2)
			}
			return ''
		}
	},
	methods: {
		init(productId) {
			this.visible = true
			this.defaultProductId = productId || ''
			this.dataForm = {
				productId: productId || '',
				stockInType: 2,
				quantity: 1,
				stockInTime: '',
				productionDate: '',
				shelfLife: 365,
				batchNumber: '',
				purchaseUnitPrice: null
			}
			this.initProductOptions()
		},
		initProductOptions() {
			// 如果传入了产品ID,只显示该产品;否则只显示上架的产品
			const query = {
				currentPage: 1,
				pageSize: 1000
			}
			// 如果传入了产品ID,则查询该产品;否则只查询上架的产品
			if (this.defaultProductId) {
				query.id = this.defaultProductId
			} else {
				query.onShelfStatus = 1  // 只获取上架的产品
			}
			request({
				url: '/api/Extend/LqProduct/GetList',
				method: 'GET',
				data: query
			}).then(res => {
				if (res.code == 200 && res.data && res.data.list) {
					if (this.defaultProductId) {
						// 如果传入了产品ID,只显示该产品
						this.productOptions = res.data.list
					} else {
						// 只显示上架的产品
						this.productOptions = res.data.list.filter(product => product.onShelfStatus === 1)
					}
				}
			}).catch(() => {
				this.productOptions = []
			})
		},
		onProductChange() {
			// 产品选择变化时的处理
		},
		onStockInTypeChange() {
			// 入库类型变化时,重置采购单价
			if (this.dataForm.stockInType !== 2) {
				this.dataForm.purchaseUnitPrice = null
			}
			// 清除验证状态
			this.$nextTick(() => {
				this.$refs.elForm && this.$refs.elForm.clearValidate('purchaseUnitPrice')
			})
		},
		calculatePurchaseAmount() {
			// 自动计算采购总金额(通过计算属性 purchaseAmountDisplay 实现)
			// 计算属性会自动响应数据变化,无需手动更新
		},
		dataFormSubmit() {
			this.$refs.elForm.validate((valid) => {
				if (!valid) {
					return false
				}
				this.btnLoading = true
				const requestData = {
					productId: this.dataForm.productId,
					stockInType: this.dataForm.stockInType,
					quantity: this.dataForm.quantity,
					stockInTime: this.dataForm.stockInTime,
					productionDate: this.dataForm.productionDate,
					shelfLife: this.dataForm.shelfLife,
					batchNumber: this.dataForm.batchNumber
				}
				// 如果是采购入库,添加采购单价(采购总金额由后端自动计算)
				if (this.dataForm.stockInType === 2) {
					requestData.purchaseUnitPrice = this.dataForm.purchaseUnitPrice
				}
				request({
					url: '/api/Extend/LqInventory/Create',
					method: 'POST',
					data: requestData
				}).then(res => {
					this.btnLoading = false
					this.$message({
						type: 'success',
						message: res.msg || '添加成功',
						onClose: () => {
							this.visible = false
							this.$emit('refresh')
						}
					})
				}).catch(() => {
					this.btnLoading = false
				})
			})
		}
	}
}
</script>

<style lang="scss" scoped>
</style>