Guest User

Untitled

a guest
Aug 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. const fs = require('fs');
  2.  
  3. const worker = async (filename) => {
  4. console.log(`Reading ${filename}`);
  5. fs.readFile(filename, (err, data) => {
  6. if (err) throw err;
  7. const lines = data.toString().split('\n');
  8. const currencies = {};
  9. const accounts = {};
  10. const openingBalances = [];
  11. let records = [];
  12. let i = 0, line, parts;
  13. line = lines[i];
  14.  
  15. const fullname = (account, name) => {
  16. if (account.parent > 0) {
  17. return fullname(accounts[account.parent], accounts[account.parent].name + ':' + name);
  18. } else {
  19. let cat;
  20. if (account.type === 2) cat = 'Income';
  21. if (account.type === 3) cat = 'Expense';
  22. if (account.type === 4 || account.type === 9) cat = 'Assets';
  23. if (account.liab) cat = 'Liabilities';
  24. return cat + ':' + name;
  25. }
  26. }
  27.  
  28. const sanitize = (str) => {
  29. return str.replace(/"/g, '');
  30. }
  31. while (i < lines.length - 1) {
  32. if (line.startsWith('[currency]')) {
  33. line = lines[++i];
  34. while (!line.startsWith('[') && i < lines.length) {
  35. parts = line.split(';');
  36. currencies[parts[0]] = { ticker: sanitize(parts[3]), name: sanitize(parts[1]) }
  37. line = lines[++i];
  38. }
  39. } else if (line.startsWith('[objects]')) {
  40. line = lines[++i];
  41. while (!line.startsWith('[') && i < lines.length) {
  42. parts = line.split(';');
  43. accounts[parts[0]] = {
  44. name: sanitize(parts[3]),
  45. parent: parts[1],
  46. type: parseInt(parts[2]),
  47. liab: parts[7] === 't'
  48. }
  49. line = lines[++i];
  50. }
  51. } else if (line.startsWith('[records]')) {
  52. Object.keys(accounts).map((k, i) => {
  53. accounts[k].fullname = fullname(accounts[k], accounts[k].name);
  54. });
  55.  
  56. line = lines[++i];
  57. while (line && !line.startsWith('[') && i < lines.length) {
  58. parts = line.split(';');
  59. if (parts.length > 4 && !parts[2].startsWith("-1")) {
  60. let account = accounts[parts[3]];
  61. let to = accounts[parts[2]];
  62. const amount = parseFloat(parts[0]) / 100;
  63. if (to.type === 2 || (to.type === 3 && amount > 0) ) { // income or rebate
  64. let sw = account;
  65. account = to;
  66. to = sw;
  67. }
  68. const isExpense = to.type === 3 && amount < 0;
  69. const date = new Date(parts[4]);
  70. const comment = sanitize(parts[5]);
  71. const prevRecord = records[records.length - 1];
  72. if (prevRecord
  73. && prevRecord.date.getTime() === date.getTime()
  74. && prevRecord.comment === comment
  75. && prevRecord.from === to.fullname && prevRecord.to === account.fullname
  76. && prevRecord.amount * amount < 0
  77. ) {
  78. prevRecord.transfer = true;
  79. if (prevRecord.amount > 0) {
  80. prevRecord.inAmount = amount;
  81. prevRecord.inCurrency = currencies[parts[1]].ticker;
  82. const sw = prevRecord.from;
  83. prevRecord.from = prevRecord.to;
  84. prevRecord.to = sw;
  85. } else {
  86. prevRecord.inAmount = prevRecord.amount;
  87. prevRecord.inCurrency = prevRecord.currency;
  88. prevRecord.amount = amount;
  89. prevRecord.currency = currencies[parts[1]].ticker;
  90. }
  91. } else {
  92. records.push({
  93. amount: isExpense ? Math.abs(amount) : amount,
  94. date,
  95. comment,
  96. from: account.fullname,
  97. to: to.fullname,
  98. expense: isExpense,
  99. currency: currencies[parts[1]].ticker
  100. });
  101. }
  102. } else if (parts[2].startsWith("-1")) {
  103. openingBalances.push({
  104. amount: parseFloat(parts[0]) / 100,
  105. acc: accounts[parts[3]].fullname,
  106. currency: currencies[parts[1]].ticker,
  107. date: new Date(parts[4])
  108. });
  109. }
  110. line = lines[++i];
  111. }
  112. }
  113. }
  114.  
  115. require('fs').writeFile('data.ledger', records.map(r => JSON.stringify(r, null, 2)).join('\n\r'), (err) => {
  116. if (err) return console.log(err);
  117. console.log(`Saved`);
  118. });
  119.  
  120. const pad = (i) => {
  121. if (i.toString().length == 1) return "0" + i;
  122. return i;
  123. }
  124.  
  125. ledgerRecords = records.reverse().map(r => {
  126. let rec = `
  127. ${r.date.getFullYear()}/${pad(r.date.getMonth() + 1)}/${pad(r.date.getDate())} ${r.comment}
  128. ${r.to} ${r.amount} ${r.currency}
  129. ${r.from}`;
  130. if (r.inAmount) {
  131. rec += '\t\t\t\t' + `${r.inAmount} ${r.inCurrency}`;
  132. }
  133. return rec;
  134. });
  135.  
  136. let ledgerOpening = openingBalances.map(b => {
  137. return `
  138. ${b.acc} ${b.amount} ${b.currency}`;
  139. });
  140.  
  141. ledgerOpening = `
  142. 2001/01/01 * Opening Balance`
  143. + ledgerOpening.join('') + `
  144. Equity:Opening Balances`;
  145.  
  146. require('fs').writeFile('t.dat', ledgerOpening + '\n\r\n\r' + ledgerRecords.join('\n\r\n\r'), (err) => {
  147. if (err) return console.log(err);
  148. console.log(`Saved`);
  149. });
  150.  
  151.  
  152. });
  153. }
  154.  
  155. worker(process.argv[2]);
Add Comment
Please, Sign In to add comment