Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TransactionsReportBuilder
- attr_reader :from_date, :to_date
- IMPORTANT_SHORTCUTS = %w[BTC LTC ETH BCH].freeze
- def initialize(from_date, to_date)
- @from_date = from_date
- @to_date = to_date
- end
- def build_report
- result = []
- IMPORTANT_SHORTCUTS.each do |shortcut|
- result << [shortcut, sum_in_currency(shortcut)]
- end
- result << ['Total quantity', total_quantity]
- result << ['Others quantity', others_quantity]
- result << ['Total in ETH', total_in_eth]
- result << ['Total in USD', total_in_usd]
- first_row = []
- last_row = []
- result.each do |name, value|
- first_row << name
- last_row << value
- end
- [first_row, last_row]
- end
- def sum_in_currency(shortcut)
- currency_id = CryptoCurrency.find_by(shortcut: shortcut)
- Transaction.joins(:personal_address)
- .where('personal_addresses.crypto_currency_id = ?', currency_id)
- .where(updated_at: date_range)
- .sum(:amount)
- end
- def total_quantity
- transactions_in_date_range.count
- end
- def others_quantity
- other_ids = other_shortcuts.map { |sc| CryptoCurrency.find_by(shortcut: sc).id }
- transactions_in_date_range.where('personal_addresses.crypto_currency_id IN (?)', other_ids).count
- end
- def total_in_eth
- shortcuts = CryptoCurrency.pluck(:shortcut)
- shortcuts.map do |shortcut|
- sum_in_currency(shortcut) * Rate.actual_for_currency(shortcut)
- end.sum
- end
- def total_in_usd
- total_in_eth * Rate.actual_eth_to_usd
- end
- private
- def date_range
- (from_date..to_date)
- end
- def transactions_in_date_range
- Transaction.joins(:personal_address).where(updated_at: date_range)
- end
- def other_shortcuts
- CryptoCurrency.pluck(:shortcut) - IMPORTANT_SHORTCUTS
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement