<template>
|
<div>
|
<el-dialog v-model="dialogVisible" title="领料台账" width="1200px" @close="handleClose">
|
<div class="material-toolbar">
|
<el-button type="primary" @click="handleAddMaterialRow">新增</el-button>
|
</div>
|
<el-table v-loading="materialTableLoading" :data="materialTableData" border row-key="tempId">
|
<el-table-column label="工序名称" min-width="180">
|
<template #default="{ row }">
|
<el-select
|
v-model="row.processId"
|
placeholder="请选择工序"
|
clearable
|
filterable
|
style="width: 100%;"
|
@change="val => handleProcessChange(row, val)"
|
>
|
<el-option v-for="item in processOptions" :key="item.id" :label="item.name" :value="item.id" />
|
</el-select>
|
</template>
|
</el-table-column>
|
<el-table-column label="原料名称" min-width="160">
|
<template #default="{ row }">
|
<el-button type="primary" link @click="openMaterialProductSelect(row)">
|
{{ row.materialName || "选择原料" }}
|
</el-button>
|
</template>
|
</el-table-column>
|
<el-table-column label="原料型号" min-width="180">
|
<template #default="{ row }">
|
{{ row.materialModel || "-" }}
|
</template>
|
</el-table-column>
|
<el-table-column label="需求数量" min-width="120">
|
<template #default="{ row }">
|
<el-input-number
|
v-model="row.requiredQty"
|
:min="0"
|
:precision="3"
|
:step="1"
|
controls-position="right"
|
style="width: 100%;"
|
@change="val => handleRequiredQtyChange(row, val)"
|
/>
|
</template>
|
</el-table-column>
|
<el-table-column label="计量单位" width="120">
|
<template #default="{ row }">
|
{{ row.unit || "-" }}
|
</template>
|
</el-table-column>
|
<el-table-column label="领用数量" min-width="120">
|
<template #default="{ row }">
|
<el-input-number
|
v-model="row.pickQty"
|
:min="0"
|
:precision="3"
|
:step="1"
|
controls-position="right"
|
style="width: 100%;"
|
/>
|
</template>
|
</el-table-column>
|
<el-table-column label="操作" width="90" fixed="right">
|
<template #default="{ $index }">
|
<el-button type="danger" link @click="handleDeleteMaterialRow($index)">删除</el-button>
|
</template>
|
</el-table-column>
|
</el-table>
|
<template #footer>
|
<span class="dialog-footer">
|
<el-button type="primary" :loading="materialSaving" @click="handleMaterialSave">保存</el-button>
|
<el-button @click="dialogVisible = false">取消</el-button>
|
</span>
|
</template>
|
</el-dialog>
|
|
<ProductSelectDialog
|
v-model="materialProductDialogVisible"
|
@confirm="handleMaterialProductConfirm"
|
single
|
/>
|
</div>
|
</template>
|
|
<script setup>
|
import { computed, ref, watch } from "vue";
|
import ProductSelectDialog from "@/views/basicData/product/ProductSelectDialog.vue";
|
import { processList } from "@/api/productionManagement/productionProcess.js";
|
import { listMaterialPickingLedger, saveMaterialPickingLedger } from "@/api/productionManagement/productionOrder.js";
|
|
const props = defineProps({
|
modelValue: { type: Boolean, default: false },
|
orderRow: { type: Object, default: null },
|
});
|
const emit = defineEmits(["update:modelValue", "saved"]);
|
|
const dialogVisible = computed({
|
get: () => props.modelValue,
|
set: val => emit("update:modelValue", val),
|
});
|
|
const materialProductDialogVisible = ref(false);
|
const materialTableLoading = ref(false);
|
const materialSaving = ref(false);
|
const materialTableData = ref([]);
|
const processOptions = ref([]);
|
const currentMaterialSelectRowIndex = ref(-1);
|
let materialTempId = 0;
|
|
const createMaterialRow = (row = {}) => ({
|
tempId: row.id || `temp_${++materialTempId}`,
|
id: row.id,
|
processId: row.processId,
|
processName: row.processName || "",
|
materialModelId: row.materialModelId,
|
materialName: row.materialName || "",
|
materialModel: row.materialModel || "",
|
requiredQty: Number(row.requiredQty ?? 0),
|
unit: row.unit || "",
|
pickQty: Number(row.pickQty ?? row.requiredQty ?? 0),
|
});
|
|
const getProcessOptions = async () => {
|
if (processOptions.value.length > 0) return;
|
const res = await processList({});
|
processOptions.value = res.data || [];
|
};
|
|
const loadMaterialData = async () => {
|
if (!props.orderRow?.id) return;
|
materialTableLoading.value = true;
|
materialTableData.value = [];
|
await getProcessOptions();
|
try {
|
const res = await listMaterialPickingLedger({ orderId: props.orderRow.id });
|
materialTableData.value = (res.data || []).map(item => createMaterialRow(item));
|
} finally {
|
materialTableLoading.value = false;
|
}
|
};
|
|
watch(
|
() => dialogVisible.value,
|
visible => {
|
if (visible) {
|
loadMaterialData();
|
}
|
}
|
);
|
|
const handleClose = () => {
|
materialTableData.value = [];
|
currentMaterialSelectRowIndex.value = -1;
|
};
|
|
const handleAddMaterialRow = () => {
|
materialTableData.value.push(createMaterialRow());
|
};
|
|
const handleDeleteMaterialRow = index => {
|
materialTableData.value.splice(index, 1);
|
};
|
|
const handleProcessChange = (row, processId) => {
|
const process = processOptions.value.find(item => item.id === processId);
|
row.processName = process?.name || "";
|
};
|
|
const handleRequiredQtyChange = (row, val) => {
|
const required = Number(val ?? 0);
|
row.requiredQty = required;
|
if (!row.pickQty || Number(row.pickQty) === 0) {
|
row.pickQty = required;
|
}
|
};
|
|
const openMaterialProductSelect = row => {
|
currentMaterialSelectRowIndex.value = materialTableData.value.findIndex(item => item.tempId === row.tempId);
|
materialProductDialogVisible.value = true;
|
};
|
|
const handleMaterialProductConfirm = products => {
|
if (!products || products.length === 0) return;
|
const index = currentMaterialSelectRowIndex.value;
|
if (index < 0 || !materialTableData.value[index]) return;
|
const product = products[0];
|
const row = materialTableData.value[index];
|
row.materialModelId = product.id;
|
row.materialName = product.productName || "";
|
row.materialModel = product.model || "";
|
row.unit = product.unit || "";
|
currentMaterialSelectRowIndex.value = -1;
|
materialProductDialogVisible.value = false;
|
};
|
|
const validateMaterialRows = () => {
|
if (materialTableData.value.length === 0) return false;
|
return !materialTableData.value.find(
|
item =>
|
!item.processId ||
|
!item.materialModelId ||
|
item.requiredQty === null ||
|
item.requiredQty === undefined ||
|
item.pickQty === null ||
|
item.pickQty === undefined
|
);
|
};
|
|
const handleMaterialSave = async () => {
|
if (!props.orderRow?.id || !validateMaterialRows()) return;
|
materialSaving.value = true;
|
try {
|
await saveMaterialPickingLedger({
|
orderId: props.orderRow.id,
|
items: materialTableData.value.map(item => ({
|
id: item.id,
|
processId: item.processId,
|
processName: item.processName,
|
materialModelId: item.materialModelId,
|
materialName: item.materialName,
|
materialModel: item.materialModel,
|
requiredQty: item.requiredQty,
|
unit: item.unit,
|
pickQty: item.pickQty,
|
})),
|
});
|
emit("saved");
|
dialogVisible.value = false;
|
} finally {
|
materialSaving.value = false;
|
}
|
};
|
</script>
|
|
<style scoped lang="scss">
|
.material-toolbar {
|
margin-bottom: 12px;
|
text-align: right;
|
}
|
</style>
|