class Keepr::Posting

Constants

SIDE_CREDIT
SIDE_DEBIT

Public Instance Methods

amount() click to toggle source
# File lib/keepr/posting.rb, line 55
def amount
  raw_amount.try(:abs)
end
amount=(value) click to toggle source
# File lib/keepr/posting.rb, line 59
def amount=(value)
  @side ||= SIDE_DEBIT

  unless value
    self.raw_amount = nil
    return
  end

  raise ArgumentError, 'Negative amount not allowed!' if value.to_d.negative?

  self.raw_amount = credit? ? -value.to_d : value.to_d
end
credit?() click to toggle source
# File lib/keepr/posting.rb, line 43
def credit?
  side == SIDE_CREDIT
end
debit?() click to toggle source
# File lib/keepr/posting.rb, line 39
def debit?
  side == SIDE_DEBIT
end
raw_amount() click to toggle source
# File lib/keepr/posting.rb, line 47
def raw_amount
  read_attribute(:amount)
end
raw_amount=(value) click to toggle source
# File lib/keepr/posting.rb, line 51
def raw_amount=(value)
  write_attribute(:amount, value)
end
side() click to toggle source
# File lib/keepr/posting.rb, line 20
def side
  @side || begin
    (raw_amount.negative? ? SIDE_CREDIT : SIDE_DEBIT) if raw_amount
  end
end
side=(value) click to toggle source
# File lib/keepr/posting.rb, line 26
def side=(value)
  raise ArgumentError unless [SIDE_DEBIT, SIDE_CREDIT].include?(value)

  @side = value
  return unless amount

  if credit?
    self.raw_amount = -amount.to_d
  elsif debit?
    self.raw_amount =  amount.to_d
  end
end

Private Instance Methods

cost_center_validation() click to toggle source
# File lib/keepr/posting.rb, line 74
def cost_center_validation
  return unless keepr_cost_center
  return if keepr_account.profit_and_loss?

  # allowed for expense or revenue accounts only
  errors.add :keepr_cost_center_id, :allowed_for_expense_or_revenue_only
end