internetee-registry/app/models/invoice.rb
2015-04-21 11:16:43 +03:00

67 lines
1.3 KiB
Ruby

class Invoice < ActiveRecord::Base
include Versions
belongs_to :seller, class_name: 'Registrar'
belongs_to :buyer, class_name: 'Registrar'
has_many :invoice_items
has_one :account_activity
accepts_nested_attributes_for :invoice_items
attr_accessor :billing_email
validates :billing_email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
validates :invoice_type, :due_date, :currency, :seller_name,
:seller_iban, :buyer_name, :invoice_items, :vat_prc, presence: true
def binded?
account_activity.present?
end
def receipt_date
account_activity.try(:created_at)
end
def to_s
I18n.t('invoice_no', no: id)
end
def seller_address
[seller_street, seller_city, seller_state, seller_zip].reject(&:blank?).compact.join(', ')
end
def buyer_address
[buyer_street, buyer_city, buyer_state, buyer_zip].reject(&:blank?).compact.join(', ')
end
def seller_country
Country.new(seller_country_code)
end
def buyer_country
Country.new(buyer_country_code)
end
def forward
return false unless valid?
return false unless billing_email
# TODO: forward invoice
true
end
def items
invoice_items
end
def sum_without_vat
items.map(&:item_sum_without_vat).sum
end
def vat
sum_without_vat * vat_prc
end
def sum
sum_without_vat + vat
end
end