From c47ca77ca655c3c1c35faecbf89ba2f3c3ac0944 Mon Sep 17 00:00:00 2001 From: Artur Beljajev Date: Thu, 23 Nov 2017 23:27:06 +0200 Subject: [PATCH] Add registrar VAT rate #623 --- .../admin/registrars_controller.rb | 3 +- app/models/registrar.rb | 20 ++++++++ app/views/admin/registrars/_billing.html.erb | 3 ++ .../admin/registrars/form/_billing.html.erb | 12 +++++ config/locales/en.yml | 1 + test/fixtures/registrars.yml | 7 +++ .../admin/registrars/show_registrar_test.rb | 8 +++ test/models/registrar_test.rb | 50 +++++++++++++++++++ test/test_helper.rb | 2 + 9 files changed, 105 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index 119abf85b..eb9ae9bd7 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -79,7 +79,8 @@ module Admin :code, :test_registrar, :accounting_customer_code, - :language) + :language, + :vat_rate,) end end end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 5fb68fea2..393bd56f2 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -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 diff --git a/app/views/admin/registrars/_billing.html.erb b/app/views/admin/registrars/_billing.html.erb index f3960233e..7ba778d08 100644 --- a/app/views/admin/registrars/_billing.html.erb +++ b/app/views/admin/registrars/_billing.html.erb @@ -7,6 +7,9 @@
<%= Registrar.human_attribute_name :vat_no %>
<%= registrar.vat_no %>
+
<%= Registrar.human_attribute_name :vat_rate %>
+
<%= number_to_percentage registrar.vat_rate, precision: 1 %>
+
<%= Registrar.human_attribute_name :accounting_customer_code %>
<%= registrar.accounting_customer_code %>
diff --git a/app/views/admin/registrars/form/_billing.html.erb b/app/views/admin/registrars/form/_billing.html.erb index a3adf1312..5bc87a93d 100644 --- a/app/views/admin/registrars/form/_billing.html.erb +++ b/app/views/admin/registrars/form/_billing.html.erb @@ -17,6 +17,18 @@ +
+
+ <%= f.label :vat_rate %> +
+
+
+ <%= f.number_field :vat_rate, class: 'form-control' %> +
%
+
+
+
+
<%= f.label :accounting_customer_code %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 58c4e4c69..e9b8316d6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -783,3 +783,4 @@ en: attributes: vat_no: VAT number + vat_rate: VAT rate diff --git a/test/fixtures/registrars.yml b/test/fixtures/registrars.yml index 0026f323b..b5715a0a5 100644 --- a/test/fixtures/registrars.yml +++ b/test/fixtures/registrars.yml @@ -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 diff --git a/test/integration/admin/registrars/show_registrar_test.rb b/test/integration/admin/registrars/show_registrar_test.rb index cb310317c..f13748191 100644 --- a/test/integration/admin/registrars/show_registrar_test.rb +++ b/test/integration/admin/registrars/show_registrar_test.rb @@ -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 diff --git a/test/models/registrar_test.rb b/test/models/registrar_test.rb index 58a06b0ae..3ace52ba7 100644 --- a/test/models/registrar_test.rb +++ b/test/models/registrar_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 7bde0991d..64cef7e0f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,6 +14,8 @@ require 'webmock/minitest' Setting.address_processing = false +Setting.registry_country_code = 'US' + class ActiveSupport::TestCase include FactoryBot::Syntax::Methods