Commit 47522ed5 authored by 方建宇's avatar 方建宇

领用申请库存更新

parent 647c9287
......@@ -138,7 +138,7 @@
<script>
import { addApply, updateApply } from '@/api/material/apply.js';
import { listMaterial } from '@/api/material/material.js';
import { listMaterial, getMaterial, updateMaterial } from '@/api/material/material.js';
import { ElMessageBox } from 'element-plus';
export default {
......@@ -149,7 +149,7 @@ export default {
applicant: '',
department: '',
items: [
{ itemType: '', itemName: '', unit: '', stock: 0, applyQuantity: 0, applyRemaining: 0 }
{ itemType: '', itemName: '', unit: '', stock: 0, applyQuantity: 0, applyRemaining: 0, materialId: '' }
],
usageType: '借用(需归还)',
remark: '',
......@@ -172,7 +172,7 @@ export default {
},
methods: {
addItem() {
this.form.items.push({ itemType: '', itemName: '', unit: '', stock: 0, applyQuantity: 0, applyRemaining: 0 });
this.form.items.push({ itemType: '', itemName: '', unit: '', stock: 0, applyQuantity: 0, applyRemaining: 0, materialId: '' });
},
pasteItem() {
this.showPasteDialog = true;
......@@ -185,15 +185,55 @@ export default {
items: JSON.stringify(this.form.items)
};
addApply(data).then(response => {
if (response && response.code === 200) {
ElMessageBox.alert('申请提交成功', '提示', {
confirmButtonText: '确定',
callback: action => {
this.$message.success('申请提交成功');
// 更新每个物品的 stock
this.form.items.forEach(item => {
if (item.materialId) {
getMaterial(item.materialId).then(materialResponse => {
if (materialResponse && materialResponse.code === 200) {
console.log('Material Response:', materialResponse); // 添加调试信息
// 获取原 stock 数量
let originalStock = materialResponse.data.stock || 0;
// 计算新的 stock 数量
let newStock = originalStock - item.applyQuantity;
// 调用 updateMaterial 接口更新 material 表中相应物品的 stock 字段
updateMaterial({ materialId: item.materialId, stock: newStock }).then(updateResponse => {
if (updateResponse && updateResponse.code === 200) {
console.log('Stock updated successfully for materialId:', item.materialId);
} else {
this.$message.error('更新库存失败: ' + updateResponse.msg);
}
}).catch(updateError => {
this.$message.error('更新库存失败: ' + updateError.message);
});
} else {
this.$message.error('获取物品信息失败: ' + materialResponse.msg);
}
}).catch(materialError => {
this.$message.error('获取物品信息失败: ' + materialError.message);
});
}
});
this.resetForm();
// 提交成功后清空草稿
localStorage.removeItem('materialApplyDraft');
}
});
} else {
ElMessageBox.alert('申请提交失败', '提示', {
confirmButtonText: '确定',
callback: action => {
this.$message.error('申请提交失败: ' + response.msg);
}
});
}
}).catch(error => {
ElMessageBox.alert('申请提交失败', '提示', {
confirmButtonText: '确定',
......@@ -221,7 +261,7 @@ export default {
applicant: '',
department: '',
items: [
{ itemType: '', itemName: '', unit: '', stock: 0, applyQuantity: 0, applyRemaining: 0 }
{ itemType: '', itemName: '', unit: '', stock: 0, applyQuantity: 0, applyRemaining: 0, materialId: '' }
],
usageType: '借用(需归还)',
remark: '',
......@@ -264,6 +304,7 @@ export default {
this.form.items[index].itemType = newItemType;
this.form.items[index].itemName = ''; // 清空物品名称
this.form.items[index].unit = ''; // 清空单位
this.form.items[index].materialId = ''; // 清空 materialId
this.updateItemNames(index); // 更新物品名称选项
},
updateUnitBasedOnItemName(index) {
......@@ -275,9 +316,14 @@ export default {
);
if (selectedItem) {
item.unit = selectedItem.unit;
item.stock = selectedItem.stock !== null ? selectedItem.stock : 0; // 处理 stock 为 null 的情况
item.materialId = selectedItem.materialId; // 设置 materialId
} else {
item.unit = '';
item.stock = 0; // 默认库存为0
item.materialId = ''; // 清空 materialId
}
this.updateRemaining(index); // 更新剩余库存
}
}
},
......
......@@ -94,7 +94,7 @@
<script>
import { ElMessageBox } from 'element-plus';
import { addEntry, listEntry } from '@/api/material/entry.js';
import { listMaterial, updateMaterial } from '@/api/material/material.js';
import {getMaterial, listMaterial, updateMaterial} from '@/api/material/material.js';
export default {
data() {
......@@ -127,26 +127,19 @@ export default {
ElMessageBox.alert('提交成功', '提示', {
confirmButtonText: '确定',
callback: action => {
// 调用 listEntry 接口查询所有与刚刚提交的物品一样的入库记录
listEntry({ materialId: this.form.materialId }).then(entryResponse => {
if (entryResponse && entryResponse.code === 200) {
console.log('Entry Response:', entryResponse); // 添加调试信息
// 调用 getMaterial 接口查询物品的 stock 信息
getMaterial(this.form.materialId).then(materialResponse => {
if (materialResponse && materialResponse.code === 200) {
console.log('Material Response:', materialResponse); // 添加调试信息
// 初始化 totalStock 为 0
let totalStock = 0;
// 获取原 stock 数量
let originalStock = materialResponse.data.stock || 0;
// 统计入库数量(entry_quantity 字段的总和)
if (entryResponse.rows && Array.isArray(entryResponse.rows)) {
totalStock = entryResponse.rows.reduce((sum, entry) => {
// 确保 entry.entry_quantity 不为 null
return sum + (entry.entryQuantity !== null ? entry.entryQuantity : 0);
}, 0);
}
console.log('Total Stock:', totalStock); // 添加调试信息
// 计算新的 stock 数量
let newStock = originalStock + this.form.entryQuantity;
// 调用 updateMaterial 接口更新 material 表中相应物品的 stock 字段
updateMaterial({ materialId: this.form.materialId, stock: totalStock }).then(updateResponse => {
updateMaterial({ materialId: this.form.materialId, stock: newStock }).then(updateResponse => {
if (updateResponse && updateResponse.code === 200) {
// 提交成功后清空草稿
localStorage.removeItem('materialApplyDraft');
......@@ -160,10 +153,10 @@ export default {
this.$message.error('更新库存失败: ' + updateError.message);
});
} else {
this.$message.error('获取入库记录失败: ' + entryResponse.msg);
this.$message.error('获取物品信息失败: ' + materialResponse.msg);
}
}).catch(entryError => {
this.$message.error('获取入库记录失败: ' + entryError.message);
}).catch(materialError => {
this.$message.error('获取物品信息失败: ' + materialError.message);
});
}
});
......@@ -179,8 +172,7 @@ export default {
callback: action => {}
});
}
},
resetForm() {
}, resetForm() {
this.form = {
entryDate: '',
entryPerson: '',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment