usePlContractToPlan.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import type { ContractProductForm, ContractVO } from '@/api/crm/contract/types'
  2. import { fetchProductDetail } from '@/api/crm/product'
  3. import {
  4. applyAliasDisplayMeta,
  5. applyBrandSelectChange,
  6. buildProductAliasOptions,
  7. initLineBrandMetaFromProduct,
  8. type BrandOption
  9. } from '@/utils/crm-product-brand'
  10. export interface PlanFormProductRow {
  11. id?: string
  12. productId: string
  13. productNo: string
  14. productName: string
  15. /** 主数据标准名(展示用) */
  16. standardProductName?: string
  17. /** 合同选用的别名(展示用) */
  18. aliasName?: string
  19. aliasOptions?: string[]
  20. brandMode?: string
  21. brandId?: string
  22. brandName?: string
  23. brandOptions?: BrandOption[]
  24. brandQtyRows?: Array<{ brandId: string; brandName: string; qty: number }>
  25. model?: string
  26. categoryName?: string
  27. unit: string
  28. quantity: number
  29. actualQty?: number
  30. packageSpec?: string
  31. deliveryMethod?: string
  32. taxRate?: number
  33. taxPrice?: number
  34. taxSubtotal?: number
  35. taxAmount?: number
  36. remark?: string
  37. isExplosive?: number
  38. isToxic?: number
  39. purchaseCertUrl?: string
  40. transportCertUrl?: string
  41. legalUseCertUrl?: string
  42. }
  43. /** 从合同交易主体/甲乙方名称解析运输计划归属公司 */
  44. export function resolveBelongsCompany(...names: Array<string | undefined | null>) {
  45. const text = names.map((n) => (n || '').trim()).filter(Boolean).join(' ')
  46. // 与合同模板/报价一致:含「云栋」视为安徽云栋,否则默认安庆山峡
  47. if (text.includes('云栋')) return 'yundong'
  48. return 'shanxia'
  49. }
  50. /** 优先取交易主体名称;缺失时从甲/乙方中识别我方主体 */
  51. export function resolveTradingEntityName(contract: Pick<ContractVO, 'supplierName' | 'partyA' | 'partyB'>) {
  52. const supplierName = (contract.supplierName || '').trim()
  53. if (supplierName) return supplierName
  54. for (const name of [contract.partyA, contract.partyB]) {
  55. const n = (name || '').trim()
  56. if (n && (n.includes('山峡') || n.includes('云栋'))) return n
  57. }
  58. return (contract.partyA || '').trim()
  59. }
  60. export function mapContractProductToPlanRow(item: ContractProductForm, index: number): PlanFormProductRow {
  61. const productId = String(item.productId || item.id || index)
  62. return {
  63. productId,
  64. productNo: productId,
  65. productName: item.productName || '',
  66. standardProductName: item.standardProductName || undefined,
  67. aliasName: item.aliasName || undefined,
  68. brandMode: item.brandMode || undefined,
  69. brandId: item.brandId || undefined,
  70. brandName: item.brandName || undefined,
  71. model: item.model || item.productModel,
  72. categoryName: item.categoryName,
  73. unit: item.unit || '吨',
  74. quantity: Number(item.quantity || 0),
  75. actualQty: 0,
  76. packageSpec: item.packageSpec,
  77. deliveryMethod: item.deliveryMethod,
  78. taxRate: item.taxRate != null ? Number(item.taxRate) : undefined,
  79. taxPrice: item.price != null ? Number(item.price) : undefined,
  80. taxSubtotal: item.subtotal != null ? Number(item.subtotal) : undefined,
  81. taxAmount: item.taxAmount != null ? Number(item.taxAmount) : undefined,
  82. remark: item.remark,
  83. isExplosive: Number(item.isExplosive ?? 0),
  84. isToxic: Number(item.isToxic ?? 0)
  85. }
  86. }
  87. export function mapContractProducts(products?: ContractProductForm[]) {
  88. return (products || []).map(mapContractProductToPlanRow)
  89. }
  90. export function applyContractToPlanForm(contract: ContractVO) {
  91. // 供方可章名取 partyA;客户名称严格跟合同「客户名称」,不取供/需方可章名
  92. const supplierName = contract.partyA || contract.supplierName || ''
  93. const customerName = contract.customerName || ''
  94. const destinationName = customerName
  95. return {
  96. contractId: String(contract.id),
  97. contractNo: contract.contractNo,
  98. // 归属公司从合同交易主体带出,表单仍允许用户手动改
  99. belongsCompany: resolveBelongsCompany(resolveTradingEntityName(contract)),
  100. supplierId: contract.supplierId != null ? String(contract.supplierId) : '',
  101. supplierName,
  102. destinationId: contract.customerId != null ? String(contract.customerId) : '',
  103. destinationName,
  104. customerId: contract.customerId != null ? String(contract.customerId) : '',
  105. customerName,
  106. contactName: contract.contactName || contract.partyBContact || '',
  107. contactPhone: contract.contactMobile || contract.partyBMobile || '',
  108. partyA: contract.partyA || '',
  109. partyB: contract.partyB || '',
  110. contractStartDate: contract.startDate || '',
  111. contractEndDate: contract.endDate || '',
  112. settlementMethod: contract.settlementMethod || '',
  113. hasFreight: contract.settlementMethod?.includes('含运') ? 1 : 0,
  114. products: mapContractProducts(contract.products)
  115. }
  116. }
  117. export function getPlanTypeLabel(planType?: string | null) {
  118. if (!planType) return '—'
  119. const map: Record<string, string> = {
  120. direct: '直销单',
  121. inbound: '入库单',
  122. outbound: '出库单'
  123. }
  124. return map[planType] || planType
  125. }
  126. export function getPlanTypeTagType(planType: string) {
  127. const map: Record<string, string> = {
  128. direct: 'primary',
  129. inbound: 'success',
  130. outbound: 'warning'
  131. }
  132. return map[planType] || 'info'
  133. }
  134. export function getDeliveryMethodLabel(method?: string) {
  135. const map: Record<string, string> = {
  136. self_pick: '山峡自提',
  137. one_ticket: '一票制',
  138. customer_pick: '客户自提'
  139. }
  140. if (!method) return '—'
  141. return map[method] || method
  142. }
  143. /** 按产品主数据回填标准名 / 别名 / 品牌选项,供运输计划表单展示与编辑 */
  144. export async function enrichPlanProductLinesAlias(lines: PlanFormProductRow[]) {
  145. await Promise.all(
  146. (lines || []).map(async (line) => {
  147. if (!line?.productId) {
  148. line.standardProductName = line?.productName
  149. line.aliasName = undefined
  150. line.aliasOptions = []
  151. line.brandOptions = []
  152. return
  153. }
  154. try {
  155. const res: any = await fetchProductDetail({ id: String(line.productId) })
  156. const data = res?.data ?? res
  157. if (!data) return
  158. applyAliasDisplayMeta(line, data)
  159. const crmNo = String(data.productNo || '').trim()
  160. if (crmNo) line.productNo = crmNo
  161. const meta = initLineBrandMetaFromProduct(data)
  162. line.aliasOptions = meta.aliasOptions?.length
  163. ? meta.aliasOptions
  164. : buildProductAliasOptions(data.aliases)
  165. line.brandOptions = meta.brandOptions
  166. if (meta.brandOptions.length) {
  167. if (line.brandMode === 'required' && line.brandId) {
  168. applyBrandSelectChange(line, line.brandId, meta.brandOptions)
  169. } else if (!line.brandMode) {
  170. line.brandMode = 'any'
  171. line.brandId = undefined
  172. line.brandName = undefined
  173. }
  174. } else {
  175. line.brandMode = undefined
  176. line.brandId = undefined
  177. line.brandName = undefined
  178. }
  179. } catch {
  180. line.standardProductName = line.productName
  181. line.aliasName = undefined
  182. line.aliasOptions = line.aliasOptions || []
  183. line.brandOptions = line.brandOptions || []
  184. }
  185. })
  186. )
  187. return lines
  188. }