import Foundation /// The starting categories and groups, and the guess Bucksley makes before any rule or edit. public enum Defaults { public static let groups: [CategoryGroup] = [ .init(id: "essentials", name: "Essentials"), .init(id: "lifestyle", name: "Lifestyle"), .init(id: "income", name: "Income"), .init(id: "transfers", name: "Transfers") ] /// Built-in categories most people can't easily cut. Each can be switched in the smart budget walkthrough. public static let fixedIDs: Set = ["home", "utilities", "gas", "health", "insurance"] /// Name words that mark an imported category as fixed spending. static let fixedWords = ["rent", "mortgage", "utilit", "insurance", "electric", "water", "internet", "phone", "medical", "health", "fuel", "gas", "loan", "childcare", "tuition"] public static let categories: [Category] = [ .init(id: "groceries", name: "Groceries", symbol: "cart", groupID: "essentials", colorIndex: 0), .init(id: "home", name: "Home", symbol: "house", groupID: "essentials", colorIndex: 2), .init(id: "utilities", name: "Utilities", symbol: "bolt", groupID: "essentials", colorIndex: 3), .init(id: "gas", name: "Gas", symbol: "fuelpump", groupID: "essentials", colorIndex: 3), .init(id: "transport", name: "Transport", symbol: "car", groupID: "essentials", colorIndex: 2), .init(id: "health", name: "Health", symbol: "cross.case", groupID: "essentials", colorIndex: 4), .init(id: "insurance", name: "Insurance", symbol: "shield", groupID: "essentials", colorIndex: 5), .init(id: "dining", name: "Dining", symbol: "fork.knife", groupID: "lifestyle", colorIndex: 1), .init(id: "shopping", name: "Shopping", symbol: "bag", groupID: "lifestyle", colorIndex: 4), .init(id: "subscriptions", name: "Subscriptions", symbol: "repeat", groupID: "lifestyle", colorIndex: 4), .init(id: "entertainment", name: "Entertainment", symbol: "ticket", groupID: "lifestyle", colorIndex: 2), .init(id: "travel", name: "Travel", symbol: "airplane", groupID: "lifestyle", colorIndex: 2), .init(id: "other", name: "Other", symbol: "square.grid.2x2", groupID: "lifestyle", colorIndex: 5), .init(id: "income", name: "Income", symbol: "arrow.down.circle", groupID: "income", colorIndex: 1, kind: .income), .init(id: "transfer", name: "Transfers", symbol: "arrow.left.arrow.right", groupID: "transfers", colorIndex: 5, kind: .transfer) ] /// Starter emoji for the built-in categories (users can pick their own on Categories & Rules). public static let emoji: [String: String] = [ "groceries": "๐Ÿ›’", "home": "๐Ÿ ", "utilities": "๐Ÿ’ก", "gas": "โ›ฝ๏ธ", "transport": "๐Ÿš—", "health": "๐Ÿฉบ", "insurance": "โ˜‚๏ธ", "dining": "๐Ÿ”", "shopping": "๐Ÿ›๏ธ", "subscriptions": "๐Ÿ“บ", "entertainment": "๐ŸŽŸ๏ธ", "travel": "โœˆ๏ธ", "other": "๐Ÿ“ฆ", "income": "๐Ÿ’ฐ", "transfer": "๐Ÿ”„" ] /// The choices offered when picking a category's emoji. public static let emojiChoices: [String] = [ "๐Ÿ›’", "๐Ÿ”", "โ˜•๏ธ", "๐Ÿ•", "๐Ÿท", "๐Ÿ ", "๐Ÿ’ก", "๐Ÿ“ฑ", "โ›ฝ๏ธ", "๐Ÿš—", "๐ŸšŒ", "โœˆ๏ธ", "๐Ÿจ", "๐Ÿฉบ", "๐Ÿ’Š", "โ˜‚๏ธ", "๐Ÿ›๏ธ", "๐Ÿ‘•", "๐Ÿ“บ", "๐ŸŽŸ๏ธ", "๐ŸŽฎ", "๐Ÿ“š", "๐ŸŽ“", "๐Ÿถ", "๐Ÿ‘ถ", "๐Ÿ’‡", "๐Ÿ‹๏ธ", "๐ŸŽ", "๐Ÿ’ผ", "๐Ÿ’ฐ", "๐Ÿ”„", "๐Ÿ“ฆ", "๐Ÿฆ", "๐Ÿ’ณ", "๐Ÿงพ", "๐Ÿ”ง", "๐ŸŒฑ", "โค๏ธ", "๐Ÿท๏ธ", "โญ๏ธ" ] /// Keyword guesses from the bank's description, payee and category text (Plaid's /// personal_finance_category, SimpleFIN's description, FinanceKit's merchant category). static let keywords: [(String, [String])] = [ ("transfer", ["transfer", "payment thank you", "autopay", "credit card payment", "zelle to", "venmo cashout", "loan_payments", "transfer_in", "transfer_out", "online payment", "mobile banking payment", "payment from checking", "payment from savings", "payment to crd", "bank transfer", "online banking payment"]), ("income", ["payroll", "direct dep", "salary", "deposit from", "interest paid", "dividend", "income", "refund"]), ("groceries", ["grocer", "trader joe", "whole foods", "safeway", "kroger", "aldi", "costco", "publix", "wegmans", "food_and_drink_groceries", "supermarket"]), ("dining", ["restaurant", "coffee", "starbucks", "cafe", "sweetgreen", "chipotle", "doordash", "uber eats", "grubhub", "pizza", "bar ", "food_and_drink"]), ("gas", ["shell", "chevron", "exxon", "bp ", "exxonmobil", "mobil ", "fuel", "gas station", "gas_stations"]), ("transport", ["uber", "lyft", "transit", "parking", "toll", "metro", "transportation"]), ("subscriptions", ["netflix", "spotify", "hulu", "disney+", "apple.com/bill", "icloud", "youtube premium", "subscription", "patreon", "github"]), ("utilities", ["electric", "water", "comcast", "xfinity", "verizon", "at&t", "t-mobile", "internet", "utility", "rent_and_utilities"]), ("home", ["home depot", "lowe", "ikea", "rent", "mortgage", "hoa", "home_improvement"]), ("health", ["pharmacy", "cvs", "walgreens", "doctor", "dental", "medical", "clinic", "medical"]), ("insurance", ["insurance", "geico", "progressive", "state farm"]), ("travel", ["airline", "delta", "united", "american air", "hotel", "airbnb", "marriott", "hilton", "travel"]), ("entertainment", ["movie", "cinema", "theater", "steam", "playstation", "xbox", "ticketmaster", "entertainment"]), ("shopping", ["amazon", "target", "walmart", "best buy", "apple store", "etsy", "general_merchandise", "shop"]) ] /// Merchant category codes (sent by SimpleFIN and Apple Wallet) for the common cases. static func category(mcc: Int) -> String? { switch mcc { case 5411, 5422, 5441, 5451, 5499: "groceries" case 5811, 5812, 5813, 5814: "dining" case 5541, 5542, 5983: "gas" case 4111, 4121, 4131, 4784, 7523: "transport" case 4814, 4816, 4899, 4900: "utilities" case 5815, 5816, 5817, 5818: "subscriptions" case 5912, 8011, 8021, 8062, 8099, 8043: "health" case 6300, 6381: "insurance" case 3000...3350, 4511, 4722, 7011, 7012: "travel" case 7832, 7841, 7922, 7991, 7996, 7999: "entertainment" case 5200, 5211, 5251, 5712, 5719, 5722: "home" case 5300...5399, 5600...5699, 5732, 5734, 5942, 5945, 5999: "shopping" default: nil } } public static func guessCategory(_ tx: Transaction) -> String { if let mcc = tx.bankCategory?.split(separator: ":").last.flatMap({ Int($0) }), tx.bankCategory?.hasPrefix("mcc:") == true, let category = category(mcc: mcc), tx.amount < 0 { return category } let text = [tx.description, tx.payee ?? "", tx.bankCategory ?? ""].joined(separator: " ").lowercased() for (category, words) in keywords where words.contains(where: { text.contains($0) }) { if category == "income" && tx.amount < 0 { continue } return category } return tx.amount > 0 ? "income" : "other" } /// "SQ *BLUE BOTTLE COFFEE 0423 OAKLAND CA" โ†’ "Blue Bottle Coffee". public static func cleanName(_ raw: String) -> String { var s = raw for prefix in ["SQ *", "SQ*", "TST* ", "TST*", "PAYPAL *", "PP*", "POS ", "DEBIT CARD PURCHASE ", "PURCHASE AUTHORIZED ON ", "ACH ", "CHECKCARD "] { if s.uppercased().hasPrefix(prefix) { s = String(s.dropFirst(prefix.count)) } } var words: [Substring] = [] for word in s.split(separator: " ") { let digits = word.filter(\.isNumber).count if digits >= 3 || word.contains("#") { break } // store numbers, dates, reference codes if word.count == 2, word.allSatisfy(\.isUppercase), !words.isEmpty, words.count >= 2 { break } // state code words.append(word) } let cleaned = (words.isEmpty ? s.split(separator: " ").prefix(3) : words[...]).joined(separator: " ") guard cleaned == cleaned.uppercased() || cleaned == cleaned.lowercased() else { return cleaned } return cleaned.lowercased().split(separator: " ").map { w in w.count <= 3 && ["llc", "inc"].contains(String(w)) ? w.uppercased() : w.prefix(1).uppercased() + w.dropFirst() }.joined(separator: " ") } }