<template>
|
<div class="purchase-page art-full-height">
|
<ArtSearchBar
|
v-model="searchForm"
|
:items="searchItems"
|
:showExpand="true"
|
@search="handleSearch"
|
@reset="handleReset"
|
/>
|
|
<ElCard class="art-table-card">
|
<ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
|
<template #left>
|
<ElSpace wrap>
|
<ElButton v-auth="'add'" @click="showDialog('add')" v-ripple>新增PO单</ElButton>
|
<ElButton
|
v-auth="'delete'"
|
type="danger"
|
:disabled="deletableSelectedRows.length === 0"
|
@click="handleBatchDelete"
|
v-ripple
|
>
|
批量删除
|
</ElButton>
|
<ListExportPrint
|
class="inline-flex"
|
:preview-visible="previewVisible"
|
@update:previewVisible="handlePreviewVisibleChange"
|
:report-title="reportTitle"
|
:selected-rows="selectedRows"
|
:query-params="reportQueryParams"
|
:columns="columns"
|
:preview-rows="previewRows"
|
:preview-meta="resolvedPreviewMeta"
|
:total="pagination.total"
|
:disabled="loading"
|
@export="handleExport"
|
@print="handlePrint"
|
/>
|
</ElSpace>
|
</template>
|
</ArtTableHeader>
|
|
<ArtTable
|
:loading="loading"
|
:data="data"
|
:columns="columns"
|
:pagination="pagination"
|
@selection-change="handleSelectionChange"
|
@pagination:size-change="handleSizeChange"
|
@pagination:current-change="handleCurrentChange"
|
/>
|
|
<PurchaseDialog
|
v-model:visible="dialogVisible"
|
:dialog-type="dialogType"
|
:purchase-data="currentPurchaseData"
|
:type-options="typeOptions"
|
:wk-type-options="wkTypeOptions"
|
@submit="handleDialogSubmit"
|
/>
|
|
<PurchaseDetailDrawer
|
v-model:visible="detailDrawerVisible"
|
:loading="detailLoading"
|
:items-loading="detailItemsLoading"
|
:detail="detailData"
|
:item-rows="detailItemRows"
|
:item-columns="purchaseItemColumns"
|
/>
|
</ElCard>
|
</div>
|
</template>
|
|
<script setup>
|
import { computed, onMounted, ref } from 'vue'
|
import { ElMessage } from 'element-plus'
|
import { useUserStore } from '@/store/modules/user'
|
import { useAuth } from '@/hooks/core/useAuth'
|
import { useTable } from '@/hooks/core/useTable'
|
import { useCrudPage } from '@/views/system/common/useCrudPage'
|
import { usePrintExportPage } from '@/views/system/common/usePrintExportPage'
|
import ListExportPrint from '@/components/biz/list-export-print/index.vue'
|
import { defaultResponseAdapter } from '@/utils/table/tableUtils'
|
import { guardRequestWithMessage } from '@/utils/sys/requestGuard'
|
import { fetchDictDataPage } from '@/api/system-manage'
|
import {
|
fetchDeletePurchase,
|
fetchExportPurchaseReport,
|
fetchPurchaseDetail,
|
fetchPurchaseItemPage,
|
fetchPurchaseMany,
|
fetchPurchasePage,
|
fetchSavePurchase,
|
fetchUpdatePurchase
|
} from '@/api/purchase'
|
import PurchaseDialog from './modules/purchase-dialog.vue'
|
import PurchaseDetailDrawer from './modules/purchase-detail-drawer.vue'
|
import { createPurchaseTableColumns } from './purchaseTable.columns'
|
import {
|
PURCHASE_REPORT_STYLE,
|
PURCHASE_REPORT_TITLE,
|
buildPurchaseDialogModel,
|
buildPurchasePageQueryParams,
|
buildPurchasePrintRows,
|
buildPurchaseReportMeta,
|
buildPurchaseSavePayload,
|
buildPurchaseSearchParams,
|
createPurchaseItemColumns,
|
createPurchaseSearchState,
|
getPurchasePaginationKey,
|
normalizePurchaseDetailRecord,
|
normalizePurchaseItemRow,
|
normalizePurchaseListRow,
|
resolveDictOptions
|
} from './purchasePage.helpers'
|
|
defineOptions({ name: 'Purchase' })
|
|
const { hasAuth } = useAuth()
|
const userStore = useUserStore()
|
|
const reportTitle = PURCHASE_REPORT_TITLE
|
const searchForm = ref(createPurchaseSearchState())
|
const typeOptions = ref([])
|
const wkTypeOptions = ref([])
|
const exceStatusOptions = ref([])
|
const detailDrawerVisible = ref(false)
|
const detailLoading = ref(false)
|
const detailItemsLoading = ref(false)
|
const detailData = ref({})
|
const detailItemRows = ref([])
|
const purchaseItemColumns = createPurchaseItemColumns()
|
let handleDeleteAction = null
|
|
const reportQueryParams = computed(() => buildPurchaseSearchParams(searchForm.value))
|
|
const searchItems = computed(() => [
|
{
|
label: '关键字',
|
key: 'condition',
|
type: 'input',
|
props: {
|
clearable: true,
|
placeholder: '请输入PO单号/来源单号/ERP单号'
|
}
|
},
|
{
|
label: 'PO单号',
|
key: 'code',
|
type: 'input',
|
props: {
|
clearable: true,
|
placeholder: '请输入PO单号'
|
}
|
},
|
{
|
label: '单据类型',
|
key: 'type',
|
type: 'select',
|
props: {
|
clearable: true,
|
filterable: true,
|
options: typeOptions.value
|
}
|
},
|
{
|
label: '业务类型',
|
key: 'wkType',
|
type: 'select',
|
props: {
|
clearable: true,
|
filterable: true,
|
options: wkTypeOptions.value
|
}
|
},
|
{
|
label: '来源单号',
|
key: 'source',
|
type: 'input',
|
props: {
|
clearable: true,
|
placeholder: '请输入来源单号'
|
}
|
},
|
{
|
label: '收货道口',
|
key: 'channel',
|
type: 'input',
|
props: {
|
clearable: true,
|
placeholder: '请输入收货道口'
|
}
|
},
|
{
|
label: 'ERP单号',
|
key: 'platCode',
|
type: 'input',
|
props: {
|
clearable: true,
|
placeholder: '请输入ERP单号'
|
}
|
},
|
{
|
label: '项目编码',
|
key: 'project',
|
type: 'input',
|
props: {
|
clearable: true,
|
placeholder: '请输入项目编码'
|
}
|
},
|
{
|
label: '执行状态',
|
key: 'exceStatus',
|
type: 'select',
|
props: {
|
clearable: true,
|
filterable: true,
|
options: exceStatusOptions.value
|
}
|
},
|
{
|
label: '状态',
|
key: 'status',
|
type: 'select',
|
props: {
|
clearable: true,
|
options: [
|
{ label: '正常', value: 1 },
|
{ label: '冻结', value: 0 }
|
]
|
}
|
}
|
])
|
|
const canDeletePurchase = (row) => Number(row?.exceStatus) === 0
|
|
async function openDetail(row) {
|
detailDrawerVisible.value = true
|
detailLoading.value = true
|
detailItemRows.value = []
|
|
try {
|
const detail = await guardRequestWithMessage(
|
fetchPurchaseDetail(row.id),
|
{},
|
{
|
timeoutMessage: 'PO单详情加载超时,已停止等待'
|
}
|
)
|
detailData.value = normalizePurchaseDetailRecord(detail)
|
await loadDetailItems(row.id)
|
} catch (error) {
|
detailDrawerVisible.value = false
|
detailData.value = {}
|
detailItemRows.value = []
|
ElMessage.error(error?.message || '获取PO单详情失败')
|
} finally {
|
detailLoading.value = false
|
}
|
}
|
|
async function openEditDialog(row) {
|
try {
|
const detail = await guardRequestWithMessage(
|
fetchPurchaseDetail(row.id),
|
{},
|
{
|
timeoutMessage: 'PO单详情加载超时,已停止等待'
|
}
|
)
|
showDialog('edit', detail)
|
} catch (error) {
|
ElMessage.error(error?.message || '获取PO单详情失败')
|
}
|
}
|
|
const {
|
columns,
|
columnChecks,
|
data,
|
loading,
|
pagination,
|
getData,
|
replaceSearchParams,
|
resetSearchParams,
|
handleSizeChange,
|
handleCurrentChange,
|
refreshData,
|
refreshCreate,
|
refreshUpdate,
|
refreshRemove
|
} = useTable({
|
core: {
|
apiFn: fetchPurchasePage,
|
apiParams: buildPurchasePageQueryParams(searchForm.value),
|
paginationKey: getPurchasePaginationKey(),
|
columnsFactory: () =>
|
createPurchaseTableColumns({
|
handleView: openDetail,
|
handleEdit: hasAuth('update') ? openEditDialog : null,
|
handleDelete: hasAuth('delete') ? (row) => handleDeleteAction?.(row) : null,
|
canEdit: hasAuth('update'),
|
canDelete: hasAuth('delete'),
|
canDeleteRow: canDeletePurchase
|
})
|
},
|
transform: {
|
dataTransformer: (records) => {
|
if (!Array.isArray(records)) {
|
return []
|
}
|
return records.map((item) => normalizePurchaseListRow(item))
|
}
|
}
|
})
|
|
const {
|
dialogVisible,
|
dialogType,
|
currentRecord: currentPurchaseData,
|
selectedRows,
|
handleSelectionChange: handleCrudSelectionChange,
|
showDialog,
|
handleDialogSubmit,
|
handleDelete,
|
handleBatchDelete: handleCrudBatchDelete
|
} = useCrudPage({
|
createEmptyModel: () => buildPurchaseDialogModel(),
|
buildEditModel: (record) => buildPurchaseDialogModel(record),
|
buildSavePayload: (formData) => buildPurchaseSavePayload(formData),
|
saveRequest: fetchSavePurchase,
|
updateRequest: fetchUpdatePurchase,
|
deleteRequest: fetchDeletePurchase,
|
entityName: 'PO单',
|
resolveRecordLabel: (record) => record?.code || record?.source || record?.id,
|
refreshCreate,
|
refreshUpdate,
|
refreshRemove
|
})
|
handleDeleteAction = handleDelete
|
|
const deletableSelectedRows = computed(() =>
|
selectedRows.value.filter((row) => canDeletePurchase(row))
|
)
|
|
const handleSelectionChange = (rows) => {
|
handleCrudSelectionChange(rows)
|
}
|
|
const handleBatchDelete = async () => {
|
const invalidRows = selectedRows.value.filter((row) => !canDeletePurchase(row))
|
if (invalidRows.length > 0) {
|
ElMessage.warning('仅执行状态为未开始的PO单允许删除,请调整勾选后重试')
|
return
|
}
|
await handleCrudBatchDelete()
|
}
|
|
async function loadTypeOptions() {
|
const response = await guardRequestWithMessage(
|
fetchDictDataPage({ current: 1, pageSize: 200, dictTypeCode: 'sys_order_type', status: 1 }),
|
{ records: [] },
|
{ timeoutMessage: '单据类型选项加载超时,已停止等待' }
|
)
|
typeOptions.value = resolveDictOptions(defaultResponseAdapter(response).records)
|
}
|
|
async function loadWkTypeOptions() {
|
const response = await guardRequestWithMessage(
|
fetchDictDataPage({
|
current: 1,
|
pageSize: 200,
|
dictTypeCode: 'sys_business_type',
|
status: 1
|
}),
|
{ records: [] },
|
{ timeoutMessage: '业务类型选项加载超时,已停止等待' }
|
)
|
wkTypeOptions.value = resolveDictOptions(defaultResponseAdapter(response).records, { group: 1 })
|
}
|
|
async function loadExceStatusOptions() {
|
const response = await guardRequestWithMessage(
|
fetchDictDataPage({
|
current: 1,
|
pageSize: 200,
|
dictTypeCode: 'sys_po_exce_status',
|
status: 1
|
}),
|
{ records: [] },
|
{ timeoutMessage: '执行状态选项加载超时,已停止等待' }
|
)
|
exceStatusOptions.value = resolveDictOptions(defaultResponseAdapter(response).records)
|
}
|
|
async function loadDetailItems(purchaseId) {
|
detailItemsLoading.value = true
|
try {
|
const response = await guardRequestWithMessage(
|
fetchPurchaseItemPage({ purchaseId, current: 1, pageSize: 200 }),
|
{ records: [] },
|
{ timeoutMessage: '采购明细加载超时,已停止等待' }
|
)
|
detailItemRows.value = defaultResponseAdapter(response).records.map((item) =>
|
normalizePurchaseItemRow(item)
|
)
|
} catch (error) {
|
detailItemRows.value = []
|
ElMessage.error(error?.message || '获取采购明细失败')
|
} finally {
|
detailItemsLoading.value = false
|
}
|
}
|
|
const buildPreviewDialogMeta = (rows) => {
|
const now = new Date()
|
return {
|
reportDate: now.toLocaleDateString('zh-CN'),
|
printedAt: now.toLocaleString('zh-CN', { hour12: false }),
|
operator: userStore.getUserInfo?.name || userStore.getUserInfo?.username || '',
|
count: rows.length
|
}
|
}
|
|
const resolvePrintRecords = async (payload) => {
|
const response =
|
Array.isArray(payload?.ids) && payload.ids.length > 0
|
? await fetchPurchaseMany(payload.ids)
|
: await fetchPurchasePage({
|
...reportQueryParams.value,
|
current: 1,
|
pageSize:
|
Number(pagination.total) > 0
|
? Number(pagination.total)
|
: Number(payload?.pageSize) || 20
|
})
|
return defaultResponseAdapter(response).records
|
}
|
|
const {
|
previewVisible,
|
previewRows,
|
previewMeta,
|
handlePreviewVisibleChange,
|
handleExport,
|
handlePrint
|
} = usePrintExportPage({
|
downloadFileName: 'purchase.xlsx',
|
requestExport: (payload) =>
|
fetchExportPurchaseReport(payload, {
|
headers: {
|
Authorization: userStore.accessToken || ''
|
}
|
}),
|
resolvePrintRecords,
|
buildPreviewRows: (records) => buildPurchasePrintRows(records),
|
buildPreviewMeta: buildPreviewDialogMeta
|
})
|
|
const resolvedPreviewMeta = computed(() =>
|
buildPurchaseReportMeta({
|
previewMeta: previewMeta.value,
|
count: previewRows.value.length,
|
orientation: previewMeta.value?.reportStyle?.orientation || PURCHASE_REPORT_STYLE.orientation
|
})
|
)
|
|
function handleSearch(params) {
|
replaceSearchParams(buildPurchaseSearchParams(params))
|
getData()
|
}
|
|
function handleReset() {
|
Object.assign(searchForm.value, createPurchaseSearchState())
|
resetSearchParams()
|
}
|
|
onMounted(async () => {
|
await Promise.allSettled([loadTypeOptions(), loadWkTypeOptions(), loadExceStatusOptions()])
|
})
|
</script>
|