app/helpers/users_helper.rb
# document user balance calculation in users/show page (mouse over for balance)
document user balance calculation in users/show page (mouse over for balance)
document user balance calculation in users / show page ( mouse over for balance )
def gift_balance_calculation_doc(gift, current_user)
    return nil
    api_gift = gift.api_gifts.find { |ag| [ag.user_id_giver, ag.user_id_receiver].index(current_user.user_id)}
    return nil if !api_gift.user_id_giver or !api_gift.user_id_receiver
    return nil if !nil or gift.price <= 0
    balance_doc = gift.balance_doc(current_user)
    return nil unless balance_doc
    users_currency = @users.first.currency
    exchange_rate1 = ExchangeRate.exchange(1.0, gift.currency, users_currency, gift.received_at)
    exchange_rate2 = ExchangeRate.exchange(1.0, 'USD', users_currency, gift.received_at)
    exchange_rate3 = ExchangeRate.exchange(1.0, 'USD', users_currency, balance_doc[:previous_date])
    return nil unless exchange_rate1 and exchange_rate2 and exchange_rate3
    old_balance_hash = balance_doc[:previous_balance]
    old_balance = (old_balance_hash[BALANCE_KEY] * exchange_rate3).round(2)
    sign_negative_interest = old_balance >= 0 ? '-' : '+'
    previous_date = balance_doc[:previous_date]
    if previous_date != gift.received_at.to_yyyymmdd
      old_exchange_rates = balance_doc[:previous_exchange_rates]
      new_exchange_rates = balance_doc[:exchange_rates]
      previous_date = balance_doc[:previous_date]
      previous_balance_hash = balance_doc[:previous_balance]
      negative_interest_hash = balance_doc[:negative_interest]
      old_sum = 0.0
      new_sum = 0.0
      previous_balance_hash.keys.each do |currency|
        next if currency == BALANCE_KEY
        logger.debug2  "gift id = #{gift.id}, currency = #{currency}"
        amount = previous_balance_hash[currency] + negative_interest_hash[currency]
        old_sum += ExchangeRate.exchange(amount / old_exchange_rates[currency], 'USD', current_user.currency, previous_date)
        new_sum += ExchangeRate.exchange(amount / new_exchange_rates[currency], 'USD', current_user.currency, gift.received_at)
      end
      gain_loss = new_sum - old_sum
      logger.debug2  "exchange rate gains/losses. gift id #{gift.id}, gain/loss #{gain_loss}"
    else
      gain_loss = 0.0
    end
    translate_key = ['.balance_title']
    translate_key << 'exchange_rate' if exchange_rate1.round(3) != 1.000
    negative_interest = gift.balance_doc(current_user)[:negative_interest][BALANCE_KEY] || 0.00
    negative_interest = negative_interest * exchange_rate3
    translate_key << 'negative_interest' if negative_interest.round(2) != 0.00
    if gain_loss.round(2) != 0.00
      if gain_loss > 0.00
        translate_key << 'gain'
        sign_gain_loss = '+'
      else
        translate_key << 'loss'
        sign_gain_loss = '-'
        gain_loss = -gain_loss
      end
    end
    translate_key = translate_key.join('_')
    number_of_days = (gift.received_at.to_date - Date.parse(previous_date)).to_i
    t translate_key, 
                        :new_balance => format_price(gift.balance(current_user, @users.first)),
                        :old_balance => format_price(old_balance),
                        :sign_price => balance_doc[:sign],
                        :price => format_price(gift.price),
                        :exchange_rate => exchange_rate1.round(6),
                        :sign_negative_interest => sign_negative_interest,
                        :negative_interest => format_price(negative_interest),
                        :sign_currency_gain_loss => sign_gain_loss,
                        :currency_gain_loss => format_price(gain_loss),
                        :new_currency => 'USD',
                        :old_currency => 'USD',
                        :number_of_days => number_of_days
  end
def gift_balance_calculation_doc ( gift , current_user ) return nil api_gift = gift . api_gifts . find { | ag | [ ag . user_id_giver , ag . user_id_receiver ] . index ( current_user . user_id ) } return nil if ! api_gift . user_id_giver or ! api_gift . user_id_receiver return nil if ! nil or gift . price <= 0 balance_doc = gift . balance_doc ( current_user ) return nil unless balance_doc users_currency = @users . first . currency exchange_rate1 = ExchangeRate . exchange ( 1.0 , gift . currency , users_currency , gift . received_at ) exchange_rate2 = ExchangeRate . exchange ( 1.0 , 'USD' , users_currency , gift . received_at ) exchange_rate3 = ExchangeRate . exchange ( 1.0 , 'USD' , users_currency , balance_doc [ :previous_date ] ) return nil unless exchange_rate1 and exchange_rate2 and exchange_rate3 old_balance_hash = balance_doc [ :previous_balance ] old_balance = ( old_balance_hash [ BALANCE_KEY ] * exchange_rate3 ) . round ( 2 ) sign_negative_interest = old_balance >= 0 ? '-' : '+' previous_date = balance_doc [ :previous_date ] if previous_date != gift . received_at . to_yyyymmdd old_exchange_rates = balance_doc [ :previous_exchange_rates ] new_exchange_rates = balance_doc [ :exchange_rates ] previous_date = balance_doc [ :previous_date ] previous_balance_hash = balance_doc [ :previous_balance ] negative_interest_hash = balance_doc [ :negative_interest ] old_sum = 0.0 new_sum = 0.0 previous_balance_hash . keys . each do | currency | next if currency == BALANCE_KEY logger . debug2 "gift id = #{gift.id}, currency = #{currency}" amount = previous_balance_hash [ currency ] + negative_interest_hash [ currency ] old_sum += ExchangeRate . exchange ( amount / old_exchange_rates [ currency ] , 'USD' , current_user . currency , previous_date ) new_sum += ExchangeRate . exchange ( amount / new_exchange_rates [ currency ] , 'USD' , current_user . currency , gift . received_at ) end gain_loss = new_sum - old_sum logger . debug2 "exchange rate gains/losses. gift id #{gift.id}, gain/loss #{gain_loss}" else gain_loss = 0.0 end translate_key = [ '.balance_title' ] translate_key << 'exchange_rate' if exchange_rate1 . round ( 3 ) != 1.000 negative_interest = gift . balance_doc ( current_user ) [ :negative_interest ] [ BALANCE_KEY ] || 0.00 negative_interest = negative_interest * exchange_rate3 translate_key << 'negative_interest' if negative_interest . round ( 2 ) != 0.00 if gain_loss . round ( 2 ) != 0.00 if gain_loss > 0.00 translate_key << 'gain' sign_gain_loss = '+' else translate_key << 'loss' sign_gain_loss = '-' gain_loss = - gain_loss end end translate_key = translate_key . join ( '_' ) number_of_days = ( gift . received_at . to_date - Date . parse ( previous_date ) ) . to_i t translate_key , :new_balance => format_price ( gift . balance ( current_user , @users . first ) ) , :old_balance => format_price ( old_balance ) , :sign_price => balance_doc [ :sign ] , :price => format_price ( gift . price ) , :exchange_rate => exchange_rate1 . round ( 6 ) , :sign_negative_interest => sign_negative_interest , :negative_interest => format_price ( negative_interest ) , :sign_currency_gain_loss => sign_gain_loss , :currency_gain_loss => format_price ( gain_loss ) , :new_currency => 'USD' , :old_currency => 'USD' , :number_of_days => number_of_days end
document user balance calculation in users/show page (mouse over for balance)
document user balance calculation in users / show page ( mouse over for balance )
# format documentation for balance. # one format without currency and exchange rate if exchange rate == 1 # one format with currency and exchange rate if exchange rate != 1 # format: new balance yyy user currency = old balance xxx +- (price - negative interest) * exchange rate # calculate exchange rate gains/losses # that is previous_balance + negative_interest in old (date=previous_date) and new (date=received_at) exchange rates # calculation is done in current users actual currency # note that gains/losses changes if current user selects to see balance in an other currency # 4 translation key elements: balance_title [, exchange_rate ] [, negative_interest] [, gain/loss] - [, xxx] are optional elements # 12 translation keys: balance_title, balance_title_gain, balance_title_loss, balance_title_exchange_rate, balance_title_exchange_rate_gain, #                      balance_title_exchange_rate_loss, balance_title_negative_interest, balance_title_negative_interest_gain, #                      balance_title_negative_interest_loss, balance_title_exchange_rate_negative_interest, #                      balance_title_exchange_rate_negative_interest_gain and balance_title_exchange_rate_negative_interest_loss # todo: add more documentation for gain/loss calculation? # calculation: new_balance = old_balance + price * exchange_rate + negative_interest + currency_gain_loss
gift
<not_specific>
current_user
<not_specific>