-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample09.ts
More file actions
160 lines (133 loc) · 5.71 KB
/
example09.ts
File metadata and controls
160 lines (133 loc) · 5.71 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
interface FinancialData {
revenue: number;
expenses: number;
period: string;
transactions: Transaction[];
}
interface Transaction {
date: string;
amount: number;
description: string;
}
abstract class ReportGenerator {
protected data: FinancialData;
protected profit: number = 0;
protected profitMargin: number = 0;
constructor(data: FinancialData) {
this.data = data;
}
public generate(): void {
console.log("[INFO] Початок генерації звіту...");
this.prepareData();
// Hook перед рендерингом
this.beforeRender();
this.renderHeader();
this.renderBody();
this.renderFooter();
// Hook після рендерингу
this.afterRender();
this.save();
console.log("[INFO] Звіт згенеровано успішно");
}
// Hook-методи, які підкласи можуть перевизначати
protected beforeRender(): void {
// За замовчуванням нічого не робить
}
protected afterRender(): void {
// За замовчуванням нічого не робить
}
protected prepareData(): void {
console.log("[INFO] Підготовка фінансових даних за період " + this.data.period);
this.calculateMetrics();
}
private calculateMetrics(): void {
this.profit = this.data.revenue - this.data.expenses;
this.profitMargin = (this.profit / this.data.revenue) * 100;
}
protected abstract renderHeader(): void;
protected abstract renderBody(): void;
protected abstract renderFooter(): void;
protected abstract save(): void;
}
class MarkdownReportGenerator extends ReportGenerator {
private content: string = "";
private draft: boolean = false;
private needsApproval: boolean = false;
constructor(data: FinancialData) {
super(data);
}
// Hook перед рендерингом - додає статус документа
protected beforeRender(): void {
if (this.draft) {
console.log("[INFO] Додавання позначки чернетки...");
this.content = "> **ЧЕРНЕТКА** - не для поширення\n\n";
}
}
// Hook після рендерингу - додає інформацію про необхідність затвердження
protected afterRender(): void {
if (this.needsApproval) {
console.log("[INFO] Додавання інформації про необхідність затвердження...");
this.content += "\n\n---\n";
this.content += "**Статус:** Потребує затвердження\n";
this.content += "- [ ] Перевірено бухгалтером\n";
this.content += "- [ ] Затверджено керівником\n";
}
}
public setDraft(isDraft: boolean): void {
this.draft = isDraft;
}
public setNeedsApproval(needsApproval: boolean): void {
this.needsApproval = needsApproval;
}
protected renderHeader(): void {
this.content += `# Фінансовий звіт за ${this.data.period}\n\n`;
this.content += `## Загальні показники\n`;
this.content += `* Дохід: ${this.data.revenue} грн\n`;
this.content += `* Витрати: ${this.data.expenses} грн\n`;
this.content += `* Прибуток: ${this.profit} грн\n`;
this.content += `* Маржа: ${this.profitMargin.toFixed(2)}%\n\n`;
}
protected renderBody(): void {
this.content += `## Транзакції\n\n`;
this.data.transactions.forEach(transaction => {
const type = transaction.amount > 0 ? "+" : "-";
this.content += `* ${transaction.date}: ${type}${Math.abs(transaction.amount)} грн - ${transaction.description}\n`;
});
this.content += "\n";
}
protected renderFooter(): void {
const balance = this.data.transactions.reduce((sum, t) => sum + t.amount, 0);
this.content += `## Підсумок\n`;
this.content += `Баланс за період: ${balance} грн\n\n`;
this.content += `---\n`;
this.content += `Звіт згенеровано: ${new Date().toLocaleString()}\n`;
}
protected save(): void {
const prefix = this.draft ? "draft-" : "";
const fileName = `${prefix}report-${this.data.period}.md`;
const fs = require('fs');
fs.writeFileSync(fileName, this.content);
console.log(`[INFO] Звіт збережено у файл: ${fileName}`);
}
}
// Тестові дані
const testData: FinancialData = {
revenue: 150000,
expenses: 85000,
period: "2024-Q1",
transactions: [
{ date: "2024-01-15", amount: 45000, description: "Продаж продукту А" },
{ date: "2024-02-01", amount: -30000, description: "Оплата оренди" },
{ date: "2024-02-15", amount: 65000, description: "Продаж продукту B" },
{ date: "2024-03-01", amount: -25000, description: "Зарплати" }
]
};
// Тестування з використанням hook-методів
console.log("=== Генерація звичайного звіту ===");
const simpleReport = new MarkdownReportGenerator(testData);
simpleReport.generate();
console.log("\n=== Генерація чернетки звіту, що потребує затвердження ===");
const draftReport = new MarkdownReportGenerator(testData);
draftReport.setDraft(true);
draftReport.setNeedsApproval(true);
draftReport.generate();