module HasAccounts::Model

Public Instance Methods

balance(value_date = nil, direct_account = nil) click to toggle source
# File lib/has_accounts/model.rb, line 56
def balance(value_date = nil, direct_account = nil)
  bookings.direct_balance(value_date, direct_account)
end
build_booking(params = {}, template_code = nil) click to toggle source

Build booking

# File lib/has_accounts/model.rb, line 41
def build_booking(params = {}, template_code = nil)
  template_code ||= self.class.to_s.underscore + ':invoice'
  booking_template = BookingTemplate.find_by_code(template_code)

  # Prepare booking parameters
  booking_params = { reference: self }
  booking_params.merge!(params)

  # Build and assign booking
  booking = booking_template.build_booking(booking_params)
  bookings << booking

  booking
end
direct_account() click to toggle source

Delegate to class

# File lib/has_accounts/model.rb, line 36
def direct_account
  self.class.direct_account
end
direct_balance(value_date = nil, direct_account = nil) click to toggle source

TODO: duplicated in Booking (without parameter)

# File lib/has_accounts/model.rb, line 10
def direct_balance(value_date = nil, direct_account = nil)
  return BigDecimal.new('0') unless proxy_association.owner.direct_account

  direct_account ||= proxy_association.owner.direct_account
  balance = BigDecimal.new('0')

  # Scope by value_date
  if value_date.is_a?(Range) || value_date.is_a?(Array)
    direct_bookings = where('date(value_date) BETWEEN :from AND :to', from: value_date.first, to: value_date.last)
  elsif value_date
    direct_bookings = where('date(value_date) <= ?', value_date) if value_date
  else
    direct_bookings = scoped
  end

  # Accumulate
  for booking in direct_bookings.all
    balance += booking.accounted_amount(direct_account)
  end

  balance
end