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

121 lines
4.1 KiB
TypeScript
Raw Normal View History

2025-01-23 15:18:15 +07:00
import { RequestWork } from 'src/stores/request-list';
import { TaskStatus } from 'src/stores/task-order/types';
2024-11-27 13:55:19 +07:00
import { formatNumberDecimal } from 'src/stores/utils';
import { i18n } from 'src/boot/i18n';
2024-11-27 13:55:19 +07:00
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;
2025-10-14 16:04:33 +07:00
name?: string;
2024-11-27 12:55:38 +07:00
}[];
}) => {
2024-11-27 13:55:19 +07:00
if (context?.paymentType === 'Full') {
return [
2025-10-14 16:04:33 +07:00
`**** ${i18n.global.t('general.additional')}`,
`- ${i18n.global.t('quotation.paymentCondition')} ${i18n.global.t('quotation.type.Full')}`,
`&nbsp; ${i18n.global.t('general.amount')} ${formatNumberDecimal(context?.amount || 0, 2)}`,
2024-11-27 13:55:19 +07:00
].join('<br/>');
} else {
return [
2025-10-14 16:04:33 +07:00
`**** ${i18n.global.t('general.additional')}`,
`- ${i18n.global.t('quotation.paymentCondition')} ${i18n.global.t('quotation.type.Split')}${context?.paymentType === 'SplitCustom' ? ` (${i18n.global.t('general.specify')}) ` : ' '}${context?.installments?.length} ${i18n.global.t('quotation.receiptDialog.installments')}`,
...(context?.installments?.map((v) => {
const installmentName = v.name ? ` (${v.name})` : '';
return `&nbsp; ${i18n.global.t('quotation.periodNo')} ${v.no}${installmentName} ${i18n.global.t('general.amount')} ${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 & {
taskStatus?: TaskStatus;
failedComment?: string;
failedType?: string;
codeRequest?: string;
})[];
2025-01-23 15:18:15 +07:00
}[];
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 (
2025-10-15 10:04:56 +07:00
` ${i + 1}. ` +
` ${v.request.code}_${branch.customer.customerType === 'PERS' ? `${branch.namePrefix}. ${branch.firstNameEN} ${branch.lastNameEN} `.toUpperCase() : i18n.global.locale.value == 'tha' ? branch.registerName : branch.registerNameEN}_` +
2025-01-24 11:16:05 +07:00
`${employee.namePrefix}. ${employee.firstNameEN} ${employee.lastNameEN} `.toUpperCase() +
`${!!v.failedType && v.failedType !== 'other' ? `${i18n.global.t(`taskOrder.${v.failedType}`)}` : !!v.failedComment ? v.failedComment : ''}`
2025-01-24 11:16:05 +07:00
);
});
return [
2025-10-15 10:04:56 +07:00
`- ${item.product.name} ${i18n.global.t('price', { price: price })} `,
2025-01-24 11:16:05 +07:00
'',
...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;
}