mirror of
https://github.com/internetee/registry.git
synced 2025-07-23 11:16:00 +02:00
parent
026c36d066
commit
7d72d9cb34
8 changed files with 121 additions and 94 deletions
|
@ -32,9 +32,11 @@ class Invoice < ActiveRecord::Base
|
|||
validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 },
|
||||
allow_nil: true
|
||||
|
||||
before_create :set_invoice_number, :check_vat
|
||||
after_initialize :apply_defaults
|
||||
before_create :set_invoice_number
|
||||
|
||||
before_save :check_vat
|
||||
attribute :vat_rate, ::Type::VATRate.new
|
||||
attr_readonly :vat_rate
|
||||
|
||||
def set_invoice_number
|
||||
last_no = Invoice.order(number: :desc).where('number IS NOT NULL').limit(1).pluck(:number).first
|
||||
|
@ -52,12 +54,6 @@ class Invoice < ActiveRecord::Base
|
|||
false
|
||||
end
|
||||
|
||||
def check_vat
|
||||
if buyer.country_code != 'EE' && buyer.vat_no.present?
|
||||
self.vat_rate = 0
|
||||
end
|
||||
end
|
||||
|
||||
before_save -> { self.sum_cache = sum }
|
||||
|
||||
class << self
|
||||
|
@ -159,10 +155,17 @@ class Invoice < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def vat
|
||||
return 0 unless vat_rate
|
||||
(sum_without_vat * vat_rate).round(2)
|
||||
end
|
||||
|
||||
def sum
|
||||
(sum_without_vat + vat).round(2)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def apply_defaults
|
||||
self.vat_rate = buyer.effective_vat_rate unless vat_rate
|
||||
end
|
||||
end
|
||||
|
|
|
@ -18,8 +18,8 @@ class Registrar < ActiveRecord::Base
|
|||
validates :accounting_customer_code, presence: true
|
||||
validates :language, presence: true
|
||||
|
||||
validates :vat_rate, presence: true, if: :vat_rate_required?
|
||||
validates :vat_rate, absence: true, if: :local_vat_payer?
|
||||
validates :vat_rate, presence: true, if: 'foreign_vat_payer? && vat_no.blank?'
|
||||
validates :vat_rate, absence: true, if: :home_vat_payer?
|
||||
validates :vat_rate, absence: true, if: 'foreign_vat_payer? && vat_no?'
|
||||
validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than: 100 },
|
||||
allow_nil: true
|
||||
|
@ -56,22 +56,11 @@ class Registrar < ActiveRecord::Base
|
|||
# rubocop:disable Metrics/MethodLength
|
||||
# rubocop:disable Metrics/AbcSize
|
||||
def issue_prepayment_invoice(amount, description = nil)
|
||||
vat_rate = if local_vat_payer?
|
||||
Registry.instance.vat_rate
|
||||
else
|
||||
if vat_no.blank?
|
||||
self.vat_rate
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
invoices.create(
|
||||
due_date: (Time.zone.now.to_date + Setting.days_to_keep_invoices_active.days).end_of_day,
|
||||
payment_term: 'prepayment',
|
||||
description: description,
|
||||
currency: 'EUR',
|
||||
vat_rate: vat_rate,
|
||||
seller_name: Setting.registry_juridical_name,
|
||||
seller_reg_no: Setting.registry_reg_no,
|
||||
seller_iban: Setting.registry_iban,
|
||||
|
@ -157,6 +146,14 @@ class Registrar < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def effective_vat_rate
|
||||
if home_vat_payer?
|
||||
Registry.instance.vat_rate
|
||||
else
|
||||
vat_rate
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_defaults
|
||||
|
@ -188,15 +185,11 @@ class Registrar < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def local_vat_payer?
|
||||
def home_vat_payer?
|
||||
country == Registry.instance.legal_address_country
|
||||
end
|
||||
|
||||
def foreign_vat_payer?
|
||||
!local_vat_payer?
|
||||
end
|
||||
|
||||
def vat_rate_required?
|
||||
foreign_vat_payer? && vat_no.blank?
|
||||
!home_vat_payer?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,19 +1,11 @@
|
|||
module Type
|
||||
class VATRate < ActiveRecord::Type::Value
|
||||
def type_cast_from_user(value)
|
||||
if value.blank?
|
||||
nil
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
class VATRate < ActiveRecord::Type::Decimal
|
||||
def type_cast_from_database(value)
|
||||
BigDecimal(value) * 100 if value
|
||||
super * 100 if value
|
||||
end
|
||||
|
||||
def type_cast_for_database(value)
|
||||
BigDecimal(value) / 100.0 if value
|
||||
super / 100.0 if value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue