-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
140 lines (125 loc) · 3.68 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {ReceiptFormat} from './types';
export function formatNumber(value: any) {
const number = Number(value);
return number.toFixed(2).replace('.', ',');
}
export const formatDate = (date: Date) => {
const options: any = {
day: 'numeric',
month: 'numeric',
year: 'numeric',
};
const formattedDate = date
.toLocaleDateString('de-DE', options)
.replace(/\./g, '.');
const formattedTime = date.toLocaleTimeString('de-DE', {
hour: '2-digit',
minute: '2-digit',
});
return `${formattedDate} ${formattedTime}`;
};
export function padString(str: string, width: number, alignRight = false) {
if (str.length >= width) {
return str.slice(0, width);
} else {
const padding = ' '.repeat(width - str.length);
return alignRight ? padding + str : str + padding;
}
}
export function formatReceiptItem(products: ReceiptFormat[]) {
const productColumnWidth = 39;
const quantityColumnWidth = 6;
const priceColumnWidth = 18;
let productItems = '';
products.forEach(product => {
const productLines = [];
let ItemName = product.name;
while (ItemName.length > productColumnWidth) {
let line = ItemName.slice(0, productColumnWidth);
productLines.push(line);
ItemName = ItemName.slice(productColumnWidth);
}
productLines.push(ItemName);
const quantityStr = padString(
product.quantity.toString(),
quantityColumnWidth,
true,
);
const multiplySymbol = ' x ';
const priceStr = padString(
formatNumber(product.price),
priceColumnWidth - multiplySymbol.length,
true,
);
const formattedLines = productLines.map((line, index) => {
if (index === 0) {
return (
padString(line, productColumnWidth) +
quantityStr +
multiplySymbol +
priceStr
);
} else {
return padString(line, productColumnWidth);
}
});
product.extras.forEach(extra => {
let extraProduct = ' ' + extra.name;
const extraLines = [];
while (extraProduct.length > productColumnWidth) {
let line = extraProduct.slice(0, productColumnWidth);
extraLines.push(line);
extraProduct = extraProduct.slice(productColumnWidth);
}
extraLines.push(
extraLines.length > 0 ? ' ' + extraProduct : extraProduct,
);
extraLines.forEach((line, index) => {
if (index === 0) {
const extraQuantityStr = padString(
extra.quantity.toString(),
quantityColumnWidth,
true,
);
const extraPriceStr = padString(
formatNumber(extra.price),
priceColumnWidth - multiplySymbol.length,
true,
);
formattedLines.push(
padString(line, productColumnWidth) +
extraQuantityStr +
multiplySymbol +
extraPriceStr,
);
} else {
formattedLines.push(padString(line, productColumnWidth));
}
});
});
productItems += formattedLines.join('\n');
});
return productItems;
}
export function formatSubtotal(
subtotal: string,
totalMva: string,
discount: string,
) {
const separator =
'--------------------------------------------------------------';
let summaryLines = [];
summaryLines.push('\n');
summaryLines.push(separator);
summaryLines.push(
padString('Subtotal:', 45, true) + padString(subtotal, 18, true),
);
summaryLines.push(
padString('Herav MVA:', 45, true) + padString(totalMva, 18, true),
);
summaryLines.push(
padString('Discount:', 45, true) + padString(discount, 18, true),
);
summaryLines.push(separator);
return summaryLines.join('\n');
}