diff --git a/src/utils/string-template.ts b/src/utils/string-template.ts index 752ef3e3..e5e76f7a 100644 --- a/src/utils/string-template.ts +++ b/src/utils/string-template.ts @@ -1,26 +1,43 @@ +import { formatNumberDecimal } from 'src/stores/utils'; + const templates = { - 'my-template': { - converter: (context?: number[]) => { - return context?.join(', ') || ''; + 'quotation-labor': { + converter: (context?: { name: string[] }) => { + return context?.name.join('
') || ''; }, }, 'quotation-payment': { converter: (context?: { - paymentType: 'full' | 'installments'; - installments: { - date: string; + paymentType: + | 'Full' + | 'Split' + | 'SplitCustom' + | 'BillFull' + | 'BillSplit' + | 'BillSplitCustom'; + + amount?: number; + installments?: { + no: number; amount: number; }[]; }) => { - return ( - context?.installments - .map( + if (context?.paymentType === 'Full') { + return [ + `**** เงื่อนไขเพิ่มเติม`, + `- เงื่อนไขการชำระเงิน แบบเต็มจำนวน`, + `  จำนวน ${formatNumberDecimal(context?.amount || 0, 2)}`, + ].join('
'); + } else { + return [ + `**** เงื่อนไขเพิ่มเติม`, + `- เงื่อนไขการชำระเงิน แบบแบ่งจ่าย${context?.paymentType === 'SplitCustom' ? ' กำหนดเอง ' : ' '}${context?.installments?.length} งวด`, + ...(context?.installments?.map( (v) => - `asdasd - `, - ) - .join(', ') || '' - ); + `  งาดที่ ${v.no} จำนวน ${formatNumberDecimal(v.amount, 2)}`, + ) || []), + ].join('
'); + } }, }, } as const; @@ -28,7 +45,7 @@ const templates = { type Template = typeof templates; type TemplateName = keyof Template; type TemplateContext = { - [key in TemplateName]: Parameters[0]; + [key in TemplateName]?: Parameters[0]; }; export function convertTemplate( @@ -40,13 +57,13 @@ export function convertTemplate( for (const [name, template] of Object.entries(templates)) { if (templateUse && !templateUse.includes(name as TemplateName)) continue; - - ret = text.replace( + ret = ret.replace( new RegExp('\\#\\[' + name.replaceAll('-', '\\-') + '\\]', 'g'), typeof template.converter === 'function' ? template.converter(context?.[name as TemplateName] as any) : template.converter, ); + console.log(ret); } return ret;