-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.h
300 lines (256 loc) 路 11.6 KB
/
statement.h
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#pragma once
#include <iostream>
namespace statement {
void trialBalance(std::vector<Journal>* journalList,
std::map<std::string, std::string>* metaDataMap, int as_on_date) {
// ledger balance (override the values)
std::map<std::string, int> ledgerBalances;
for (size_t i = 0; i < journalList->size(); i++) {
if (journalList->at(i).getTime() < as_on_date) {
ledgerBalances[journalList->at(i).getDebit().getName()] += journalList->at(i).getAmount();
ledgerBalances[journalList->at(i).getCredit().getName()] -= journalList->at(i).getAmount();
}
if (journalList->at(i).getTime() > as_on_date) break;
}
std::string currency = (*metaDataMap)["CURRENCY"];
std::string currencyFormat = (*metaDataMap)["CURRENCY_FORMAT"];
std::string businessName = (*metaDataMap)["BUSINESS"];
std::cout << std::endl;
clitable::Table table;
table.addTitle("In the books of " + businessName);
table.addTitle("As on " + timestampToString(as_on_date));
clitable::Column c[3] = {
clitable::Column("Particular", clitable::Column::CENTER_ALIGN, clitable::Column::LEFT_ALIGN, 1,24, clitable::Column::NON_RESIZABLE),
clitable::Column("Debit", clitable::Column::CENTER_ALIGN, clitable::Column::RIGHT_ALIGN, 1,5, clitable::Column::RESIZABLE),
clitable::Column("Credit", clitable::Column::CENTER_ALIGN, clitable::Column::RIGHT_ALIGN, 1,5, clitable::Column::RESIZABLE)
};
for (size_t i = 0; i < 3; i++) table.addColumn(c[i]);
int total = 0; // total at the end of trial balance
std::map<std::string, int>::iterator itr;
for (itr = ledgerBalances.begin(); itr != ledgerBalances.end(); itr++) {
std::string r[3];
r[0] = itr->first + " A/c";
if (itr->second > 0) {
r[1] = currency + formatCurrency(itr->second, currencyFormat);
r[2] = "-";
total += itr->second;
}
else if (itr->second < 0) {
int v = itr->second * -1; // since negative values need to converted to positive
r[1] = "-";
r[2] = currency + formatCurrency(v, currencyFormat);
}
else continue;
table.addRow(r);
}
std::string r[3] = {"TOTAL",
currency+formatCurrency(total, currencyFormat),
currency+formatCurrency(total, currencyFormat)
};
table.addRow(r);
std::cout << table.draw();
}
void journalEntries(std::vector<Journal>* journalList,
std::map<std::string, std::string>* metaDataMap, int from_time, int to_time) {
clitable::Table table;
short particular_column_size = 30;
std::string currency = (*metaDataMap)["CURRENCY"];
std::string currencyFormat = (*metaDataMap)["CURRENCY_FORMAT"];
std::string businessName = (*metaDataMap)["BUSINESS"];
std::cout << std::endl;
table.addTitle("In the books of " + businessName);
clitable::Column c[4] = {
clitable::Column("Date", clitable::Column::CENTER_ALIGN, clitable::Column::LEFT_ALIGN, 1,10, clitable::Column::NON_RESIZABLE),
clitable::Column("Particular", clitable::Column::CENTER_ALIGN, clitable::Column::LEFT_ALIGN, 1,particular_column_size, clitable::Column::NON_RESIZABLE),
clitable::Column("Debit", clitable::Column::CENTER_ALIGN, clitable::Column::RIGHT_ALIGN, 1,5, clitable::Column::RESIZABLE),
clitable::Column("Credit", clitable::Column::CENTER_ALIGN, clitable::Column::RIGHT_ALIGN, 1,5, clitable::Column::RESIZABLE)
};
for (size_t i = 0; i < 4; i++) table.addColumn(c[i]);
for (size_t i = 0; i < journalList->size(); i++) {
if (journalList->at(i).getTime() > from_time &&
journalList->at(i).getTime() < to_time) {
std::string particulars = journalList->at(i).getDebit().getName() + " A/c";
particulars += dots((particular_column_size - journalList->at(i).getDebit().getName().size())-7) + "Dr.";
particulars += "To. " + journalList->at(i).getCredit().getName() + " A/c\n";
particulars += "(" + journalList->at(i).getNarration() + ")";
std::string r[4] {
timestampToString(journalList->at(i).getTime()),
particulars,
currency+formatCurrency(journalList->at(i).getAmount(), currencyFormat),
"\n"+currency+formatCurrency( journalList->at(i).getAmount(), currencyFormat)
};
table.addRow(r);
}
else if(journalList->at(i).getTime() > to_time) break;
}
std::cout << table.draw();
}
void ledger(std::vector<Journal>* journalList,
std::map<std::string, std::string>* metaDataMap, int from_time, int to_time, std::string account) {
int openingBalance=0, closingBalance=0, adjustedBalance=0, total=0;
std::vector<std::array<std::string, 4>> drRows, crRows;
std::string currency = (*metaDataMap)["CURRENCY"];
std::string currencyFormat = (*metaDataMap)["CURRENCY_FORMAT"];
std::string businessName = (*metaDataMap)["BUSINESS"];
// first loop -> goes through all the entries (till starting date) for calculating opening balance
// second loop -> goes through all the entries (from starting date to ending date) sorting the postings into debit and credit side
size_t x = 0;
for (size_t i = 0; i < journalList->size(); i++) {
// entry is related to given account
x+=1;
if (journalList->at(i).getDebit().getName() == account
|| journalList->at(i).getCredit().getName() == account) {
// calculation of opening balance
if (journalList->at(i).getTime() < from_time) {
if (journalList->at(i).getDebit().getName() == account) openingBalance += journalList->at(i).getAmount();
else openingBalance -= journalList->at(i).getAmount();
}
else {
// minus 1 because otherwise one entry will get ignored
if (i > 0) x = i-1;
else x = 0;
break;
}
}
}
// added opening balance
if (openingBalance > 0) { // debit balance
std::array<std::string, 4> dr;
dr[0] = timestampToString(from_time);
dr[1] = "To.Balance B/D";
dr[2] = "-";
dr[3] = currency + formatCurrency(openingBalance, currencyFormat);
drRows.push_back(dr);
}
else if (openingBalance < 0) { // credit balance
std::array<std::string, 4> cr;
cr[0] = timestampToString(from_time);
cr[1] = "By.Balance B/D";
cr[2] = "-";
cr[3] = currency + formatCurrency((openingBalance*-1), currencyFormat);
crRows.push_back(cr);
}
for (size_t i = x; i < journalList->size(); i++) {
if (journalList->at(i).getDebit().getName() == account
|| journalList->at(i).getCredit().getName() == account) {
// calculation of closing balance
if (journalList->at(i).getTime() <= to_time && journalList->at(i).getTime() > from_time) {
if (journalList->at(i).getDebit().getName() == account) {
adjustedBalance += journalList->at(i).getAmount();
total += journalList->at(i).getAmount(); // total needs to keep track of one side
std::array<std::string, 4> dr;
dr[0] = timestampToString(journalList->at(i).getTime());
dr[1] = ("To." + journalList->at(i).getCredit().getName() + " A/c");
dr[2] = std::to_string(journalList->at(i).getId());
dr[3] = currency + formatCurrency(journalList->at(i).getAmount(), currencyFormat);
drRows.push_back(dr);
}
else { // if (journalList->at(i).getCredit() == account)
adjustedBalance -= journalList->at(i).getAmount();
std::array<std::string, 4> cr;
cr[0] = timestampToString(journalList->at(i).getTime());
cr[1] = ("By." + journalList->at(i).getDebit().getName() + " A/c");
cr[2] = std::to_string(journalList->at(i).getId());
cr[3] = currency + formatCurrency(journalList->at(i).getAmount(), currencyFormat);
crRows.push_back(cr);
}
}
}
}
// clitable boilerplate (drawing the statement)
clitable::Table table;
short particular_column_size = 16;
std::cout << std::endl;
table.addTitle("In the books of " + businessName);
table.addTitle(account + " A/c");
clitable::Column c[8] = {
clitable::Column("Date", clitable::Column::CENTER_ALIGN, clitable::Column::LEFT_ALIGN, 1,10, clitable::Column::NON_RESIZABLE),
clitable::Column("Particular", clitable::Column::CENTER_ALIGN, clitable::Column::LEFT_ALIGN, 1,particular_column_size, clitable::Column::NON_RESIZABLE),
clitable::Column("F", clitable::Column::CENTER_ALIGN, clitable::Column::RIGHT_ALIGN, 0,0, clitable::Column::RESIZABLE),
clitable::Column("Amount", clitable::Column::CENTER_ALIGN, clitable::Column::RIGHT_ALIGN, 1,5, clitable::Column::RESIZABLE),
clitable::Column("Date", clitable::Column::CENTER_ALIGN, clitable::Column::LEFT_ALIGN, 1,10, clitable::Column::NON_RESIZABLE),
clitable::Column("Particular", clitable::Column::CENTER_ALIGN, clitable::Column::LEFT_ALIGN, 1,particular_column_size, clitable::Column::NON_RESIZABLE),
clitable::Column("F", clitable::Column::CENTER_ALIGN, clitable::Column::RIGHT_ALIGN, 0,0, clitable::Column::RESIZABLE),
clitable::Column("Amount", clitable::Column::CENTER_ALIGN, clitable::Column::RIGHT_ALIGN, 1,5, clitable::Column::RESIZABLE)
};
for (size_t i = 0; i < 8; i++) table.addColumn(c[i]);
// adjusting to get the actual closing balance
closingBalance = openingBalance + adjustedBalance;
// maximum length of the ledger
size_t maxLen = drRows.size(); // just taking a default value
if (drRows.size() < crRows.size()) maxLen = crRows.size();
// added closing balance
if (closingBalance < 0) { // debit balance
std::array<std::string, 4> dr;
dr[0] = timestampToString(to_time);
dr[1] = "To.Balance C/D";
dr[2] = "-";
dr[3] = currency + formatCurrency((closingBalance*-1), currencyFormat);
// this code is for making sure the balance is the last item
if (drRows.size() == maxLen) {
drRows.push_back(dr);
maxLen += 1;
}
else {
// creating empty rows to make the balance apprear at the end
for(size_t i=drRows.size(); i<maxLen-1; i++) { // "< maxLen-1" means 2nd last, last one is reserved for the closing entry
std::array<std::string, 4> drBlank = {"","","",""};
drRows.push_back(drBlank);
}
drRows.push_back(dr);
}
}
else if (closingBalance > 0) { // credit balance
std::array<std::string, 4> cr;
cr[0] = timestampToString(to_time);
cr[1] = "By.Balance C/D";
cr[2] = "-";
cr[3] = currency + formatCurrency(closingBalance, currencyFormat);
// this code is for making sure the balance is the last item
if (crRows.size() == maxLen) {
crRows.push_back(cr);
maxLen += 1;
}
else {
// creating empty rows to make the balance apprear at the end
for(size_t i=crRows.size(); i<maxLen-1; i++) { // "< maxLen-1" means 2nd last, last one is reserved for the closing entry
std::array<std::string, 4> crBlank = {"","","",""};
crRows.push_back(crBlank);
}
crRows.push_back(cr);
}
}
// adding all the rows to the table
for (size_t i=0; i<maxLen; i++) {
std::string r[8];
if (drRows.size() > i ) {
r[0] = drRows.at(i)[0];
r[1] = drRows.at(i)[1];
r[2] = drRows.at(i)[2];
r[3] = drRows.at(i)[3];
}
else {r[0]=""; r[1]=""; r[2]=""; r[3]="";} // empty values
if (crRows.size() > i ) {
r[4] = crRows.at(i)[0];
r[5] = crRows.at(i)[1];
r[6] = crRows.at(i)[2];
r[7] = crRows.at(i)[3];
}
else {r[4]=""; r[5]=""; r[6]=""; r[7]="";} // empty values
table.addRow(r);
}
// adjusting to get the total
if (openingBalance > 0) total += openingBalance;
if (closingBalance < 0) total += closingBalance*-1;
// added the total
std::string r[8];
r[0] = "TOTAL";
r[1] = "-"; r[2] = "-";
r[3] = currency + formatCurrency(total, currencyFormat);
r[4] = "TOTAL";
r[5] = "-"; r[6] = "-";
r[7] = currency + formatCurrency(total, currencyFormat);
table.addRow(r);
std::cout << table.draw();
}
}