diff --git a/app/models/provider/registry.rb b/app/models/provider/registry.rb index 408f6d87..dc6ee0a0 100644 --- a/app/models/provider/registry.rb +++ b/app/models/provider/registry.rb @@ -13,7 +13,6 @@ class Provider::Registry end def get_provider(name) - puts "NAME: #{name}" send(name) rescue NoMethodError raise Error.new("Provider '#{name}' not found in registry") diff --git a/app/models/provider/simple_fin.rb b/app/models/provider/simple_fin.rb index a6ef03a1..0617d145 100644 --- a/app/models/provider/simple_fin.rb +++ b/app/models/provider/simple_fin.rb @@ -84,19 +84,27 @@ class Provider::SimpleFin # For more spec information, see: https://www.simplefin.org/protocol.html#setup-token # # @param [str] accountable_type The name of the account type we're looking for. - # @param [int?] trans_start_date A linux epoch of the start date to get transactions of. - # @param [int?] trans_end_date A linux epoch of the end date to get transactions between. - # @param [Boolean] trans_pending If we should include pending transactions. Default is true. - def get_available_accounts(accountable_type, trans_start_date = nil, trans_end_date = nil, trans_pending = true) + # @param [int?] trans_start_date A linux epoch of the start date to get transactions of. Default is based on the config + # @param [int?] trans_end_date A linux epoch of the end date to get transactions between. Default is now. + # @param [Boolean] trans_pending If we should include pending transactions. Default is false as I don't think maybe supports pending. + def get_available_accounts(accountable_type, trans_start_date = nil, trans_end_date = nil, trans_pending = false) check_rate_limit endpoint = "/accounts?pending=#{trans_pending}" + if trans_end_date == nil + trans_end_date = Time.now.to_i + end + + if trans_start_date == nil + trans_start_date = Provider::SimpleFin.provider_config.look_back_days.days.ago.to_i + end + # Add any parameters we care about if trans_start_date - endpoint += "&trans_start_date=#{trans_start_date}" + endpoint += "&start-date=#{trans_start_date}" end if trans_end_date - endpoint += "&trans_end_date=#{trans_end_date}" + endpoint += "&end-date=#{trans_end_date}" end # account_info = send_request_to_sf(endpoint) diff --git a/app/models/simple_fin_account.rb b/app/models/simple_fin_account.rb index 53b37331..23ffe543 100644 --- a/app/models/simple_fin_account.rb +++ b/app/models/simple_fin_account.rb @@ -19,31 +19,41 @@ class SimpleFinAccount < ApplicationRecord class << self + # Gets what balance we should use as our account balance + def get_adjusted_balance(sf_account_data) + balance_from_sf = sf_account_data["balance"].to_d + account_type = sf_account_data["type"] + # Adjust balance: liabilities (CreditCard, Loan) should be negative + if [ "CreditCard", "Loan" ].include?(account_type) + balance_from_sf * -1 + else + balance_from_sf + end + end + def find_or_create_from_simple_fin_data!(sf_account_data, sfc) sfc.simple_fin_accounts.find_or_create_by!(external_id: sf_account_data["id"]) do |sfa| - sfa.current_balance = sf_account_data["balance"].to_d - sfa.available_balance = sf_account_data["balance"].to_d + balance = get_adjusted_balance(sf_account_data) + sfa.current_balance = balance + sfa.available_balance = balance sfa.currency = sf_account_data["currency"] new_account = sfc.family.accounts.new( name: sf_account_data["name"], - balance: sf_account_data["balance"].to_d, + balance: 0, currency: sf_account_data["currency"], accountable: TYPE_MAPPING[sf_account_data["type"]].new, subtype: sf_account_data["subtype"], simple_fin_account: sfa, # Explicitly associate back last_synced_at: Time.current, # Mark as synced upon creation # Set cash_balance similar to how Account.create_and_sync might - cash_balance: sf_account_data["balance"].to_d + cash_balance: 0 ) - # Add initial valuations - current_balance_amount = new_account.balance - new_account.entries.build( name: "Current Balance", date: Date.current, - amount: current_balance_amount, + amount: balance, currency: new_account.currency, entryable: Valuation.new ) @@ -72,13 +82,14 @@ class SimpleFinAccount < ApplicationRecord # Syncs all account data for the given sf_account_data parameter def sync_account_data!(sf_account_data) accountable_attributes = { id: self.account.accountable_id } + balance = SimpleFinAccount.get_adjusted_balance(sf_account_data) self.update!( - current_balance: sf_account_data["balance"].to_d, + current_balance: balance, available_balance: sf_account_data["available-balance"]&.to_d, currency: sf_account_data["currency"], account_attributes: { id: self.account.id, - balance: sf_account_data["balance"].to_d, + balance: balance, last_synced_at: Time.current, accountable_attributes: accountable_attributes } @@ -159,8 +170,17 @@ class SimpleFinAccount < ApplicationRecord entry.entryable = Transaction.new end + entry.entryable.simple_fin_category = transaction_data.dig("extra", "category") if entry.entryable.respond_to?(:simple_fin_category=) + # Auto associate a category to our transaction + if entry.entryable.simple_fin_category.present? + category_name = entry.entryable.simple_fin_category + category = self.account.family.categories.find_or_create_by!(name: category_name) + entry.entryable.category = category + end + + if entry.changed? || entry.entryable.changed? # Check if entryable also changed entry.save! else diff --git a/app/views/accounts/show/_activity.html.erb b/app/views/accounts/show/_activity.html.erb index 704c6789..06989684 100644 --- a/app/views/accounts/show/_activity.html.erb +++ b/app/views/accounts/show/_activity.html.erb @@ -4,7 +4,7 @@