Coinly/Coinly/Features/Models/AccountModel.swift

164 lines
4.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
import SwiftUI
enum AccountType: String, Codable, CaseIterable {
case wallet = "Wallet"
case bankAccount = "Bank Account"
case creditCard = "Credit Card"
case deposit = "Deposit"
case debt = "Debt"
var icon: String {
switch self {
case .wallet: return "banknote"
case .bankAccount: return "building.columns.fill"
case .creditCard: return "creditcard.fill"
case .deposit: return "vault.fill"
case .debt: return "exclamationmark.circle.fill"
}
}
var color: Color {
switch self {
case .wallet: return Color(uiColor: .systemGreen)
case .bankAccount: return Color(uiColor: .systemBlue)
case .creditCard: return Color(uiColor: .systemPurple)
case .deposit: return Color(uiColor: .systemOrange)
case .debt: return Color(uiColor: .systemRed)
}
}
}
struct AccountModel: Identifiable, Codable {
let id: String
var name: String
var type: AccountType
var currency: AppSettings.Currency
var balance: Double
var isActive: Bool
// Кредитная карта
var creditLimit: Double?
var interestRate: Double?
var dueDate: Date?
var minimumPayment: Double?
// Депозит
var depositEndDate: Date?
var depositInterestRate: Double?
var isAutoRenewable: Bool?
// Долг
var creditorName: String?
var debtInterestRate: Double?
var paymentSchedule: [DebtPayment]?
init(
name: String,
type: AccountType,
currency: AppSettings.Currency,
balance: Double = 0,
isActive: Bool = true,
creditLimit: Double? = nil,
interestRate: Double? = nil,
dueDate: Date? = nil,
minimumPayment: Double? = nil,
depositEndDate: Date? = nil,
depositInterestRate: Double? = nil,
isAutoRenewable: Bool? = nil,
creditorName: String? = nil,
debtInterestRate: Double? = nil,
paymentSchedule: [DebtPayment]? = nil
) {
self.id = UUID().uuidString
self.name = name
self.type = type
self.currency = currency
self.balance = balance
self.isActive = isActive
self.creditLimit = creditLimit
self.interestRate = interestRate
self.dueDate = dueDate
self.minimumPayment = minimumPayment
self.depositEndDate = depositEndDate
self.depositInterestRate = depositInterestRate
self.isAutoRenewable = isAutoRenewable
self.creditorName = creditorName
self.debtInterestRate = debtInterestRate
self.paymentSchedule = paymentSchedule
}
// Вычисляемые свойства
var availableCredit: Double? {
guard type == .creditCard, let limit = creditLimit else { return nil }
return limit - balance
}
var isOverdue: Bool {
guard let dueDate = dueDate else { return false }
return Date() > dueDate
}
var formattedBalance: String {
balance.formatAsCurrency()
}
// Методы для работы с кредитной картой
mutating func addPurchase(_ amount: Double) -> Bool {
guard type == .creditCard,
let limit = creditLimit,
balance + amount <= limit else {
return false
}
balance += amount
return true
}
mutating func makePayment(_ amount: Double) -> Bool {
guard amount <= balance else { return false }
balance -= amount
return true
}
}
// Структура для платежей по долгу
struct DebtPayment: Codable, Identifiable {
let id: String
var dueDate: Date
var amount: Double
var isPaid: Bool
init(dueDate: Date, amount: Double, isPaid: Bool = false) {
self.id = UUID().uuidString
self.dueDate = dueDate
self.amount = amount
self.isPaid = isPaid
}
}
// Sample Data
extension AccountModel {
static let sampleData = [
AccountModel(
name: "Cash Wallet",
type: .wallet,
currency: .usd,
balance: 1000
),
AccountModel(
name: "Main Bank Account",
type: .bankAccount,
currency: .usd,
balance: 5000
),
AccountModel(
name: "Credit Card",
type: .creditCard,
currency: .usd,
balance: 2000,
creditLimit: 10000,
interestRate: 19.99,
dueDate: Calendar.current.date(byAdding: .day, value: 15, to: Date())
)
]
}