Files
shanxia2.0/web-admin/src/views/crm/contract/components/ContractFormDialog.vue
T
2026-07-01 18:52:26 +08:00

2318 lines
76 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<el-dialog
v-model="dialogVisible"
:title="formTitle"
width="calc(100vw - 300px)"
top="20px"
class="dialog-form-layout contract-form-dialog"
destroy-on-close
append-to-body
modal-class="order-module-dialog-mask"
:close-on-click-modal="false"
@opened="handleOpened"
>
<div class="contract-form-body">
<el-form ref="formRef" :model="form" :rules="rules" label-position="top" label-width="120px">
<!-- 合同基础信息(一行 5 列) -->
<div class="contract-basic-info">
<el-form-item label="合同编号" prop="contractNo">
<el-input v-model="form.contractNo" placeholder="系统自动生成,格式:SX/X-JT20250926002" maxlength="50" />
</el-form-item>
<el-form-item label="合同名称" prop="contractName">
<el-input v-model="form.contractName" placeholder="可不填,系统将自动生成" maxlength="100" show-word-limit />
</el-form-item>
<el-form-item label="合同类型" prop="contractType" required>
<el-select v-model="form.contractType" placeholder="请选择合同类型" style="width: 100%" @change="handleContractTypeChange">
<el-option v-for="dict in dictStore.getOptions('crm_contract_type')" :key="dict.value" :label="dict.label" :value="dict.value" />
</el-select>
</el-form-item>
<el-form-item label="合同模版" prop="contractTemplate" required>
<el-select v-model="form.contractTemplate" placeholder="请选择合同模版" style="width: 100%" @change="handleContractTemplateChange">
<el-option v-for="dict in dictStore.getOptions('crm_contract_template')" :key="dict.value" :label="dict.label" :value="dict.value" />
</el-select>
</el-form-item>
<el-form-item label="客户名称" prop="customerId" required>
<el-input v-model="form.customerName" placeholder="请选择客户" readonly>
<template #append>
<el-button text @click="handleSelectCustomer">
<el-icon><Search /></el-icon>
</el-button>
</template>
</el-input>
</el-form-item>
<el-form-item label="关联报价单" prop="quoteId">
<el-input v-model="form.quoteNo" placeholder="请选择报价单(可选)" readonly clearable>
<template #append>
<el-button text @click="handleSelectQuote">
<el-icon><Search /></el-icon>
</el-button>
</template>
</el-input>
</el-form-item>
<el-form-item label="联系人" prop="contactId" required>
<el-input v-model="form.contactsName" placeholder="请选择联系人" maxlength="50">
<template #append>
<el-button text @click="handleSelectContact" :disabled="!form.customerId">
<el-icon><Search /></el-icon>
</el-button>
</template>
</el-input>
</el-form-item>
<el-form-item label="联系电话" prop="contactsPhone">
<el-input v-model="form.contactsPhone" placeholder="从客户/联系人带出" maxlength="20" />
</el-form-item>
<el-form-item label="签订日期" prop="signDate">
<el-date-picker
v-model="form.signDate"
type="date"
placeholder="选择签订日期"
value-format="YYYY-MM-DD"
style="width: 100%"
@change="handleSignDateChange"
/>
</el-form-item>
<el-form-item label="负责人" prop="ownerUserId" required>
<el-select
v-model="form.ownerUserId"
placeholder="请选择负责人"
filterable
clearable
style="width: 100%"
@change="handleOwnerChange"
>
<el-option v-for="user in userList" :key="user.userId" :label="user.nickName" :value="user.userId" />
</el-select>
</el-form-item>
<el-form-item label="合同金额(元)" prop="amount">
<el-input-number
v-model="form.amount"
:precision="2"
:min="0"
placeholder="可选填,留空则从产品明细自动计算"
style="width: 100%"
class="money-field-input"
/>
</el-form-item>
<el-form-item label="合同期限" prop="startDate" required>
<el-date-picker
v-model="contractDateRange"
type="daterange"
range-separator="-"
start-placeholder="开始日期"
end-placeholder="结束日期"
value-format="YYYY-MM-DD"
style="width: 100%"
@change="handleDateRangeChange"
/>
</el-form-item>
<el-form-item label="交易主体" prop="supplierId" required>
<el-select v-model="form.supplierId" placeholder="请选择交易主体" clearable style="width: 100%" @change="handleSupplierChange">
<el-option v-for="supplier in supplierList" :key="supplier.id" :label="supplier.supplierName" :value="supplier.id" />
</el-select>
</el-form-item>
<el-form-item label="结算方式" prop="settlementMethod">
<el-select v-model="form.settlementMethod" placeholder="请选择结算方式" clearable style="width: 100%">
<el-option v-for="dict in dictStore.getOptions('crm_settlement_method')" :key="dict.value" :label="dict.label" :value="dict.value" />
</el-select>
</el-form-item>
</div>
<!-- 供方 / 需方信息(左右布局) -->
<div class="parties-section">
<div class="parties-section-toolbar">
<el-button type="primary" size="small" @click="handleSwapParties">
<el-icon><Sort /></el-icon>
供需方调换
</el-button>
</div>
<div class="parties-row">
<div class="party-panel">
<div class="party-panel__title">供方信息</div>
<el-row :gutter="12">
<el-col :span="6">
<el-form-item label="单位名称(章)" prop="partyA">
<el-input v-model="form.partyA" placeholder="请输入单位名称" maxlength="100" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="单位地址" prop="partyAAddress">
<el-input v-model="form.partyAAddress" placeholder="请输入单位地址" maxlength="200" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="法定代表人" prop="partyALegalPerson">
<el-input v-model="form.partyALegalPerson" placeholder="请输入法定代表人" maxlength="100" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="代理人" prop="partyAAgent">
<el-input v-model="form.partyAAgent" placeholder="请输入代理人(可选)" maxlength="100" />
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="12">
<el-col :span="6">
<el-form-item label="电话/传真" prop="partyAPhoneFax">
<el-input v-model="form.partyAPhoneFax" placeholder="请输入电话/传真" maxlength="100" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="开户银行" prop="partyABank">
<el-input v-model="form.partyABank" placeholder="请输入开户银行" maxlength="200" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="账号" prop="partyAAccount">
<el-input v-model="form.partyAAccount" placeholder="请输入账号" maxlength="100" />
</el-form-item>
</el-col>
</el-row>
</div>
<div class="party-flow-bridge" aria-hidden="true">
<span class="party-flow-bridge__label">供货</span>
<span class="party-flow-bridge__arrow">
<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
<path
d="M5 12h11.5"
stroke="currentColor"
stroke-width="2.75"
stroke-linecap="round"
/>
<path
d="M13.5 8.5 18.5 12l-5 3.5"
stroke="currentColor"
stroke-width="2.75"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</span>
</div>
<div class="party-panel">
<div class="party-panel__title">需方信息</div>
<el-row :gutter="12">
<el-col :span="6">
<el-form-item label="单位名称(章)" prop="partyB">
<el-input v-model="form.partyB" placeholder="从客户开票信息带出" maxlength="200" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="单位地址" prop="partyBAddress">
<el-input v-model="form.partyBAddress" placeholder="从客户开票信息带出" maxlength="500" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="法定代表人" prop="partyBLegalPerson">
<el-input v-model="form.partyBLegalPerson" placeholder="从客户开票信息带出" maxlength="100" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="代理人" prop="partyBAgent">
<el-input v-model="form.partyBAgent" placeholder="从客户开票信息带出(可选)" maxlength="100" />
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="12">
<el-col :span="6">
<el-form-item label="电话/传真" prop="partyBPhoneFax">
<el-input v-model="form.partyBPhoneFax" placeholder="从客户开票信息带出" maxlength="100" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="开户银行" prop="partyBBank">
<el-input v-model="form.partyBBank" placeholder="从客户开票信息带出" maxlength="200" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="账号" prop="partyBAccount">
<el-input v-model="form.partyBAccount" placeholder="从客户开票信息带出" maxlength="100" />
</el-form-item>
</el-col>
</el-row>
</div>
</div>
</div>
<!-- 产品明细部分 -->
<el-divider content-position="left">产品明细</el-divider>
<div class="panel dialog-list-panel mb-4">
<div class="list-toolbar">
<div class="list-toolbar-row">
<div class="list-toolbar-selected">
<el-button class="btn-main" type="primary" @click="handleAddProduct">
<el-icon><Plus /></el-icon>添加产品
</el-button>
<el-button class="btn-ghost" @click="handleRemoveSelectedProducts" :disabled="selectedProducts.length === 0">
<el-icon><Delete /></el-icon>删除选中
</el-button>
</div>
</div>
</div>
<div class="list-body list-wrap">
<el-table :data="form.products" class="list-table" stripe @selection-change="handleProductSelectionChange">
<el-table-column type="selection" min-width="55" align="center" header-align="center" />
<el-table-column label="序号" type="index" min-width="60" align="center" header-align="center" />
<el-table-column label="产品名称" prop="productName" min-width="160" :show-overflow-tooltip="true">
<template #default="scope">
<el-input v-model="scope.row.productName" placeholder="请输入产品名称" clearable size="small" readonly />
</template>
</el-table-column>
<el-table-column label="型号/规格" prop="productModel" min-width="130" :show-overflow-tooltip="true">
<template #default="scope">
<el-input v-model="scope.row.productModel" placeholder="请输入型号/规格" clearable size="small" readonly />
</template>
</el-table-column>
<el-table-column label="产品分类" prop="categoryName" min-width="110" :show-overflow-tooltip="true" />
<el-table-column label="税率(%)" prop="taxRate" width="110">
<template #default="scope">
<el-input v-model="scope.row.taxRate" placeholder="税率" @input="handleTaxRateChange(scope.row)" clearable size="small" />
</template>
</el-table-column>
<el-table-column label="单位" prop="unit" width="100" align="center">
<template #default="scope">
<el-select
v-model="scope.row.unit"
placeholder="请选择单位"
clearable
size="small"
style="width: 100%"
@change="handleUnitChange(scope.row)"
>
<el-option v-for="dict in dictStore.getOptions('crm_product_unit')" :key="dict.value" :label="dict.label" :value="dict.value" />
</el-select>
</template>
</el-table-column>
<el-table-column prop="quantity" width="110">
<template #header> <span style="color: #f56c6c">*</span> 数量</template>
<template #default="scope">
<el-input v-model="scope.row.quantity" placeholder="数量" @input="handleQuantityChange(scope.row)" clearable size="small" />
</template>
</el-table-column>
<el-table-column prop="price" width="130">
<template #header>
<span v-if="isFrameworkContract">含税单价(元)</span>
<span v-else><span style="color: #f56c6c">*</span> 含税单价(元)</span>
</template>
<template #default="scope">
<span v-if="isFrameworkContract" class="text-muted">随行就市</span>
<el-input
v-else
v-model="scope.row.price"
placeholder="含税单价"
@input="handlePriceChange(scope.row)"
clearable
size="small"
class="money-field-input"
/>
</template>
</el-table-column>
<el-table-column prop="subtotal" width="130" align="right">
<template #header>
<span v-if="isFrameworkContract">含税总价(元)</span>
<span v-else><span style="color: #f56c6c">*</span> 含税总价(元)</span>
</template>
<template #default="scope">
<span v-if="isFrameworkContract" class="text-muted">随行就市</span>
<span v-else class="money-display">¥{{ parseFloat(scope.row.subtotal || 0).toFixed(2) }}</span>
</template>
</el-table-column>
<el-table-column label="税费(元)" prop="taxAmount" width="120" align="right">
<template #default="scope">
<span v-if="isFrameworkContract" class="text-muted">随行就市</span>
<span v-else class="money-display">¥{{ parseFloat(scope.row.taxAmount || 0).toFixed(2) }}</span>
</template>
</el-table-column>
<el-table-column prop="packageSpec" min-width="150">
<template #header> <span style="color: #f56c6c">*</span> 包装规格</template>
<template #default="scope">
<el-select
v-model="scope.row.packageSpec"
placeholder="请选择包装规格"
filterable
allow-create
default-first-option
clearable
size="small"
style="width: 100%"
@change="handlePackageSpecChange(scope.row, $event)"
>
<el-option v-for="dict in dictStore.getOptions('crm_product_gg')" :key="dict.value" :label="dict.label" :value="dict.value" />
</el-select>
</template>
</el-table-column>
<el-table-column prop="deliveryMethod" min-width="150">
<template #header> <span style="color: #f56c6c">*</span> 送货方式</template>
<template #default="scope">
<el-select v-model="scope.row.deliveryMethod" placeholder="请选择送货方式" clearable size="small" style="width: 100%">
<el-option v-for="dict in dictStore.getOptions('crm_shfs')" :key="dict.value" :label="dict.label" :value="dict.value" />
</el-select>
</template>
</el-table-column>
<el-table-column prop="remark" label="备注" min-width="160">
<template #default="scope">
<el-input
v-model="scope.row.remark"
type="textarea"
:rows="2"
placeholder="请输入备注"
maxlength="500"
show-word-limit
clearable
size="small"
/>
</template>
</el-table-column>
<el-table-column label="操作" min-width="80" align="center" header-align="center" fixed="right" class-name="col-actions">
<template #default="scope">
<el-button link type="danger" @click="handleRemoveProduct(scope.$index)">
<el-icon><Delete /></el-icon>
</el-button>
</template>
</el-table-column>
</el-table>
</div>
</div>
<!-- 框架合同调价区间 -->
<template v-if="form.contractType === 'framework'">
<el-divider content-position="left">框架合同调价区间</el-divider>
<p class="text-muted mb-3" style="font-size: 13px">
按产品维护生效日期区间与含税单价;每条区间须上传调价函。合同总金额不与此联动,仍为 0。提交审批前将校验每条产品至少一条完整区间。
</p>
<div v-for="(prod, pIdx) in form.products" :key="'fw-tier-' + pIdx" class="framework-tier-block mb-4">
<div class="fw-tier-title mb-2">{{ prod.productName || '产品' + (pIdx + 1) }}</div>
<el-button type="primary" link class="mb-2 fw-tier-letter-add-btn" @click="addPriceTierRow(pIdx)">
<el-icon><Plus /></el-icon>调价函
</el-button>
<div class="panel dialog-list-panel mb-3">
<div class="list-body list-wrap dialog-list-body-only">
<el-table :data="prod.priceTiers || []" class="list-table" stripe empty-text="暂无调价区间">
<el-table-column type="index" label="#" min-width="50" align="center" header-align="center" />
<el-table-column label="开始日期" min-width="150">
<template #default="scope">
<el-date-picker v-model="scope.row.startDate" type="date" value-format="YYYY-MM-DD" placeholder="开始日期" style="width: 100%" />
</template>
</el-table-column>
<el-table-column label="结束日期" min-width="150">
<template #default="scope">
<el-date-picker v-model="scope.row.endDate" type="date" value-format="YYYY-MM-DD" placeholder="结束日期" style="width: 100%" />
</template>
</el-table-column>
<el-table-column label="含税单价(元)" width="140">
<template #default="scope">
<el-input v-model="scope.row.price" placeholder="含税单价" clearable size="small" class="money-field-input" />
</template>
</el-table-column>
<el-table-column label="调价函" min-width="300">
<template #default="scope">
<el-upload
:http-request="(opt) => handleTierLetterUploadRequest(opt, pIdx, scope.$index)"
list-type="picture-card"
:limit="20"
multiple
accept="image/*"
:file-list="scope.row._letterFiles || []"
:on-remove="(file) => handleTierLetterRemove(file, pIdx, scope.$index)"
class="tier-letter-upload"
>
<el-icon><Plus /></el-icon>
</el-upload>
<div class="text-muted" style="font-size: 11px; margin-top: 4px">支持多张图片</div>
</template>
</el-table-column>
<el-table-column label="操作" min-width="70" align="center" header-align="center" fixed="right" class-name="col-actions">
<template #default="scope">
<el-button link type="danger" @click="removePriceTierRow(pIdx, scope.$index)">
<el-icon><Delete /></el-icon>
</el-button>
</template>
</el-table-column>
</el-table>
</div>
</div>
</div>
</template>
<el-divider />
<div class="total-section">
<el-row :gutter="20">
<el-col :span="16"></el-col>
<el-col :span="8">
<div class="total-item">
<span class="total-label">合计金额(元):</span>
<span class="total-value money-display">¥{{ parseFloat(form.amount || 0).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) }}</span>
</div>
</el-col>
</el-row>
</div>
<!-- 附件上传部分 - OpenSpec 标准卡片化布局 -->
<el-divider content-position="left">附件上传</el-divider>
<el-form-item label="附件" prop="attachmentFiles" :required="form.contractTemplate === 'customer_template'">
<ContractAttachmentPanel
v-model="attachmentList"
:biz-type="CRM_UPLOAD_BIZ_TYPE.CONTRACT_ATTACHMENT"
/>
</el-form-item>
</el-form>
</div>
<template #footer>
<div class="dialog-footer">
<el-button class="btn-ghost" @click="handleClose">
<el-icon><Close /></el-icon>取消
</el-button>
<el-button
v-if="form.id ? hasContractPerm('edit') : hasContractPerm('add')"
class="btn-ghost"
:loading="saveDraftLoading"
@click="handleSaveDraft"
>
<el-icon><FolderChecked /></el-icon>保存草稿
</el-button>
<el-button v-if="hasContractPerm('submit')" class="btn-main" type="primary" :loading="submitLoading" @click="handleSubmit">
<el-icon><Promotion /></el-icon>提交审批
</el-button>
</div>
</template>
<!-- 产品选择器 -->
<ProductSelector :visible="productSelectorVisible" @update:visible="productSelectorVisible = $event" @confirm="handleProductSelected" />
<!-- 客户选择器 -->
<CustomerSelector :visible="customerSelectorVisible" @update:visible="customerSelectorVisible = $event" @confirm="handleCustomerConfirm" />
<!-- 联系人选择器 -->
<ContactSelector
:visible="contactSelectorVisible"
@update:visible="contactSelectorVisible = $event"
:customerId="form.customerId"
@confirm="handleContactConfirm"
/>
<!-- 报价单选择器 -->
<QuoteSelector
:visible="quoteSelectorVisible"
@update:visible="quoteSelectorVisible = $event"
:customerId="form.customerId"
@confirm="handleQuoteConfirm"
/>
<!-- 用户选择器 -->
<!-- 审批弹窗 -->
<ApprovalDialog ref="approvalDialogRef" @success="handleApprovalSuccess" />
</el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive, computed, nextTick } from 'vue'
import type { FormInstance, UploadRequestOptions } from 'element-plus'
import { ElMessage } from 'element-plus'
import {
getContract,
addContract,
updateContract,
type ContractForm,
type ContractProductForm,
type ContractProductPriceTierForm
} from '@/api/crm/contract/index'
import { useDictStore } from '@/store/modules/dict'
import { useUserStore } from '@/store/modules/user'
import { $modal } from '@/utils/modal'
import {
Document,
Delete,
Sort,
Plus,
Search,
Close,
FolderChecked,
Promotion,
Loading
} from '@element-plus/icons-vue'
import {
buildUploadFileListFromField,
fillAttachmentInfoCache,
serializeContractAttachments,
type ContractAttachmentItem
} from '@/utils/crm-attachment'
import { buildAttachmentUploadFormData, CRM_UPLOAD_BIZ_TYPE } from '@/utils/crm-upload'
import ContractAttachmentPanel from './ContractAttachmentPanel.vue'
import { getOpportunity } from '@/api/crm/opportunity'
import { getQuote } from '@/api/crm/quote'
import ProductSelector from './ProductSelector.vue'
import CustomerSelector from '@/components/CustomerSelector.vue'
import ContactSelector from '@/components/ContactSelector.vue'
import QuoteSelector from '@/components/QuoteSelector.vue'
import ApprovalDialog from './ApprovalDialog.vue'
import { getContactByCustomerId } from '@/api/crm/customer'
import type { CustomerVO, ContactVO } from '@/api/crm/customer/types'
import { getDefaultInvoiceInfoByCustomerId } from '@/api/crm/invoiceInfo'
import { listAllSupplierInfo, type SupplierInfoVO } from '@/api/crm/supplierInfo'
import { fetchUploadAttachment, fetchUserOptions } from '@/api/admin'
import { useContractPerm } from '@/hooks/business/useContractPerm'
interface UserVO {
userId: string | number
nickName: string
userName?: string
}
// 扩展表单类型,添加显示用字段
type ExtendedContractForm = ContractForm & {
customerName?: string
opportunityName?: string
quoteNo?: string
contactsName?: string
contactsPhone?: string
ownerUserName?: string
supplierId?: number
settlementMethod?: string
templateContent?: string
}
// 扩展产品类型,添加显示字段
type PriceTierRow = ContractProductPriceTierForm & { _letterFiles?: any[] }
type ExtendedContractProductForm = ContractProductForm & {
productName?: string
productModel?: string
unit?: string
unitPrice?: number
subtotal?: number
priceTiers?: PriceTierRow[]
}
const props = defineProps<{
visible: boolean
editId: string | null
}>()
const emit = defineEmits<{
'update:visible': [value: boolean]
success: []
}>()
const dialogVisible = computed({
get: () => props.visible,
set: (val) => emit('update:visible', val)
})
const dictStore = useDictStore()
const userStore = useUserStore()
const { loadPerms, hasContractPerm } = useContractPerm()
// 表单标题
const formTitle = computed(() => {
return form.value.id ? '编辑合同' : '新增合同'
})
// 合同期限日期范围
const contractDateRange = ref<[string, string]>([])
// 表单数据
const form = ref<ExtendedContractForm>({
id: undefined,
contractNo: undefined,
contractName: '',
contractType: 'sale',
contractTemplate: 'shanxia',
customerId: 0,
customerName: '',
contactId: undefined,
opportunityId: undefined,
opportunityName: '',
quoteId: undefined,
quoteNo: '',
contactsName: '',
contactsPhone: '',
signDate: '',
startDate: '',
endDate: '',
ownerUserId: undefined,
ownerUserName: '',
amount: 0,
supplierId: undefined,
settlementMethod: undefined,
partyA: '',
partyAAddress: '',
partyAContact: '',
partyAMobile: '',
partyALegalPerson: '',
partyAAgent: '',
partyAPhoneFax: '',
partyABank: '',
partyAAccount: '',
partyB: '',
partyBAddress: '',
partyBContact: '',
partyBMobile: '',
partyBLegalPerson: '',
partyBAgent: '',
partyBPhoneFax: '',
partyBBank: '',
partyBAccount: '',
paymentTerms: '',
deliveryTerms: '',
contractTerms: '',
products: [] as ExtendedContractProductForm[],
remark: ''
})
// 供需方是否已调换的标志
const isPartiesSwapped = ref(false)
// 记录原始的模板内容(用于检测是否被意外删除)
const originalTemplateContent = ref<string>('')
// 供方信息列表
const supplierList = ref<SupplierInfoVO[]>([])
// 客户简称(用于按合同类型重建合同名称)
const customerShortName = ref('')
// 表单校验规则
const rules = ref({
contractType: [{ required: true, message: '合同类型不能为空', trigger: 'change' }],
customerId: [{ required: true, message: '客户不能为空', trigger: 'change' }],
contactId: [{ required: true, message: '联系人不能为空', trigger: 'change' }],
signDate: [{ required: true, message: '签订日期不能为空', trigger: 'change' }],
ownerUserId: [{ required: true, message: '负责人不能为空', trigger: 'change' }],
supplierId: [{ required: true, message: '交易主体不能为空', trigger: 'change' }],
startDate: [
{
validator: (_rule: any, _value: any, callback: any) => {
if (!contractDateRange.value || contractDateRange.value.length !== 2 || !contractDateRange.value[0] || !contractDateRange.value[1]) {
callback(new Error('合同期限不能为空'))
} else {
callback()
}
},
trigger: ['change', 'blur']
}
],
attachmentFiles: []
})
// 附件列表
const attachmentList = ref<ContractAttachmentItem[]>([])
const isFrameworkContract = computed(() => form.value.contractType === 'framework')
// 加载状态
const saveDraftLoading = ref(false)
const submitLoading = ref(false)
// 组件引用
const formRef = ref<FormInstance>()
const approvalDialogRef = ref()
// 选择器状态
const customerSelectorVisible = ref(false)
const contactSelectorVisible = ref(false)
const opportunitySelectorVisible = ref(false)
const quoteSelectorVisible = ref(false)
const userSelectorVisible = ref(false)
const productSelectorVisible = ref(false)
// 用户列表(负责人下拉)
const userList = ref<any[]>([])
// 选中的产品列表
const selectedProducts = ref<ExtendedContractProductForm[]>([])
// 临时接口定义
interface OpportunityVO {
id: number
opportunityName: string
customerId?: number
customerName?: string
}
interface QuoteVO {
id: number
quoteNo: string
quoteTitle: string
customerId?: number
customerName?: string
contactId?: number
contactName?: string
productList?: any[]
quoteDate?: string
}
/** 对话框打开后的初始化 */
const handleOpened = async () => {
await loadPerms()
await Promise.all([loadSupplierList(), loadUserList()])
if (props.editId) {
await loadContractDetail(props.editId)
} else {
resetForm()
form.value.signDate = new Date().toISOString().split('T')[0]
handleSignDateChange(form.value.signDate)
// 设置默认供方主体为"SX"
const defaultSupplier = supplierList.value.find((supplier) => supplier.supplierCode === 'SX')
if (defaultSupplier) {
form.value.supplierId = defaultSupplier.id
await handleSupplierChange(defaultSupplier.id)
}
}
}
/** 重置表单 */
const resetForm = () => {
form.value = {
id: undefined,
contractNo: undefined,
contractName: '',
contractType: 'sale',
contractTemplate: 'shanxia',
customerId: 0,
customerName: '',
contactId: undefined,
opportunityId: undefined,
opportunityName: '',
quoteId: undefined,
quoteNo: '',
contactsName: '',
contactsPhone: '',
signDate: '',
startDate: '',
endDate: '',
ownerUserId: undefined,
ownerUserName: '',
amount: 0,
supplierId: undefined,
settlementMethod: undefined,
partyA: '',
partyAAddress: '',
partyAContact: '',
partyAMobile: '',
partyALegalPerson: '',
partyAAgent: '',
partyAPhoneFax: '',
partyABank: '',
partyAAccount: '',
partyB: '',
partyBAddress: '',
partyBContact: '',
partyBMobile: '',
partyBLegalPerson: '',
partyBAgent: '',
partyBPhoneFax: '',
partyBBank: '',
partyBAccount: '',
paymentTerms: '',
deliveryTerms: '',
contractTerms: '',
products: [] as ExtendedContractProductForm[],
remark: ''
}
isPartiesSwapped.value = false
originalTemplateContent.value = ''
customerShortName.value = ''
contractDateRange.value = [] as any
attachmentList.value = []
selectedProducts.value = []
}
/** 加载合同详情 */
const loadContractDetail = async (id: number | string) => {
const res = await getContract(id)
const contractData = res.data
Object.assign(form.value, contractData)
originalTemplateContent.value = contractData.templateContent || ''
if (contractData.contactName) {
form.value.contactsName = contractData.contactName
}
if (contractData.contactMobile) {
form.value.contactsPhone = contractData.contactMobile
}
if (contractData.ownerUserName) {
form.value.ownerUserName = contractData.ownerUserName
}
if (contractData.ownerUserId) {
form.value.ownerUserId = contractData.ownerUserId
}
if (contractData.opportunityId) {
try {
const oppRes = await getOpportunity(contractData.opportunityId)
if (oppRes.data) {
form.value.opportunityName = oppRes.data.opportunityName
}
} catch (error) {
console.warn('加载商机信息失败:', error)
}
}
if (contractData.quoteId) {
try {
const quoteRes = await getQuote(contractData.quoteId)
if (quoteRes.data) {
form.value.quoteNo = quoteRes.data.quoteNo
}
} catch (error) {
console.warn('加载报价单信息失败:', error)
}
}
if (contractData.products && Array.isArray(contractData.products)) {
form.value.products = contractData.products.map((product: any) => ({
...product,
productModel: product.model || '',
priceTiers: (product.priceTiers || []).map((t: any) => {
let ids: string[] = []
if (Array.isArray(t.letterOssIds) && t.letterOssIds.length) {
ids = t.letterOssIds.map((id: any) => String(id))
} else if (t.letterOssId != null && t.letterOssId !== '') {
ids = [String(t.letterOssId)]
}
return {
...t,
letterOssIds: ids,
_letterFiles: [] as any[]
}
}),
taxRate: product.taxRate || 6,
taxAmount: 0,
packageSpec: product.packageSpec || '',
deliveryMethod: product.deliveryMethod || ''
}))
if (contractData.contractType === 'framework') {
form.value.products.forEach((product: any) => {
product.price = 0
product.subtotal = 0
product.taxAmount = 0
})
form.value.amount = 0
await enrichFrameworkTierLetterFiles()
} else {
form.value.products.forEach((product: any) => {
calculateProductAmount(product)
})
}
}
if (form.value.startDate && form.value.endDate) {
contractDateRange.value = [form.value.startDate, form.value.endDate]
}
if (form.value.attachmentFiles) {
await handleAttachmentData()
}
customerShortName.value = inferCustomerShortNameFromContractName()
}
/** 选择客户 */
const handleSelectCustomer = () => {
customerSelectorVisible.value = true
}
/** 客户选择确认 */
const handleCustomerConfirm = async (customer: CustomerVO) => {
if (customer) {
form.value.customerId = customer.id
form.value.customerName = customer.customerName
const addressParts: string[] = []
if (customer.province) addressParts.push(customer.province)
if (customer.city) addressParts.push(customer.city)
if (customer.district) addressParts.push(customer.district)
if (customer.address) addressParts.push(customer.address)
const fullAddress = addressParts.join('')
form.value.contactId = undefined
form.value.opportunityId = undefined
form.value.opportunityName = ''
form.value.quoteId = undefined
form.value.quoteNo = ''
try {
const invoiceRes = await getDefaultInvoiceInfoByCustomerId(customer.id)
const invoiceData = invoiceRes?.data
if (invoiceData && invoiceData.companyName) {
if (isPartiesSwapped.value) {
form.value.partyA = invoiceData.companyName || ''
form.value.partyAAddress = invoiceData.companyAddress || fullAddress || ''
form.value.partyALegalPerson = invoiceData.legalRepresentative || ''
form.value.partyAAgent = invoiceData.agent || ''
form.value.partyAPhoneFax = invoiceData.phoneFax || ''
form.value.partyABank = invoiceData.bankName || ''
form.value.partyAAccount = invoiceData.bankAccount || ''
} else {
form.value.partyB = invoiceData.companyName || ''
form.value.partyBAddress = invoiceData.companyAddress || fullAddress || ''
form.value.partyBLegalPerson = invoiceData.legalRepresentative || ''
form.value.partyBAgent = invoiceData.agent || ''
form.value.partyBPhoneFax = invoiceData.phoneFax || ''
form.value.partyBBank = invoiceData.bankName || ''
form.value.partyBAccount = invoiceData.bankAccount || ''
}
} else {
if (isPartiesSwapped.value) {
form.value.partyA = customer.customerName
form.value.partyAAddress = fullAddress
form.value.partyALegalPerson = ''
form.value.partyAAgent = ''
form.value.partyAPhoneFax = customer.telephone || ''
form.value.partyABank = ''
form.value.partyAAccount = ''
} else {
form.value.partyB = customer.customerName
form.value.partyBAddress = fullAddress
form.value.partyBLegalPerson = ''
form.value.partyBAgent = ''
form.value.partyBPhoneFax = customer.telephone || ''
form.value.partyBBank = ''
form.value.partyBAccount = ''
}
}
} catch (error) {
console.error('获取客户开票信息失败:', error)
if (isPartiesSwapped.value) {
form.value.partyA = customer.customerName
form.value.partyAAddress = fullAddress
form.value.partyALegalPerson = ''
form.value.partyAAgent = ''
form.value.partyAPhoneFax = customer.telephone || ''
form.value.partyABank = ''
form.value.partyAAccount = ''
} else {
form.value.partyB = customer.customerName
form.value.partyBAddress = fullAddress
form.value.partyBLegalPerson = ''
form.value.partyBAgent = ''
form.value.partyBPhoneFax = customer.telephone || ''
form.value.partyBBank = ''
form.value.partyBAccount = ''
}
}
if (isPartiesSwapped.value) {
form.value.partyAContact = ''
form.value.partyAMobile = customer.mobile || customer.telephone || ''
} else {
form.value.partyBContact = ''
form.value.partyBMobile = customer.mobile || customer.telephone || ''
}
form.value.contactsName = ''
form.value.contactsPhone = customer.mobile || customer.telephone || ''
try {
const res = await getContactByCustomerId(customer.id)
if (res.data && res.data.length > 0) {
const primaryContact = res.data.find((contact: any) => contact.isPrimary === 1)
const contact = primaryContact || res.data[0]
form.value.contactId = contact.id
form.value.contactsName = contact.contactName
form.value.contactsPhone = contact.mobile || contact.telephone || ''
if (isPartiesSwapped.value) {
form.value.partyAContact = contact.contactName
form.value.partyAMobile = contact.mobile || contact.telephone || ''
} else {
form.value.partyBContact = contact.contactName
form.value.partyBMobile = contact.mobile || contact.telephone || ''
}
}
} catch (error) {
console.error('获取客户联系人失败:', error)
}
customerShortName.value = customer.shortName || ''
rebuildContractName()
}
}
/** 选择联系人 */
const handleSelectContact = () => {
if (!form.value.customerId) {
$modal.msgWarning('请先选择客户')
return
}
contactSelectorVisible.value = true
}
/** 联系人选择确认 */
const handleContactConfirm = (contact: ContactVO) => {
if (contact) {
form.value.contactId = contact.id
form.value.contactsName = contact.contactName
form.value.contactsPhone = contact.mobile || contact.telephone || ''
if (isPartiesSwapped.value) {
form.value.partyAContact = contact.contactName
form.value.partyAMobile = contact.mobile || contact.telephone || ''
} else {
form.value.partyBContact = contact.contactName
form.value.partyBMobile = contact.mobile || contact.telephone || ''
}
formRef.value?.validateField('contactId')
}
}
/** 选择商机 */
const handleSelectOpportunity = () => {
opportunitySelectorVisible.value = true
}
/** 商机选择确认 */
const handleOpportunityConfirm = (opportunity: OpportunityVO) => {
if (opportunity) {
form.value.opportunityId = opportunity.id
form.value.opportunityName = opportunity.opportunityName
if (opportunity.customerId && !form.value.customerId) {
form.value.customerId = opportunity.customerId
form.value.customerName = opportunity.customerName || ''
}
}
}
/** 选择报价单 */
const handleSelectQuote = () => {
quoteSelectorVisible.value = true
}
/** 报价单选择确认 */
const handleQuoteConfirm = (quote: QuoteVO) => {
if (quote) {
form.value.quoteId = quote.id
form.value.quoteNo = quote.quoteNo
if (quote.quoteDate) {
form.value.signDate = quote.quoteDate
handleSignDateChange(quote.quoteDate)
}
if (quote.customerId && !form.value.customerId) {
form.value.customerId = quote.customerId
form.value.customerName = quote.customerName || ''
}
if (quote.contactId) {
form.value.contactId = quote.contactId
form.value.contactsName = quote.contactName || ''
if (isPartiesSwapped.value) {
form.value.partyAContact = quote.contactName || ''
} else {
form.value.partyBContact = quote.contactName || ''
}
}
if (quote.productList && quote.productList.length > 0) {
$modal
.confirm('是否要从报价单中带入产品明细?')
.then(() => {
const isFw = form.value.contractType === 'framework'
form.value.products = quote.productList!.map((item: any) => ({
productId: item.productId,
productName: item.productName,
productModel: item.model,
categoryId: item.categoryId,
categoryName: item.categoryName || '',
unit: item.unit || '个',
quantity: item.quantity || 1,
price: isFw ? 0 : item.price || 0,
unitPrice: isFw ? 0 : item.price || 0,
taxRate: item.taxRate || 13,
taxAmount: isFw ? 0 : item.taxAmount || 0,
discount: item.discount || 0,
subtotal: isFw ? 0 : item.subtotal || (item.quantity || 1) * (item.price || 0) * (1 - (item.discount || 0) / 100),
packageSpec: item.packageSpec || '',
deliveryMethod: item.deliveryMethod || '',
deliveryDate: '',
description: item.description || '',
remark: item.remark || '',
sort: item.sort,
priceTiers: isFw ? [] : undefined
}))
calculateTotalAmount()
})
.catch(() => {})
}
}
}
/** 加载用户列表(负责人下拉) */
const loadUserList = async () => {
try {
const res = await fetchUserOptions()
const options = (res as any)?.data ?? res ?? []
userList.value = (Array.isArray(options) ? options : []).map((item: any) => ({
userId: item.value ?? item.userId ?? item.id,
nickName: item.label ?? item.nickName ?? item.userName ?? ''
}))
} catch (error) {
console.error('加载用户列表失败:', error)
userList.value = []
}
}
/** 负责人变化 */
const handleOwnerChange = (userId: string | number) => {
const user = userList.value.find((item: any) => String(item.userId) === String(userId))
form.value.ownerUserName = user?.nickName || ''
}
/** 选择负责人 */
const handleSelectUser = () => {
userSelectorVisible.value = true
}
/** 用户选择确认 */
const handleUserConfirm = (user: UserVO) => {
if (user) {
form.value.ownerUserId = user.userId
form.value.ownerUserName = user.nickName
}
}
/** 供方主体变化处理 */
const handleSupplierChange = async (supplierId: number | undefined) => {
if (!supplierId) {
if (isPartiesSwapped.value) {
form.value.partyB = ''
form.value.partyBAddress = ''
form.value.partyBLegalPerson = ''
form.value.partyBAgent = ''
form.value.partyBPhoneFax = ''
form.value.partyBBank = ''
form.value.partyBAccount = ''
} else {
form.value.partyA = ''
form.value.partyAAddress = ''
form.value.partyALegalPerson = ''
form.value.partyAAgent = ''
form.value.partyAPhoneFax = ''
form.value.partyABank = ''
form.value.partyAAccount = ''
}
return
}
const supplier = supplierList.value.find((s) => s.id === supplierId)
if (supplier) {
if (isPartiesSwapped.value) {
form.value.partyB = supplier.supplierName || ''
form.value.partyBAddress = supplier.supplierAddress || ''
form.value.partyBLegalPerson = supplier.legalPerson || ''
form.value.partyBAgent = supplier.agent || ''
form.value.partyBPhoneFax = supplier.phoneFax || ''
form.value.partyBBank = supplier.bankName || ''
form.value.partyBAccount = supplier.bankAccount || ''
} else {
form.value.partyA = supplier.supplierName || ''
form.value.partyAAddress = supplier.supplierAddress || ''
form.value.partyALegalPerson = supplier.legalPerson || ''
form.value.partyAAgent = supplier.agent || ''
form.value.partyAPhoneFax = supplier.phoneFax || ''
form.value.partyABank = supplier.bankName || ''
form.value.partyAAccount = supplier.bankAccount || ''
}
updateContractNoBySupplier(supplier.supplierCode)
rebuildContractName()
}
}
/** 更新合同编号中的供方编码 */
const updateContractNoBySupplier = (newSupplierCode: string | undefined) => {
if (!form.value.contractNo || !newSupplierCode) return
const contractNoPattern = /^([A-Z]+)\/X-JT(\d{8})(\d{3})$/
const match = form.value.contractNo.match(contractNoPattern)
if (match) {
const date = match[2]
const serialNum = match[3]
form.value.contractNo = `${newSupplierCode}/X-JT${date}${serialNum}`
} else {
const index = form.value.contractNo.indexOf('/X-JT')
if (index > 0) {
const oldSupplierCode = form.value.contractNo.substring(0, index)
form.value.contractNo = form.value.contractNo.replace(oldSupplierCode, newSupplierCode)
}
}
}
const COMMON_SUPPLIER_SHORT_NAMES = ['山峡', 'SX']
const extractContractNameParts = (contractName: string) => {
const contractNoMatch = contractName.match(/[((](.+?)[))]$/)
const contractNoInName = contractNoMatch ? contractNoMatch[1] : ''
const nameBody = contractNoMatch ? contractName.substring(0, contractNoMatch.index).trim() : contractName.trim()
return { contractNoInName, nameBody }
}
const getSupplierShortName = (): string => {
const supplier = form.value.supplierId ? supplierList.value.find((s) => s.id === form.value.supplierId) : undefined
return supplier?.shortName || supplier?.supplierName || '山峡'
}
const isSupplierShortNamePart = (part: string, supplierShort: string) => {
return part === supplierShort || COMMON_SUPPLIER_SHORT_NAMES.some((name) => part.includes(name))
}
/** 从现有合同名称推断客户简称(切换合同类型时不能依赖新类型解析旧格式) */
const inferCustomerShortNameFromContractName = (): string => {
if (customerShortName.value) return customerShortName.value
if (!form.value.contractName) return ''
const { nameBody } = extractContractNameParts(form.value.contractName)
const parts = nameBody
.split('-')
.map((p) => p.trim())
.filter(Boolean)
if (parts.length === 0) return ''
const supplierShort = getSupplierShortName()
if (parts.length === 1) {
return isSupplierShortNamePart(parts[0], supplierShort) ? '' : parts[0]
}
if (isSupplierShortNamePart(parts[0], supplierShort)) return parts[1]
if (isSupplierShortNamePart(parts[1], supplierShort)) return parts[0]
if (form.value.contractType === 'purchase') return parts[0]
return parts[1]
}
/** 按合同类型重建合同名称:销售/框架=供方简称-客户简称,采购=客户简称-供方简称 */
const rebuildContractName = () => {
if (!form.value.contractName) return
const customerShort = inferCustomerShortNameFromContractName()
const supplierShort = getSupplierShortName()
if (!customerShort && !supplierShort) return
const { contractNoInName } = extractContractNameParts(form.value.contractName)
const contractNo = form.value.contractNo || contractNoInName
const isPurchase = form.value.contractType === 'purchase'
const nameBody = isPurchase
? [customerShort, supplierShort].filter(Boolean).join('-')
: [supplierShort, customerShort].filter(Boolean).join('-')
if (!nameBody) return
form.value.contractName = contractNo ? `${nameBody}(${contractNo})` : nameBody
if (customerShort) {
customerShortName.value = customerShort
}
}
/** 供需方调换 */
const handleSwapParties = async () => {
const currentSupplierId = form.value.supplierId
const temp = {
partyA: form.value.partyA,
partyAAddress: form.value.partyAAddress,
partyAContact: form.value.partyAContact,
partyAMobile: form.value.partyAMobile,
partyALegalPerson: form.value.partyALegalPerson,
partyAAgent: form.value.partyAAgent,
partyAPhoneFax: form.value.partyAPhoneFax,
partyABank: form.value.partyABank,
partyAAccount: form.value.partyAAccount
}
form.value.partyA = form.value.partyB
form.value.partyAAddress = form.value.partyBAddress
form.value.partyAContact = form.value.partyBContact
form.value.partyAMobile = form.value.partyBMobile
form.value.partyALegalPerson = form.value.partyBLegalPerson
form.value.partyAAgent = form.value.partyBAgent
form.value.partyAPhoneFax = form.value.partyBPhoneFax
form.value.partyABank = form.value.partyBBank
form.value.partyAAccount = form.value.partyBAccount
form.value.partyB = temp.partyA
form.value.partyBAddress = temp.partyAAddress
form.value.partyBContact = temp.partyAContact
form.value.partyBMobile = temp.partyAMobile
form.value.partyBLegalPerson = temp.partyALegalPerson
form.value.partyBAgent = temp.partyAAgent
form.value.partyBPhoneFax = temp.partyAPhoneFax
form.value.partyBBank = temp.partyABank
form.value.partyBAccount = temp.partyAAccount
isPartiesSwapped.value = !isPartiesSwapped.value
if (currentSupplierId) {
await handleSupplierChange(currentSupplierId)
}
$modal.msgSuccess('供需方信息已调换')
}
/** 合同类型变化处理 */
const handleContractTypeChange = (value: string) => {
if (value === 'framework') {
form.value.amount = 0
form.value.products?.forEach((p) => {
p.price = 0
p.subtotal = 0
p.taxAmount = 0
if (!p.priceTiers) {
p.priceTiers = []
}
})
}
if (value === 'purchase' && !isPartiesSwapped.value) {
$modal
.confirm('检测到您选择了采购合同,系统将自动调换供需方信息,是否继续?')
.then(async () => {
await handleSwapParties()
})
.catch(() => {
rebuildContractName()
})
} else {
rebuildContractName()
}
}
/** 合同模版变化处理 */
const handleContractTemplateChange = (value: string) => {
if (value === 'customer_template') {
ElMessage({
message: '使用客户模版时,必须上传合同附件!',
type: 'warning',
duration: 3000
})
}
}
/** 合同期限日期范围变化 */
const handleDateRangeChange = (val: [string, string] | null) => {
if (val && val.length === 2) {
form.value.startDate = val[0]
form.value.endDate = val[1]
} else {
form.value.startDate = ''
form.value.endDate = ''
contractDateRange.value = [] as any
}
nextTick(() => {
formRef.value?.validateField('startDate')
})
}
/** 签订日期变化处理 */
const handleSignDateChange = (val: string | null) => {
if (val) {
const signDate = new Date(val)
const endDate = new Date(signDate)
endDate.setDate(endDate.getDate() + 30)
const formatDate = (date: Date): string => {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
const startDateStr = formatDate(signDate)
const endDateStr = formatDate(endDate)
contractDateRange.value = [startDateStr, endDateStr]
form.value.startDate = startDateStr
form.value.endDate = endDateStr
} else {
contractDateRange.value = [] as any
form.value.startDate = ''
form.value.endDate = ''
}
nextTick(() => {
formRef.value?.validateField('startDate')
})
}
/** 添加产品 */
const handleAddProduct = () => {
productSelectorVisible.value = true
}
/** 产品选择确认 */
const handleProductSelected = (products: any[]) => {
const isFw = form.value.contractType === 'framework'
products.forEach((product) => {
form.value.products.push({
productId: product.id,
productName: product.productName,
productModel: product.model || '',
categoryId: product.categoryId,
categoryName: product.categoryName || '',
unit: product.unit || '个',
quantity: 1,
price: isFw ? 0 : product.price || 0,
taxRate: normalizeTaxRateDisplay(product.taxRate, 13),
taxAmount: 0,
subtotal: isFw ? 0 : product.price || 0,
packageSpec: '',
deliveryMethod: '',
description: product.description || '',
remark: '',
priceTiers: isFw ? [] : undefined
})
})
form.value.products.forEach((p) => {
calculateProductAmount(p)
})
calculateTotalAmount()
}
/** 规范化税率展示 */
const normalizeTaxRateDisplay = (value: any, defaultValue = 0): number => {
if (value === null || value === undefined || value === '') {
return defaultValue
}
const num = Number(value)
if (Number.isNaN(num)) {
return defaultValue
}
return Number.isInteger(num) ? num : parseFloat(num.toString())
}
/** 产品选择变化 */
const handleProductSelectionChange = (selection: ExtendedContractProductForm[]) => {
selectedProducts.value = selection
}
/** 删除选中的产品 */
const handleRemoveSelectedProducts = () => {
if (selectedProducts.value.length === 0) return
$modal
.confirm(`确认删除选中的 ${selectedProducts.value.length} 个产品吗?`)
.then(() => {
selectedProducts.value.forEach((selectedProduct) => {
const index = form.value.products.findIndex((p) => p === selectedProduct)
if (index > -1) {
form.value.products.splice(index, 1)
}
})
selectedProducts.value = []
calculateTotalAmount()
$modal.msgSuccess('删除成功')
})
.catch(() => {})
}
/** 删除产品 */
const handleRemoveProduct = (index: number) => {
form.value.products.splice(index, 1)
calculateTotalAmount()
}
/** 单价变化 */
const handlePriceChange = (row: ExtendedContractProductForm) => {
calculateProductAmount(row)
calculateTotalAmount()
}
/** 数量变化 */
const handleQuantityChange = (row: ExtendedContractProductForm) => {
calculateProductAmount(row)
calculateTotalAmount()
}
/** 税率变化 */
const handleTaxRateChange = (row: ExtendedContractProductForm) => {
calculateProductAmount(row)
calculateTotalAmount()
}
/** 单位变化 */
const handleUnitChange = (_row: ExtendedContractProductForm) => {
// 单位变化通常不需要重新计算金额
}
/** 包装规格变更处理 */
const handlePackageSpecChange = (product: ExtendedContractProductForm, value: string) => {
product.packageSpec = value
}
/** 计算产品金额 */
const calculateProductAmount = (product: ExtendedContractProductForm) => {
if (form.value.contractType === 'framework') {
product.price = 0
product.subtotal = 0
product.taxAmount = 0
return
}
const price = parseFloat(product.price?.toString() || '0')
const quantity = parseFloat(product.quantity?.toString() || '0')
const taxRate = parseFloat(product.taxRate?.toString() || '0')
const subtotal = Math.round(price * quantity * 100) / 100
product.subtotal = subtotal
const taxRateDecimal = taxRate / 100
let taxAmount = 0
if (taxRate > 0 && subtotal > 0) {
const taxBase = subtotal / (1 + taxRateDecimal)
taxAmount = Math.round(taxBase * taxRateDecimal * 100) / 100
}
product.taxAmount = taxAmount
}
/** 计算合计金额 */
const calculateTotalAmount = () => {
if (form.value.contractType === 'framework') {
form.value.amount = 0
return
}
let total = 0
form.value.products.forEach((item) => {
const quantity = item.quantity || 0
const price = item.price || 0
const discount = item.discount || 0
const subtotal = quantity * price * (1 - discount / 100)
;(item as ExtendedContractProductForm).subtotal = subtotal
total += subtotal
})
form.value.amount = total
}
/** 框架合同:批量解析调价函 URL */
const enrichFrameworkTierLetterFiles = async () => {
const products = form.value.products || []
const idSet = new Set<string>()
products.forEach((p: any) => {
;(p.priceTiers || []).forEach((t: any) => {
;(t.letterOssIds || []).forEach((id: string) => idSet.add(String(id)))
})
})
if (idSet.size === 0) return
const cache = new Map<string, { name: string; url: string }>()
fillAttachmentInfoCache([...idSet], cache)
products.forEach((p: any) => {
;(p.priceTiers || []).forEach((t: any) => {
const ids = t.letterOssIds || []
t._letterFiles = ids.map((ossId: string, i: number) => {
const info = cache.get(String(ossId))
return {
name: info?.name || `调价函${i + 1}`,
ossId: String(ossId),
url: info?.url || ossId,
uid: `tier-prev-${t.id ?? 'n'}-${i}-${ossId}`,
status: 'success'
}
})
})
})
}
/** 组装提交/保存的产品明细 */
const buildProductsPayload = () => {
const list = form.value.products || []
return list.map((product: any, idx: number) => {
const base: any = {
contractId: product.contractId || form.value.id,
productId: product.productId,
productName: product.productName,
productNo: product.productNo,
categoryId: product.categoryId,
categoryName: product.categoryName,
model: product.productModel || product.model,
unit: product.unit,
quantity: product.quantity,
price: product.price,
discount: product.discount,
taxRate: product.taxRate,
taxAmount: product.taxAmount,
subtotal: product.subtotal,
deliveryDate: product.deliveryDate,
description: product.description,
packageSpec: product.packageSpec,
deliveryMethod: product.deliveryMethod,
sort: product.sort ?? idx + 1,
remark: product.remark
}
if (product.id) {
base.id = product.id
}
if (form.value.contractType === 'framework') {
base.price = 0
base.subtotal = 0
base.taxAmount = 0
base.priceTiers = (product.priceTiers || [])
.filter(
(t: any) => t.startDate && t.endDate && t.price != null && t.price !== '' && Array.isArray(t.letterOssIds) && t.letterOssIds.length > 0
)
.map((t: any, i: number) => ({
id: t.id,
startDate: t.startDate,
endDate: t.endDate,
price: Number(t.price),
letterOssIds: (t.letterOssIds || []).map((id: string) => String(id).trim()).filter(Boolean),
sort: t.sort ?? i + 1
}))
}
return base
})
}
const addPriceTierRow = (pIdx: number) => {
const p = form.value.products[pIdx]
if (!p) return
if (!p.priceTiers) {
p.priceTiers = []
}
p.priceTiers.push({ startDate: '', endDate: '', price: undefined, letterOssIds: [], _letterFiles: [] })
}
const removePriceTierRow = (pIdx: number, tIdx: number) => {
const p = form.value.products[pIdx]
if (!p?.priceTiers) return
p.priceTiers.splice(tIdx, 1)
}
const handleTierLetterUploadRequest = async (
options: UploadRequestOptions,
pIdx: number,
tIdx: number
) => {
const p = form.value.products[pIdx]
const tiers = p?.priceTiers
if (!tiers || !tiers[tIdx]) {
options.onError?.(new Error('调价区间不存在'))
return
}
const row = tiers[tIdx]
const formData = buildAttachmentUploadFormData(options.file, {
bizType: CRM_UPLOAD_BIZ_TYPE.CONTRACT_PRICE_LETTER
})
try {
const res = await fetchUploadAttachment(formData)
const fileUrl = res?.fileUrl
if (!fileUrl) {
throw new Error('上传失败')
}
const oid = String(fileUrl)
row.letterOssIds = row.letterOssIds || []
row.letterOssIds.push(oid)
row._letterFiles = row._letterFiles || []
row._letterFiles.push({
name: options.file.name || `调价函${row._letterFiles.length + 1}`,
ossId: oid,
url: oid,
uid: options.file.uid,
status: 'success'
})
options.onSuccess?.(res)
} catch (error: any) {
ElMessage.error(error?.message || '上传失败')
options.onError?.(error)
}
}
const handleTierLetterRemove = (file: any, pIdx: number, tIdx: number) => {
const p = form.value.products[pIdx]
const tiers = p?.priceTiers
if (!tiers || !tiers[tIdx]) return
const row = tiers[tIdx]
const uid = file.uid
const idx = (row._letterFiles || []).findIndex((f: any) => f.uid === uid)
if (idx >= 0) {
const removed = row._letterFiles[idx]
row._letterFiles.splice(idx, 1)
if (removed?.ossId) {
row.letterOssIds = (row.letterOssIds || []).filter((id: string) => String(id) !== String(removed.ossId))
}
}
}
/** 处理附件数据 */
const handleAttachmentData = async () => {
attachmentList.value = buildUploadFileListFromField(
form.value.attachmentFiles
) as ContractAttachmentItem[]
}
/** 保存草稿 */
const handleSaveDraft = async () => {
if (!form.value.customerId || form.value.customerId === 0) {
$modal.msgWarning('客户名称不能为空')
return
}
if (!form.value.supplierId) {
$modal.msgWarning('交易主体不能为空')
return
}
saveDraftLoading.value = true
try {
const attachmentJson = serializeContractAttachments(attachmentList.value)
const data: any = { ...form.value }
data.status = 'draft'
data.approvalStatus = null
if (attachmentJson) {
data.attachmentFiles = attachmentJson
}
Object.keys(data).forEach((key) => {
if (data[key] === undefined || data[key] === '') {
delete data[key]
}
})
if (data.contactsName) {
data.contactName = data.contactsName
}
if (data.contactsPhone) {
data.contactMobile = data.contactsPhone
}
delete data.customerName
delete data.opportunityName
delete data.quoteNo
delete data.contactsName
delete data.contactsPhone
delete data.ownerUserName
delete data.templateContent
if (data.products && Array.isArray(data.products) && data.products.length > 0) {
for (let i = 0; i < data.products.length; i++) {
const product = data.products[i]
const rowNum = i + 1
if (!product.productName || product.productName.trim() === '') {
$modal.msgWarning(`第${rowNum}行产品的产品名称不能为空`)
return
}
}
data.products = buildProductsPayload()
} else {
delete data.products
}
if (form.value.id) {
await updateContract(data)
$modal.msgSuccess('保存成功')
} else {
await addContract(data)
$modal.msgSuccess('保存成功')
}
handleClose()
emit('success')
} finally {
saveDraftLoading.value = false
}
}
/** 提交审批 */
const handleSubmit = async () => {
try {
await formRef.value?.validate()
} catch (error) {
ElMessage.warning({
message: '请填写完整的必填信息',
duration: 3000
})
return
}
if (form.value.contractTemplate === 'customer') {
const hasAttachments = attachmentList.value.some((file) => file.status === 'success' && file.ossId)
if (!hasAttachments) {
ElMessage.warning({
message: '使用客户模版时,必须上传合同附件',
duration: 3000
})
return
}
}
const requiresProducts = form.value.contractType === 'sale' || form.value.contractType === 'purchase' || form.value.contractType === 'framework'
const validateProductRowCommon = (product: any, rowNum: number): boolean => {
if (!product.productName || String(product.productName).trim() === '') {
$modal.msgWarning(`第${rowNum}行产品的产品名称不能为空`)
return false
}
if (!product.quantity || Number(product.quantity) <= 0) {
$modal.msgWarning(`第${rowNum}行产品的数量不能为空且必须大于0`)
return false
}
if (!product.packageSpec || String(product.packageSpec).trim() === '') {
$modal.msgWarning(`第${rowNum}行产品的包装规格不能为空`)
return false
}
if (!product.deliveryMethod || String(product.deliveryMethod).trim() === '') {
$modal.msgWarning(`第${rowNum}行产品的送货方式不能为空`)
return false
}
return true
}
if (requiresProducts) {
if (!form.value.products || form.value.products.length === 0) {
$modal.msgWarning('销售合同、采购合同或框架合同必须至少添加一个产品')
return
}
for (let i = 0; i < form.value.products.length; i++) {
const product = form.value.products[i]
const rowNum = i + 1
if (!validateProductRowCommon(product, rowNum)) return
const isFw = form.value.contractType === 'framework'
if (!isFw) {
if (!product.price || Number(product.price) <= 0) {
$modal.msgWarning(`第${rowNum}行产品的含税单价不能为空且必须大于0`)
return
}
if (!product.subtotal || Number(product.subtotal) <= 0) {
$modal.msgWarning(`第${rowNum}行产品的含税总价不能为空且必须大于0`)
return
}
} else {
const tiers = product.priceTiers || []
if (tiers.length === 0) {
$modal.msgWarning(`框架合同产品「${product.productName || rowNum}」至少添加一条调价区间`)
return
}
for (let j = 0; j < tiers.length; j++) {
const t = tiers[j]
const tRow = j + 1
if (!t.startDate || !t.endDate) {
$modal.msgWarning(`产品「${product.productName}」调价区间第${tRow}行:请选择开始与结束日期`)
return
}
if (String(t.startDate) > String(t.endDate)) {
$modal.msgWarning(`产品「${product.productName}」调价区间第${tRow}行:开始日期不能晚于结束日期`)
return
}
const pVal = t.price === null || t.price === '' ? NaN : Number(t.price)
if (Number.isNaN(pVal) || pVal <= 0) {
$modal.msgWarning(`产品「${product.productName}」调价区间第${tRow}行:含税单价必须大于0`)
return
}
if (!(t.letterOssIds?.length > 0)) {
$modal.msgWarning(`产品「${product.productName}」调价区间第${tRow}行:请上传调价函`)
return
}
}
}
}
} else if (form.value.products && form.value.products.length > 0) {
for (let i = 0; i < form.value.products.length; i++) {
const product = form.value.products[i]
const rowNum = i + 1
if (!validateProductRowCommon(product, rowNum)) return
if (!product.price || Number(product.price) <= 0) {
$modal.msgWarning(`第${rowNum}行产品的含税单价不能为空且必须大于0`)
return
}
if (!product.subtotal || Number(product.subtotal) <= 0) {
$modal.msgWarning(`第${rowNum}行产品的含税总价不能为空且必须大于0`)
return
}
}
}
submitLoading.value = true
try {
const attachmentJson = serializeContractAttachments(attachmentList.value)
const data: any = { ...form.value }
if (attachmentJson) {
data.attachmentFiles = attachmentJson
}
Object.keys(data).forEach((key) => {
if (data[key] === undefined || data[key] === '') {
delete data[key]
}
})
if (data.contactsName) {
data.contactName = data.contactsName
}
if (data.contactsPhone) {
data.contactMobile = data.contactsPhone
}
delete data.customerName
delete data.opportunityName
delete data.quoteNo
delete data.contactsName
delete data.contactsPhone
delete data.ownerUserName
delete data.templateContent
if (data.products && Array.isArray(data.products) && data.products.length > 0) {
data.products = buildProductsPayload()
} else {
delete data.products
}
let contractId: number
if (form.value.id) {
await updateContract(data)
contractId = form.value.id as number
$modal.msgSuccess('保存成功')
} else {
const res = await addContract(data)
contractId = res.data
form.value.id = contractId
$modal.msgSuccess('保存成功')
}
approvalDialogRef.value?.open(contractId, 'submit')
} finally {
submitLoading.value = false
}
}
/** 审批成功回调 */
const handleApprovalSuccess = () => {
handleClose()
emit('success')
}
/** 关闭对话框 */
const handleClose = () => {
dialogVisible.value = false
}
/** 加载供方信息列表 */
const loadSupplierList = async () => {
try {
const res = await listAllSupplierInfo({ status: '0' })
supplierList.value = res.data || []
return res.data || []
} catch (error) {
console.error('加载供方信息列表失败:', error)
return []
}
}
</script>
<style scoped lang="scss">
.contract-form-dialog {
:deep(.el-dialog__wrapper) {
padding: 0 !important;
top: 20px !important;
height: calc(100vh - 40px) !important;
}
:deep(.el-dialog) {
backdrop-filter: none;
height: 100% !important;
max-height: calc(100vh - 40px);
display: flex;
flex-direction: column;
}
:deep(.dialog-footer) {
display: flex;
justify-content: center;
align-items: center;
gap: 16px;
width: 100%;
}
:deep(.el-dialog__header),
:deep(.el-dialog__footer) {
flex: 0 0 auto;
}
:deep(.el-dialog__body) {
padding-top: 8px;
flex: 1 1 auto;
min-height: 0;
max-height: none;
overflow-y: auto;
overflow-x: hidden;
}
}
.contract-form-body {
padding: 0 4px;
}
.contract-basic-info {
display: grid;
grid-template-columns: repeat(5, minmax(0, 1fr));
gap: 12px 16px;
margin-bottom: 20px;
:deep(.el-form-item) {
margin-bottom: 0;
}
:deep(.el-input-number) {
width: 100%;
}
}
@media (max-width: 1400px) {
.contract-basic-info {
grid-template-columns: repeat(4, minmax(0, 1fr));
}
}
@media (max-width: 1100px) {
.contract-basic-info {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
@media (max-width: 560px) {
.contract-basic-info {
grid-template-columns: 1fr;
}
}
.parties-section {
margin: 8px 0 20px;
}
.parties-section-toolbar {
display: flex;
justify-content: flex-end;
margin-bottom: 12px;
}
.parties-row {
display: flex;
align-items: stretch;
gap: 0;
min-height: 0;
}
.party-panel {
flex: 1 1 0;
min-width: 0;
padding: 16px;
border: 1px solid var(--border, #d9e8e2);
border-radius: 10px;
background: #fff;
:deep(.el-row + .el-row) {
margin-top: 12px;
}
:deep(.el-form-item) {
margin-bottom: 0;
}
}
.party-panel__title {
margin-bottom: 12px;
padding-left: 10px;
font-size: 14px;
font-weight: 600;
color: var(--text, #1e293b);
line-height: 1.5;
border-left: 3px solid var(--primary, #1ca851);
}
.party-flow-bridge {
flex: 0 0 88px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 10px;
padding: 0 8px;
}
.party-flow-bridge__label {
font-size: 12px;
font-weight: 600;
color: var(--primary-dark, #168f45);
letter-spacing: 0.06em;
padding: 4px 10px;
border-radius: 999px;
background: #e6f8ed;
border: 1px solid #a8e0b8;
line-height: 1.2;
}
.party-flow-bridge__arrow {
display: flex;
align-items: center;
justify-content: center;
width: 44px;
height: 44px;
border-radius: 50%;
color: var(--primary-dark, #168f45);
background: linear-gradient(145deg, #e6f8ed 0%, #d4f0de 100%);
border: 1px solid #a8e0b8;
box-shadow: 0 2px 8px rgba(28, 168, 81, 0.14);
svg {
width: 22px;
height: 22px;
display: block;
}
}
@media (max-width: 1100px) {
.parties-row {
flex-direction: column;
min-height: 0;
}
.party-flow-bridge {
flex: 0 0 auto;
flex-direction: row;
gap: 12px;
padding: 12px 0;
}
.party-flow-bridge__arrow {
transform: rotate(90deg);
}
}
.total-section {
margin-top: 20px;
padding: 15px;
background-color: #f5f7fa;
border-radius: 4px;
}
.total-item {
display: flex;
justify-content: flex-end;
align-items: center;
margin-bottom: 10px;
font-size: 16px;
&:last-child {
margin-bottom: 0;
}
}
.total-label {
font-weight: bold;
margin-right: 10px;
}
.total-value {
font-size: 18px;
font-weight: bold;
}
.text-muted {
color: #1e293b;
}
:deep(.money-field-input.el-input-number .el-input__inner),
:deep(.money-field-input.el-input-number .el-input__wrapper input) {
color: #dc2626 !important;
-webkit-text-fill-color: #dc2626 !important;
font-weight: 600;
}
/* 统一表格内输入框和下拉框高度 */
:deep(.el-table .el-input__wrapper) {
height: 32px !important;
min-height: 32px !important;
}
:deep(.el-table .el-select__wrapper) {
height: 32px !important;
min-height: 32px !important;
}
:deep(.el-table .el-input--small .el-input__wrapper) {
height: 32px !important;
min-height: 32px !important;
}
:deep(.el-table .el-select--small .el-select__wrapper) {
height: 32px !important;
min-height: 32px !important;
}
/* 价格输入框红色文字 */
.price-input :deep(.el-input__inner) {
color: #e34d59;
}
/* 去除输入框后置按钮的边框和背景 */
:deep(.el-input-group__append) {
background: transparent !important;
border: none !important;
box-shadow: none !important;
}
:deep(.el-input-group__append .el-button) {
border: none !important;
background: transparent !important;
box-shadow: none !important;
padding: 0 15px !important;
}
:deep(.el-input-group__append .el-button:hover) {
background: transparent !important;
border: none !important;
}
:deep(.el-input-group__append .el-button:focus) {
background: transparent !important;
border: none !important;
}
/* 框架合同调价函:表格内多图卡片上传 */
.tier-letter-upload :deep(.el-upload-list--picture-card) {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.tier-letter-upload :deep(.el-upload--picture-card) {
width: 72px;
height: 72px;
line-height: 72px;
}
.tier-letter-upload :deep(.el-upload-list--picture-card .el-upload-list__item) {
width: 72px;
height: 72px;
margin: 0;
}
/* 框架合同:增加「调价函」行内按钮可点区域宽度 */
.fw-tier-letter-add-btn.el-button--primary.is-link {
min-width: 108px;
padding-left: 12px;
padding-right: 12px;
justify-content: center;
}
.mb-3 {
margin-bottom: 12px;
}
.mb-4 {
margin-bottom: 16px;
}
.ml-2 {
margin-left: 8px;
}
</style>