Add registrar VAT rate

#623
This commit is contained in:
Artur Beljajev 2017-11-23 23:27:06 +02:00
parent 6f9ea85a95
commit c47ca77ca6
9 changed files with 105 additions and 1 deletions

View file

@ -79,7 +79,8 @@ module Admin
:code,
:test_registrar,
:accounting_customer_code,
:language)
:language,
:vat_rate,)
end
end
end

View file

@ -17,10 +17,22 @@ class Registrar < ActiveRecord::Base
validates :name, :reg_no, :reference_no, :code, uniqueness: true
validates :accounting_customer_code, presence: true
validates :language, presence: true
validates :vat_rate, :vat_no, absence: true, if: :local_vat_payer?
validates :vat_rate, presence: true, if: 'foreign_vat_payer? && vat_no.blank?'
validates :vat_rate, absence: true, if: 'foreign_vat_payer? && vat_no?'
validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 99 }, allow_nil: true
validate :forbidden_codes
after_initialize :set_defaults
before_save do
self.vat_rate = vat_rate / 100.0 if vat_rate
end
after_find do |registrar|
registrar.vat_rate = registrar.vat_rate * 100 if registrar.vat_rate
end
def forbidden_codes
return true unless ['CID'].include? code
errors.add(:code, I18n.t(:forbidden_code))
@ -163,4 +175,12 @@ class Registrar < ActiveRecord::Base
def set_defaults
self.language = Setting.default_language unless language
end
def local_vat_payer?
country == Registry.instance.legal_address_country
end
def foreign_vat_payer?
!local_vat_payer?
end
end

View file

@ -7,6 +7,9 @@
<dt><%= Registrar.human_attribute_name :vat_no %></dt>
<dd><%= registrar.vat_no %></dd>
<dt><%= Registrar.human_attribute_name :vat_rate %></dt>
<dd><%= number_to_percentage registrar.vat_rate, precision: 1 %></dd>
<dt><%= Registrar.human_attribute_name :accounting_customer_code %></dt>
<dd><%= registrar.accounting_customer_code %></dd>

View file

@ -17,6 +17,18 @@
</div>
</div>
<div class="form-group">
<div class="col-md-4 control-label">
<%= f.label :vat_rate %>
</div>
<div class="col-md-3">
<div class="input-group">
<%= f.number_field :vat_rate, class: 'form-control' %>
<div class="input-group-addon">%</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-4 control-label">
<%= f.label :accounting_customer_code %>

View file

@ -783,3 +783,4 @@ en:
attributes:
vat_no: VAT number
vat_rate: VAT rate

View file

@ -16,3 +16,10 @@ goodnames:
country_code: US
accounting_customer_code: goodnames
language: en
foreign_vat_payer_with_rate:
<<: *DEFAULTS
name: 3
reg_no: 3
code: 3
vat_rate: 0.2

View file

@ -16,4 +16,12 @@ class ShowRegistrarTest < ActionDispatch::IntegrationTest
def test_language
assert_text 'Language English'
end
def test_vat_number
assert_text 'US12345'
end
def test_vat_rate
assert_text number_to_percentage(@registrar.vat_rate, precision: 1)
end
end

View file

@ -38,4 +38,54 @@ class RegistrarTest < ActiveSupport::TestCase
registrar = Registrar.new(language: 'de')
assert_equal 'de', registrar.language
end
def test_rejects_vat_number_when_local_vat_payer
Registry.instance.stub(:legal_address_country, Country.new('US')) do
@registrar.vat_no = 'US1'
@registrar.validate
assert @registrar.invalid?
end
end
def test_rejects_vat_rate_when_local_vat_payer
Registry.instance.stub(:legal_address_country, Country.new('US')) do
@registrar.vat_rate = 20
@registrar.validate
assert @registrar.invalid?
end
end
def test_rejects_negative_vat_rate
@registrar.vat_rate = -1
@registrar.validate
assert @registrar.invalid?
end
def test_rejects_vat_rate_greater_than_max
@registrar.vat_rate = 100
@registrar.validate
assert @registrar.invalid?
end
def test_converts_integer_vat_rate_to_fractional
@registrar = registrars(:foreign_vat_payer_with_rate)
assert_equal 20, @registrar.vat_rate
end
def test_requires_vat_rate_when_foreign_vat_payer_without_number
Registry.instance.stub(:legal_address_country, Country.new('GB')) do
@registrar.vat_no = nil
@registrar.validate
assert @registrar.invalid?
end
end
def test_rejects_vat_rate_when_foreign_vat_payer_with_number
Registry.instance.stub(:legal_address_country, Country.new('GB')) do
@registrar.vat_no = 'US1'
@registrar.vat_rate = 1
@registrar.validate
assert @registrar.invalid?
end
end
end

View file

@ -14,6 +14,8 @@ require 'webmock/minitest'
Setting.address_processing = false
Setting.registry_country_code = 'US'
class ActiveSupport::TestCase
include FactoryBot::Syntax::Methods