运输证按收货单位精确匹配系统客户;Web 与移动 H5 同步改名与回填逻辑。 Co-authored-by: Cursor <cursoragent@cursor.com>
304 lines
10 KiB
TypeScript
304 lines
10 KiB
TypeScript
import type { CertBinding, CertBody, CertType } from '@/api/pl/quota-cert'
|
|
import { normalizeProductUnitValue } from '@/utils/crm-product-display'
|
|
|
|
export type CertFaceField = readonly [string, string] | readonly [string, string, 'date']
|
|
|
|
export interface CertFaceGroup {
|
|
title: string
|
|
fields: CertFaceField[]
|
|
}
|
|
|
|
/** 运输证「运输物品」里这一格的原栏名。 */
|
|
export const purchasePermitLabel = '购买许可/备案/出口许可证号'
|
|
|
|
export const commonFields = [
|
|
['certNo', '编号'],
|
|
['issueDate', '核发日期', 'date'],
|
|
['productNameOnCert', '品名'],
|
|
['totalQty', '数量'],
|
|
['quantityUnit', '数量单位'],
|
|
['validFrom', '有效期限起', 'date'],
|
|
['validTo', '有效期限止', 'date'],
|
|
['validUseText', '有效次数']
|
|
] as const
|
|
export const purchaseFields = [
|
|
['buyerName', '名称或姓名'],
|
|
['buyerAddress', '住所/住址'],
|
|
['buyerLegalRepresentative', '法定代表人'],
|
|
['buyerPhone', '电话'],
|
|
['sellerName', '名称'],
|
|
['sellerAddress', '住所'],
|
|
['sellerLegalRepresentative', '法定代表人'],
|
|
['sellerPhone', '电话'],
|
|
['purchasePurpose', '用途']
|
|
] as const
|
|
export const transportFields = [
|
|
['shipperName', '名称或姓名'],
|
|
['shipperAddress', '住所或住址'],
|
|
['shipperLegalRepresentative', '法定代表人'],
|
|
['shipperPhone', '电话'],
|
|
['packagingText', '包装'],
|
|
['carrierName', '名称'],
|
|
['carrierAddressPhone', '住所/电话'],
|
|
['transportMode', '运输方式'],
|
|
['routeText', '运输路线'],
|
|
['consigneeName', '名称/姓名'],
|
|
['applicantName', '名称/姓名']
|
|
] as const
|
|
|
|
function labeledFaceField(kind: CertType, field: CertFaceField): CertFaceField {
|
|
if (field[0] !== 'productNameOnCert') return field
|
|
return [field[0], kind === 'purchase' ? '购买物品' : '品名']
|
|
}
|
|
|
|
function pickFaceFields(
|
|
kind: CertType,
|
|
catalog: readonly CertFaceField[],
|
|
keys: string[]
|
|
): CertFaceField[] {
|
|
return keys.map((key) => {
|
|
const field = catalog.find((item) => item[0] === key)
|
|
if (!field) throw new Error(`未知证面字段 ${key}`)
|
|
return labeledFaceField(kind, field)
|
|
})
|
|
}
|
|
|
|
/** 表单、详情按证件原栏名分组。同名格子靠分组标题区分。 */
|
|
export function certFormGroups(kind: CertType): CertFaceGroup[] {
|
|
if (kind === 'purchase') {
|
|
const catalog = [...commonFields, ...purchaseFields]
|
|
return [
|
|
{ title: '', fields: pickFaceFields(kind, catalog, ['certNo', 'issueDate']) },
|
|
{
|
|
title: '购买单位或个人',
|
|
fields: pickFaceFields(kind, catalog, [
|
|
'buyerName',
|
|
'buyerAddress',
|
|
'buyerLegalRepresentative',
|
|
'buyerPhone'
|
|
])
|
|
},
|
|
{
|
|
title: '销售单位',
|
|
fields: pickFaceFields(kind, catalog, [
|
|
'sellerName',
|
|
'sellerAddress',
|
|
'sellerLegalRepresentative',
|
|
'sellerPhone'
|
|
])
|
|
},
|
|
{
|
|
title: '',
|
|
fields: pickFaceFields(kind, catalog, [
|
|
'productNameOnCert',
|
|
'purchasePurpose',
|
|
'totalQty',
|
|
'quantityUnit',
|
|
'validUseText',
|
|
'validFrom',
|
|
'validTo'
|
|
])
|
|
}
|
|
]
|
|
}
|
|
const catalog = [...commonFields, ...transportFields]
|
|
return [
|
|
{ title: '', fields: pickFaceFields(kind, catalog, ['certNo', 'issueDate']) },
|
|
{
|
|
title: '货主',
|
|
fields: pickFaceFields(kind, catalog, [
|
|
'shipperName',
|
|
'shipperAddress',
|
|
'shipperLegalRepresentative',
|
|
'shipperPhone'
|
|
])
|
|
},
|
|
{
|
|
title: '运输物品',
|
|
fields: pickFaceFields(kind, catalog, [
|
|
'productNameOnCert',
|
|
'totalQty',
|
|
'quantityUnit',
|
|
'packagingText'
|
|
])
|
|
},
|
|
{
|
|
title: '运输单位',
|
|
fields: [
|
|
...pickFaceFields(kind, catalog, ['carrierName', 'carrierAddressPhone', 'transportMode']),
|
|
['vehicleNumbersJson', '车号(次)'],
|
|
...pickFaceFields(kind, catalog, ['routeText'])
|
|
]
|
|
},
|
|
{ title: '收货单位', fields: pickFaceFields(kind, catalog, ['consigneeName']) },
|
|
{
|
|
title: '',
|
|
fields: pickFaceFields(kind, catalog, ['validFrom', 'validTo', 'validUseText'])
|
|
},
|
|
{ title: '申请单位', fields: pickFaceFields(kind, catalog, ['applicantName']) }
|
|
]
|
|
}
|
|
|
|
export function requiredFaceLabel(kind: CertType, key: string) {
|
|
for (const group of certFormGroups(kind)) {
|
|
const field = group.fields.find((item) => item[0] === key)
|
|
if (!field) continue
|
|
return group.title ? `${group.title}的${field[1]}` : field[1]
|
|
}
|
|
return key
|
|
}
|
|
|
|
export function newCert(kind: CertType): CertBody {
|
|
const body: Record<string, unknown> = {
|
|
entryMode: 'image_parse',
|
|
attachmentUrls: '',
|
|
remark: '',
|
|
orderNum: null,
|
|
customerId: null,
|
|
categoryId: null
|
|
}
|
|
for (const field of [
|
|
...commonFields,
|
|
...(kind === 'purchase' ? purchaseFields : transportFields)
|
|
]) {
|
|
body[field[0]] =
|
|
field[0].endsWith('Date') || field[0].startsWith('validF') || field[0] === 'validTo'
|
|
? null
|
|
: ''
|
|
}
|
|
if (kind === 'transport') Object.assign(body, { vehicleNumbersJson: [], purchaseRefs: [] })
|
|
return body as unknown as CertBody
|
|
}
|
|
|
|
/** 删除当前证件附件后,清掉证面、合同绑定和关联购买证,保留备注与录入方式。 */
|
|
export function clearCertAfterAttachmentRemoved<T extends Record<string, any>>(
|
|
kind: CertType,
|
|
form: T
|
|
): T {
|
|
const blank = newCert(kind) as unknown as Record<string, unknown>
|
|
const next: Record<string, unknown> = {
|
|
...blank,
|
|
entryMode: form.entryMode || blank.entryMode,
|
|
attachmentUrls: '',
|
|
remark: form.remark ?? '',
|
|
orderNum: form.orderNum ?? null,
|
|
contractBindings: [],
|
|
bindingsEditable: form.bindingsEditable !== false,
|
|
referencesEditable: form.referencesEditable !== false
|
|
}
|
|
if (form.status) next.status = form.status
|
|
if (form.id) next.id = form.id
|
|
return next as T
|
|
}
|
|
|
|
export function certPayload(kind: CertType, form: Record<string, any>): CertBody {
|
|
const result = newCert(kind) as unknown as Record<string, any>
|
|
for (const key of Object.keys(result)) if (key in form) result[key] = form[key]
|
|
if (result.quantityUnit) result.quantityUnit = normalizeProductUnitValue(result.quantityUnit)
|
|
if (['draft', 'suspended', 'closed', 'void'].includes(form.status)) result.status = form.status
|
|
result.customerId = form.customerId || null
|
|
if (kind === 'purchase') result.categoryId = form.categoryId || null
|
|
else delete result.categoryId
|
|
if (kind === 'transport')
|
|
result.purchaseRefs = (form.purchaseRefs || []).map((r: Record<string, unknown>) =>
|
|
Object.fromEntries(
|
|
['id', 'referenceType', 'referenceNo', 'purchaseCertId', 'remark', 'orderNum']
|
|
.filter((k) => k in r)
|
|
.map((k) => [k, r[k]])
|
|
)
|
|
)
|
|
// 新规则以客户+品类为准;合同产品绑定仅兼容旧数据,有提交时仍透传
|
|
if (form.bindingsEditable !== false && Array.isArray(form.contractBindings)) {
|
|
const pick = (row: Record<string, unknown>, keys: string[]) =>
|
|
Object.fromEntries(keys.filter((key) => key in row).map((key) => [key, row[key]]))
|
|
result.contractBindings = form.contractBindings.map((binding: Record<string, any>) => ({
|
|
...pick(binding, ['id', 'contractId', 'linkStatus', 'remark', 'orderNum']),
|
|
products: (binding.products || []).map((product: Record<string, unknown>) =>
|
|
pick(product, ['id', 'contractProductId', 'scopeStatus', 'remark', 'orderNum'])
|
|
)
|
|
}))
|
|
}
|
|
return result as CertBody
|
|
}
|
|
|
|
export function mergeCandidates(
|
|
form: CertBody,
|
|
candidates: Partial<CertBody>,
|
|
edited: Set<string>
|
|
) {
|
|
const target = form as unknown as Record<string, unknown>
|
|
const allowed = new Set([
|
|
...commonFields.map(([key]) => key),
|
|
...purchaseFields.map(([key]) => key),
|
|
...transportFields.map(([key]) => key),
|
|
'vehicleNumbersJson',
|
|
'purchaseRefs'
|
|
])
|
|
for (const [key, value] of Object.entries(candidates)) {
|
|
if (!allowed.has(key)) continue
|
|
const current = target[key]
|
|
if (
|
|
!edited.has(key) &&
|
|
(current == null || current === '' || (Array.isArray(current) && current.length === 0))
|
|
) {
|
|
target[key] = value
|
|
}
|
|
}
|
|
}
|
|
|
|
/** 只替换当前证件的产品范围,保留同一合同上其他证件的关联。 */
|
|
export function replaceCertProducts<
|
|
T extends { certType: string; certId: string; products: unknown[] }
|
|
>(bindings: T[], certType: string, certId: string, products: T['products']): T[] {
|
|
const others = bindings.filter(
|
|
(binding) => !(binding.certType === certType && binding.certId === certId)
|
|
)
|
|
const current = bindings.find(
|
|
(binding) => binding.certType === certType && binding.certId === certId
|
|
)
|
|
const next = current
|
|
? { ...current, products }
|
|
: ({ certType, certId, linkStatus: 'active', products } as T)
|
|
return [...others, next]
|
|
}
|
|
|
|
export function bindingPayload(bindings: CertBinding[]): CertBinding[] {
|
|
return bindings.map((b) => ({
|
|
...(b.id ? { id: b.id } : {}),
|
|
certType: b.certType,
|
|
certId: b.certId,
|
|
linkStatus: b.linkStatus || 'active',
|
|
...(b.remark !== undefined ? { remark: b.remark } : {}),
|
|
...(b.orderNum !== undefined ? { orderNum: b.orderNum } : {}),
|
|
products: b.products.map((p) => ({
|
|
...(p.id ? { id: p.id } : {}),
|
|
contractProductId: p.contractProductId,
|
|
scopeStatus: p.scopeStatus || 'active',
|
|
...(p.remark !== undefined ? { remark: p.remark } : {}),
|
|
...(p.orderNum !== undefined ? { orderNum: p.orderNum } : {})
|
|
}))
|
|
}))
|
|
}
|
|
|
|
/** 列表和详情展示用:保留 3 位小数,不改库存精度。 */
|
|
export function formatCertQty(value: unknown): string {
|
|
if (value === null || value === undefined || value === '') return '—'
|
|
const text = String(value).trim()
|
|
if (!/^-?\d+(\.\d+)?$/.test(text)) return text
|
|
const negative = text.startsWith('-')
|
|
const [rawInt, rawFrac = ''] = text.slice(negative ? 1 : 0).split('.')
|
|
const frac = (rawFrac + '0000').slice(0, 4)
|
|
let scaled = BigInt(rawInt + frac.slice(0, 3))
|
|
if (frac[3] >= '5') scaled += 1n
|
|
const body = scaled.toString().padStart(4, '0')
|
|
const intPart = body.slice(0, -3).replace(/^0+(?=\d)/, '')
|
|
return `${negative && scaled !== 0n ? '-' : ''}${intPart}.${body.slice(-3)}`
|
|
}
|
|
|
|
export function bindingProductEnabled(binding: CertBinding, productId: string): boolean {
|
|
return (
|
|
binding.linkStatus !== 'inactive' &&
|
|
binding.products.some((p) => p.contractProductId === productId && p.scopeStatus !== 'inactive')
|
|
)
|
|
}
|