| | |
| | | <script lang="ts" setup> |
| | | import type { MdmItemApi } from '#/api/mdm/item'; |
| | | import type { ErpPurchaseRequestApi } from '#/api/erp/purchase/request'; |
| | | |
| | | import { computed, nextTick, onMounted, ref, watch } from 'vue'; |
| | |
| | | erpCountInputFormatter, |
| | | erpPriceInputFormatter, |
| | | erpPriceMultiply, |
| | | } from '../../../../../packages/utils/src'; |
| | | } from '@vben/utils'; |
| | | |
| | | import { Input, InputNumber, Select, DatePicker } from 'ant-design-vue'; |
| | | import { Input, InputNumber, DatePicker, Switch } from 'ant-design-vue'; |
| | | |
| | | import { TableAction, useVbenVxeGrid } from '#/adapter/vxe-table'; |
| | | import { getItemPage } from '#/api/mdm/item'; |
| | | import { MdmItemSelect } from '#/views/basicData/mdm/components'; |
| | | |
| | | import { useFormItemColumns } from '../data'; |
| | | |
| | |
| | | const emit = defineEmits(['update:items']); |
| | | |
| | | const tableData = ref<ErpPurchaseRequestApi.PurchaseRequestItem[]>([]); |
| | | const productOptions = ref<MdmItemApi.Item[]>([]); |
| | | |
| | | /** 获取表格合计数据 */ |
| | | /** 获取表格合计数据(行上 totalPrice 为不含税金额,含税合计需加上税额) */ |
| | | const summaries = computed(() => { |
| | | return { |
| | | count: tableData.value.reduce((sum, item) => sum + (item.count || 0), 0), |
| | | totalPrice: tableData.value.reduce( |
| | | (sum, item) => { |
| | | const price = erpPriceMultiply(item.productPrice || 0, item.count || 0) ?? 0; |
| | | return sum + price; |
| | | }, |
| | | (sum, item) => sum + (item.totalPrice || 0), |
| | | 0, |
| | | ), |
| | | taxPrice: tableData.value.reduce((sum, item) => sum + (item.taxPrice || 0), 0), |
| | | }; |
| | | }); |
| | | |
| | | /** 含税合计 = 不含税金额 + 税额 */ |
| | | const totalTaxInclusivePrice = computed( |
| | | () => summaries.value.totalPrice + summaries.value.taxPrice, |
| | | ); |
| | | |
| | | /** 表格配置 */ |
| | | const [Grid, gridApi] = useVbenVxeGrid({ |
| | |
| | | demandTime: undefined, |
| | | remark: undefined, |
| | | totalPrice: 0, |
| | | qcCheckFlag: false, |
| | | }; |
| | | tableData.value.push(newRow); |
| | | emit('update:items', [...tableData.value]); |
| | |
| | | } |
| | | |
| | | /** 处理产品变更 */ |
| | | async function handleProductChange(productId: any, row: any) { |
| | | const product = productOptions.value.find((p) => p.id === productId); |
| | | function handleProductChange(product: any, row: any) { |
| | | if (!product) { |
| | | return; |
| | | } |
| | | row.productId = productId; |
| | | row.productId = product.id; |
| | | row.productBarCode = product.barCode; |
| | | row.productUnitId = product.unitMeasureId; |
| | | row.productUnitName = product.unitMeasureName; |
| | |
| | | handleRowChange(row); |
| | | } |
| | | |
| | | /** |
| | | * 重算行金额(单价为**含税**单价) |
| | | * 含税金额 = 含税单价 × 数量;税额 = 含税金额 × 税率;不含税金额 = 含税金额 - 税额 |
| | | */ |
| | | function recalcRow(row: ErpPurchaseRequestApi.PurchaseRequestItem) { |
| | | if (row.productPrice && row.count) { |
| | | const taxInclusivePrice = erpPriceMultiply(row.productPrice, row.count) ?? 0; |
| | | row.taxPrice = |
| | | erpPriceMultiply(taxInclusivePrice, (row.taxPercent || 0) / 100) ?? 0; |
| | | row.totalPrice = taxInclusivePrice - row.taxPrice; |
| | | } |
| | | } |
| | | |
| | | /** 处理行数据变更 */ |
| | | function handleRowChange(row: any) { |
| | | recalcRow(row); |
| | | const index = tableData.value.findIndex((item) => item.seq === row.seq); |
| | | if (index === -1) { |
| | | tableData.value.push(row); |
| | | } else { |
| | | tableData.value[index] = row; |
| | | } |
| | | // 计算预估金额 |
| | | if (row.productPrice && row.count) { |
| | | row.totalPrice = erpPriceMultiply(row.productPrice, row.count) ?? 0; |
| | | } |
| | | emit('update:items', [...tableData.value]); |
| | | } |
| | | |
| | | /** 初始化行数据 */ |
| | | /** |
| | | * 初始化行数据 |
| | | * 已有单据的明细行(带 id)沿用后端返回的金额,避免历史单据被新口径覆盖; |
| | | * 只有未保存的新增行才按新口径重算。 |
| | | */ |
| | | function initRow(row: ErpPurchaseRequestApi.PurchaseRequestItem) { |
| | | if (row.productPrice && row.count) { |
| | | row.totalPrice = erpPriceMultiply(row.productPrice, row.count) ?? 0; |
| | | if (!row.id) { |
| | | recalcRow(row); |
| | | } |
| | | } |
| | | |
| | |
| | | }); |
| | | |
| | | /** 初始化 */ |
| | | onMounted(async () => { |
| | | const res = await getItemPage({ pageNo: 1, pageSize: 100, status: 0 }); |
| | | productOptions.value = res.list || []; |
| | | onMounted(() => { |
| | | if (tableData.value.length === 0) { |
| | | handleAdd(); |
| | | } |
| | |
| | | <template> |
| | | <Grid class="w-full"> |
| | | <template #productId="{ row }"> |
| | | <Select |
| | | <MdmItemSelect |
| | | v-if="!disabled" |
| | | v-model:value="row.productId" |
| | | :options="productOptions" |
| | | :field-names="{ label: 'name', value: 'id' }" |
| | | class="w-full" |
| | | :model-value="row.productId" |
| | | placeholder="请选择产品" |
| | | show-search |
| | | @update:model-value="row.productId = $event" |
| | | @change="handleProductChange($event, row)" |
| | | /> |
| | | <span v-else>{{ row.productName || '-' }}</span> |
| | |
| | | /> |
| | | <span v-else>{{ row.taxPercent ? `${row.taxPercent}%` : '-' }}</span> |
| | | </template> |
| | | <template #totalTaxInclusivePrice="{ row }"> |
| | | <span>{{ erpPriceInputFormatter((row.totalPrice || 0) + (row.taxPrice || 0)) }}</span> |
| | | </template> |
| | | <template #demandTime="{ row }"> |
| | | <DatePicker |
| | | v-if="!disabled" |
| | |
| | | <template #remark="{ row }"> |
| | | <Input v-if="!disabled" v-model:value="row.remark" class="w-full" @change="handleRowChange(row)" /> |
| | | <span v-else>{{ row.remark || '-' }}</span> |
| | | </template> |
| | | <template #qcCheckFlag="{ row }"> |
| | | <Switch |
| | | v-if="!disabled" |
| | | v-model:checked="row.qcCheckFlag" |
| | | checked-children="是" |
| | | un-checked-children="否" |
| | | @change="handleRowChange(row)" |
| | | /> |
| | | <span v-else>{{ row.qcCheckFlag ? '是' : '否' }}</span> |
| | | </template> |
| | | <template #actions="{ row }"> |
| | | <TableAction |
| | |
| | | <span class="font-medium text-foreground">合计:</span> |
| | | <div class="flex space-x-4"> |
| | | <span>数量:{{ erpCountInputFormatter(summaries.count) }}</span> |
| | | <span>预估金额:{{ erpPriceInputFormatter(summaries.totalPrice) }}</span> |
| | | <span>不含税金额:{{ erpPriceInputFormatter(summaries.totalPrice) }}</span> |
| | | <span>税额:{{ erpPriceInputFormatter(summaries.taxPrice) }}</span> |
| | | <span>含税合计:{{ erpPriceInputFormatter(totalTaxInclusivePrice) }}</span> |
| | | </div> |
| | | </div> |
| | | </div> |