|
| 1 | +class UI::Account::BalanceReconciliation < ApplicationComponent |
| 2 | + attr_reader :balance, :account |
| 3 | + |
| 4 | + def initialize(balance:, account:) |
| 5 | + @balance = balance |
| 6 | + @account = account |
| 7 | + end |
| 8 | + |
| 9 | + def reconciliation_items |
| 10 | + case account.accountable_type |
| 11 | + when "Depository", "OtherAsset", "OtherLiability" |
| 12 | + default_items |
| 13 | + when "CreditCard" |
| 14 | + credit_card_items |
| 15 | + when "Investment" |
| 16 | + investment_items |
| 17 | + when "Loan" |
| 18 | + loan_items |
| 19 | + when "Property", "Vehicle" |
| 20 | + asset_items |
| 21 | + when "Crypto" |
| 22 | + crypto_items |
| 23 | + else |
| 24 | + default_items |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + private |
| 29 | + |
| 30 | + def default_items |
| 31 | + items = [ |
| 32 | + { label: "Start balance", value: balance.start_balance_money, tooltip: "The account balance at the beginning of this day", style: :start }, |
| 33 | + { label: "Net cash flow", value: net_cash_flow, tooltip: "Net change in balance from all transactions during the day", style: :flow } |
| 34 | + ] |
| 35 | + |
| 36 | + if has_adjustments? |
| 37 | + items << { label: "End balance", value: end_balance_before_adjustments, tooltip: "The calculated balance after all transactions", style: :subtotal } |
| 38 | + items << { label: "Adjustments", value: total_adjustments, tooltip: "Manual reconciliations or other adjustments", style: :adjustment } |
| 39 | + end |
| 40 | + |
| 41 | + items << { label: "Final balance", value: balance.end_balance_money, tooltip: "The final account balance for the day", style: :final } |
| 42 | + items |
| 43 | + end |
| 44 | + |
| 45 | + def credit_card_items |
| 46 | + items = [ |
| 47 | + { label: "Start balance", value: balance.start_balance_money, tooltip: "The balance owed at the beginning of this day", style: :start }, |
| 48 | + { label: "Charges", value: balance.cash_outflows_money, tooltip: "New charges made during the day", style: :flow }, |
| 49 | + { label: "Payments", value: balance.cash_inflows_money * -1, tooltip: "Payments made to the card during the day", style: :flow } |
| 50 | + ] |
| 51 | + |
| 52 | + if has_adjustments? |
| 53 | + items << { label: "End balance", value: end_balance_before_adjustments, tooltip: "The calculated balance after all transactions", style: :subtotal } |
| 54 | + items << { label: "Adjustments", value: total_adjustments, tooltip: "Manual reconciliations or other adjustments", style: :adjustment } |
| 55 | + end |
| 56 | + |
| 57 | + items << { label: "Final balance", value: balance.end_balance_money, tooltip: "The final balance owed for the day", style: :final } |
| 58 | + items |
| 59 | + end |
| 60 | + |
| 61 | + def investment_items |
| 62 | + items = [ |
| 63 | + { label: "Start balance", value: balance.start_balance_money, tooltip: "The total portfolio value at the beginning of this day", style: :start } |
| 64 | + ] |
| 65 | + |
| 66 | + # Change in brokerage cash (includes deposits, withdrawals, and cash from trades) |
| 67 | + items << { label: "Change in brokerage cash", value: net_cash_flow, tooltip: "Net change in cash from deposits, withdrawals, and trades", style: :flow } |
| 68 | + |
| 69 | + # Change in holdings from trading activity |
| 70 | + items << { label: "Change in holdings (buys/sells)", value: net_non_cash_flow, tooltip: "Impact on holdings from buying and selling securities", style: :flow } |
| 71 | + |
| 72 | + # Market price changes |
| 73 | + items << { label: "Change in holdings (market price activity)", value: balance.net_market_flows_money, tooltip: "Change in holdings value from market price movements", style: :flow } |
| 74 | + |
| 75 | + if has_adjustments? |
| 76 | + items << { label: "End balance", value: end_balance_before_adjustments, tooltip: "The calculated balance after all activity", style: :subtotal } |
| 77 | + items << { label: "Adjustments", value: total_adjustments, tooltip: "Manual reconciliations or other adjustments", style: :adjustment } |
| 78 | + end |
| 79 | + |
| 80 | + items << { label: "Final balance", value: balance.end_balance_money, tooltip: "The final portfolio value for the day", style: :final } |
| 81 | + items |
| 82 | + end |
| 83 | + |
| 84 | + def loan_items |
| 85 | + items = [ |
| 86 | + { label: "Start principal", value: balance.start_balance_money, tooltip: "The principal balance at the beginning of this day", style: :start }, |
| 87 | + { label: "Net principal change", value: net_non_cash_flow, tooltip: "Principal payments and new borrowing during the day", style: :flow } |
| 88 | + ] |
| 89 | + |
| 90 | + if has_adjustments? |
| 91 | + items << { label: "End principal", value: end_balance_before_adjustments, tooltip: "The calculated principal after all transactions", style: :subtotal } |
| 92 | + items << { label: "Adjustments", value: balance.non_cash_adjustments_money, tooltip: "Manual reconciliations or other adjustments", style: :adjustment } |
| 93 | + end |
| 94 | + |
| 95 | + items << { label: "Final principal", value: balance.end_balance_money, tooltip: "The final principal balance for the day", style: :final } |
| 96 | + items |
| 97 | + end |
| 98 | + |
| 99 | + def asset_items # Property/Vehicle |
| 100 | + items = [ |
| 101 | + { label: "Start value", value: balance.start_balance_money, tooltip: "The asset value at the beginning of this day", style: :start }, |
| 102 | + { label: "Net value change", value: net_total_flow, tooltip: "All value changes including improvements and depreciation", style: :flow } |
| 103 | + ] |
| 104 | + |
| 105 | + if has_adjustments? |
| 106 | + items << { label: "End value", value: end_balance_before_adjustments, tooltip: "The calculated value after all changes", style: :subtotal } |
| 107 | + items << { label: "Adjustments", value: total_adjustments, tooltip: "Manual value adjustments or appraisals", style: :adjustment } |
| 108 | + end |
| 109 | + |
| 110 | + items << { label: "Final value", value: balance.end_balance_money, tooltip: "The final asset value for the day", style: :final } |
| 111 | + items |
| 112 | + end |
| 113 | + |
| 114 | + def crypto_items |
| 115 | + items = [ |
| 116 | + { label: "Start balance", value: balance.start_balance_money, tooltip: "The crypto holdings value at the beginning of this day", style: :start } |
| 117 | + ] |
| 118 | + |
| 119 | + items << { label: "Buys", value: balance.cash_outflows_money * -1, tooltip: "Crypto purchases during the day", style: :flow } if balance.cash_outflows != 0 |
| 120 | + items << { label: "Sells", value: balance.cash_inflows_money, tooltip: "Crypto sales during the day", style: :flow } if balance.cash_inflows != 0 |
| 121 | + items << { label: "Market changes", value: balance.net_market_flows_money, tooltip: "Value changes from market price movements", style: :flow } if balance.net_market_flows != 0 |
| 122 | + |
| 123 | + if has_adjustments? |
| 124 | + items << { label: "End balance", value: end_balance_before_adjustments, tooltip: "The calculated balance after all activity", style: :subtotal } |
| 125 | + items << { label: "Adjustments", value: total_adjustments, tooltip: "Manual reconciliations or other adjustments", style: :adjustment } |
| 126 | + end |
| 127 | + |
| 128 | + items << { label: "Final balance", value: balance.end_balance_money, tooltip: "The final crypto holdings value for the day", style: :final } |
| 129 | + items |
| 130 | + end |
| 131 | + |
| 132 | + def net_cash_flow |
| 133 | + balance.cash_inflows_money - balance.cash_outflows_money |
| 134 | + end |
| 135 | + |
| 136 | + def net_non_cash_flow |
| 137 | + balance.non_cash_inflows_money - balance.non_cash_outflows_money |
| 138 | + end |
| 139 | + |
| 140 | + def net_total_flow |
| 141 | + net_cash_flow + net_non_cash_flow + balance.net_market_flows_money |
| 142 | + end |
| 143 | + |
| 144 | + def total_adjustments |
| 145 | + balance.cash_adjustments_money + balance.non_cash_adjustments_money |
| 146 | + end |
| 147 | + |
| 148 | + def has_adjustments? |
| 149 | + balance.cash_adjustments != 0 || balance.non_cash_adjustments != 0 |
| 150 | + end |
| 151 | + |
| 152 | + def end_balance_before_adjustments |
| 153 | + balance.end_balance_money - total_adjustments |
| 154 | + end |
| 155 | +end |
0 commit comments