diff --git a/pages/work/storage/receiving/components/ProductInfoEditor.vue b/pages/work/storage/receiving/components/ProductInfoEditor.vue index 7bb759c..acb1f3f 100644 --- a/pages/work/storage/receiving/components/ProductInfoEditor.vue +++ b/pages/work/storage/receiving/components/ProductInfoEditor.vue @@ -98,9 +98,9 @@ - + - + @@ -109,6 +109,17 @@ + + + + + + 个/箱 + + + + @@ -125,7 +136,6 @@ - cm @@ -135,7 +145,6 @@ - cm @@ -145,7 +154,15 @@ - cm + + + + + + + + @@ -254,6 +271,7 @@ barcode: '', batch_number: '', box_quantity: '', + tail_box_quantity: '', boxes_per_pallet: '', expiry_date: '', pallet_quantity: '', @@ -272,6 +290,7 @@ box_width: "", box_height: "", box_length: "", + box_weight: "", weight: "", }, spaceName: "默认收货区", @@ -317,13 +336,31 @@ box_quantity: [{ validator: (rule, value, callback) => { if (value && parseFloat(value) < 0) { - callback(new Error('箱数不能为负数')); + callback(new Error('总箱数不能为负数')); } else { callback(); } }, trigger: ['blur', 'change'] }], + tail_box_quantity: [{ + validator: (rule, value, callback) => { + if (value && parseFloat(value) < 0) { + callback(new Error('尾箱数不能为负数')); + return; + } + + const tailBoxQuantity = parseFloat(value); + const boxesPerPallet = parseFloat(this.formData.boxes_per_pallet); + if (!isNaN(tailBoxQuantity) && !isNaN(boxesPerPallet) && boxesPerPallet > 0 && tailBoxQuantity >= boxesPerPallet) { + callback(new Error('尾箱数必须小于箱规')); + return; + } + + callback(); + }, + trigger: ['blur', 'change'] + }], production_date: [{ validator: (rule, value, callback) => { if (this.isShelfLifeMgmt && !String(value || '').trim()) { @@ -368,6 +405,7 @@ isInitializing: false, initialized: false, boxQuantityManuallyEdited: false, + tailBoxQuantityManuallyEdited: false, }; }, computed: { @@ -527,6 +565,7 @@ this.formData = newFormData; this.boxQuantityManuallyEdited = false; + this.tailBoxQuantityManuallyEdited = false; if (this.formData.production_date && this.formData.expiry_date && !String(this.formData.shelf_life || '').trim()) { this.syncShelfLifeFromDates(); } @@ -538,8 +577,11 @@ })); } - // 无已有箱数时默认自动计算;有值时保留原值,允许后续手动修改 - if (newFormData.box_quantity === null || newFormData.box_quantity === undefined || newFormData.box_quantity === '') { + // 无已有箱数/尾箱数时默认自动计算;有值时保留原值,允许后续手动修改 + if ( + newFormData.box_quantity === null || newFormData.box_quantity === undefined || newFormData.box_quantity === '' || + newFormData.tail_box_quantity === null || newFormData.tail_box_quantity === undefined || newFormData.tail_box_quantity === '' + ) { this.calculateBoxQuantity(false, { force: true }); @@ -660,6 +702,15 @@ // 箱规输入处理 handleBoxesPerPalletBlur() { + const hasManualBoxes = this.boxQuantityManuallyEdited || this.tailBoxQuantityManuallyEdited; + if (hasManualBoxes) { + this.calculateActualQuantityFromBoxes(false); + this.$refs.uForm.validateField('tail_box_quantity'); + this.emitFormData(); + return; + } + + this.$refs.uForm.validateField('tail_box_quantity'); this.calculateBoxQuantity(true); }, @@ -675,14 +726,53 @@ } this.boxQuantityManuallyEdited = true; - this.emitFormData(); + this.calculateActualQuantityFromBoxes(true); + }, + + // 尾箱数输入处理 + handleTailBoxQuantityBlur() { + const tailBoxQuantity = String(this.formData.tail_box_quantity || '').trim(); + if (!tailBoxQuantity) { + this.tailBoxQuantityManuallyEdited = false; + this.calculateBoxQuantity(true, { + force: true + }); + return; + } + + this.tailBoxQuantityManuallyEdited = true; + this.calculateActualQuantityFromBoxes(true); + }, + + // 根据总箱数、尾箱数回算实际数量 + calculateActualQuantityFromBoxes(shouldEmit = true) { + const boxesPerPallet = parseFloat(this.formData.boxes_per_pallet); + if (isNaN(boxesPerPallet) || boxesPerPallet <= 0) { + if (shouldEmit) { + this.emitFormData(); + } + return; + } + + const boxQuantity = parseFloat(this.formData.box_quantity); + const tailBoxQuantity = parseFloat(this.formData.tail_box_quantity); + + const safeBoxQuantity = !isNaN(boxQuantity) && boxQuantity >= 0 ? boxQuantity : 0; + const safeTailBoxQuantity = !isNaN(tailBoxQuantity) && tailBoxQuantity >= 0 ? tailBoxQuantity : 0; + + const actualQuantity = safeBoxQuantity * boxesPerPallet + safeTailBoxQuantity; + this.formData.actual_quantity = String(Number(actualQuantity.toFixed(6))); + + if (shouldEmit) { + this.emitFormData(); + } }, // 计算箱数 calculateBoxQuantity(shouldEmit = true, options = {}) { console.log("计算箱数", shouldEmit); const force = !!options.force; - if (this.boxQuantityManuallyEdited && !force) { + if (this.boxQuantityManuallyEdited && this.tailBoxQuantityManuallyEdited && !force) { if (shouldEmit) { this.emitFormData(); } @@ -693,18 +783,28 @@ let boxesPerPallet = parseFloat(this.formData.boxes_per_pallet); let newBoxQuantity = ""; + let newTailBoxQuantity = ""; if (!isNaN(actualQuantity) && actualQuantity > 0) { if (!isNaN(boxesPerPallet) && boxesPerPallet > 0) { - // 计算箱数:实际数量 ÷ 箱规,向上取整 - let boxQuantity = Math.ceil(actualQuantity / boxesPerPallet); + // 计算总箱数:实际数量 ÷ 箱规,向下取整 + // 保证关系:实际数量 = 箱规 * 总箱数 + 尾箱数 + let boxQuantity = Math.floor(actualQuantity / boxesPerPallet); newBoxQuantity = boxQuantity.toString(); + const remainder = actualQuantity % boxesPerPallet; + newTailBoxQuantity = String(Number(remainder.toFixed(6))); } else { - // 箱规未设置,默认1箱 - newBoxQuantity = "1"; + // 箱规未设置时无法计算总箱数和尾箱数 + newBoxQuantity = ""; + newTailBoxQuantity = ''; } } - this.formData.box_quantity = newBoxQuantity; + if (!this.boxQuantityManuallyEdited || force) { + this.formData.box_quantity = newBoxQuantity; + } + if (!this.tailBoxQuantityManuallyEdited || force) { + this.formData.tail_box_quantity = newTailBoxQuantity; + } if (shouldEmit) { console.log("提交更改"); this.emitFormData(); @@ -728,7 +828,9 @@ // 确保数字类型 actual_quantity: parseFloat(this.formData.actual_quantity) || 0, box_quantity: this.formData.box_quantity ? parseInt(this.formData.box_quantity) : null, + tail_box_quantity: this.formData.tail_box_quantity ? parseFloat(this.formData.tail_box_quantity) : null, boxes_per_pallet: this.formData.boxes_per_pallet ? parseFloat(this.formData.boxes_per_pallet) : null, + box_weight: this.formData.box_weight ? parseFloat(this.formData.box_weight) : null, shelf_life: this.formData.shelf_life ? parseInt(this.formData.shelf_life) : null, pallet_quantity: this.formData.pallet_quantity ? parseFloat(this.formData.pallet_quantity) : null, };