mirror of
https://github.com/internetee/registry.git
synced 2025-06-11 23:24:48 +02:00
parent
4f51f6c736
commit
d6d20a81a9
5 changed files with 108 additions and 23 deletions
|
@ -1,21 +1,3 @@
|
|||
class VATRateType < ActiveRecord::Type::Value
|
||||
def type_cast_from_user(value)
|
||||
if value.blank?
|
||||
nil
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def type_cast_from_database(value)
|
||||
BigDecimal.new(value) * 100 if value
|
||||
end
|
||||
|
||||
def type_cast_for_database(value)
|
||||
BigDecimal.new(value) / 100 if value
|
||||
end
|
||||
end
|
||||
|
||||
class Registrar < ActiveRecord::Base
|
||||
include Versions # version/registrar_version.rb
|
||||
|
||||
|
@ -35,13 +17,16 @@ 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, presence: true, if: :vat_rate_required?
|
||||
validates :vat_rate, absence: true, if: :local_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_or_equal_to: 99 }, allow_nil: true
|
||||
validates :vat_rate, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 99.9 },
|
||||
allow_nil: true
|
||||
|
||||
validate :forbid_special_code
|
||||
|
||||
attribute :vat_rate, VATRateType.new
|
||||
attribute :vat_rate, ::Type::VATRate.new
|
||||
after_initialize :set_defaults
|
||||
before_validation :generate_iso_11649_reference_no
|
||||
|
||||
|
@ -216,4 +201,8 @@ class Registrar < ActiveRecord::Base
|
|||
def foreign_vat_payer?
|
||||
!local_vat_payer?
|
||||
end
|
||||
|
||||
def vat_rate_required?
|
||||
foreign_vat_payer? && vat_no.blank?
|
||||
end
|
||||
end
|
||||
|
|
19
app/models/type/vat_rate.rb
Normal file
19
app/models/type/vat_rate.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
module Type
|
||||
class VATRate < ActiveRecord::Type::Value
|
||||
def type_cast_from_user(value)
|
||||
if value.blank?
|
||||
nil
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def type_cast_from_database(value)
|
||||
BigDecimal(value) * 100 if value
|
||||
end
|
||||
|
||||
def type_cast_for_database(value)
|
||||
BigDecimal(value) / 100.0 if value
|
||||
end
|
||||
end
|
||||
end
|
|
@ -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>
|
||||
|
||||
|
|
|
@ -23,7 +23,8 @@
|
|||
</div>
|
||||
<div class="col-md-3">
|
||||
<div class="input-group">
|
||||
<%= f.number_field :vat_rate, min: 0, max: 99, class: 'form-control' %>
|
||||
<%= f.number_field :vat_rate, min: 0, max: 99, step: 0.1,
|
||||
class: 'form-control' %>
|
||||
<div class="input-group-addon">%</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
73
test/models/registrar/vat_test.rb
Normal file
73
test/models/registrar/vat_test.rb
Normal file
|
@ -0,0 +1,73 @@
|
|||
require 'test_helper'
|
||||
|
||||
class RegistrarVATTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
@registrar = registrars(:bestnames)
|
||||
end
|
||||
|
||||
def test_optional_vat_no
|
||||
@registrar.vat_no = ''
|
||||
assert @registrar.valid?
|
||||
|
||||
@registrar.vat_no = 'any'
|
||||
assert @registrar.valid?
|
||||
end
|
||||
|
||||
def test_requires_vat_rate_when_registrar_is_foreign_vat_payer_and_vat_no_is_absent
|
||||
@registrar.vat_no = ''
|
||||
|
||||
Registry.instance.stub(:legal_address_country, Country.new('GB')) do
|
||||
@registrar.vat_rate = ''
|
||||
assert @registrar.invalid?
|
||||
assert @registrar.errors.added?(:vat_rate, :blank)
|
||||
|
||||
@registrar.vat_rate = -1
|
||||
assert @registrar.invalid?
|
||||
|
||||
@registrar.vat_rate = 1
|
||||
assert @registrar.valid?
|
||||
|
||||
@registrar.vat_rate = 99.9
|
||||
assert @registrar.valid?
|
||||
|
||||
@registrar.vat_rate = 100
|
||||
assert @registrar.invalid?
|
||||
end
|
||||
end
|
||||
|
||||
def test_vat_is_not_applied_when_registrar_is_local_vat_payer
|
||||
@registrar.vat_rate = 1
|
||||
assert @registrar.invalid?
|
||||
|
||||
@registrar.vat_rate = nil
|
||||
assert @registrar.valid?
|
||||
end
|
||||
|
||||
def test_vat_is_not_applied_when_registrar_is_foreign_vat_payer_and_vat_no_is_present
|
||||
@registrar.vat_no = 'valid'
|
||||
|
||||
Registry.instance.stub(:legal_address_country, Country.new('GB')) do
|
||||
@registrar.vat_rate = 1
|
||||
assert @registrar.invalid?
|
||||
|
||||
@registrar.vat_rate = nil
|
||||
assert @registrar.valid?
|
||||
end
|
||||
end
|
||||
|
||||
def test_serializes_and_deserializes_vat_rate
|
||||
@registrar.vat_rate = '25.5'
|
||||
|
||||
Registry.instance.stub(:legal_address_country, Country.new('GB')) do
|
||||
@registrar.save!
|
||||
end
|
||||
|
||||
@registrar.reload
|
||||
assert_equal 25.5, @registrar.vat_rate
|
||||
end
|
||||
|
||||
def test_treats_empty_vat_rate_as_absent
|
||||
@registrar.vat_rate = ''
|
||||
assert_nil @registrar.vat_rate
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue