Bucksley / app / Sources / BucksleyKit / Defaults.swift
| 1 | import Foundation |
| 2 | |
| 3 | /// The starting categories and groups, and the guess Bucksley makes before any rule or edit. |
| 4 | public enum Defaults { |
| 5 | public static let groups: [CategoryGroup] = [ |
| 6 | .init(id: "essentials", name: "Essentials"), |
| 7 | .init(id: "lifestyle", name: "Lifestyle"), |
| 8 | .init(id: "income", name: "Income"), |
| 9 | .init(id: "transfers", name: "Transfers") |
| 10 | ] |
| 11 | |
| 12 | /// Built-in categories most people can't easily cut. Each can be switched in the smart budget walkthrough. |
| 13 | public static let fixedIDs: Set<String> = ["home", "utilities", "gas", "health", "insurance"] |
| 14 | /// Name words that mark an imported category as fixed spending. |
| 15 | static let fixedWords = ["rent", "mortgage", "utilit", "insurance", "electric", "water", "internet", "phone", "medical", "health", "fuel", "gas", "loan", "childcare", "tuition"] |
| 16 | |
| 17 | public static let categories: [Category] = [ |
| 18 | .init(id: "groceries", name: "Groceries", symbol: "cart", groupID: "essentials", colorIndex: 0), |
| 19 | .init(id: "home", name: "Home", symbol: "house", groupID: "essentials", colorIndex: 2), |
| 20 | .init(id: "utilities", name: "Utilities", symbol: "bolt", groupID: "essentials", colorIndex: 3), |
| 21 | .init(id: "gas", name: "Gas", symbol: "fuelpump", groupID: "essentials", colorIndex: 3), |
| 22 | .init(id: "transport", name: "Transport", symbol: "car", groupID: "essentials", colorIndex: 2), |
| 23 | .init(id: "health", name: "Health", symbol: "cross.case", groupID: "essentials", colorIndex: 4), |
| 24 | .init(id: "insurance", name: "Insurance", symbol: "shield", groupID: "essentials", colorIndex: 5), |
| 25 | .init(id: "dining", name: "Dining", symbol: "fork.knife", groupID: "lifestyle", colorIndex: 1), |
| 26 | .init(id: "shopping", name: "Shopping", symbol: "bag", groupID: "lifestyle", colorIndex: 4), |
| 27 | .init(id: "subscriptions", name: "Subscriptions", symbol: "repeat", groupID: "lifestyle", colorIndex: 4), |
| 28 | .init(id: "entertainment", name: "Entertainment", symbol: "ticket", groupID: "lifestyle", colorIndex: 2), |
| 29 | .init(id: "travel", name: "Travel", symbol: "airplane", groupID: "lifestyle", colorIndex: 2), |
| 30 | .init(id: "other", name: "Other", symbol: "square.grid.2x2", groupID: "lifestyle", colorIndex: 5), |
| 31 | .init(id: "income", name: "Income", symbol: "arrow.down.circle", groupID: "income", colorIndex: 1, kind: .income), |
| 32 | .init(id: "transfer", name: "Transfers", symbol: "arrow.left.arrow.right", groupID: "transfers", colorIndex: 5, kind: .transfer) |
| 33 | ] |
| 34 | |
| 35 | /// Starter emoji for the built-in categories (users can pick their own on Categories & Rules). |
| 36 | public static let emoji: [String: String] = [ |
| 37 | "groceries": "๐", "home": "๐ ", "utilities": "๐ก", "gas": "โฝ๏ธ", "transport": "๐", "health": "๐ฉบ", |
| 38 | "insurance": "โ๏ธ", "dining": "๐", "shopping": "๐๏ธ", "subscriptions": "๐บ", "entertainment": "๐๏ธ", |
| 39 | "travel": "โ๏ธ", "other": "๐ฆ", "income": "๐ฐ", "transfer": "๐" |
| 40 | ] |
| 41 | |
| 42 | /// The choices offered when picking a category's emoji. |
| 43 | public static let emojiChoices: [String] = [ |
| 44 | "๐", "๐", "โ๏ธ", "๐", "๐ท", "๐ ", "๐ก", "๐ฑ", "โฝ๏ธ", "๐", "๐", "โ๏ธ", "๐จ", "๐ฉบ", "๐", "โ๏ธ", |
| 45 | "๐๏ธ", "๐", "๐บ", "๐๏ธ", "๐ฎ", "๐", "๐", "๐ถ", "๐ถ", "๐", "๐๏ธ", "๐", "๐ผ", "๐ฐ", "๐", "๐ฆ", |
| 46 | "๐ฆ", "๐ณ", "๐งพ", "๐ง", "๐ฑ", "โค๏ธ", "๐ท๏ธ", "โญ๏ธ" |
| 47 | ] |
| 48 | |
| 49 | /// Keyword guesses from the bank's description, payee and category text (Plaid's |
| 50 | /// personal_finance_category, SimpleFIN's description, FinanceKit's merchant category). |
| 51 | static let keywords: [(String, [String])] = [ |
| 52 | ("transfer", ["transfer", "payment thank you", "autopay", "credit card payment", "zelle to", "venmo cashout", "loan_payments", "transfer_in", "transfer_out", "online payment", |
| 53 | "mobile banking payment", "payment from checking", "payment from savings", "payment to crd", "bank transfer", "online banking payment"]), |
| 54 | ("income", ["payroll", "direct dep", "salary", "deposit from", "interest paid", "dividend", "income", "refund"]), |
| 55 | ("groceries", ["grocer", "trader joe", "whole foods", "safeway", "kroger", "aldi", "costco", "publix", "wegmans", "food_and_drink_groceries", "supermarket"]), |
| 56 | ("dining", ["restaurant", "coffee", "starbucks", "cafe", "sweetgreen", "chipotle", "doordash", "uber eats", "grubhub", "pizza", "bar ", "food_and_drink"]), |
| 57 | ("gas", ["shell", "chevron", "exxon", "bp ", "exxonmobil", "mobil ", "fuel", "gas station", "gas_stations"]), |
| 58 | ("transport", ["uber", "lyft", "transit", "parking", "toll", "metro", "transportation"]), |
| 59 | ("subscriptions", ["netflix", "spotify", "hulu", "disney+", "apple.com/bill", "icloud", "youtube premium", "subscription", "patreon", "github"]), |
| 60 | ("utilities", ["electric", "water", "comcast", "xfinity", "verizon", "at&t", "t-mobile", "internet", "utility", "rent_and_utilities"]), |
| 61 | ("home", ["home depot", "lowe", "ikea", "rent", "mortgage", "hoa", "home_improvement"]), |
| 62 | ("health", ["pharmacy", "cvs", "walgreens", "doctor", "dental", "medical", "clinic", "medical"]), |
| 63 | ("insurance", ["insurance", "geico", "progressive", "state farm"]), |
| 64 | ("travel", ["airline", "delta", "united", "american air", "hotel", "airbnb", "marriott", "hilton", "travel"]), |
| 65 | ("entertainment", ["movie", "cinema", "theater", "steam", "playstation", "xbox", "ticketmaster", "entertainment"]), |
| 66 | ("shopping", ["amazon", "target", "walmart", "best buy", "apple store", "etsy", "general_merchandise", "shop"]) |
| 67 | ] |
| 68 | |
| 69 | /// Merchant category codes (sent by SimpleFIN and Apple Wallet) for the common cases. |
| 70 | static func category(mcc: Int) -> String? { |
| 71 | switch mcc { |
| 72 | case 5411, 5422, 5441, 5451, 5499: "groceries" |
| 73 | case 5811, 5812, 5813, 5814: "dining" |
| 74 | case 5541, 5542, 5983: "gas" |
| 75 | case 4111, 4121, 4131, 4784, 7523: "transport" |
| 76 | case 4814, 4816, 4899, 4900: "utilities" |
| 77 | case 5815, 5816, 5817, 5818: "subscriptions" |
| 78 | case 5912, 8011, 8021, 8062, 8099, 8043: "health" |
| 79 | case 6300, 6381: "insurance" |
| 80 | case 3000...3350, 4511, 4722, 7011, 7012: "travel" |
| 81 | case 7832, 7841, 7922, 7991, 7996, 7999: "entertainment" |
| 82 | case 5200, 5211, 5251, 5712, 5719, 5722: "home" |
| 83 | case 5300...5399, 5600...5699, 5732, 5734, 5942, 5945, 5999: "shopping" |
| 84 | default: nil |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public static func guessCategory(_ tx: Transaction) -> String { |
| 89 | if let mcc = tx.bankCategory?.split(separator: ":").last.flatMap({ Int($0) }), tx.bankCategory?.hasPrefix("mcc:") == true, |
| 90 | let category = category(mcc: mcc), tx.amount < 0 { return category } |
| 91 | let text = [tx.description, tx.payee ?? "", tx.bankCategory ?? ""].joined(separator: " ").lowercased() |
| 92 | for (category, words) in keywords where words.contains(where: { text.contains($0) }) { |
| 93 | if category == "income" && tx.amount < 0 { continue } |
| 94 | return category |
| 95 | } |
| 96 | return tx.amount > 0 ? "income" : "other" |
| 97 | } |
| 98 | |
| 99 | /// "SQ *BLUE BOTTLE COFFEE 0423 OAKLAND CA" โ "Blue Bottle Coffee". |
| 100 | public static func cleanName(_ raw: String) -> String { |
| 101 | var s = raw |
| 102 | for prefix in ["SQ *", "SQ*", "TST* ", "TST*", "PAYPAL *", "PP*", "POS ", "DEBIT CARD PURCHASE ", "PURCHASE AUTHORIZED ON ", "ACH ", "CHECKCARD "] { |
| 103 | if s.uppercased().hasPrefix(prefix) { s = String(s.dropFirst(prefix.count)) } |
| 104 | } |
| 105 | var words: [Substring] = [] |
| 106 | for word in s.split(separator: " ") { |
| 107 | let digits = word.filter(\.isNumber).count |
| 108 | if digits >= 3 || word.contains("#") { break } // store numbers, dates, reference codes |
| 109 | if word.count == 2, word.allSatisfy(\.isUppercase), !words.isEmpty, words.count >= 2 { break } // state code |
| 110 | words.append(word) |
| 111 | } |
| 112 | let cleaned = (words.isEmpty ? s.split(separator: " ").prefix(3) : words[...]).joined(separator: " ") |
| 113 | guard cleaned == cleaned.uppercased() || cleaned == cleaned.lowercased() else { return cleaned } |
| 114 | return cleaned.lowercased().split(separator: " ").map { w in |
| 115 | w.count <= 3 && ["llc", "inc"].contains(String(w)) ? w.uppercased() : w.prefix(1).uppercased() + w.dropFirst() |
| 116 | }.joined(separator: " ") |
| 117 | } |
| 118 | } |