173 lines
5.8 KiB
TypeScript
173 lines
5.8 KiB
TypeScript
import { plPlanCertTabToFilters, type PlPlanCertTab } from '../../../../utils/pl-display'
|
|
import { applyAliasDisplayMeta } from '../../../../utils/crm-product-brand'
|
|
|
|
export function buildQuery(
|
|
tab: string,
|
|
columns: Record<string, any>,
|
|
keyword: string,
|
|
dates: string[] = []
|
|
) {
|
|
const q: Record<string, any> = { ...columns, hasHazardProduct: 1 }
|
|
const scope = plPlanCertTabToFilters(tab as PlPlanCertTab)
|
|
if (keyword.trim()) q.q = keyword.trim()
|
|
if (q.followStatusIn) q.followStatus = q.followStatusIn
|
|
delete q.followStatusIn
|
|
if (scope.certUploadStatus) q.certUploadStatus = scope.certUploadStatus
|
|
if (scope.hazardCertReady !== undefined && scope.hazardCertReady !== '') {
|
|
q.hazardCertReady = Number(scope.hazardCertReady)
|
|
delete q.hazardCertReadyIn
|
|
} else if (q.hazardCertReadyIn && String(q.hazardCertReadyIn).split(',').length === 1) {
|
|
q.hazardCertReady = Number(q.hazardCertReadyIn)
|
|
delete q.hazardCertReadyIn
|
|
}
|
|
if (scope.planType) {
|
|
q.planType = scope.planType
|
|
delete q.planTypeIn
|
|
}
|
|
if (!q.planDateFrom && !q.planDateTo) {
|
|
if (dates[0]) q.planDateFrom = dates[0]
|
|
if (dates[1]) q.planDateTo = dates[1]
|
|
}
|
|
return q
|
|
}
|
|
|
|
type Editable = { archiveStatus?: string; followStatus?: string; hazardCertReady?: number }
|
|
export function canEdit(plan: Editable | null | undefined) {
|
|
return !!plan && plan.archiveStatus !== 'archived' && plan.followStatus !== 'cancelled'
|
|
}
|
|
export function readyTargets<T extends Editable>(
|
|
plans: T[],
|
|
ready: boolean,
|
|
hasHazard: (p: T) => boolean
|
|
): T[] {
|
|
return plans.filter(
|
|
(p) => canEdit(p) && hasHazard(p) && (Number(p.hazardCertReady) === 1) !== ready
|
|
)
|
|
}
|
|
export function certPayload(
|
|
products: Array<{
|
|
id: string
|
|
purchaseCertUrl?: string
|
|
transportCertUrl?: string
|
|
legalUseCertUrl?: string
|
|
[key: string]: any
|
|
}>
|
|
) {
|
|
return products.map((p) => ({
|
|
id: p.id,
|
|
purchaseCertUrl: p.purchaseCertUrl?.trim() || '',
|
|
transportCertUrl: p.transportCertUrl?.trim() || '',
|
|
legalUseCertUrl: p.legalUseCertUrl?.trim() || ''
|
|
}))
|
|
}
|
|
export function canSave(allowed: boolean, uploading: boolean, submitting: boolean, count: number) {
|
|
return allowed && !uploading && !submitting && count > 0
|
|
}
|
|
export function dateShortcut(kind: string, now = new Date()): string[] {
|
|
if (kind === 'all') return []
|
|
const a = new Date(now.getFullYear(), now.getMonth(), now.getDate())
|
|
if (kind === 'tomorrow') a.setDate(a.getDate() + 1)
|
|
if (kind === 'yesterday') a.setDate(a.getDate() - 1)
|
|
if (kind === 'week') a.setDate(a.getDate() - ((a.getDay() + 6) % 7))
|
|
const b = new Date(a)
|
|
if (kind === 'week') b.setDate(b.getDate() + 6)
|
|
const fmt = (d: Date) =>
|
|
`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
|
|
return [fmt(a), fmt(b)]
|
|
}
|
|
|
|
/** A draft date bound replaces the whole shortcut range; clearing a column range removes it. */
|
|
export function facetPlanDateParams(
|
|
context: Record<string, any>,
|
|
draft?: { from?: string; to?: string },
|
|
applied?: { from?: string; to?: string }
|
|
) {
|
|
const result: Record<string, string> = {}
|
|
const hasDraft = Boolean(draft?.from || draft?.to)
|
|
const hasApplied = Boolean(applied?.from || applied?.to)
|
|
if (hasDraft) {
|
|
if (draft?.from) result.planDateFrom = draft.from
|
|
if (draft?.to) result.planDateTo = draft.to
|
|
} else if (!hasApplied) {
|
|
if (context.planDateFrom) result.planDateFrom = context.planDateFrom
|
|
if (context.planDateTo) result.planDateTo = context.planDateTo
|
|
}
|
|
return result
|
|
}
|
|
|
|
type ProductMetadata = {
|
|
id?: string
|
|
planProductId?: string
|
|
productId?: string
|
|
aliasName?: string
|
|
brandName?: string
|
|
brandMode?: string
|
|
mobileProductMeta?: { aliasName: string | null; brandName: string; brandMode: string }
|
|
}
|
|
export function stopProductMeta(stop: ProductMetadata, products: ProductMetadata[]) {
|
|
if (stop.mobileProductMeta) {
|
|
const { aliasName, brandName, brandMode } = stop.mobileProductMeta
|
|
return { aliasName, brandName, brandMode }
|
|
}
|
|
const linked =
|
|
products.find((p) => stop.planProductId && p.id === stop.planProductId) ||
|
|
products.find((p) => stop.productId && p.productId === stop.productId)
|
|
return {
|
|
aliasName: linked?.aliasName ?? stop.aliasName,
|
|
brandName: linked?.brandName ?? stop.brandName,
|
|
brandMode: linked?.brandMode ?? stop.brandMode
|
|
}
|
|
}
|
|
|
|
/** Read-only enrichment; failed lookups keep the saved snapshot and surface a partial-load hint. */
|
|
export async function enrichContractReadFields(
|
|
source: Record<string, any>,
|
|
lookup: {
|
|
product: (id: string) => Promise<Record<string, any>>
|
|
opportunity: (id: string) => Promise<Record<string, any>>
|
|
quote: (id: string) => Promise<Record<string, any>>
|
|
}
|
|
) {
|
|
const contract: Record<string, any> = {
|
|
...source,
|
|
products: (source.products || []).map((p: any) => ({ ...p }))
|
|
}
|
|
const warnings: string[] = []
|
|
const tasks = contract.products.map(async (p: any) => {
|
|
if (!p.productId) {
|
|
p.standardProductName = p.productName
|
|
return
|
|
}
|
|
try {
|
|
applyAliasDisplayMeta(p, await lookup.product(String(p.productId)))
|
|
} catch {
|
|
p.standardProductName = p.standardProductName || p.productName
|
|
warnings.push('产品名称/别名')
|
|
}
|
|
})
|
|
if (source.opportunityId)
|
|
tasks.push(
|
|
(async () => {
|
|
try {
|
|
contract.opportunityName =
|
|
(await lookup.opportunity(String(source.opportunityId))).opportunityName ||
|
|
source.opportunityName
|
|
} catch {
|
|
warnings.push('关联商机')
|
|
}
|
|
})()
|
|
)
|
|
if (source.quoteId)
|
|
tasks.push(
|
|
(async () => {
|
|
try {
|
|
contract.quoteNo = (await lookup.quote(String(source.quoteId))).quoteNo || source.quoteNo
|
|
} catch {
|
|
warnings.push('关联报价单')
|
|
}
|
|
})()
|
|
)
|
|
await Promise.all(tasks)
|
|
return { contract, warnings: [...new Set(warnings)] }
|
|
}
|