```ruby
class Invoice < ActiveRecord::Base
  # Aspects
  include ApplicationHelper
  
  # Associations
  belongs_to :customer, :class_name => 'Person'
  belongs_to :company, :class_name => 'Person'
  
  # Validations
  validates_date :due_date, :value_date
  validates_presence_of :customer, :company, :title, :amount, :state
  
  # Attachments
  # ===========
  has_many :attachments, :as => :object
  accepts_nested_attributes_for :attachments
  
  # States
  # ======
  STATES = ['booked', 'canceled', 'paid']
  scope :by_state, lambda {|value| where(:state => value)}
  
  # Bookings
  # ========
  def direct_account
    nil
  end
  
  def direct_account
    self.class.direct_account
  end

  has_many :bookings, :as => :reference, :dependent => :destroy do
    # TODO: duplicated in Booking (without parameter)
    def direct_balance(value_date = nil, direct_account = nil)
      return BigDecimal.new('0') unless proxy_owner.direct_account
      
      direct_account ||= proxy_owner.direct_account
      balance = BigDecimal.new('0')

      direct_bookings = scoped
      direct_bookings = direct_bookings.where("value_date <= ?", value_date) if value_date

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

      balance
    end
  end

  def build_booking
    booking_template = BookingTemplate.find_by_code(self.class.to_s.underscore + ':invoice')
    
    booking = booking_template.build_booking(:value_date => value_date, :amount => amount)
    bookings << booking
    
    booking
  end

  # TODO: called due_amount in CyDoc
  def balance(value_date = nil)
    bookings.direct_balance(value_date)
  end
  
  # Helpers
  def to_s
    return "" if amount.nil?

    "%s für %s à %s"  % [title, customer, currency_fmt(amount)]
  end
end
```

Use HasAccounts::Model concern for Invoice.
user
```ruby
class Invoice < ActiveRecord::Base
  # Aspects
  include ApplicationHelper
  
  # Associations
  belongs_to :customer, :class_name => 'Person'
  belongs_to :company, :class_name => 'Person'
  
  # Validations
  validates_date :due_date, :value_date
  validates_presence_of :customer, :company, :title, :amount, :state
  
  # Attachments
  # ===========
  has_many :attachments, :as => :object
  accepts_nested_attributes_for :attachments
  
  # States
  # ======
  STATES = ['booked', 'canceled', 'paid']
  scope :by_state, lambda {|value| where(:state => value)}
  
  # Bookings
  # ========
  include HasAccounts::Model
  
  # Helpers
  def to_s
    return "" if amount.nil?

    "%s für %s à %s"  % [title, customer, currency_fmt(amount)]
  end
end
```
assistant