@tasenor/common-node 1.9.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +4 -0
- package/LICENSE +21 -0
- package/dist/tasenor-common-node/src/cli.d.ts +81 -0
- package/dist/tasenor-common-node/src/cli.js +242 -0
- package/dist/tasenor-common-node/src/cli.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/account.d.ts +12 -0
- package/dist/tasenor-common-node/src/commands/account.js +58 -0
- package/dist/tasenor-common-node/src/commands/account.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/balance.d.ts +11 -0
- package/dist/tasenor-common-node/src/commands/balance.js +117 -0
- package/dist/tasenor-common-node/src/commands/balance.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/db.d.ts +14 -0
- package/dist/tasenor-common-node/src/commands/db.js +69 -0
- package/dist/tasenor-common-node/src/commands/db.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/entry.d.ts +13 -0
- package/dist/tasenor-common-node/src/commands/entry.js +106 -0
- package/dist/tasenor-common-node/src/commands/entry.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/import.d.ts +17 -0
- package/dist/tasenor-common-node/src/commands/import.js +140 -0
- package/dist/tasenor-common-node/src/commands/import.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/importer.d.ts +13 -0
- package/dist/tasenor-common-node/src/commands/importer.js +71 -0
- package/dist/tasenor-common-node/src/commands/importer.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/index.d.ts +191 -0
- package/dist/tasenor-common-node/src/commands/index.js +482 -0
- package/dist/tasenor-common-node/src/commands/index.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/period.d.ts +12 -0
- package/dist/tasenor-common-node/src/commands/period.js +48 -0
- package/dist/tasenor-common-node/src/commands/period.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/plugin.d.ts +15 -0
- package/dist/tasenor-common-node/src/commands/plugin.js +78 -0
- package/dist/tasenor-common-node/src/commands/plugin.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/report.d.ts +11 -0
- package/dist/tasenor-common-node/src/commands/report.js +96 -0
- package/dist/tasenor-common-node/src/commands/report.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/settings.d.ts +10 -0
- package/dist/tasenor-common-node/src/commands/settings.js +64 -0
- package/dist/tasenor-common-node/src/commands/settings.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/stock.d.ts +8 -0
- package/dist/tasenor-common-node/src/commands/stock.js +73 -0
- package/dist/tasenor-common-node/src/commands/stock.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/tag.d.ts +13 -0
- package/dist/tasenor-common-node/src/commands/tag.js +89 -0
- package/dist/tasenor-common-node/src/commands/tag.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/tx.d.ts +12 -0
- package/dist/tasenor-common-node/src/commands/tx.js +81 -0
- package/dist/tasenor-common-node/src/commands/tx.js.map +1 -0
- package/dist/tasenor-common-node/src/commands/user.d.ts +12 -0
- package/dist/tasenor-common-node/src/commands/user.js +52 -0
- package/dist/tasenor-common-node/src/commands/user.js.map +1 -0
- package/dist/tasenor-common-node/src/database/BookkeeperImporter.d.ts +77 -0
- package/dist/tasenor-common-node/src/database/BookkeeperImporter.js +343 -0
- package/dist/tasenor-common-node/src/database/BookkeeperImporter.js.map +1 -0
- package/dist/tasenor-common-node/src/database/DB.d.ts +51 -0
- package/dist/tasenor-common-node/src/database/DB.js +354 -0
- package/dist/tasenor-common-node/src/database/DB.js.map +1 -0
- package/dist/tasenor-common-node/src/database/index.d.ts +7 -0
- package/dist/tasenor-common-node/src/database/index.js +8 -0
- package/dist/tasenor-common-node/src/database/index.js.map +1 -0
- package/dist/tasenor-common-node/src/doccer.d.ts +29 -0
- package/dist/tasenor-common-node/src/doccer.js +30 -0
- package/dist/tasenor-common-node/src/doccer.js.map +1 -0
- package/dist/tasenor-common-node/src/error.d.ts +30 -0
- package/dist/tasenor-common-node/src/error.js +35 -0
- package/dist/tasenor-common-node/src/error.js.map +1 -0
- package/dist/tasenor-common-node/src/export/Exporter.d.ts +69 -0
- package/dist/tasenor-common-node/src/export/Exporter.js +123 -0
- package/dist/tasenor-common-node/src/export/Exporter.js.map +1 -0
- package/dist/tasenor-common-node/src/export/TasenorExporter.d.ts +55 -0
- package/dist/tasenor-common-node/src/export/TasenorExporter.js +135 -0
- package/dist/tasenor-common-node/src/export/TasenorExporter.js.map +1 -0
- package/dist/tasenor-common-node/src/export/TilitinExporter.d.ts +71 -0
- package/dist/tasenor-common-node/src/export/TilitinExporter.js +290 -0
- package/dist/tasenor-common-node/src/export/TilitinExporter.js.map +1 -0
- package/dist/tasenor-common-node/src/export/index.d.ts +8 -0
- package/dist/tasenor-common-node/src/export/index.js +9 -0
- package/dist/tasenor-common-node/src/export/index.js.map +1 -0
- package/dist/tasenor-common-node/src/import/TextFileProcessHandler.d.ts +104 -0
- package/dist/tasenor-common-node/src/import/TextFileProcessHandler.js +354 -0
- package/dist/tasenor-common-node/src/import/TextFileProcessHandler.js.map +1 -0
- package/dist/tasenor-common-node/src/import/TransactionImportConnector.d.ts +38 -0
- package/dist/tasenor-common-node/src/import/TransactionImportConnector.js +27 -0
- package/dist/tasenor-common-node/src/import/TransactionImportConnector.js.map +1 -0
- package/dist/tasenor-common-node/src/import/TransactionImportHandler.d.ts +173 -0
- package/dist/tasenor-common-node/src/import/TransactionImportHandler.js +733 -0
- package/dist/tasenor-common-node/src/import/TransactionImportHandler.js.map +1 -0
- package/dist/tasenor-common-node/src/import/TransactionRules.d.ts +238 -0
- package/dist/tasenor-common-node/src/import/TransactionRules.js +522 -0
- package/dist/tasenor-common-node/src/import/TransactionRules.js.map +1 -0
- package/dist/tasenor-common-node/src/import/TransactionUI.d.ts +181 -0
- package/dist/tasenor-common-node/src/import/TransactionUI.js +482 -0
- package/dist/tasenor-common-node/src/import/TransactionUI.js.map +1 -0
- package/dist/tasenor-common-node/src/import/TransferAnalyzer.d.ts +324 -0
- package/dist/tasenor-common-node/src/import/TransferAnalyzer.js +1379 -0
- package/dist/tasenor-common-node/src/import/TransferAnalyzer.js.map +1 -0
- package/dist/tasenor-common-node/src/import/index.d.ts +11 -0
- package/dist/tasenor-common-node/src/import/index.js +12 -0
- package/dist/tasenor-common-node/src/import/index.js.map +1 -0
- package/dist/tasenor-common-node/src/index.d.ts +12 -0
- package/dist/tasenor-common-node/src/index.js +13 -0
- package/dist/tasenor-common-node/src/index.js.map +1 -0
- package/dist/tasenor-common-node/src/net/crypto.d.ts +33 -0
- package/dist/tasenor-common-node/src/net/crypto.js +63 -0
- package/dist/tasenor-common-node/src/net/crypto.js.map +1 -0
- package/dist/tasenor-common-node/src/net/git.d.ts +49 -0
- package/dist/tasenor-common-node/src/net/git.js +137 -0
- package/dist/tasenor-common-node/src/net/git.js.map +1 -0
- package/dist/tasenor-common-node/src/net/index.d.ts +10 -0
- package/dist/tasenor-common-node/src/net/index.js +11 -0
- package/dist/tasenor-common-node/src/net/index.js.map +1 -0
- package/dist/tasenor-common-node/src/net/middleware.d.ts +61 -0
- package/dist/tasenor-common-node/src/net/middleware.js +220 -0
- package/dist/tasenor-common-node/src/net/middleware.js.map +1 -0
- package/dist/tasenor-common-node/src/net/tokens.d.ts +50 -0
- package/dist/tasenor-common-node/src/net/tokens.js +141 -0
- package/dist/tasenor-common-node/src/net/tokens.js.map +1 -0
- package/dist/tasenor-common-node/src/net/vault.d.ts +67 -0
- package/dist/tasenor-common-node/src/net/vault.js +145 -0
- package/dist/tasenor-common-node/src/net/vault.js.map +1 -0
- package/dist/tasenor-common-node/src/plugins/BackendPlugin.d.ts +91 -0
- package/dist/tasenor-common-node/src/plugins/BackendPlugin.js +165 -0
- package/dist/tasenor-common-node/src/plugins/BackendPlugin.js.map +1 -0
- package/dist/tasenor-common-node/src/plugins/DataPlugin.d.ts +13 -0
- package/dist/tasenor-common-node/src/plugins/DataPlugin.js +26 -0
- package/dist/tasenor-common-node/src/plugins/DataPlugin.js.map +1 -0
- package/dist/tasenor-common-node/src/plugins/ImportPlugin.d.ts +188 -0
- package/dist/tasenor-common-node/src/plugins/ImportPlugin.js +204 -0
- package/dist/tasenor-common-node/src/plugins/ImportPlugin.js.map +1 -0
- package/dist/tasenor-common-node/src/plugins/ReportPlugin.d.ts +132 -0
- package/dist/tasenor-common-node/src/plugins/ReportPlugin.js +393 -0
- package/dist/tasenor-common-node/src/plugins/ReportPlugin.js.map +1 -0
- package/dist/tasenor-common-node/src/plugins/SchemePlugin.d.ts +34 -0
- package/dist/tasenor-common-node/src/plugins/SchemePlugin.js +47 -0
- package/dist/tasenor-common-node/src/plugins/SchemePlugin.js.map +1 -0
- package/dist/tasenor-common-node/src/plugins/ServicePlugin.d.ts +80 -0
- package/dist/tasenor-common-node/src/plugins/ServicePlugin.js +168 -0
- package/dist/tasenor-common-node/src/plugins/ServicePlugin.js.map +1 -0
- package/dist/tasenor-common-node/src/plugins/ToolPlugin.d.ts +27 -0
- package/dist/tasenor-common-node/src/plugins/ToolPlugin.js +37 -0
- package/dist/tasenor-common-node/src/plugins/ToolPlugin.js.map +1 -0
- package/dist/tasenor-common-node/src/plugins/index.d.ts +13 -0
- package/dist/tasenor-common-node/src/plugins/index.js +14 -0
- package/dist/tasenor-common-node/src/plugins/index.js.map +1 -0
- package/dist/tasenor-common-node/src/plugins/plugins.d.ts +101 -0
- package/dist/tasenor-common-node/src/plugins/plugins.js +292 -0
- package/dist/tasenor-common-node/src/plugins/plugins.js.map +1 -0
- package/dist/tasenor-common-node/src/process/Process.d.ts +108 -0
- package/dist/tasenor-common-node/src/process/Process.js +335 -0
- package/dist/tasenor-common-node/src/process/Process.js.map +1 -0
- package/dist/tasenor-common-node/src/process/ProcessConnector.d.ts +24 -0
- package/dist/tasenor-common-node/src/process/ProcessConnector.js +28 -0
- package/dist/tasenor-common-node/src/process/ProcessConnector.js.map +1 -0
- package/dist/tasenor-common-node/src/process/ProcessFile.d.ts +69 -0
- package/dist/tasenor-common-node/src/process/ProcessFile.js +145 -0
- package/dist/tasenor-common-node/src/process/ProcessFile.js.map +1 -0
- package/dist/tasenor-common-node/src/process/ProcessHandler.d.ts +60 -0
- package/dist/tasenor-common-node/src/process/ProcessHandler.js +73 -0
- package/dist/tasenor-common-node/src/process/ProcessHandler.js.map +1 -0
- package/dist/tasenor-common-node/src/process/ProcessStep.d.ts +52 -0
- package/dist/tasenor-common-node/src/process/ProcessStep.js +78 -0
- package/dist/tasenor-common-node/src/process/ProcessStep.js.map +1 -0
- package/dist/tasenor-common-node/src/process/ProcessingSystem.d.ts +60 -0
- package/dist/tasenor-common-node/src/process/ProcessingSystem.js +182 -0
- package/dist/tasenor-common-node/src/process/ProcessingSystem.js.map +1 -0
- package/dist/tasenor-common-node/src/process/index.d.ts +11 -0
- package/dist/tasenor-common-node/src/process/index.js +12 -0
- package/dist/tasenor-common-node/src/process/index.js.map +1 -0
- package/dist/tasenor-common-node/src/reports/conversions.d.ts +8 -0
- package/dist/tasenor-common-node/src/reports/conversions.js +47 -0
- package/dist/tasenor-common-node/src/reports/conversions.js.map +1 -0
- package/dist/tasenor-common-node/src/reports/index.d.ts +6 -0
- package/dist/tasenor-common-node/src/reports/index.js +7 -0
- package/dist/tasenor-common-node/src/reports/index.js.map +1 -0
- package/dist/tasenor-common-node/src/server/ISPDemoServer.d.ts +43 -0
- package/dist/tasenor-common-node/src/server/ISPDemoServer.js +112 -0
- package/dist/tasenor-common-node/src/server/ISPDemoServer.js.map +1 -0
- package/dist/tasenor-common-node/src/server/api.d.ts +15 -0
- package/dist/tasenor-common-node/src/server/api.js +27 -0
- package/dist/tasenor-common-node/src/server/api.js.map +1 -0
- package/dist/tasenor-common-node/src/server/index.d.ts +7 -0
- package/dist/tasenor-common-node/src/server/index.js +8 -0
- package/dist/tasenor-common-node/src/server/index.js.map +1 -0
- package/dist/tasenor-common-node/src/server/router.d.ts +5 -0
- package/dist/tasenor-common-node/src/server/router.js +37 -0
- package/dist/tasenor-common-node/src/server/router.js.map +1 -0
- package/dist/tasenor-common-node/src/system.d.ts +27 -0
- package/dist/tasenor-common-node/src/system.js +95 -0
- package/dist/tasenor-common-node/src/system.js.map +1 -0
- package/dist/tasenor-common-node/src/testing/ProcessingSystemMock.d.ts +21 -0
- package/dist/tasenor-common-node/src/testing/ProcessingSystemMock.js +33 -0
- package/dist/tasenor-common-node/src/testing/ProcessingSystemMock.js.map +1 -0
- package/dist/tasenor-common-node/src/testing/UnitTestImportConnector.d.ts +24 -0
- package/dist/tasenor-common-node/src/testing/UnitTestImportConnector.js +68 -0
- package/dist/tasenor-common-node/src/testing/UnitTestImportConnector.js.map +1 -0
- package/dist/tasenor-common-node/src/testing/UnitTester.d.ts +64 -0
- package/dist/tasenor-common-node/src/testing/UnitTester.js +199 -0
- package/dist/tasenor-common-node/src/testing/UnitTester.js.map +1 -0
- package/dist/tasenor-common-node/src/testing/index.d.ts +4 -0
- package/dist/tasenor-common-node/src/testing/index.js +5 -0
- package/dist/tasenor-common-node/src/testing/index.js.map +1 -0
- package/dist/tasenor-common-node/src/testing/test-handlers.d.ts +13 -0
- package/dist/tasenor-common-node/src/testing/test-handlers.js +52 -0
- package/dist/tasenor-common-node/src/testing/test-handlers.js.map +1 -0
- package/dist/tasenor-common-node/tests/TransactionRules.spec.d.ts +1 -0
- package/dist/tasenor-common-node/tests/TransactionRules.spec.js +64 -0
- package/dist/tasenor-common-node/tests/TransactionRules.spec.js.map +1 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-account-address.spec.d.ts +1 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-account-address.spec.js +80 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-account-address.spec.js.map +1 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-buying-and-selling.spec.d.ts +1 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-buying-and-selling.spec.js +342 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-buying-and-selling.spec.js.map +1 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-loans.spec.d.ts +1 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-loans.spec.js +174 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-loans.spec.js.map +1 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-multiple-null-amounts.spec.d.ts +1 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-multiple-null-amounts.spec.js +175 -0
- package/dist/tasenor-common-node/tests/TransferAnalyzer-multiple-null-amounts.spec.js.map +1 -0
- package/dist/tasenor-common-node/tests/password.spec.d.ts +1 -0
- package/dist/tasenor-common-node/tests/password.spec.js +8 -0
- package/dist/tasenor-common-node/tests/password.spec.js.map +1 -0
- package/dist/tasenor-common-node/tests/tokens.spec.d.ts +1 -0
- package/dist/tasenor-common-node/tests/tokens.spec.js +49 -0
- package/dist/tasenor-common-node/tests/tokens.spec.js.map +1 -0
- package/dist/tasenor-common-node/tests/vault.spec.d.ts +1 -0
- package/dist/tasenor-common-node/tests/vault.spec.js +19 -0
- package/dist/tasenor-common-node/tests/vault.spec.js.map +1 -0
- package/dist/tasenor-common-plugins/src/CoinbaseImport/backend/CoinbaseHandler.d.ts +11 -0
- package/dist/tasenor-common-plugins/src/CoinbaseImport/backend/CoinbaseHandler.js +30 -0
- package/dist/tasenor-common-plugins/src/CoinbaseImport/backend/CoinbaseHandler.js.map +1 -0
- package/dist/tasenor-common-plugins/src/IncomeAndExpenses/backend/index.d.ts +5 -0
- package/dist/tasenor-common-plugins/src/IncomeAndExpenses/backend/index.js +350 -0
- package/dist/tasenor-common-plugins/src/IncomeAndExpenses/backend/index.js.map +1 -0
- package/dist/tasenor-common-plugins/src/KrakenImport/backend/KrakenHandler.d.ts +23 -0
- package/dist/tasenor-common-plugins/src/KrakenImport/backend/KrakenHandler.js +83 -0
- package/dist/tasenor-common-plugins/src/KrakenImport/backend/KrakenHandler.js.map +1 -0
- package/dist/tasenor-common-plugins/src/LynxImport/backend/LynxHandler.d.ts +28 -0
- package/dist/tasenor-common-plugins/src/LynxImport/backend/LynxHandler.js +340 -0
- package/dist/tasenor-common-plugins/src/LynxImport/backend/LynxHandler.js.map +1 -0
- package/dist/tasenor-common-plugins/src/NordeaImport/backend/NordeaHandler.d.ts +11 -0
- package/dist/tasenor-common-plugins/src/NordeaImport/backend/NordeaHandler.js +39 -0
- package/dist/tasenor-common-plugins/src/NordeaImport/backend/NordeaHandler.js.map +1 -0
- package/dist/tasenor-common-plugins/src/NordnetImport/backend/NordnetHandler.d.ts +17 -0
- package/dist/tasenor-common-plugins/src/NordnetImport/backend/NordnetHandler.js +66 -0
- package/dist/tasenor-common-plugins/src/NordnetImport/backend/NordnetHandler.js.map +1 -0
- package/dist/tasenor-common-plugins/src/TITOImport/backend/TITOHandler.d.ts +13 -0
- package/dist/tasenor-common-plugins/src/TITOImport/backend/TITOHandler.js +241 -0
- package/dist/tasenor-common-plugins/src/TITOImport/backend/TITOHandler.js.map +1 -0
- package/jest.config.js +1 -0
- package/package.json +62 -0
- package/src/cli.ts +267 -0
- package/src/commands/account.ts +69 -0
- package/src/commands/balance.ts +131 -0
- package/src/commands/db.ts +84 -0
- package/src/commands/entry.ts +117 -0
- package/src/commands/import.ts +160 -0
- package/src/commands/importer.ts +84 -0
- package/src/commands/index.ts +534 -0
- package/src/commands/period.ts +59 -0
- package/src/commands/plugin.ts +95 -0
- package/src/commands/report.ts +113 -0
- package/src/commands/settings.ts +75 -0
- package/src/commands/stock.ts +80 -0
- package/src/commands/tag.ts +102 -0
- package/src/commands/tx.ts +93 -0
- package/src/commands/user.ts +65 -0
- package/src/database/BookkeeperImporter.ts +358 -0
- package/src/database/DB.ts +396 -0
- package/src/database/index.ts +7 -0
- package/src/doccer.ts +29 -0
- package/src/error.ts +32 -0
- package/src/export/Exporter.ts +136 -0
- package/src/export/TasenorExporter.ts +144 -0
- package/src/export/TilitinExporter.ts +302 -0
- package/src/export/index.ts +8 -0
- package/src/import/TextFileProcessHandler.ts +384 -0
- package/src/import/TransactionImportConnector.ts +65 -0
- package/src/import/TransactionImportHandler.ts +819 -0
- package/src/import/TransactionRules.ts +570 -0
- package/src/import/TransactionUI.ts +520 -0
- package/src/import/TransferAnalyzer.ts +1450 -0
- package/src/import/index.ts +11 -0
- package/src/index.ts +12 -0
- package/src/net/crypto.ts +69 -0
- package/src/net/git.ts +151 -0
- package/src/net/index.ts +10 -0
- package/src/net/middleware.ts +261 -0
- package/src/net/tokens.ts +140 -0
- package/src/net/vault.ts +161 -0
- package/src/plugins/BackendPlugin.ts +188 -0
- package/src/plugins/DataPlugin.ts +29 -0
- package/src/plugins/ImportPlugin.ts +211 -0
- package/src/plugins/ReportPlugin.ts +443 -0
- package/src/plugins/SchemePlugin.ts +56 -0
- package/src/plugins/ServicePlugin.ts +188 -0
- package/src/plugins/ToolPlugin.ts +44 -0
- package/src/plugins/index.ts +13 -0
- package/src/plugins/plugins.ts +345 -0
- package/src/process/Process.ts +368 -0
- package/src/process/ProcessConnector.ts +45 -0
- package/src/process/ProcessFile.ts +169 -0
- package/src/process/ProcessHandler.ts +94 -0
- package/src/process/ProcessStep.ts +100 -0
- package/src/process/ProcessingSystem.ts +202 -0
- package/src/process/index.ts +11 -0
- package/src/reports/conversions.ts +52 -0
- package/src/reports/index.ts +6 -0
- package/src/server/ISPDemoServer.ts +122 -0
- package/src/server/api.ts +37 -0
- package/src/server/index.ts +7 -0
- package/src/server/router.ts +60 -0
- package/src/system.ts +96 -0
- package/src/testing/ProcessingSystemMock.ts +45 -0
- package/src/testing/UnitTestImportConnector.ts +86 -0
- package/src/testing/UnitTester.ts +231 -0
- package/src/testing/index.ts +4 -0
- package/src/testing/test-handlers.ts +55 -0
- package/tests/TransactionRules.spec.ts +73 -0
- package/tests/TransferAnalyzer-account-address.spec.ts +87 -0
- package/tests/TransferAnalyzer-buying-and-selling.spec.ts +354 -0
- package/tests/TransferAnalyzer-loans.spec.ts +197 -0
- package/tests/TransferAnalyzer-multiple-null-amounts.spec.ts +181 -0
- package/tests/password.spec.ts +8 -0
- package/tests/tokens.spec.ts +52 -0
- package/tests/vault.spec.ts +20 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
import { TasenorElement, AssetTransfer, isAssetTransfer, Language, RuleParsingError, RulesEngine, TransactionDescription, UIQuery, isUIQueryRef, warning, ImportRule, ImportRuleResult, Currency, AssetTransferReason, debug, error, Tag, ImportSegment, ProcessConfig, SegmentId, TextFileLine, RuleVariables } from '@dataplug/tasenor-common'
|
|
2
|
+
import { TransactionUI } from './TransactionUI'
|
|
3
|
+
import { TransactionImportHandler } from './TransactionImportHandler'
|
|
4
|
+
import clone from 'clone'
|
|
5
|
+
import { BadState, SystemError } from '../error'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* ## Transaction rule system
|
|
9
|
+
*
|
|
10
|
+
* The classification of the import data uses rule system describing how to transform segmented
|
|
11
|
+
* data to *transfers*, i.e. generic description of bookkeeping events. Initially in the beginning
|
|
12
|
+
* of the processing the settings and rules defined for the particular importer are copied to the
|
|
13
|
+
* initial state of the process. During the processing we may ask questions and add more information
|
|
14
|
+
* to the process.
|
|
15
|
+
*
|
|
16
|
+
* So the structure of the configration is
|
|
17
|
+
* ```json
|
|
18
|
+
* {
|
|
19
|
+
* "language": "fi",
|
|
20
|
+
* "currency": "EUR",
|
|
21
|
+
* ...
|
|
22
|
+
* "account.income.currency.EUR": "1910",
|
|
23
|
+
* ...
|
|
24
|
+
* "rules": [...],
|
|
25
|
+
* "questions": {...},
|
|
26
|
+
* "answers": {...}
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
* There are
|
|
30
|
+
* 1. Generic universal settings like *language* or *currency*.
|
|
31
|
+
* 2. Then there is an account and import setting configuration that has been possibly resolved during the import process
|
|
32
|
+
* by asking from user, but which will also apply universally afterwards and are copied to the future import
|
|
33
|
+
* configuration.
|
|
34
|
+
* 3. **Rules** section defines how to map segments to transfers.
|
|
35
|
+
* 4. **Question** section defines UI questions to resolve some cases, that always require user interaction and
|
|
36
|
+
* cannot be resolved automatically.
|
|
37
|
+
* 5. **Answers** section is a collection of responses to questions stored by each segment. They are not universally
|
|
38
|
+
* copied to the importer, but are only relevant the current import only.
|
|
39
|
+
*
|
|
40
|
+
* ### Settings
|
|
41
|
+
*
|
|
42
|
+
* The following general settings are used
|
|
43
|
+
* - `currency` - A main currency of the bookkeeping database.
|
|
44
|
+
* - `language` - A translation language for the imported texts.
|
|
45
|
+
* - `tags.*.*.*` - A list of tags to be added for every transaction descriptions. Also some specific tags
|
|
46
|
+
* can be specified, since `*.*.*` uses the same convention than account configurations.
|
|
47
|
+
*
|
|
48
|
+
* Accounts are defined as the following.
|
|
49
|
+
* - `account.<reason>.<type>.<asset>` - Defines the account number to be used for the given purpose.
|
|
50
|
+
* Parts can be `'*'` to allow any purpose. Otherwise they are
|
|
51
|
+
* explained in more detail in {@link TransferAnalyzer}.
|
|
52
|
+
*
|
|
53
|
+
* Miscellaneous optional settings:
|
|
54
|
+
* - `isTradeFeePartOfTotal` If set to `true`, assume that trading fee is included in the total.
|
|
55
|
+
* Otherwise it is assumed to be paid on the top of the total.
|
|
56
|
+
* - `recordDeposits` If set to false, skip deposits.
|
|
57
|
+
* - `recordWithdrawals` If set to false, skip withdrawls.
|
|
58
|
+
* - `allowShortSelling` If set, allow short selling, i.e. selling assets we don't have.
|
|
59
|
+
*
|
|
60
|
+
* #### Example
|
|
61
|
+
* ```json
|
|
62
|
+
* {
|
|
63
|
+
* "currency": "EUR",
|
|
64
|
+
* "language": "en",
|
|
65
|
+
* "tags.*.*.*": ["Lynx"],
|
|
66
|
+
* "account.deposit.currency.EUR": "1918",
|
|
67
|
+
* "account.deposit.external.EUR": "9999",
|
|
68
|
+
* "account.withdrawal.currency.EUR": "1918",
|
|
69
|
+
* "account.withdrawal.external.EUR": "9999",
|
|
70
|
+
* "account.expense.statement.INTEREST_EXPENSE": "9550",
|
|
71
|
+
* "account.expense.currency.EUR": "1918",
|
|
72
|
+
*
|
|
73
|
+
* "rules": [],
|
|
74
|
+
* "questions": [],
|
|
75
|
+
* "answers": {}
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* ### Rules
|
|
80
|
+
*
|
|
81
|
+
* Rules sections is a list of rule definitions of form
|
|
82
|
+
* ```json
|
|
83
|
+
* {
|
|
84
|
+
* "name": "Name of the rule",
|
|
85
|
+
* "filter": "<expression>",
|
|
86
|
+
* "comment": "<optional description>",
|
|
87
|
+
* "options": {
|
|
88
|
+
* <optional flags>
|
|
89
|
+
* },
|
|
90
|
+
* "result": [
|
|
91
|
+
* <transfer1>, <transfer2>...
|
|
92
|
+
* ]
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
* The *name* is any string describing the rule. Rules are used so that each segment resulting from the segmentation
|
|
96
|
+
* step are handled in the order of their timestamps. Lines belonging to the segment are offered one by one to the
|
|
97
|
+
* *filter* expression and if returning true, the entries in *result* are concatenated together. Each entry in the
|
|
98
|
+
* result is a *transfer description*.
|
|
99
|
+
*
|
|
100
|
+
* The filtering and result expressions has various variables set during the processing. All variables from
|
|
101
|
+
* the segmentation is included. Typically they are the same as the column names in the CSV file for example.
|
|
102
|
+
* See {@link TransactionRules.classifyLines} for other variables available.
|
|
103
|
+
*
|
|
104
|
+
* The structure of transfers are explained in {@link TransferAnalyzer}.
|
|
105
|
+
*
|
|
106
|
+
* The syntax of the filter and result is explained in {@link RulesEngine}.
|
|
107
|
+
*
|
|
108
|
+
* Currently one one boolean option is supported: `singleMatch` which means that matching any of the lines in the
|
|
109
|
+
* segment suffices and the parsing result is returned immediately when matching rule is found. The rest of the
|
|
110
|
+
* lines are ignored. It is useful when for example using `sum(lines, 'field')` to gather values from all lines
|
|
111
|
+
* of the segment at once.
|
|
112
|
+
*
|
|
113
|
+
* ### Questions
|
|
114
|
+
*
|
|
115
|
+
* There are situation, where importer cannot deduct some part of the transfer automatically. In that case we can
|
|
116
|
+
* define a question that needs to be answered every time, when the matching rule has been found. For example
|
|
117
|
+
* we may determine based on the transaction data that it is related to computers but we want to know the exact
|
|
118
|
+
* type of the purchase. Then we can define a question
|
|
119
|
+
* ```json
|
|
120
|
+
* {
|
|
121
|
+
* "name": "Computer purchase",
|
|
122
|
+
* "label": "What category is the purchase",
|
|
123
|
+
* "ask": {
|
|
124
|
+
* "Hardware equipment": "HARDWARE",
|
|
125
|
+
* "Software": "SOFTWARE"
|
|
126
|
+
* }
|
|
127
|
+
* },
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* The question can be used in the transfer as explained in {@link TransferAnalyzer}.
|
|
131
|
+
*
|
|
132
|
+
* Different question types are documented in {@link TransactionUI.parseQuery}.
|
|
133
|
+
*
|
|
134
|
+
* ### Answers
|
|
135
|
+
*
|
|
136
|
+
* This section collects answers given earlier during the processing. They are grouped per segment ID per transfer.
|
|
137
|
+
* For example
|
|
138
|
+
* ```json
|
|
139
|
+
* "d3e89d9af37dda4609bed94770fc5c52be946175": {
|
|
140
|
+
* "type": "HARDWARE"
|
|
141
|
+
* },
|
|
142
|
+
* ```
|
|
143
|
+
* It may contain also complete transaction definition, which will override all parsing
|
|
144
|
+
* ```
|
|
145
|
+
* "581e46d024678ddcddc01ae36369bf6fc54f16b2": {
|
|
146
|
+
* "transfers": [
|
|
147
|
+
* {
|
|
148
|
+
* "data": {
|
|
149
|
+
* "text": "Payment for something"
|
|
150
|
+
* },
|
|
151
|
+
* "type": "account",
|
|
152
|
+
* "asset": "8650",
|
|
153
|
+
* "amount": 59.27,
|
|
154
|
+
* "reason": "expense"
|
|
155
|
+
* },
|
|
156
|
+
* {
|
|
157
|
+
* "type": "account",
|
|
158
|
+
* "asset": "3020",
|
|
159
|
+
* "amount": -59.27,
|
|
160
|
+
* "reason": "expense"
|
|
161
|
+
* }
|
|
162
|
+
* ]
|
|
163
|
+
* }
|
|
164
|
+
* ```
|
|
165
|
+
* There is also a global answer section applied to all imports. If an asset has changed its name, it can be
|
|
166
|
+
* stored like this in the empty segment ID:
|
|
167
|
+
* ```
|
|
168
|
+
* "": {
|
|
169
|
+
* "asset-renaming": [
|
|
170
|
+
* {
|
|
171
|
+
* "date": "<YYYY-MM-DD>"
|
|
172
|
+
* "type": "stock",
|
|
173
|
+
* "old": "<OLD ASSET>"
|
|
174
|
+
* "new": "<NEW ASSET>"
|
|
175
|
+
* }
|
|
176
|
+
* ]
|
|
177
|
+
* }
|
|
178
|
+
* }
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export class TransactionRules {
|
|
182
|
+
// TODO: Could also define extension from ProcessConfig configuration type definition and start building precise description.
|
|
183
|
+
// Most of the use cases in files of this dir, are referring to the import configuration.
|
|
184
|
+
private handler: TransactionImportHandler
|
|
185
|
+
private UI: TransactionUI
|
|
186
|
+
private cache: Record<string, UIQuery>
|
|
187
|
+
constructor(handler: TransactionImportHandler) {
|
|
188
|
+
this.handler = handler
|
|
189
|
+
this.UI = handler.UI
|
|
190
|
+
this.clearCache()
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Clear colleciton of named UI questions.
|
|
195
|
+
*/
|
|
196
|
+
clearCache() {
|
|
197
|
+
this.cache = {}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Handle query caching.
|
|
202
|
+
* @param query
|
|
203
|
+
*
|
|
204
|
+
* If query has no name, we do nothing. Return query itself.
|
|
205
|
+
* Otherwise it depends if query has anything else but name.
|
|
206
|
+
* For name-only we look from cache and throw error if not found.
|
|
207
|
+
* Otherwise it is saved to cache.
|
|
208
|
+
*/
|
|
209
|
+
cachedQuery(query: UIQuery): UIQuery {
|
|
210
|
+
if (query.name) {
|
|
211
|
+
if (isUIQueryRef(query)) {
|
|
212
|
+
if (!this.cache[query.name]) {
|
|
213
|
+
throw new BadState(`Cannot use a reference to question '${query.name}' before it is defined.`)
|
|
214
|
+
}
|
|
215
|
+
return this.cache[query.name]
|
|
216
|
+
} else {
|
|
217
|
+
this.cache[query.name] = query
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return query
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Collect answers for questions or if not yet given, throw new query to get them.
|
|
225
|
+
* @param questions
|
|
226
|
+
* @param config
|
|
227
|
+
*/
|
|
228
|
+
async getAnswers(segmentId: SegmentId, lines: TextFileLine[], questions: Record<string, UIQuery>, config: ProcessConfig): Promise<Record<string, unknown>> {
|
|
229
|
+
|
|
230
|
+
// Check existing answers.
|
|
231
|
+
const language = config.language as Language
|
|
232
|
+
const results: Record<string, unknown> = {}
|
|
233
|
+
const missing: TasenorElement[] = []
|
|
234
|
+
for (let [variable, query] of Object.entries(questions)) {
|
|
235
|
+
query = this.cachedQuery(query)
|
|
236
|
+
|
|
237
|
+
const answers: Record<string, Record<string, unknown>> = config.answers as Record<string, Record<string, unknown>> || {}
|
|
238
|
+
if (segmentId in answers && variable in answers[segmentId]) {
|
|
239
|
+
results[variable] = answers[segmentId][variable]
|
|
240
|
+
} else {
|
|
241
|
+
missing.push(await this.UI.parseQuery(`answer.${segmentId}.${variable}`, query, language))
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// If not all answered, ask them.
|
|
246
|
+
if (missing.length) {
|
|
247
|
+
const element: TasenorElement = {
|
|
248
|
+
type: 'flat',
|
|
249
|
+
elements: [
|
|
250
|
+
await this.UI.describeLines(lines, language),
|
|
251
|
+
...missing,
|
|
252
|
+
await this.UI.submit('Continue', 2, language)
|
|
253
|
+
]
|
|
254
|
+
}
|
|
255
|
+
this.UI.throw(element)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return results
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Use the rules from the configuration to classify importer transfer lines.
|
|
263
|
+
* @param lines
|
|
264
|
+
* @param config
|
|
265
|
+
* @returns
|
|
266
|
+
*
|
|
267
|
+
* Each rule is checked against each line.
|
|
268
|
+
* For evaluation of the filter expression, all column values of the segment are provided.
|
|
269
|
+
* In addition the following special variables are provided:
|
|
270
|
+
* * `config` - all configuration variables
|
|
271
|
+
* * `rule` - the current rule we are evaluating
|
|
272
|
+
* * `options` - the options of the current rule we are evaluating
|
|
273
|
+
* * `text` - original text of the corresponding line
|
|
274
|
+
* * `lineNumber` - original line number of the corresponding line
|
|
275
|
+
* If the filter match is found, then questions are provided to UI unless already
|
|
276
|
+
* answered. The reponses to the questions are passed to the any further evaluations.
|
|
277
|
+
*/
|
|
278
|
+
async classifyLines(lines: TextFileLine[], config: ProcessConfig, segment: ImportSegment): Promise<TransactionDescription> {
|
|
279
|
+
|
|
280
|
+
let transfers: AssetTransfer[] = []
|
|
281
|
+
const rules: ImportRule[] = config.rules as ImportRule[] || []
|
|
282
|
+
const engine = new RulesEngine()
|
|
283
|
+
let matched = false
|
|
284
|
+
|
|
285
|
+
// Make private copy.
|
|
286
|
+
config = clone(config)
|
|
287
|
+
// Cache questions.
|
|
288
|
+
if (config.questions) {
|
|
289
|
+
(config.questions as UIQuery[]).forEach(q => this.cachedQuery(q))
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
debug('RULES', '============================================================')
|
|
293
|
+
debug('RULES', 'Classifying segment', segment.id)
|
|
294
|
+
debug('RULES', '============================================================')
|
|
295
|
+
|
|
296
|
+
// Check if we have explicit answer for the segment.
|
|
297
|
+
const explicit = await this.checkExplicitResult(segment, config.answers)
|
|
298
|
+
if (explicit) {
|
|
299
|
+
return explicit
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
try {
|
|
303
|
+
|
|
304
|
+
const lineValues = lines.map(line => clone(line.columns))
|
|
305
|
+
|
|
306
|
+
let index = 0
|
|
307
|
+
for (const line of lines) {
|
|
308
|
+
let lineHasMatch = false
|
|
309
|
+
|
|
310
|
+
const columns = lineValues[index++]
|
|
311
|
+
|
|
312
|
+
debug('RULES', '-----------------------------------------------------')
|
|
313
|
+
debug('RULES', line.text)
|
|
314
|
+
debug('RULES', '-----------------------------------------------------')
|
|
315
|
+
debug('RULES', columns)
|
|
316
|
+
|
|
317
|
+
// Find the rule that has matching filter expression.
|
|
318
|
+
for (let rule of rules) {
|
|
319
|
+
|
|
320
|
+
rule = clone(rule)
|
|
321
|
+
|
|
322
|
+
const values: RuleVariables = {
|
|
323
|
+
...columns,
|
|
324
|
+
lines: lineValues,
|
|
325
|
+
config,
|
|
326
|
+
rule,
|
|
327
|
+
options: rule.options || {},
|
|
328
|
+
text: line.text,
|
|
329
|
+
lineNumber: line.line
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const singleMatch = rule.options && rule.options.singleMatch
|
|
333
|
+
|
|
334
|
+
if (engine.eval(rule.filter, values)) {
|
|
335
|
+
debug('RULES', 'Rule', rule.name, 'with filter', rule.filter, 'matches.')
|
|
336
|
+
matched = true
|
|
337
|
+
lineHasMatch = true
|
|
338
|
+
// Check that result is defined.
|
|
339
|
+
if (!rule.result) {
|
|
340
|
+
throw new BadState(`The rule ${JSON.stringify(rule)} has no result section.`)
|
|
341
|
+
}
|
|
342
|
+
// We have found the match. Now construct transfers from the rule.
|
|
343
|
+
const answers = rule.questions ? await this.getAnswers(segment.id, lines, rule.questions, config) : {}
|
|
344
|
+
|
|
345
|
+
// Replace cached queries to the variables passed to the rule engine.
|
|
346
|
+
if (rule.questions) {
|
|
347
|
+
const q = rule.questions as Record<string, UIQuery<unknown>>
|
|
348
|
+
Object.keys(q).forEach(key => {
|
|
349
|
+
q[key] = this.cachedQuery(q[key])
|
|
350
|
+
})
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
transfers = transfers.concat(this.parseResults(engine, lines, rule, values, answers as RuleVariables))
|
|
354
|
+
|
|
355
|
+
// Continue to next line unless single match.
|
|
356
|
+
if (singleMatch) {
|
|
357
|
+
return await this.postProcess(segment, {
|
|
358
|
+
type: 'transfers',
|
|
359
|
+
transfers
|
|
360
|
+
})
|
|
361
|
+
}
|
|
362
|
+
break
|
|
363
|
+
|
|
364
|
+
} // if (engine.eval(rule.filter, values))
|
|
365
|
+
|
|
366
|
+
} // for (let rule of rules)
|
|
367
|
+
|
|
368
|
+
if (!lineHasMatch) {
|
|
369
|
+
await this.UI.throwNoFilterMatchForLine(lines, config, this.handler.importOptions)
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
} // for (const line of lines)
|
|
373
|
+
|
|
374
|
+
if (transfers.length > 0) {
|
|
375
|
+
return await this.postProcess(segment, {
|
|
376
|
+
type: 'transfers',
|
|
377
|
+
transfers
|
|
378
|
+
})
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
} catch (err) {
|
|
382
|
+
|
|
383
|
+
if (err instanceof RuleParsingError) {
|
|
384
|
+
await this.throwErrorRetry(err, config.language as Language)
|
|
385
|
+
} else {
|
|
386
|
+
throw err
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Decide the error when passing through without finding an answer.
|
|
391
|
+
if (matched) {
|
|
392
|
+
throw new Error(`Found matches but the result list is empty for ${JSON.stringify(lines)}.`)
|
|
393
|
+
}
|
|
394
|
+
throw new Error(`Could not find rules matching ${JSON.stringify(lines)}.`)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Check if there is an explicit answer already that needs to be returned for this segment.
|
|
399
|
+
*/
|
|
400
|
+
private async checkExplicitResult(segment: ImportSegment, ans: unknown): Promise<TransactionDescription | undefined> {
|
|
401
|
+
|
|
402
|
+
if (ans && segment.id) {
|
|
403
|
+
|
|
404
|
+
const answers: Record<SegmentId, Record<string, unknown>> = ans as Record<SegmentId, Record<string, unknown>>
|
|
405
|
+
|
|
406
|
+
if (answers[segment.id]) {
|
|
407
|
+
// Explicit transfer.
|
|
408
|
+
if (answers[segment.id].transfers) {
|
|
409
|
+
return await this.postProcess(segment, {
|
|
410
|
+
type: 'transfers',
|
|
411
|
+
transfers: answers[segment.id].transfers as AssetTransfer[]
|
|
412
|
+
})
|
|
413
|
+
}
|
|
414
|
+
// Explicit skipping.
|
|
415
|
+
if (answers[segment.id].skip) {
|
|
416
|
+
return {
|
|
417
|
+
type: 'transfers',
|
|
418
|
+
transfers: [],
|
|
419
|
+
transactions: [
|
|
420
|
+
{
|
|
421
|
+
date: segment.time,
|
|
422
|
+
segmentId: segment.id,
|
|
423
|
+
entries: [],
|
|
424
|
+
executionResult: 'skipped'
|
|
425
|
+
}
|
|
426
|
+
]
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Compute results from a rule.
|
|
435
|
+
*/
|
|
436
|
+
private parseResults(engine: RulesEngine, lines: TextFileLine[], rule: ImportRule, values: RuleVariables, answers: RuleVariables): AssetTransfer[] {
|
|
437
|
+
|
|
438
|
+
const transfers: AssetTransfer[] = []
|
|
439
|
+
const results: ImportRuleResult[] = 'length' in rule.result ? rule.result : [rule.result]
|
|
440
|
+
|
|
441
|
+
let index = 0
|
|
442
|
+
if (results.length === 0) {
|
|
443
|
+
debug('RULES', 'Result: NONE')
|
|
444
|
+
}
|
|
445
|
+
for (const result of results) {
|
|
446
|
+
debug('RULES', `Result[${index}]:`)
|
|
447
|
+
const transfer: Partial<AssetTransfer> = {}
|
|
448
|
+
// Collect fields evaluating directly from formula.
|
|
449
|
+
for (const [name, formula] of Object.entries(result)) {
|
|
450
|
+
if (name in transfer) {
|
|
451
|
+
warning(`A rule '${rule.name}' resulted duplicate value in formula '${formula}' for the field '${name}''. Already having ${JSON.stringify(transfer)}.`)
|
|
452
|
+
} else {
|
|
453
|
+
transfer[name] = engine.eval(formula, { ...values, ...answers })
|
|
454
|
+
debug('RULES', ` ${name} =`, JSON.stringify(transfer[name]))
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
// Verify condition before adding.
|
|
458
|
+
if (transfer.if === undefined || engine.eval(transfer.if, { ...values, ...answers })) {
|
|
459
|
+
// Catch bad results from formulas. Hit two jokers as well.
|
|
460
|
+
if (isAssetTransfer(transfer) && transfer.asset !== 'undefined' && transfer.asset !== 'null') {
|
|
461
|
+
transfers.push(transfer as AssetTransfer)
|
|
462
|
+
if (transfer.if) {
|
|
463
|
+
debug('RULES', ' Accepted condition', transfer.if)
|
|
464
|
+
}
|
|
465
|
+
} else {
|
|
466
|
+
console.log('Failing lines:')
|
|
467
|
+
console.dir(lines, { depth: null })
|
|
468
|
+
console.log('Matching rule:')
|
|
469
|
+
console.dir(rule, { depth: null })
|
|
470
|
+
throw new BadState(`Asset transfer ${JSON.stringify(transfer)} is incomplete.`)
|
|
471
|
+
}
|
|
472
|
+
} else {
|
|
473
|
+
debug('RULES', ' Dropped due to condition', transfer.if)
|
|
474
|
+
}
|
|
475
|
+
index++
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return transfers
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Throw UI error with retry option.
|
|
483
|
+
*/
|
|
484
|
+
private async throwErrorRetry(err: RuleParsingError, lang: Language) {
|
|
485
|
+
error(`Parsing error in expression '${err.expression}': ${err.message}`)
|
|
486
|
+
if (err.variables.rule) {
|
|
487
|
+
error(`While parsig rule ${JSON.stringify(err.variables.rule)}`)
|
|
488
|
+
}
|
|
489
|
+
if (err.variables && err.variables.text) {
|
|
490
|
+
error(`Failure in line ${err.variables.lineNumber}: ${err.variables.text}`)
|
|
491
|
+
|
|
492
|
+
const variables = clone(err.variables)
|
|
493
|
+
delete variables.config
|
|
494
|
+
delete variables.rule
|
|
495
|
+
delete variables.text
|
|
496
|
+
delete variables.lineNumber
|
|
497
|
+
error(`Variables when processing the line: ${JSON.stringify(variables)}.`)
|
|
498
|
+
}
|
|
499
|
+
// For parsing errors we can expect user editing configuration and then retrying.
|
|
500
|
+
const msg = (await this.UI.getTranslation('Parsing error in expression `{expr}`: {message}', lang)).replace('{expr}', err.expression).replace('{message}', err.message)
|
|
501
|
+
await this.UI.throwErrorRetry(msg, lang)
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Check for needed adjustments like VAT before returning the result.
|
|
506
|
+
* @param result
|
|
507
|
+
* @returns
|
|
508
|
+
*/
|
|
509
|
+
private async postProcess(segment: ImportSegment, result: TransactionDescription): Promise<TransactionDescription> {
|
|
510
|
+
|
|
511
|
+
// Find currency.
|
|
512
|
+
const vatReasons = new Set<AssetTransferReason>(['dividend', 'income', 'expense'])
|
|
513
|
+
const currencies: Set<Currency> = new Set(result.transfers.filter(t => vatReasons.has(t.reason) && t.type === 'currency').map(t => t.asset as Currency))
|
|
514
|
+
if (currencies.size > 1) {
|
|
515
|
+
throw new SystemError(`Not yet able to sort out VAT for multiple different currencies in ${JSON.stringify(result.transfers)}`)
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// If no currencies, assume no VAT.
|
|
519
|
+
if (currencies.size) {
|
|
520
|
+
|
|
521
|
+
const currency: Currency = [...currencies][0]
|
|
522
|
+
|
|
523
|
+
// Add VAT where needed.
|
|
524
|
+
const vatTransfers: AssetTransfer[] = []
|
|
525
|
+
for (const transfer of result.transfers) {
|
|
526
|
+
let vatPct
|
|
527
|
+
if (transfer.data && 'vat' in transfer.data) {
|
|
528
|
+
vatPct = transfer.data.vat
|
|
529
|
+
} else {
|
|
530
|
+
vatPct = await this.handler.getVAT(segment.time, transfer, currency)
|
|
531
|
+
}
|
|
532
|
+
const vatValue = (transfer.data && 'vatValue' in transfer.data) ? transfer.data.vatValue : null
|
|
533
|
+
|
|
534
|
+
if ((vatPct || vatValue) && transfer.amount) {
|
|
535
|
+
const oldAmount = Math.round(transfer.amount * 100)
|
|
536
|
+
const newAmount = vatValue !== null && vatValue !== undefined ? Math.round(oldAmount - vatValue * 100) : Math.round(transfer.amount * 100 / (1 + vatPct / 100))
|
|
537
|
+
transfer.amount = newAmount / 100
|
|
538
|
+
const vat = oldAmount - newAmount
|
|
539
|
+
const vatEntry: AssetTransfer = {
|
|
540
|
+
reason: 'tax',
|
|
541
|
+
type: 'statement',
|
|
542
|
+
asset: vat > 0 ? 'VAT_FROM_PURCHASES' : 'VAT_FROM_SALES',
|
|
543
|
+
amount: vat / 100,
|
|
544
|
+
data: {
|
|
545
|
+
currency
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
if (transfer.tags) {
|
|
550
|
+
vatEntry.tags = transfer.tags
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
vatTransfers.push(vatEntry)
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
result.transfers = result.transfers.concat(vatTransfers)
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// Bundle tags if given as an object.
|
|
561
|
+
for (let i = 0; i < result.transfers.length; i++) {
|
|
562
|
+
if ('tags' in result.transfers[i] && typeof result.transfers[i].tags === 'object' && result.transfers[i].tags?.length === undefined && result.transfers[i].tags !== null) {
|
|
563
|
+
const tags: Record<string, unknown> = result.transfers[i].tags as unknown as Record<string, unknown>
|
|
564
|
+
result.transfers[i].tags = Object.keys(tags).filter(t => !!tags[t]).sort() as Tag[]
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
return result
|
|
569
|
+
}
|
|
570
|
+
}
|