import type { ContractProductForm, ContractVO } from '@/api/crm/contract/types' import { fetchProductDetail } from '@/api/crm/product' import { applyAliasDisplayMeta, applyBrandSelectChange, buildProductAliasOptions, initLineBrandMetaFromProduct, type BrandOption } from '@/utils/crm-product-brand' export interface PlanFormProductRow { id?: string productId: string productNo: string productName: string /** 主数据标准名(展示用) */ standardProductName?: string /** 合同选用的别名(展示用) */ aliasName?: string aliasOptions?: string[] brandMode?: string brandId?: string brandName?: string brandOptions?: BrandOption[] brandQtyRows?: Array<{ brandId: string; brandName: string; qty: number }> model?: string categoryName?: string unit: string quantity: number actualQty?: number packageSpec?: string deliveryMethod?: string taxRate?: number taxPrice?: number taxSubtotal?: number taxAmount?: number remark?: string isExplosive?: number isToxic?: number purchaseCertUrl?: string transportCertUrl?: string legalUseCertUrl?: string } /** 从合同交易主体/甲乙方名称解析运输计划归属公司 */ export function resolveBelongsCompany(...names: Array) { const text = names.map((n) => (n || '').trim()).filter(Boolean).join(' ') // 与合同模板/报价一致:含「云栋」视为安徽云栋,否则默认安庆山峡 if (text.includes('云栋')) return 'yundong' return 'shanxia' } /** 优先取交易主体名称;缺失时从甲/乙方中识别我方主体 */ export function resolveTradingEntityName(contract: Pick) { const supplierName = (contract.supplierName || '').trim() if (supplierName) return supplierName for (const name of [contract.partyA, contract.partyB]) { const n = (name || '').trim() if (n && (n.includes('山峡') || n.includes('云栋'))) return n } return (contract.partyA || '').trim() } export function mapContractProductToPlanRow(item: ContractProductForm, index: number): PlanFormProductRow { const productId = String(item.productId || item.id || index) return { productId, productNo: productId, productName: item.productName || '', standardProductName: item.standardProductName || undefined, aliasName: item.aliasName || undefined, brandMode: item.brandMode || undefined, brandId: item.brandId || undefined, brandName: item.brandName || undefined, model: item.model || item.productModel, categoryName: item.categoryName, unit: item.unit || '吨', quantity: Number(item.quantity || 0), actualQty: 0, packageSpec: item.packageSpec, deliveryMethod: item.deliveryMethod, taxRate: item.taxRate != null ? Number(item.taxRate) : undefined, taxPrice: item.price != null ? Number(item.price) : undefined, taxSubtotal: item.subtotal != null ? Number(item.subtotal) : undefined, taxAmount: item.taxAmount != null ? Number(item.taxAmount) : undefined, remark: item.remark, isExplosive: Number(item.isExplosive ?? 0), isToxic: Number(item.isToxic ?? 0) } } export function mapContractProducts(products?: ContractProductForm[]) { return (products || []).map(mapContractProductToPlanRow) } export function applyContractToPlanForm(contract: ContractVO) { // 供方可章名取 partyA;客户名称严格跟合同「客户名称」,不取供/需方可章名 const supplierName = contract.partyA || contract.supplierName || '' const customerName = contract.customerName || '' const destinationName = customerName return { contractId: String(contract.id), contractNo: contract.contractNo, // 归属公司从合同交易主体带出,表单仍允许用户手动改 belongsCompany: resolveBelongsCompany(resolveTradingEntityName(contract)), supplierId: contract.supplierId != null ? String(contract.supplierId) : '', supplierName, destinationId: contract.customerId != null ? String(contract.customerId) : '', destinationName, customerId: contract.customerId != null ? String(contract.customerId) : '', customerName, contactName: contract.contactName || contract.partyBContact || '', contactPhone: contract.contactMobile || contract.partyBMobile || '', partyA: contract.partyA || '', partyB: contract.partyB || '', contractStartDate: contract.startDate || '', contractEndDate: contract.endDate || '', settlementMethod: contract.settlementMethod || '', hasFreight: contract.settlementMethod?.includes('含运') ? 1 : 0, products: mapContractProducts(contract.products) } } export function getPlanTypeLabel(planType?: string | null) { if (!planType) return '—' const map: Record = { direct: '直销单', inbound: '入库单', outbound: '出库单' } return map[planType] || planType } export function getPlanTypeTagType(planType: string) { const map: Record = { direct: 'primary', inbound: 'success', outbound: 'warning' } return map[planType] || 'info' } export function getDeliveryMethodLabel(method?: string) { const map: Record = { self_pick: '山峡自提', one_ticket: '一票制', customer_pick: '客户自提' } if (!method) return '—' return map[method] || method } /** 按产品主数据回填标准名 / 别名 / 品牌选项,供运输计划表单展示与编辑 */ export async function enrichPlanProductLinesAlias(lines: PlanFormProductRow[]) { await Promise.all( (lines || []).map(async (line) => { if (!line?.productId) { line.standardProductName = line?.productName line.aliasName = undefined line.aliasOptions = [] line.brandOptions = [] return } try { const res: any = await fetchProductDetail({ id: String(line.productId) }) const data = res?.data ?? res if (!data) return applyAliasDisplayMeta(line, data) const crmNo = String(data.productNo || '').trim() if (crmNo) line.productNo = crmNo const meta = initLineBrandMetaFromProduct(data) line.aliasOptions = meta.aliasOptions?.length ? meta.aliasOptions : buildProductAliasOptions(data.aliases) line.brandOptions = meta.brandOptions if (meta.brandOptions.length) { if (line.brandMode === 'required' && line.brandId) { applyBrandSelectChange(line, line.brandId, meta.brandOptions) } else if (!line.brandMode) { line.brandMode = 'any' line.brandId = undefined line.brandName = undefined } } else { line.brandMode = undefined line.brandId = undefined line.brandName = undefined } } catch { line.standardProductName = line.productName line.aliasName = undefined line.aliasOptions = line.aliasOptions || [] line.brandOptions = line.brandOptions || [] } }) ) return lines }