| | |
| | | <script lang="ts" setup> |
| | | import type { FormType } from '../data'; |
| | | |
| | | import type { ErpPurchaseOrderApi } from '#/api/erp/purchase/order'; |
| | | import type { ErpPurchaseOrderApi, ProcessDefinition } from '#/api/erp/purchase/order'; |
| | | |
| | | import { computed, ref } from 'vue'; |
| | | |
| | |
| | | import { getAccountSimpleList } from '#/api/erp/finance/account'; |
| | | import { |
| | | createPurchaseOrder, |
| | | getPurchaseApproveProcessList, |
| | | getPurchaseOrder, |
| | | updatePurchaseOrder, |
| | | } from '#/api/erp/purchase/order'; |
| | |
| | | |
| | | import { useFormSchema } from '../data'; |
| | | import PurchaseOrderItemForm from './item-form.vue'; |
| | | import ProcessSelectModal from './process-select-modal.vue'; |
| | | |
| | | const emit = defineEmits(['success']); |
| | | const formData = ref<ErpPurchaseOrderApi.PurchaseOrder>(); |
| | | const formType = ref<FormType>('create'); // 表单类型:'create' | 'edit' | 'detail' |
| | | const formType = ref<FormType>('create'); |
| | | const itemFormRef = ref<InstanceType<typeof PurchaseOrderItemForm>>(); |
| | | const selectedProcessId = ref<string>(''); |
| | | |
| | | const getTitle = computed(() => { |
| | | if (formType.value === 'create') { |
| | |
| | | schema: useFormSchema(formType.value), |
| | | showDefaultActions: false, |
| | | handleValuesChange: (values, changedFields) => { |
| | | // 目的:同步到 item-form 组件,触发整体的价格计算 |
| | | if (formData.value && changedFields.includes('discountPercent')) { |
| | | formData.value.discountPercent = values.discountPercent; |
| | | } |
| | | }, |
| | | }); |
| | | |
| | | const [ProcessModal, processModalApi] = useVbenModal({ |
| | | connectedComponent: ProcessSelectModal, |
| | | destroyOnClose: true, |
| | | }); |
| | | |
| | | /** 更新采购订单项 */ |
| | |
| | | }); |
| | | } |
| | | |
| | | /** 流程选择成功 */ |
| | | function handleProcessSelectSuccess(processId: string) { |
| | | selectedProcessId.value = processId; |
| | | submitOrder(); |
| | | } |
| | | |
| | | /** 提交订单 */ |
| | | async function submitOrder() { |
| | | const data = (await formApi.getValues()) as ErpPurchaseOrderApi.PurchaseOrder; |
| | | data.items = formData.value?.items?.map((item) => ({ |
| | | ...item, |
| | | id: undefined, |
| | | })); |
| | | if (data.fileUrl && Array.isArray(data.fileUrl)) { |
| | | data.fileUrl = data.fileUrl.length > 0 ? data.fileUrl[0] : ''; |
| | | } |
| | | if (selectedProcessId.value) { |
| | | data.processDefinitionId = selectedProcessId.value; |
| | | } |
| | | await (formType.value === 'create' |
| | | ? createPurchaseOrder(data) |
| | | : updatePurchaseOrder(data)); |
| | | await modalApi.close(); |
| | | emit('success'); |
| | | message.success($t('ui.actionMessage.operationSuccess')); |
| | | } |
| | | |
| | | /** 创建或更新采购订单 */ |
| | | const [Modal, modalApi] = useVbenModal({ |
| | | async onConfirm() { |
| | |
| | | } |
| | | |
| | | modalApi.lock(); |
| | | // 提交表单 |
| | | const data = |
| | | (await formApi.getValues()) as ErpPurchaseOrderApi.PurchaseOrder; |
| | | data.items = formData.value?.items?.map((item) => ({ |
| | | ...item, |
| | | // 解决新增采购订单报错 |
| | | id: undefined, |
| | | })); |
| | | // 将文件数组转换为字符串 |
| | | if (data.fileUrl && Array.isArray(data.fileUrl)) { |
| | | data.fileUrl = data.fileUrl.length > 0 ? data.fileUrl[0] : ''; |
| | | } |
| | | try { |
| | | await (formType.value === 'create' |
| | | ? createPurchaseOrder(data) |
| | | : updatePurchaseOrder(data)); |
| | | // 关闭并提示 |
| | | await modalApi.close(); |
| | | emit('success'); |
| | | message.success($t('ui.actionMessage.operationSuccess')); |
| | | } finally { |
| | | modalApi.unlock(); |
| | | |
| | | if (formType.value === 'create') { |
| | | try { |
| | | const processes = await getPurchaseApproveProcessList(); |
| | | if (processes.length === 1) { |
| | | selectedProcessId.value = processes[0].id; |
| | | await submitOrder(); |
| | | } else if (processes.length > 1) { |
| | | processModalApi.setData(processes).open(); |
| | | } else { |
| | | message.error('采购审核分类下没有可用的流程模型,请先创建并发布流程'); |
| | | } |
| | | } catch (error: any) { |
| | | message.error(error.message || '获取流程列表失败'); |
| | | } finally { |
| | | modalApi.unlock(); |
| | | } |
| | | } else { |
| | | try { |
| | | await submitOrder(); |
| | | } finally { |
| | | modalApi.unlock(); |
| | | } |
| | | } |
| | | }, |
| | | async onOpenChange(isOpen: boolean) { |
| | | if (!isOpen) { |
| | | formData.value = undefined; |
| | | selectedProcessId.value = ''; |
| | | return; |
| | | } |
| | | // 加载数据 |
| | | const data = modalApi.getData<{ formType: FormType; id?: number }>(); |
| | | formType.value = data.formType; |
| | | formApi.setDisabled(formType.value === 'detail'); |
| | | formApi.updateSchema(useFormSchema(formType.value)); |
| | | if (!data || !data.id) { |
| | | // 新增时,默认选中账户 |
| | | const accountList = await getAccountSimpleList(); |
| | | const defaultAccount = accountList.find((item) => item.defaultStatus); |
| | | if (defaultAccount) { |
| | |
| | | modalApi.lock(); |
| | | try { |
| | | formData.value = await getPurchaseOrder(data.id); |
| | | // 设置到 values |
| | | await formApi.setValues(formData.value); |
| | | } finally { |
| | | modalApi.unlock(); |
| | |
| | | class="w-3/4" |
| | | :show-confirm-button="formType !== 'detail'" |
| | | > |
| | | <ProcessModal @success="handleProcessSelectSuccess" /> |
| | | <Form class="mx-3"> |
| | | <template #items> |
| | | <PurchaseOrderItemForm |