feat:【antd】【erp 系统】purchase/in 重构 1/4
This commit is contained in:
@@ -3,7 +3,7 @@ import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||
|
||||
import { DICT_TYPE } from '@vben/constants';
|
||||
import { getDictOptions } from '@vben/hooks';
|
||||
import { erpPriceInputFormatter } from '@vben/utils';
|
||||
import { erpNumberFormatter, erpPriceInputFormatter } from '@vben/utils';
|
||||
|
||||
import { z } from '#/adapter/form';
|
||||
import { getAccountSimpleList } from '#/api/erp/finance/account';
|
||||
@@ -11,31 +11,55 @@ import { getProductSimpleList } from '#/api/erp/product/product';
|
||||
import { getSupplierSimpleList } from '#/api/erp/purchase/supplier';
|
||||
import { getWarehouseSimpleList } from '#/api/erp/stock/warehouse';
|
||||
import { getSimpleUserList } from '#/api/system/user';
|
||||
import { getRangePickerDefaultProps } from '#/utils';
|
||||
|
||||
/** 表单的配置项 */
|
||||
export function useFormSchema(formType: string): VbenFormSchema[] {
|
||||
return [
|
||||
{
|
||||
component: 'Input',
|
||||
componentProps: {
|
||||
style: { display: 'none' },
|
||||
},
|
||||
fieldName: 'id',
|
||||
label: 'ID',
|
||||
hideLabel: true,
|
||||
formItemClass: 'hidden',
|
||||
component: 'Input',
|
||||
dependencies: {
|
||||
triggerFields: [''],
|
||||
show: () => false,
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'no',
|
||||
label: '入库单号',
|
||||
component: 'Input',
|
||||
componentProps: {
|
||||
placeholder: '系统自动生成',
|
||||
disabled: true,
|
||||
},
|
||||
fieldName: 'no',
|
||||
label: '入库单号',
|
||||
},
|
||||
|
||||
{
|
||||
fieldName: 'inTime',
|
||||
label: '入库时间',
|
||||
component: 'DatePicker',
|
||||
componentProps: {
|
||||
disabled: formType === 'detail',
|
||||
placeholder: '选择入库时间',
|
||||
showTime: true,
|
||||
format: 'YYYY-MM-DD HH:mm:ss',
|
||||
valueFormat: 'x',
|
||||
},
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
fieldName: 'orderNo',
|
||||
label: '关联订单',
|
||||
component: 'Input',
|
||||
formItemClass: 'col-span-1',
|
||||
rules: 'required',
|
||||
componentProps: {
|
||||
placeholder: '请选择关联订单',
|
||||
disabled: formType === 'detail',
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'supplierId',
|
||||
label: '供应商',
|
||||
component: 'ApiSelect',
|
||||
componentProps: {
|
||||
disabled: true,
|
||||
@@ -48,53 +72,39 @@ export function useFormSchema(formType: string): VbenFormSchema[] {
|
||||
value: 'id',
|
||||
},
|
||||
},
|
||||
fieldName: 'supplierId',
|
||||
label: '供应商',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
fieldName: 'orderNo',
|
||||
label: '关联订单',
|
||||
component: 'Input',
|
||||
formItemClass: 'col-span-1',
|
||||
fieldName: 'purchaseUserId',
|
||||
label: '采购人员',
|
||||
component: 'ApiSelect',
|
||||
componentProps: {
|
||||
disabled: formType === 'detail',
|
||||
placeholder: '请选择关联订单',
|
||||
placeholder: '请选择采购人员',
|
||||
allowClear: true,
|
||||
showSearch: true,
|
||||
api: getSimpleUserList,
|
||||
fieldNames: {
|
||||
label: 'nickname',
|
||||
value: 'id',
|
||||
},
|
||||
},
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
component: 'DatePicker',
|
||||
componentProps: {
|
||||
disabled: formType === 'detail',
|
||||
placeholder: '选择订单时间',
|
||||
showTime: true,
|
||||
format: 'YYYY-MM-DD HH:mm:ss',
|
||||
valueFormat: 'x',
|
||||
style: { width: '100%' },
|
||||
},
|
||||
fieldName: 'inTime',
|
||||
label: '入库时间',
|
||||
rules: 'required',
|
||||
},
|
||||
|
||||
{
|
||||
fieldName: 'remark',
|
||||
label: '备注',
|
||||
component: 'Textarea',
|
||||
componentProps: {
|
||||
placeholder: '请输入备注',
|
||||
disabled: formType === 'detail',
|
||||
autoSize: { minRows: 1, maxRows: 1 },
|
||||
class: 'w-full',
|
||||
disabled: formType === 'detail',
|
||||
},
|
||||
fieldName: 'remark',
|
||||
label: '备注',
|
||||
formItemClass: 'col-span-2',
|
||||
},
|
||||
|
||||
{
|
||||
fieldName: 'fileUrl',
|
||||
label: '附件',
|
||||
component: 'FileUpload',
|
||||
componentProps: {
|
||||
disabled: formType === 'detail',
|
||||
maxNumber: 1,
|
||||
maxSize: 10,
|
||||
accept: [
|
||||
@@ -108,69 +118,74 @@ export function useFormSchema(formType: string): VbenFormSchema[] {
|
||||
'jpeg',
|
||||
'png',
|
||||
],
|
||||
showDescription: true,
|
||||
showDescription: formType !== 'detail',
|
||||
disabled: formType === 'detail',
|
||||
},
|
||||
fieldName: 'fileUrl',
|
||||
label: '附件',
|
||||
formItemClass: 'col-span-3',
|
||||
},
|
||||
{
|
||||
fieldName: 'product',
|
||||
label: '产品清单',
|
||||
fieldName: 'items',
|
||||
label: '入库产品清单',
|
||||
component: 'Input',
|
||||
formItemClass: 'col-span-3',
|
||||
},
|
||||
{
|
||||
component: 'InputNumber',
|
||||
fieldName: 'discountPercent',
|
||||
label: '优惠率(%)',
|
||||
component: 'InputNumber',
|
||||
componentProps: {
|
||||
placeholder: '优惠率',
|
||||
placeholder: '请输入优惠率',
|
||||
min: 0,
|
||||
max: 100,
|
||||
disabled: true,
|
||||
precision: 2,
|
||||
style: { width: '100%' },
|
||||
},
|
||||
|
||||
label: '优惠率(%)',
|
||||
rules: z.number().min(0).optional(),
|
||||
},
|
||||
{
|
||||
fieldName: 'discountPrice',
|
||||
label: '付款优惠',
|
||||
component: 'InputNumber',
|
||||
componentProps: {
|
||||
placeholder: '付款优惠',
|
||||
precision: 2,
|
||||
formatter: erpPriceInputFormatter,
|
||||
disabled: true,
|
||||
style: { width: '100%' },
|
||||
},
|
||||
fieldName: 'discountPrice',
|
||||
label: '付款优惠',
|
||||
},
|
||||
{
|
||||
fieldName: 'discountedPrice',
|
||||
label: '优惠后金额',
|
||||
component: 'InputNumber',
|
||||
componentProps: {
|
||||
placeholder: '优惠后金额',
|
||||
precision: 2,
|
||||
formatter: erpPriceInputFormatter,
|
||||
disabled: true,
|
||||
style: { width: '100%' },
|
||||
},
|
||||
fieldName: 'discountedPrice',
|
||||
label: '优惠后金额',
|
||||
dependencies: {
|
||||
triggerFields: ['totalPrice', 'otherPrice'],
|
||||
componentProps: (values) => {
|
||||
const totalPrice = values.totalPrice || 0;
|
||||
const otherPrice = values.otherPrice || 0;
|
||||
values.discountedPrice = totalPrice - otherPrice;
|
||||
return {};
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'otherPrice',
|
||||
label: '其他费用',
|
||||
component: 'InputNumber',
|
||||
componentProps: {
|
||||
disabled: formType === 'detail',
|
||||
placeholder: '请输入其他费用',
|
||||
precision: 2,
|
||||
formatter: erpPriceInputFormatter,
|
||||
style: { width: '100%' },
|
||||
},
|
||||
fieldName: 'otherPrice',
|
||||
label: '其他费用',
|
||||
},
|
||||
{
|
||||
fieldName: 'accountId',
|
||||
label: '结算账户',
|
||||
component: 'ApiSelect',
|
||||
componentProps: {
|
||||
placeholder: '请选择结算账户',
|
||||
@@ -183,26 +198,25 @@ export function useFormSchema(formType: string): VbenFormSchema[] {
|
||||
value: 'id',
|
||||
},
|
||||
},
|
||||
fieldName: 'accountId',
|
||||
label: '结算账户',
|
||||
},
|
||||
{
|
||||
fieldName: 'totalPrice',
|
||||
label: '应付金额',
|
||||
component: 'InputNumber',
|
||||
componentProps: {
|
||||
precision: 2,
|
||||
style: { width: '100%' },
|
||||
disabled: true,
|
||||
min: 0,
|
||||
disabled: true,
|
||||
},
|
||||
fieldName: 'totalPrice',
|
||||
label: '应付金额',
|
||||
rules: z.number().min(0).optional(),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/** 采购订单项表格列定义 */
|
||||
export function usePurchaseOrderItemTableColumns(): VxeTableGridOptions['columns'] {
|
||||
/** 表单的明细表格列 */
|
||||
export function useFormItemColumns(
|
||||
formData?: any[],
|
||||
): VxeTableGridOptions['columns'] {
|
||||
return [
|
||||
{ type: 'seq', title: '序号', minWidth: 50, fixed: 'left' },
|
||||
{
|
||||
@@ -219,7 +233,7 @@ export function usePurchaseOrderItemTableColumns(): VxeTableGridOptions['columns
|
||||
},
|
||||
{
|
||||
field: 'stockCount',
|
||||
title: '仓库库存',
|
||||
title: '库存',
|
||||
minWidth: 80,
|
||||
},
|
||||
{
|
||||
@@ -232,15 +246,27 @@ export function usePurchaseOrderItemTableColumns(): VxeTableGridOptions['columns
|
||||
title: '单位',
|
||||
minWidth: 80,
|
||||
},
|
||||
{
|
||||
field: 'remark',
|
||||
title: '备注',
|
||||
minWidth: 150,
|
||||
slots: { default: 'remark' },
|
||||
},
|
||||
{
|
||||
field: 'totalCount',
|
||||
title: '原数量',
|
||||
formatter: 'formatAmount3',
|
||||
minWidth: 120,
|
||||
fixed: 'right',
|
||||
visible: formData && formData[0]?.inCount !== undefined,
|
||||
},
|
||||
{
|
||||
field: 'inCount',
|
||||
title: '已入库数量',
|
||||
title: '已入库',
|
||||
formatter: 'formatAmount3',
|
||||
minWidth: 120,
|
||||
fixed: 'right',
|
||||
visible: formData && formData[0]?.returnCount !== undefined,
|
||||
},
|
||||
{
|
||||
field: 'count',
|
||||
@@ -267,7 +293,8 @@ export function usePurchaseOrderItemTableColumns(): VxeTableGridOptions['columns
|
||||
fixed: 'right',
|
||||
field: 'taxPercent',
|
||||
title: '税率(%)',
|
||||
minWidth: 100,
|
||||
minWidth: 105,
|
||||
slots: { default: 'taxPercent' },
|
||||
},
|
||||
{
|
||||
fixed: 'right',
|
||||
@@ -283,9 +310,22 @@ export function usePurchaseOrderItemTableColumns(): VxeTableGridOptions['columns
|
||||
minWidth: 120,
|
||||
formatter: 'formatAmount2',
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
width: 50,
|
||||
fixed: 'right',
|
||||
slots: { default: 'actions' },
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/** 采购订单项表格列(兼容旧代码) */
|
||||
export function usePurchaseOrderItemTableColumns(
|
||||
formData?: any[],
|
||||
): VxeTableGridOptions['columns'] {
|
||||
return useFormItemColumns(formData);
|
||||
}
|
||||
|
||||
/** 列表的搜索表单 */
|
||||
export function useGridFormSchema(): VbenFormSchema[] {
|
||||
return [
|
||||
@@ -318,10 +358,8 @@ export function useGridFormSchema(): VbenFormSchema[] {
|
||||
label: '入库时间',
|
||||
component: 'RangePicker',
|
||||
componentProps: {
|
||||
placeholder: ['开始时间', '结束时间'],
|
||||
showTime: true,
|
||||
format: 'YYYY-MM-DD HH:mm:ss',
|
||||
valueFormat: 'YYYY-MM-DD HH:mm:ss',
|
||||
...getRangePickerDefaultProps(),
|
||||
allowClear: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -386,18 +424,18 @@ export function useGridFormSchema(): VbenFormSchema[] {
|
||||
{ label: '部分付款', value: 1 },
|
||||
{ label: '全部付款', value: 2 },
|
||||
],
|
||||
placeholder: '请选择退货状态',
|
||||
placeholder: '请选择付款状态',
|
||||
allowClear: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'status',
|
||||
label: '状态',
|
||||
label: '审批状态',
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
placeholder: '请选择状态',
|
||||
allowClear: true,
|
||||
options: getDictOptions(DICT_TYPE.ERP_AUDIT_STATUS, 'number'),
|
||||
placeholder: '请选择审批状态',
|
||||
allowClear: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -441,7 +479,7 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
|
||||
field: 'inTime',
|
||||
title: '入库时间',
|
||||
width: 160,
|
||||
formatter: 'formatDateTime',
|
||||
formatter: 'formatDate',
|
||||
},
|
||||
{
|
||||
field: 'creatorName',
|
||||
@@ -466,9 +504,17 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
|
||||
formatter: 'formatAmount2',
|
||||
minWidth: 120,
|
||||
},
|
||||
{
|
||||
field: 'unPaymentPrice',
|
||||
title: '未付金额',
|
||||
formatter: ({ row }) => {
|
||||
return `${erpNumberFormatter(row.totalPrice - row.paymentPrice, 2)}元`;
|
||||
},
|
||||
minWidth: 120,
|
||||
},
|
||||
{
|
||||
field: 'status',
|
||||
title: '状态',
|
||||
title: '审批状态',
|
||||
minWidth: 120,
|
||||
cellRender: {
|
||||
name: 'CellDict',
|
||||
@@ -477,7 +523,7 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
minWidth: 250,
|
||||
width: 220,
|
||||
fixed: 'right',
|
||||
slots: { default: 'actions' },
|
||||
},
|
||||
@@ -493,7 +539,6 @@ export function useOrderGridFormSchema(): VbenFormSchema[] {
|
||||
componentProps: {
|
||||
placeholder: '请输入订单单号',
|
||||
allowClear: true,
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -516,10 +561,8 @@ export function useOrderGridFormSchema(): VbenFormSchema[] {
|
||||
label: '订单时间',
|
||||
component: 'RangePicker',
|
||||
componentProps: {
|
||||
placeholder: ['开始时间', '结束时间'],
|
||||
showTime: true,
|
||||
format: 'YYYY-MM-DD HH:mm:ss',
|
||||
valueFormat: 'YYYY-MM-DD HH:mm:ss',
|
||||
...getRangePickerDefaultProps(),
|
||||
allowClear: true,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
@@ -19,85 +19,79 @@ import {
|
||||
import { $t } from '#/locales';
|
||||
|
||||
import { useGridColumns, useGridFormSchema } from './data';
|
||||
import PurchaseInForm from './modules/purchase-in-form.vue';
|
||||
import Form from './modules/form.vue';
|
||||
|
||||
/** ERP 采购入库列表 */
|
||||
defineOptions({ name: 'ErpPurchaseIn' });
|
||||
|
||||
const [FormModal, formModalApi] = useVbenModal({
|
||||
connectedComponent: PurchaseInForm,
|
||||
connectedComponent: Form,
|
||||
destroyOnClose: true,
|
||||
});
|
||||
|
||||
/** 刷新表格 */
|
||||
function onRefresh() {
|
||||
function handleRefresh() {
|
||||
gridApi.query();
|
||||
}
|
||||
|
||||
// TODO @Xuzhiqiang:批量删除待实现
|
||||
const checkedIds = ref<number[]>([]);
|
||||
|
||||
/** 详情 */
|
||||
function handleDetail(row: ErpPurchaseInApi.PurchaseIn) {
|
||||
formModalApi.setData({ type: 'detail', id: row.id }).open();
|
||||
/** 导出表格 */
|
||||
async function handleExport() {
|
||||
const data = await exportPurchaseIn(await gridApi.formApi.getValues());
|
||||
downloadFileFromBlobPart({ fileName: '采购入库.xls', source: data });
|
||||
}
|
||||
|
||||
/** 新增 */
|
||||
/** 新增采购入库 */
|
||||
function handleCreate() {
|
||||
formModalApi.setData({ type: 'create' }).open();
|
||||
}
|
||||
|
||||
/** 编辑 */
|
||||
/** 编辑采购入库 */
|
||||
function handleEdit(row: ErpPurchaseInApi.PurchaseIn) {
|
||||
formModalApi.setData({ type: 'edit', id: row.id }).open();
|
||||
}
|
||||
|
||||
/** 删除 */
|
||||
/** 删除采购入库 */
|
||||
async function handleDelete(ids: number[]) {
|
||||
const hideLoading = message.loading({
|
||||
content: $t('ui.actionMessage.deleting'),
|
||||
duration: 0,
|
||||
key: 'action_process_msg',
|
||||
});
|
||||
try {
|
||||
await deletePurchaseIn(ids);
|
||||
message.success({
|
||||
content: $t('ui.actionMessage.deleteSuccess'),
|
||||
key: 'action_process_msg',
|
||||
});
|
||||
onRefresh();
|
||||
message.success($t('ui.actionMessage.deleteSuccess'));
|
||||
handleRefresh();
|
||||
} finally {
|
||||
hideLoading();
|
||||
}
|
||||
}
|
||||
|
||||
/** 审批/反审批操作 */
|
||||
function handleUpdateStatus(row: ErpPurchaseInApi.PurchaseIn, status: number) {
|
||||
async function handleUpdateStatus(row: ErpPurchaseInApi.PurchaseIn, status: number) {
|
||||
const hideLoading = message.loading({
|
||||
content: `确定${status === 20 ? '审批' : '反审批'}该订单吗?`,
|
||||
duration: 0,
|
||||
key: 'action_process_msg',
|
||||
});
|
||||
updatePurchaseInStatus(row.id!, status)
|
||||
.then(() => {
|
||||
message.success({
|
||||
content: `${status === 20 ? '审批' : '反审批'}成功`,
|
||||
key: 'action_process_msg',
|
||||
});
|
||||
onRefresh();
|
||||
})
|
||||
.catch(() => {
|
||||
// 处理错误
|
||||
})
|
||||
.finally(() => {
|
||||
hideLoading();
|
||||
});
|
||||
try {
|
||||
await updatePurchaseInStatus(row.id!, status);
|
||||
message.success(`${status === 20 ? '审批' : '反审批'}成功`);
|
||||
handleRefresh();
|
||||
} finally {
|
||||
hideLoading();
|
||||
}
|
||||
}
|
||||
|
||||
/** 导出 */
|
||||
async function handleExport() {
|
||||
const data = await exportPurchaseIn(await gridApi.formApi.getValues());
|
||||
downloadFileFromBlobPart({ fileName: '采购入库.xls', source: data });
|
||||
const checkedIds = ref<number[]>([]);
|
||||
function handleRowCheckboxChange({
|
||||
records,
|
||||
}: {
|
||||
records: ErpPurchaseInApi.PurchaseIn[];
|
||||
}) {
|
||||
checkedIds.value = records.map((item) => item.id!);
|
||||
}
|
||||
|
||||
/** 查看详情 */
|
||||
function handleDetail(row: ErpPurchaseInApi.PurchaseIn) {
|
||||
formModalApi.setData({ type: 'detail', id: row.id }).open();
|
||||
}
|
||||
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
@@ -128,6 +122,10 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
search: true,
|
||||
},
|
||||
} as VxeTableGridOptions<ErpPurchaseInApi.PurchaseIn>,
|
||||
gridEvents: {
|
||||
checkboxAll: handleRowCheckboxChange,
|
||||
checkboxChange: handleRowCheckboxChange,
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -139,8 +137,8 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
url="https://doc.iocoder.cn/erp/purchase/"
|
||||
/>
|
||||
</template>
|
||||
<FormModal @success="onRefresh" />
|
||||
|
||||
<FormModal @success="handleRefresh" />
|
||||
<Grid table-title="采购入库列表">
|
||||
<template #toolbar-tools>
|
||||
<TableAction
|
||||
@@ -168,14 +166,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
auth: ['erp:purchase-in:delete'],
|
||||
popConfirm: {
|
||||
title: `是否删除所选中数据?`,
|
||||
confirm: () => {
|
||||
const checkboxRecords = gridApi.grid.getCheckboxRecords();
|
||||
if (checkboxRecords.length === 0) {
|
||||
message.warning('请选择要删除的数据');
|
||||
return;
|
||||
}
|
||||
handleDelete(checkboxRecords.map((item) => item.id!));
|
||||
},
|
||||
confirm: handleDelete.bind(null, checkedIds),
|
||||
},
|
||||
},
|
||||
]"
|
||||
@@ -220,7 +211,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
auth: ['erp:purchase-in:delete'],
|
||||
popConfirm: {
|
||||
title: $t('ui.actionMessage.deleteConfirm', [row.no]),
|
||||
confirm: () => handleDelete([row.id!]),
|
||||
confirm: handleDelete.bind(null, [row.id!]),
|
||||
},
|
||||
},
|
||||
]"
|
||||
|
||||
223
apps/web-antd/src/views/erp/purchase/in/modules/form.vue
Normal file
223
apps/web-antd/src/views/erp/purchase/in/modules/form.vue
Normal file
@@ -0,0 +1,223 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ErpPurchaseInApi } from '#/api/erp/purchase/in';
|
||||
import type { ErpPurchaseOrderApi } from '#/api/erp/purchase/order';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
import { $t } from '@vben/locales';
|
||||
|
||||
import { message } from 'ant-design-vue';
|
||||
|
||||
import { useVbenForm } from '#/adapter/form';
|
||||
import {
|
||||
createPurchaseIn,
|
||||
getPurchaseIn,
|
||||
updatePurchaseIn,
|
||||
} from '#/api/erp/purchase/in';
|
||||
|
||||
import { useFormSchema } from '../data';
|
||||
import ItemForm from './item-form.vue';
|
||||
import PurchaseOrderSelect from './purchase-order-select.vue';
|
||||
|
||||
const emit = defineEmits(['success']);
|
||||
const formData = ref<
|
||||
ErpPurchaseInApi.PurchaseIn & {
|
||||
accountId?: number;
|
||||
customerId?: number;
|
||||
discountPercent?: number;
|
||||
fileUrl?: string;
|
||||
order?: ErpPurchaseOrderApi.PurchaseOrder;
|
||||
orderId?: number;
|
||||
orderNo?: string;
|
||||
}
|
||||
>({
|
||||
id: undefined,
|
||||
no: undefined,
|
||||
accountId: undefined,
|
||||
inTime: undefined,
|
||||
remark: undefined,
|
||||
fileUrl: undefined,
|
||||
discountPercent: 0,
|
||||
supplierId: undefined,
|
||||
discountPrice: 0,
|
||||
totalPrice: 0,
|
||||
otherPrice: 0,
|
||||
items: [],
|
||||
});
|
||||
const formType = ref(''); // 表单类型:'create' | 'edit' | 'detail'
|
||||
const itemFormRef = ref<InstanceType<typeof ItemForm>>();
|
||||
|
||||
/* eslint-disable unicorn/no-nested-ternary */
|
||||
const getTitle = computed(() =>
|
||||
formType.value === 'create'
|
||||
? $t('ui.actionTitle.create', ['采购入库'])
|
||||
: formType.value === 'edit'
|
||||
? $t('ui.actionTitle.edit', ['采购入库'])
|
||||
: '采购入库详情',
|
||||
);
|
||||
|
||||
const [Form, formApi] = useVbenForm({
|
||||
commonConfig: {
|
||||
componentProps: {
|
||||
class: 'w-full',
|
||||
},
|
||||
labelWidth: 120,
|
||||
},
|
||||
wrapperClass: 'grid-cols-3',
|
||||
layout: 'vertical',
|
||||
schema: useFormSchema(formType.value),
|
||||
showDefaultActions: false,
|
||||
handleValuesChange: (values, changedFields) => {
|
||||
// 目的:同步到 item-form 组件,触发整体的价格计算
|
||||
if (formData.value) {
|
||||
if (changedFields.includes('otherPrice')) {
|
||||
formData.value.otherPrice = values.otherPrice;
|
||||
}
|
||||
if (changedFields.includes('discountPercent')) {
|
||||
formData.value.discountPercent = values.discountPercent;
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
/** 更新采购入库项 */
|
||||
const handleUpdateItems = (items: ErpPurchaseInApi.PurchaseInItem[]) => {
|
||||
formData.value.items = items;
|
||||
formApi.setValues({
|
||||
items,
|
||||
});
|
||||
};
|
||||
|
||||
/** 更新其他费用 */
|
||||
const handleUpdateOtherPrice = (otherPrice: number) => {
|
||||
formApi.setValues({
|
||||
otherPrice,
|
||||
});
|
||||
};
|
||||
|
||||
/** 更新优惠金额 */
|
||||
const handleUpdateDiscountPrice = (discountPrice: number) => {
|
||||
formApi.setValues({
|
||||
discountPrice,
|
||||
});
|
||||
};
|
||||
|
||||
/** 更新总金额 */
|
||||
const handleUpdateTotalPrice = (totalPrice: number) => {
|
||||
formApi.setValues({
|
||||
totalPrice,
|
||||
});
|
||||
};
|
||||
|
||||
/** 选择采购订单 */
|
||||
const handleUpdateOrder = (order: ErpPurchaseOrderApi.PurchaseOrder) => {
|
||||
formData.value = {
|
||||
...formData.value,
|
||||
orderId: order.id,
|
||||
orderNo: order.no!,
|
||||
supplierId: order.supplierId!,
|
||||
accountId: order.accountId!,
|
||||
remark: order.remark!,
|
||||
discountPercent: order.discountPercent!,
|
||||
fileUrl: order.fileUrl!,
|
||||
};
|
||||
// 将订单项设置到入库单项
|
||||
order.items!.forEach((item: any) => {
|
||||
item.totalCount = item.count;
|
||||
item.count = item.totalCount - item.inCount;
|
||||
item.orderItemId = item.id;
|
||||
item.id = undefined;
|
||||
});
|
||||
formData.value.items = order.items!.filter(
|
||||
(item) => item.count && item.count > 0,
|
||||
) as ErpPurchaseInApi.PurchaseInItem[];
|
||||
formApi.setValues(formData.value, false);
|
||||
};
|
||||
|
||||
/** 创建或更新采购入库 */
|
||||
const [Modal, modalApi] = useVbenModal({
|
||||
async onConfirm() {
|
||||
const { valid } = await formApi.validate();
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
const itemFormInstance = Array.isArray(itemFormRef.value)
|
||||
? itemFormRef.value[0]
|
||||
: itemFormRef.value;
|
||||
try {
|
||||
itemFormInstance.validate();
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '子表单验证失败');
|
||||
return;
|
||||
}
|
||||
|
||||
modalApi.lock();
|
||||
// 提交表单
|
||||
const data = (await formApi.getValues()) as ErpPurchaseInApi.PurchaseIn;
|
||||
try {
|
||||
await (formType.value === 'create'
|
||||
? createPurchaseIn(data)
|
||||
: updatePurchaseIn(data));
|
||||
// 关闭并提示
|
||||
await modalApi.close();
|
||||
emit('success');
|
||||
message.success($t('ui.actionMessage.operationSuccess'));
|
||||
} finally {
|
||||
modalApi.unlock();
|
||||
}
|
||||
},
|
||||
async onOpenChange(isOpen: boolean) {
|
||||
if (!isOpen) {
|
||||
formData.value = undefined;
|
||||
return;
|
||||
}
|
||||
// 加载数据
|
||||
const data = modalApi.getData<{ id?: number; type: string }>();
|
||||
formType.value = data.type;
|
||||
formApi.setDisabled(formType.value === 'detail');
|
||||
formApi.updateSchema(useFormSchema(formType.value));
|
||||
if (!data || !data.id) {
|
||||
return;
|
||||
}
|
||||
modalApi.lock();
|
||||
try {
|
||||
formData.value = await getPurchaseIn(data.id);
|
||||
// 设置到 values
|
||||
await formApi.setValues(formData.value, false);
|
||||
} finally {
|
||||
modalApi.unlock();
|
||||
}
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Modal
|
||||
:title="getTitle"
|
||||
class="w-3/4"
|
||||
:show-confirm-button="formType !== 'detail'"
|
||||
>
|
||||
<Form class="mx-3">
|
||||
<template #items>
|
||||
<ItemForm
|
||||
ref="itemFormRef"
|
||||
:items="formData?.items ?? []"
|
||||
:disabled="formType === 'detail'"
|
||||
:discount-percent="formData?.discountPercent ?? 0"
|
||||
:other-price="formData?.otherPrice ?? 0"
|
||||
@update:items="handleUpdateItems"
|
||||
@update:discount-price="handleUpdateDiscountPrice"
|
||||
@update:other-price="handleUpdateOtherPrice"
|
||||
@update:total-price="handleUpdateTotalPrice"
|
||||
/>
|
||||
</template>
|
||||
<template #orderNo>
|
||||
<PurchaseOrderSelect
|
||||
:order-no="formData?.orderNo"
|
||||
@update:order="handleUpdateOrder"
|
||||
/>
|
||||
</template>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
293
apps/web-antd/src/views/erp/purchase/in/modules/item-form.vue
Normal file
293
apps/web-antd/src/views/erp/purchase/in/modules/item-form.vue
Normal file
@@ -0,0 +1,293 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ErpPurchaseInApi } from '#/api/erp/purchase/in';
|
||||
|
||||
import { computed, nextTick, onMounted, ref, watch } from 'vue';
|
||||
|
||||
import {
|
||||
erpCountInputFormatter,
|
||||
erpPriceInputFormatter,
|
||||
erpPriceMultiply,
|
||||
} from '@vben/utils';
|
||||
|
||||
import { Input, InputNumber, Select } from 'ant-design-vue';
|
||||
|
||||
import { TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import { getProductSimpleList } from '#/api/erp/product/product';
|
||||
import { getWarehouseStockCount } from '#/api/erp/stock/stock';
|
||||
import { getWarehouseSimpleList } from '#/api/erp/stock/warehouse';
|
||||
|
||||
import { useFormItemColumns } from '../data';
|
||||
|
||||
interface Props {
|
||||
items?: ErpPurchaseInApi.PurchaseInItem[];
|
||||
disabled?: boolean;
|
||||
discountPercent?: number;
|
||||
otherPrice?: number;
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
items: () => [],
|
||||
disabled: false,
|
||||
discountPercent: 0,
|
||||
otherPrice: 0,
|
||||
});
|
||||
|
||||
const emit = defineEmits([
|
||||
'update:items',
|
||||
'update:discount-price',
|
||||
'update:other-price',
|
||||
'update:total-price',
|
||||
]);
|
||||
|
||||
const tableData = ref<ErpPurchaseInApi.PurchaseInItem[]>([]); // 表格数据
|
||||
const productOptions = ref<any[]>([]); // 产品下拉选项
|
||||
const warehouseOptions = ref<any[]>([]); // 仓库下拉选项
|
||||
|
||||
/** 获取表格合计数据 */
|
||||
const summaries = computed(() => {
|
||||
return {
|
||||
count: tableData.value.reduce((sum, item) => sum + (item.count || 0), 0),
|
||||
totalProductPrice: tableData.value.reduce(
|
||||
(sum, item) => sum + (item.totalProductPrice || 0),
|
||||
0,
|
||||
),
|
||||
taxPrice: tableData.value.reduce(
|
||||
(sum, item) => sum + (item.taxPrice || 0),
|
||||
0,
|
||||
),
|
||||
totalPrice: tableData.value.reduce(
|
||||
(sum, item) => sum + (item.totalPrice || 0),
|
||||
0,
|
||||
),
|
||||
};
|
||||
});
|
||||
|
||||
/** 表格配置 */
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
gridOptions: {
|
||||
columns: useFormItemColumns(),
|
||||
data: tableData.value,
|
||||
minHeight: 250,
|
||||
autoResize: true,
|
||||
border: true,
|
||||
rowConfig: {
|
||||
keyField: 'row_id',
|
||||
isHover: true,
|
||||
},
|
||||
pagerConfig: {
|
||||
enabled: false,
|
||||
},
|
||||
toolbarConfig: {
|
||||
enabled: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
/** 监听外部传入的列数据 */
|
||||
watch(
|
||||
() => props.items,
|
||||
async (items) => {
|
||||
if (!items) {
|
||||
return;
|
||||
}
|
||||
items.forEach((item) => initRow(item));
|
||||
tableData.value = [...items];
|
||||
await nextTick(); // 特殊:保证 gridApi 已经初始化
|
||||
await gridApi.grid.reloadData(tableData.value);
|
||||
// 更新表格列配置(目的:原数量、已入库动态列)
|
||||
const columns = useFormItemColumns(tableData.value);
|
||||
await gridApi.grid.reloadColumn(columns);
|
||||
},
|
||||
{
|
||||
immediate: true,
|
||||
},
|
||||
);
|
||||
|
||||
/** 计算 discountPrice、otherPrice、totalPrice 价格 */
|
||||
watch(
|
||||
() => [tableData.value, props.discountPercent, props.otherPrice],
|
||||
() => {
|
||||
if (!tableData.value || tableData.value.length === 0) {
|
||||
return;
|
||||
}
|
||||
const totalPrice = tableData.value.reduce(
|
||||
(prev, curr) => prev + (curr.totalPrice || 0),
|
||||
0,
|
||||
);
|
||||
const discountPrice =
|
||||
props.discountPercent === null
|
||||
? 0
|
||||
: erpPriceMultiply(totalPrice, props.discountPercent / 100);
|
||||
const discountedPrice = totalPrice - discountPrice!;
|
||||
const finalTotalPrice = discountedPrice + (props.otherPrice || 0);
|
||||
|
||||
// 通知父组件更新
|
||||
emit('update:discount-price', discountPrice);
|
||||
emit('update:other-price', props.otherPrice || 0);
|
||||
emit('update:total-price', finalTotalPrice);
|
||||
},
|
||||
{ deep: true },
|
||||
);
|
||||
|
||||
/** 处理删除 */
|
||||
function handleDelete(row: ErpPurchaseInApi.PurchaseInItem) {
|
||||
const index = tableData.value.findIndex((item) => item.id === row.id);
|
||||
if (index !== -1) {
|
||||
tableData.value.splice(index, 1);
|
||||
}
|
||||
// 通知父组件更新
|
||||
emit('update:items', [...tableData.value]);
|
||||
}
|
||||
|
||||
/** 处理仓库变更 */
|
||||
const handleWarehouseChange = async (row: ErpPurchaseInApi.PurchaseInItem) => {
|
||||
const stockCount = await getWarehouseStockCount({
|
||||
productId: row.productId!,
|
||||
warehouseId: row.warehouseId!,
|
||||
});
|
||||
row.stockCount = stockCount || 0;
|
||||
handleRowChange(row);
|
||||
};
|
||||
|
||||
/** 处理行数据变更 */
|
||||
function handleRowChange(row: any) {
|
||||
const index = tableData.value.findIndex((item) => item.id === row.id);
|
||||
if (index === -1) {
|
||||
tableData.value.push(row);
|
||||
} else {
|
||||
tableData.value[index] = row;
|
||||
}
|
||||
emit('update:items', [...tableData.value]);
|
||||
}
|
||||
|
||||
/** 初始化行数据 */
|
||||
const initRow = (row: ErpPurchaseInApi.PurchaseInItem): void => {
|
||||
if (row.productPrice && row.count) {
|
||||
row.totalProductPrice = erpPriceMultiply(row.productPrice, row.count) ?? 0;
|
||||
row.taxPrice =
|
||||
erpPriceMultiply(row.totalProductPrice, (row.taxPercent || 0) / 100) ?? 0;
|
||||
row.totalPrice = row.totalProductPrice + row.taxPrice;
|
||||
}
|
||||
};
|
||||
|
||||
/** 表单校验 */
|
||||
function validate() {
|
||||
for (let i = 0; i < tableData.value.length; i++) {
|
||||
const item = tableData.value[i];
|
||||
if (item) {
|
||||
if (!item.warehouseId) {
|
||||
throw new Error(`第 ${i + 1} 行:仓库不能为空`);
|
||||
}
|
||||
if (!item.count || item.count <= 0) {
|
||||
throw new Error(`第 ${i + 1} 行:产品数量不能为空`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
validate,
|
||||
});
|
||||
|
||||
/** 初始化 */
|
||||
onMounted(async () => {
|
||||
productOptions.value = await getProductSimpleList();
|
||||
warehouseOptions.value = await getWarehouseSimpleList();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Grid class="w-full">
|
||||
<template #warehouseId="{ row }">
|
||||
<Select
|
||||
v-model:value="row.warehouseId"
|
||||
:options="warehouseOptions"
|
||||
:field-names="{ label: 'name', value: 'id' }"
|
||||
placeholder="请选择仓库"
|
||||
:disabled="disabled"
|
||||
show-search
|
||||
class="w-full"
|
||||
@change="handleWarehouseChange(row)"
|
||||
/>
|
||||
</template>
|
||||
<template #productId="{ row }">
|
||||
<Select
|
||||
disabled
|
||||
v-model:value="row.productId"
|
||||
:options="productOptions"
|
||||
:field-names="{ label: 'name', value: 'id' }"
|
||||
class="w-full"
|
||||
placeholder="请选择产品"
|
||||
show-search
|
||||
/>
|
||||
</template>
|
||||
<template #count="{ row }">
|
||||
<InputNumber
|
||||
v-if="!disabled"
|
||||
v-model:value="row.count"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
@change="handleRowChange(row)"
|
||||
/>
|
||||
<span v-else>{{ row.count || '-' }}</span>
|
||||
</template>
|
||||
<template #productPrice="{ row }">
|
||||
<InputNumber
|
||||
disabled
|
||||
v-model:value="row.productPrice"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
@change="handleRowChange(row)"
|
||||
/>
|
||||
</template>
|
||||
<template #remark="{ row }">
|
||||
<Input v-if="!disabled" v-model:value="row.remark" class="w-full" />
|
||||
<span v-else>{{ row.remark || '-' }}</span>
|
||||
</template>
|
||||
<template #taxPercent="{ row }">
|
||||
<InputNumber
|
||||
v-if="!disabled"
|
||||
v-model:value="row.taxPercent"
|
||||
:min="0"
|
||||
:max="100"
|
||||
:precision="2"
|
||||
@change="handleRowChange(row)"
|
||||
/>
|
||||
<span v-else>{{ row.taxPercent || '-' }}</span>
|
||||
</template>
|
||||
<template #actions="{ row }">
|
||||
<TableAction
|
||||
v-if="!disabled"
|
||||
:actions="[
|
||||
{
|
||||
label: '删除',
|
||||
type: 'link',
|
||||
danger: true,
|
||||
popConfirm: {
|
||||
title: '确认删除该产品吗?',
|
||||
confirm: handleDelete.bind(null, row),
|
||||
},
|
||||
},
|
||||
]"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<template #bottom>
|
||||
<div class="border-border bg-muted mt-2 rounded border p-2">
|
||||
<div class="text-muted-foreground flex justify-between text-sm">
|
||||
<span class="text-foreground font-medium">合计:</span>
|
||||
<div class="flex space-x-4">
|
||||
<span>数量:{{ erpCountInputFormatter(summaries.count) }}</span>
|
||||
<span>
|
||||
金额:{{ erpPriceInputFormatter(summaries.totalProductPrice) }}
|
||||
</span>
|
||||
<span>税额:{{ erpPriceInputFormatter(summaries.taxPrice) }}</span>
|
||||
<span>
|
||||
税额合计:{{ erpPriceInputFormatter(summaries.totalPrice) }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</Grid>
|
||||
</template>
|
||||
@@ -1,311 +0,0 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ErpPurchaseInApi } from '#/api/erp/purchase/in';
|
||||
import type { ErpPurchaseOrderApi } from '#/api/erp/purchase/order';
|
||||
|
||||
import { computed, nextTick, ref, watch } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
|
||||
import { message } from 'ant-design-vue';
|
||||
|
||||
import { useVbenForm } from '#/adapter/form';
|
||||
import {
|
||||
createPurchaseIn,
|
||||
getPurchaseIn,
|
||||
updatePurchaseIn,
|
||||
} from '#/api/erp/purchase/in';
|
||||
|
||||
import { useFormSchema } from '../data';
|
||||
import PurchaseInItemForm from './purchase-in-item-form.vue';
|
||||
import SelectPurchaseOrderForm from './select-purchase-order-form.vue';
|
||||
|
||||
const emit = defineEmits(['success']);
|
||||
const formData = ref<
|
||||
ErpPurchaseInApi.PurchaseIn & {
|
||||
accountId?: number;
|
||||
discountedPrice?: number;
|
||||
discountPercent?: number;
|
||||
fileUrl?: string;
|
||||
order?: ErpPurchaseOrderApi.PurchaseOrder;
|
||||
orderId?: number;
|
||||
orderNo?: string;
|
||||
supplierId?: number;
|
||||
}
|
||||
>({
|
||||
id: undefined,
|
||||
no: undefined,
|
||||
accountId: undefined,
|
||||
inTime: undefined,
|
||||
remark: undefined,
|
||||
fileUrl: undefined,
|
||||
discountPercent: 0,
|
||||
supplierId: undefined,
|
||||
discountPrice: 0,
|
||||
totalPrice: 0,
|
||||
otherPrice: 0,
|
||||
discountedPrice: 0,
|
||||
items: [],
|
||||
});
|
||||
const formType = ref('');
|
||||
const itemFormRef = ref();
|
||||
|
||||
const getTitle = computed(() => {
|
||||
if (formType.value === 'create') return '添加采购入库';
|
||||
if (formType.value === 'update') return '编辑采购入库';
|
||||
return '采购入库详情';
|
||||
});
|
||||
|
||||
const [Form, formApi] = useVbenForm({
|
||||
commonConfig: {
|
||||
componentProps: {
|
||||
class: 'w-full',
|
||||
},
|
||||
labelWidth: 120,
|
||||
},
|
||||
wrapperClass: 'grid-cols-3',
|
||||
layout: 'vertical',
|
||||
schema: useFormSchema(formType.value),
|
||||
showDefaultActions: false,
|
||||
handleValuesChange: (values, changedFields) => {
|
||||
if (formData.value && changedFields.includes('otherPrice')) {
|
||||
formData.value.otherPrice = values.otherPrice;
|
||||
formData.value.totalPrice =
|
||||
(formData.value.discountedPrice || 0) +
|
||||
(formData.value.otherPrice || 0);
|
||||
|
||||
formApi.setValues(formData.value, false);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// TODO @XuZhiqiang 注释风格:使用 /** */
|
||||
// 更新采购入库项
|
||||
const handleUpdateItems = async (items: ErpPurchaseInApi.PurchaseInItem[]) => {
|
||||
if (formData.value) {
|
||||
const data = await formApi.getValues();
|
||||
formData.value = { ...data, items };
|
||||
await formApi.setValues(formData.value, false);
|
||||
}
|
||||
};
|
||||
|
||||
// 选择采购订单
|
||||
const handleUpdateOrder = (order: ErpPurchaseOrderApi.PurchaseOrder) => {
|
||||
formData.value = {
|
||||
...formData.value,
|
||||
orderId: order.id,
|
||||
orderNo: order.no!,
|
||||
supplierId: order.supplierId!,
|
||||
accountId: order.accountId!,
|
||||
remark: order.remark!,
|
||||
discountPercent: order.discountPercent!,
|
||||
fileUrl: order.fileUrl!,
|
||||
};
|
||||
// 将订单项设置到入库单项
|
||||
order.items!.forEach((item: any) => {
|
||||
item.totalCount = item.count;
|
||||
item.count = item.totalCount - item.inCount;
|
||||
item.orderItemId = item.id;
|
||||
item.id = undefined;
|
||||
});
|
||||
formData.value.items = order.items!.filter(
|
||||
(item) => item.count && item.count > 0,
|
||||
) as ErpPurchaseInApi.PurchaseInItem[];
|
||||
|
||||
formApi.setValues(formData.value, false);
|
||||
};
|
||||
|
||||
watch(
|
||||
() => formData.value.items!,
|
||||
(newItems: ErpPurchaseInApi.PurchaseInItem[]) => {
|
||||
if (newItems && newItems.length > 0) {
|
||||
// 计算每个产品的总价、税额和总价
|
||||
newItems.forEach((item) => {
|
||||
item.totalProductPrice = (item.productPrice || 0) * (item.count || 0);
|
||||
item.taxPrice =
|
||||
(item.totalProductPrice || 0) * ((item.taxPercent || 0) / 100);
|
||||
item.totalPrice = (item.totalProductPrice || 0) + (item.taxPrice || 0);
|
||||
});
|
||||
// 计算总价
|
||||
formData.value.totalPrice = newItems.reduce((sum, item) => {
|
||||
return sum + (item.totalProductPrice || 0) + (item.taxPrice || 0);
|
||||
}, 0);
|
||||
} else {
|
||||
formData.value.totalPrice = 0;
|
||||
}
|
||||
// 优惠金额
|
||||
formData.value.discountPrice =
|
||||
((formData.value.totalPrice || 0) *
|
||||
(formData.value.discountPercent || 0)) /
|
||||
100;
|
||||
// 优惠后价格
|
||||
formData.value.discountedPrice =
|
||||
formData.value.totalPrice - formData.value.discountPrice;
|
||||
|
||||
// 计算最终价格(包含其他费用)
|
||||
formData.value.totalPrice =
|
||||
formData.value.discountedPrice + (formData.value.otherPrice || 0);
|
||||
formApi.setValues(formData.value, false);
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
/** 创建或更新采购入库 */
|
||||
const [Modal, modalApi] = useVbenModal({
|
||||
async onConfirm() {
|
||||
const { valid } = await formApi.validate();
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
await nextTick();
|
||||
|
||||
const itemFormInstance = Array.isArray(itemFormRef.value)
|
||||
? itemFormRef.value[0]
|
||||
: itemFormRef.value;
|
||||
if (itemFormInstance && typeof itemFormInstance.validate === 'function') {
|
||||
try {
|
||||
const isValid = await itemFormInstance.validate();
|
||||
if (!isValid) {
|
||||
message.error('子表单验证失败');
|
||||
return;
|
||||
}
|
||||
} catch (error: any) {
|
||||
message.error(error.message || '子表单验证失败');
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
message.error('子表单验证方法不存在');
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证产品清单不能为空
|
||||
if (!formData.value?.items || formData.value.items.length === 0) {
|
||||
message.error('产品清单不能为空,请至少添加一个产品');
|
||||
return;
|
||||
}
|
||||
|
||||
modalApi.lock();
|
||||
// 提交表单
|
||||
const data = (await formApi.getValues()) as ErpPurchaseInApi.PurchaseIn;
|
||||
data.items = formData.value?.items?.map((item) => ({
|
||||
...item,
|
||||
// 解决新增采购入库报错
|
||||
id: undefined,
|
||||
}));
|
||||
try {
|
||||
await (formType.value === 'create'
|
||||
? createPurchaseIn(data)
|
||||
: updatePurchaseIn(data));
|
||||
// 关闭并提示
|
||||
await modalApi.close();
|
||||
emit('success');
|
||||
message.success(formType.value === 'create' ? '新增成功' : '更新成功');
|
||||
} finally {
|
||||
modalApi.unlock();
|
||||
}
|
||||
},
|
||||
async onOpenChange(isOpen: boolean) {
|
||||
if (!isOpen) {
|
||||
formData.value = {
|
||||
id: undefined,
|
||||
no: undefined,
|
||||
accountId: undefined,
|
||||
inTime: undefined,
|
||||
remark: undefined,
|
||||
fileUrl: undefined,
|
||||
discountPercent: 0,
|
||||
supplierId: undefined,
|
||||
discountPrice: 0,
|
||||
totalPrice: 0,
|
||||
otherPrice: 0,
|
||||
items: [],
|
||||
};
|
||||
await formApi.setValues(formData.value, false);
|
||||
return;
|
||||
}
|
||||
// 加载数据
|
||||
const data = modalApi.getData<{ id?: number; type: string }>();
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
formType.value = data.type;
|
||||
formApi.updateSchema(useFormSchema(formType.value));
|
||||
|
||||
if (!data.id) {
|
||||
// 初始化空的表单数据
|
||||
formData.value = {
|
||||
id: undefined,
|
||||
no: undefined,
|
||||
accountId: undefined,
|
||||
inTime: undefined,
|
||||
remark: undefined,
|
||||
fileUrl: undefined,
|
||||
discountPercent: 0,
|
||||
supplierId: undefined,
|
||||
discountPrice: 0,
|
||||
totalPrice: 0,
|
||||
otherPrice: 0,
|
||||
items: [],
|
||||
} as unknown as ErpPurchaseInApi.PurchaseIn;
|
||||
await nextTick();
|
||||
const itemFormInstance = Array.isArray(itemFormRef.value)
|
||||
? itemFormRef.value[0]
|
||||
: itemFormRef.value;
|
||||
if (itemFormInstance && typeof itemFormInstance.init === 'function') {
|
||||
itemFormInstance.init([]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
modalApi.lock();
|
||||
try {
|
||||
formData.value = await getPurchaseIn(data.id);
|
||||
|
||||
// 设置到 values
|
||||
await formApi.setValues(formData.value, false);
|
||||
// 初始化子表单
|
||||
await nextTick();
|
||||
const itemFormInstance = Array.isArray(itemFormRef.value)
|
||||
? itemFormRef.value[0]
|
||||
: itemFormRef.value;
|
||||
if (itemFormInstance && typeof itemFormInstance.init === 'function') {
|
||||
itemFormInstance.init(formData.value.items || []);
|
||||
}
|
||||
} finally {
|
||||
modalApi.unlock();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
defineExpose({ modalApi });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Modal
|
||||
v-bind="$attrs"
|
||||
:title="getTitle"
|
||||
class="w-2/3"
|
||||
:closable="true"
|
||||
:mask-closable="true"
|
||||
:show-confirm-button="formType !== 'detail'"
|
||||
>
|
||||
<Form class="mx-3">
|
||||
<template #product="slotProps">
|
||||
<PurchaseInItemForm
|
||||
v-bind="slotProps"
|
||||
ref="itemFormRef"
|
||||
class="w-full"
|
||||
:items="formData?.items ?? []"
|
||||
:disabled="formType === 'detail'"
|
||||
@update:items="handleUpdateItems"
|
||||
/>
|
||||
</template>
|
||||
<template #orderNo="slotProps">
|
||||
<SelectPurchaseOrderForm
|
||||
v-bind="slotProps"
|
||||
:order-no="formData?.orderNo"
|
||||
@update:order="handleUpdateOrder"
|
||||
/>
|
||||
</template>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
@@ -1,256 +0,0 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ErpPurchaseInApi } from '#/api/erp/purchase/in';
|
||||
|
||||
import { nextTick, onMounted, ref, watch } from 'vue';
|
||||
|
||||
import { erpPriceMultiply } from '@vben/utils';
|
||||
|
||||
import { InputNumber, Select } from 'ant-design-vue';
|
||||
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import { getProductSimpleList } from '#/api/erp/product/product';
|
||||
import { getWarehouseStockCount } from '#/api/erp/stock/stock';
|
||||
import { getWarehouseSimpleList } from '#/api/erp/stock/warehouse';
|
||||
|
||||
import { usePurchaseOrderItemTableColumns } from '../data';
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
items: () => [],
|
||||
disabled: false,
|
||||
});
|
||||
|
||||
const emit = defineEmits(['update:items', 'update:totalPrice']);
|
||||
|
||||
interface Props {
|
||||
items?: ErpPurchaseInApi.PurchaseInItem[];
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
const tableData = ref<ErpPurchaseInApi.PurchaseInItem[]>([]);
|
||||
const productOptions = ref<any[]>([]);
|
||||
const warehouseOptions = ref<any[]>([]);
|
||||
|
||||
/** 表格配置 */
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
gridOptions: {
|
||||
editConfig: {
|
||||
trigger: 'click',
|
||||
mode: 'cell',
|
||||
},
|
||||
columns: usePurchaseOrderItemTableColumns(),
|
||||
data: tableData.value,
|
||||
border: true,
|
||||
showOverflow: true,
|
||||
autoResize: true,
|
||||
minHeight: 250,
|
||||
keepSource: true,
|
||||
rowConfig: {
|
||||
keyField: 'id',
|
||||
},
|
||||
pagerConfig: {
|
||||
enabled: false,
|
||||
},
|
||||
toolbarConfig: {
|
||||
enabled: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
/** 监听外部传入的列数据 */
|
||||
watch(
|
||||
() => props.items,
|
||||
async (items) => {
|
||||
if (!items) {
|
||||
return;
|
||||
}
|
||||
await nextTick();
|
||||
tableData.value = [...items];
|
||||
await nextTick();
|
||||
gridApi.grid.reloadData(tableData.value);
|
||||
},
|
||||
{
|
||||
immediate: true,
|
||||
},
|
||||
);
|
||||
|
||||
/** 初始化 */
|
||||
onMounted(async () => {
|
||||
productOptions.value = await getProductSimpleList();
|
||||
warehouseOptions.value = await getWarehouseSimpleList();
|
||||
});
|
||||
|
||||
function handlePriceChange(row: any) {
|
||||
if (row.productPrice && row.count) {
|
||||
row.totalProductPrice = erpPriceMultiply(row.productPrice, row.count) ?? 0;
|
||||
row.taxPrice =
|
||||
erpPriceMultiply(row.totalProductPrice, (row.taxPercent || 0) / 100) ?? 0;
|
||||
row.totalPrice = row.totalProductPrice + row.taxPrice;
|
||||
}
|
||||
handleUpdateValue(row);
|
||||
}
|
||||
|
||||
const handleWarehouseChange = async (row: ErpPurchaseInApi.PurchaseInItem) => {
|
||||
const warehouseId = row.warehouseId;
|
||||
const stockCount = await getWarehouseStockCount({
|
||||
productId: row.productId!,
|
||||
warehouseId: warehouseId!,
|
||||
});
|
||||
row.stockCount = stockCount || 0;
|
||||
handleUpdateValue(row);
|
||||
};
|
||||
|
||||
function handleUpdateValue(row: any) {
|
||||
const index = tableData.value.findIndex((item) => item.id === row.id);
|
||||
if (index === -1) {
|
||||
tableData.value.push(row);
|
||||
} else {
|
||||
tableData.value[index] = row;
|
||||
}
|
||||
emit('update:items', [...tableData.value]);
|
||||
}
|
||||
|
||||
const getSummaries = (): {
|
||||
count: number;
|
||||
productName: string;
|
||||
taxPrice: number;
|
||||
totalPrice: number;
|
||||
totalProductPrice: number;
|
||||
} => {
|
||||
const count = tableData.value.reduce(
|
||||
(sum, item) => sum + (item.count || 0),
|
||||
0,
|
||||
);
|
||||
const totalProductPrice = tableData.value.reduce(
|
||||
(sum, item) => sum + (item.totalProductPrice || 0),
|
||||
0,
|
||||
);
|
||||
const taxPrice = tableData.value.reduce(
|
||||
(sum, item) => sum + (item.taxPrice || 0),
|
||||
0,
|
||||
);
|
||||
const totalPrice = tableData.value.reduce(
|
||||
(sum, item) => sum + (item.totalPrice || 0),
|
||||
0,
|
||||
);
|
||||
return {
|
||||
productName: '合计',
|
||||
count,
|
||||
totalProductPrice,
|
||||
taxPrice,
|
||||
totalPrice,
|
||||
};
|
||||
};
|
||||
|
||||
const validate = async (): Promise<boolean> => {
|
||||
try {
|
||||
for (let i = 0; i < tableData.value.length; i++) {
|
||||
const item = tableData.value[i];
|
||||
if (item) {
|
||||
if (!item.warehouseId) {
|
||||
throw new Error(`第 ${i + 1} 行:仓库不能为空`);
|
||||
}
|
||||
if (!item.count || item.count <= 0) {
|
||||
throw new Error(`第 ${i + 1} 行:产品数量不能为空`);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('验证失败:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const getData = (): ErpPurchaseInApi.PurchaseInItem[] => tableData.value;
|
||||
const init = (items: ErpPurchaseInApi.PurchaseInItem[] | undefined): void => {
|
||||
tableData.value =
|
||||
items && items.length > 0
|
||||
? items.map((item) => {
|
||||
const newItem = { ...item };
|
||||
if (newItem.productPrice && newItem.count) {
|
||||
newItem.totalProductPrice =
|
||||
erpPriceMultiply(newItem.productPrice, newItem.count) ?? 0;
|
||||
newItem.taxPrice =
|
||||
erpPriceMultiply(
|
||||
newItem.totalProductPrice,
|
||||
(newItem.taxPercent || 0) / 100,
|
||||
) ?? 0;
|
||||
newItem.totalPrice = newItem.totalProductPrice + newItem.taxPrice;
|
||||
}
|
||||
return newItem;
|
||||
})
|
||||
: [];
|
||||
// TODO @XuZhiqiang:使用 await 风格哈;
|
||||
nextTick(() => {
|
||||
gridApi.grid.reloadData(tableData.value);
|
||||
});
|
||||
};
|
||||
|
||||
defineExpose({
|
||||
validate,
|
||||
getData,
|
||||
init,
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Grid class="w-full">
|
||||
<template #warehouseId="{ row }">
|
||||
<Select
|
||||
v-model:value="row.warehouseId"
|
||||
:options="warehouseOptions"
|
||||
:field-names="{ label: 'name', value: 'id' }"
|
||||
placeholder="请选择仓库"
|
||||
:disabled="disabled"
|
||||
show-search
|
||||
class="w-full"
|
||||
@change="handleWarehouseChange(row)"
|
||||
/>
|
||||
</template>
|
||||
<template #productId="{ row }">
|
||||
<Select
|
||||
disabled
|
||||
v-model:value="row.productId"
|
||||
:options="productOptions"
|
||||
:field-names="{ label: 'name', value: 'id' }"
|
||||
style="width: 100%"
|
||||
placeholder="请选择产品"
|
||||
show-search
|
||||
/>
|
||||
</template>
|
||||
|
||||
<template #count="{ row }">
|
||||
<InputNumber
|
||||
v-if="!disabled"
|
||||
v-model:value="row.count"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
@change="handlePriceChange(row)"
|
||||
/>
|
||||
<span v-else>{{ row.count || '-' }}</span>
|
||||
</template>
|
||||
<template #productPrice="{ row }">
|
||||
<InputNumber
|
||||
disabled
|
||||
v-model:value="row.productPrice"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
@change="handlePriceChange(row)"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<template #bottom>
|
||||
<div class="border-border bg-muted mt-2 rounded border p-2">
|
||||
<div class="text-muted-foreground flex justify-between text-sm">
|
||||
<span class="text-foreground font-medium">合计:</span>
|
||||
<div class="flex space-x-4">
|
||||
<span>数量:{{ getSummaries().count }}</span>
|
||||
<span>金额:{{ getSummaries().totalProductPrice }}</span>
|
||||
<span>税额:{{ getSummaries().taxPrice }}</span>
|
||||
<span>税额合计:{{ getSummaries().totalPrice }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</Grid>
|
||||
</template>
|
||||
@@ -7,7 +7,7 @@ import { IconifyIcon } from '@vben/icons';
|
||||
|
||||
import { Input, message, Modal } from 'ant-design-vue';
|
||||
|
||||
import SelectPurchaseOrderGrid from './select-purchase-order-grid.vue';
|
||||
import SelectPurchaseOrderGrid from './purchase-order-select-grid.vue';
|
||||
|
||||
const props = defineProps({
|
||||
orderNo: {
|
||||
Reference in New Issue
Block a user