jws-frontend/src/utils/string-template.ts

112 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-01-23 15:18:15 +07:00
import { RequestWork } from 'src/stores/request-list';
2024-11-27 13:55:19 +07:00
import { formatNumberDecimal } from 'src/stores/utils';
const templates = {
2024-11-27 13:55:19 +07:00
'quotation-labor': {
converter: (context?: { name: string[] }) => {
return context?.name.join('<br />') || '';
},
},
2024-11-27 12:55:38 +07:00
'quotation-payment': {
converter: (context?: {
2024-11-27 13:55:19 +07:00
paymentType:
| 'Full'
| 'Split'
| 'SplitCustom'
| 'BillFull'
| 'BillSplit'
| 'BillSplitCustom';
amount?: number;
installments?: {
no: number;
2024-11-27 12:55:38 +07:00
amount: number;
}[];
}) => {
2024-11-27 13:55:19 +07:00
if (context?.paymentType === 'Full') {
return [
'**** เงื่อนไขเพิ่มเติม',
'- เงื่อนไขการชำระเงิน แบบเต็มจำนวน',
2024-11-27 13:55:19 +07:00
`&nbsp; จำนวน ${formatNumberDecimal(context?.amount || 0, 2)}`,
].join('<br/>');
} else {
return [
'**** เงื่อนไขเพิ่มเติม',
2024-11-27 13:55:19 +07:00
`- เงื่อนไขการชำระเงิน แบบแบ่งจ่าย${context?.paymentType === 'SplitCustom' ? ' กำหนดเอง ' : ' '}${context?.installments?.length} งวด`,
...(context?.installments?.map(
2024-11-27 12:55:38 +07:00
(v) =>
2024-12-03 12:57:48 +07:00
`&nbsp; งวดที่ ${v.no} จำนวน ${formatNumberDecimal(v.amount, 2)}`,
2024-11-27 13:55:19 +07:00
) || []),
].join('<br />');
}
2024-11-27 12:55:38 +07:00
},
},
2025-01-23 15:18:15 +07:00
'order-detail': {
converter: (context?: {
2025-01-24 11:16:05 +07:00
items?: {
2025-01-23 15:18:15 +07:00
product: RequestWork['productService']['product'];
list: RequestWork[];
}[];
itemsDiscount?: {
productId: string;
discount?: number;
}[];
}) => {
2025-01-24 11:16:05 +07:00
return (
context?.items?.flatMap((item) => {
const price = formatNumberDecimal(
item.product.serviceCharge -
(context.itemsDiscount?.find(
(v) => v.productId === item.product.id,
)?.discount || 0),
2025-01-23 15:18:15 +07:00
);
2025-01-24 11:16:05 +07:00
const list = item.list.map((v, i) => {
const employee = v.request.employee;
const branch = v.request.quotation.customerBranch;
return (
`${i + 1}. ` +
`${employee.namePrefix}. ${employee.firstNameEN} ${employee.lastNameEN} `.toUpperCase() +
`(${branch.customer.customerType === 'PERS' ? `นายจ้าง ${branch.namePrefix}. ${branch.firstNameEN} ${branch.lastNameEN} `.toUpperCase() : branch.registerName})`
);
});
return [
`- ${item.product.name} ราคา ${price} บาท`,
'',
...list,
'',
].join('<br />');
}) || []
).join('<br />');
2025-01-23 15:18:15 +07:00
},
},
} as const;
type Template = typeof templates;
type TemplateName = keyof Template;
type TemplateContext = {
2024-11-27 13:55:19 +07:00
[key in TemplateName]?: Parameters<Template[key]['converter']>[0];
};
export function convertTemplate(
text: string,
context?: TemplateContext,
templateUse?: TemplateName[],
) {
let ret = text;
for (const [name, template] of Object.entries(templates)) {
if (templateUse && !templateUse.includes(name as TemplateName)) continue;
2024-11-27 13:55:19 +07:00
ret = ret.replace(
new RegExp('\\#\\[' + name.replaceAll('-', '\\-') + '\\]', 'g'),
typeof template.converter === 'function'
2024-11-27 12:55:38 +07:00
? template.converter(context?.[name as TemplateName] as any)
: template.converter,
);
// console.log(ret);
}
return ret;
}