-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseImporter.swift
124 lines (106 loc) · 4.49 KB
/
BaseImporter.swift
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
//
// BaseImporter.swift
// SwiftBeanCountImporter
//
// Created by Steffen Kötte on 2020-05-14.
// Copyright © 2020 Steffen Kötte. All rights reserved.
//
import Foundation
import SwiftBeanCountModel
class BaseImporter: Importer {
private static let regexe: [NSRegularExpression] = { // swiftlint:disable force_try
[
try! NSRegularExpression(pattern: "(C-)?IDP PURCHASE( )?-( )?[0-9]{4}", options: []),
try! NSRegularExpression(pattern: "VISA DEBIT (PUR|REF)-[0-9]{4}", options: []),
try! NSRegularExpression(pattern: "WWWINTERAC PUR [0-9]{4}", options: []),
try! NSRegularExpression(pattern: "INTERAC E-TRF- [0-9]{4}", options: []),
try! NSRegularExpression(pattern: "[0-9]* ~ Internet Withdrawal", options: []),
try! NSRegularExpression(pattern: "(-)? SAP(?! CANADA)", options: []),
try! NSRegularExpression(pattern: "-( )?(MAY|JUNE)( )?201(4|6)", options: []),
try! NSRegularExpression(pattern: " BC CA", options: []),
try! NSRegularExpression(pattern: "#( )?[0-9]{1,5}", options: []),
]
}() // swiftlint:enable force_try
class var importerName: String { "" } // Override
class var importerType: String { "" } // Override
class var helpText: String { "" } // Override
weak var delegate: ImporterDelegate?
var ledger: Ledger?
private var accountName: AccountName?
var importName: String { "" } // Override
var configuredAccountName: AccountName {
if let existingAccountName = accountName {
return existingAccountName
}
let ledgerAccounts = accountsFromLedger()
if ledgerAccounts.count == 1 {
accountName = ledgerAccounts.first!
return accountName!
}
guard let delegate else {
fatalError("No delegate")
}
let group = DispatchGroup()
group.enter()
delegate.requestInput(name: "Account", suggestions: ledgerAccounts.map { $0.fullName }, isSecret: false) {
guard let name = try? AccountName($0) else {
return false
}
self.accountName = name
group.leave()
return true
}
group.wait()
return accountName!
}
var commoditySymbol: String {
ledger?.accounts.first { $0.name == configuredAccountName }?.commoditySymbol ?? Settings.fallbackCommodity
}
init(ledger: Ledger?) {
self.ledger = ledger
}
func accountsFromLedger() -> [AccountName] {
// Override if neccessary, e.g. to do more filtering based on more meta data, e.g. account numbers
ledger?.accounts.filter { $0.metaData[Settings.importerTypeKey] == Self.importerType }.map { $0.name } ?? []
}
func load() {
// Override if neccessary
}
func nextTransaction() -> ImportedTransaction? {
nil // Override
}
func balancesToImport() -> [Balance] {
[] // Override if neccessary
}
func pricesToImport() -> [Price] {
[] // Override if neccessary
}
func sanitize(description: String) -> String {
var result = description
for regex in Self.regexe {
result = regex.stringByReplacingMatches(in: result,
options: .withoutAnchoringBounds,
range: NSRange(result.startIndex..., in: result),
withTemplate: "")
}
return result.trimmingCharacters(in: .whitespaces)
}
func savedDescriptionAndPayeeFor(description: String) -> (String?, String?) {
(Settings.allDescriptionMappings[description], Settings.allPayeeMappings[description])
}
func savedAccountNameFor(payee: String) -> AccountName? {
if let accountNameString = Settings.allAccountMappings[payee], let accountName = try? AccountName(accountNameString) {
return accountName
}
return nil
}
func getPossibleDuplicateFor(_ transaction: Transaction) -> Transaction? {
guard let ledger else {
return nil
}
return ledger.transactions.first {
$0.postings.contains { $0.accountName == transaction.postings.first?.accountName && $0.amount == transaction.postings.first?.amount }
&& $0.metaData.date + Settings.dateTolerance >= transaction.metaData.date && $0.metaData.date - Settings.dateTolerance <= transaction.metaData.date
}
}
}