Skip to content

Commit

Permalink
Merge pull request #1188 from internetee/estonian-vat-rate-toggle
Browse files Browse the repository at this point in the history
added toggle to switch estonian tax rate
  • Loading branch information
vohmar authored Dec 29, 2023
2 parents 22aae79 + b7a1f66 commit e7d945f
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 18 deletions.
14 changes: 13 additions & 1 deletion app/models/billing_profile.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'countries'

class BillingProfile < ApplicationRecord
OLD_EST_RATE_VAT = '0.2'.freeze

alias_attribute :country_code, :alpha_two_country_code

validates :name, presence: true
Expand Down Expand Up @@ -56,7 +58,17 @@ def address
end

def vat_rate
return Countries.vat_rate_from_alpha2_code(country_code) if country_code == 'EE'
# return Countries.vat_rate_from_alpha2_code(country_code) if country_code == 'EE'
# return BigDecimal(Setting.find_by(code: :estonian_vat_rate).retrieve, 2) if country_code == 'EE'

if country_code == 'EE'
# if Time.zone.now.year < 2024
# return BigDecimal(OLD_EST_RATE_VAT)
# else
return BigDecimal(Setting.find_by(code: :estonian_vat_rate).retrieve, 2)
# end
end

return BigDecimal('0') if vat_code.present?

Countries.vat_rate_from_alpha2_code(country_code)
Expand Down
13 changes: 12 additions & 1 deletion app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Invoice < ApplicationRecord
include Payable
include Linkpayable

OLD_EST_RATE_VAT = '0.2'.freeze

alias_attribute :country_code, :alpha_two_country_code
enum status: { issued: 'issued', paid: 'paid', cancelled: 'cancelled' }

Expand Down Expand Up @@ -189,7 +191,16 @@ def address
end

def vat_rate
return Countries.vat_rate_from_alpha2_code(country_code) if country_code == 'EE'
# return Countries.vat_rate_from_alpha2_code(country_code) if country_code == 'EE'

if country_code == 'EE'
if created_at.year < 2023
return BigDecimal(OLD_EST_RATE_VAT)
else
return BigDecimal(Setting.find_by(code: :estonian_vat_rate).retrieve, 2)
end
end

return BigDecimal('0') if vat_code.present?

Countries.vat_rate_from_alpha2_code(country_code)
Expand Down
7 changes: 6 additions & 1 deletion app/models/offer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ def price=(value)
def total
return price * (DEFAULT_PRICE_VALUE + billing_profile.vat_rate) if billing_profile.present?

default_vat = Countries.vat_rate_from_alpha2_code(user&.country_code || 'EE')
if user&.country_code == 'EE' || user&.country_code.nil?
default_vat = Setting.find_by(code: :estonian_vat_rate).retrieve
else
default_vat = Countries.vat_rate_from_alpha2_code(user.country_code)
end

price * (DEFAULT_PRICE_VALUE + (Invoice.find_by(result: result)&.vat_rate || default_vat))
end
end
5 changes: 5 additions & 0 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Setting < ApplicationRecord
boolean: :boolean_format,
hash: :hash_format,
array: :array_format,
decimal: :decimal_format
}.with_indifferent_access.freeze

def self.default_scope
Expand Down Expand Up @@ -66,6 +67,10 @@ def array_format
JSON.parse(value).to_a
end

def decimal_format
value.to_f
end

def auction_duration_value
if value == 'end_of_day'
:end_of_day
Expand Down
14 changes: 7 additions & 7 deletions db/migrate/20231006095158_update_invoice_issuer_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ def up
end

def down
Seting.find_by(code: :invoice_issuer_reg_no).delete
Seting.find_by(code: :invoice_issuer_vat_no).delete
Seting.find_by(code: :invoice_issuer_street).delete
Seting.find_by(code: :invoice_issuer_state).delete
Seting.find_by(code: :invoice_issuer_zip).delete
Seting.find_by(code: :invoice_issuer_city).delete
Seting.find_by(code: :invoice_issuer_country_code).delete
Setting.find_by(code: :invoice_issuer_reg_no).delete
Setting.find_by(code: :invoice_issuer_vat_no).delete
Setting.find_by(code: :invoice_issuer_street).delete
Setting.find_by(code: :invoice_issuer_state).delete
Setting.find_by(code: :invoice_issuer_zip).delete
Setting.find_by(code: :invoice_issuer_city).delete
Setting.find_by(code: :invoice_issuer_country_code).delete
end
end
17 changes: 17 additions & 0 deletions db/migrate/20231222085647_add_estonian_vat_rate_to_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class AddEstonianVatRateToSettings < ActiveRecord::Migration[7.0]
def up
estonian_vat_rate_description = <<~TEXT.squish
Default Estonian vat rate. Should be in decimal format like this: 0.2
TEXT

estonian_vat_rate_setting = Setting.new(code: :estonian_vat_rate,
value: '0.2',
description: estonian_vat_rate_description,
value_format: 'decimal')
estonian_vat_rate_setting.save!
end

def down
Setting.find_by(code: :estonian_vat_rate).delete
end
end
11 changes: 11 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,14 @@
description: openai_evaluation_prompt_description,
value_format: 'string')
openai_domains_evaluation_prompt_setting.save

# Estonian VAT rate
estonian_vat_rate_description = <<~TEXT.squish
Default Estonian vat rate. Should be in decimal format like this: 0.2
TEXT

estonian_vat_rate_setting = Setting.new(code: :estonian_vat_rate,
value: '0.2',
description: estonian_vat_rate_description,
value_format: 'decimal')
estonian_vat_rate_setting.save!
5 changes: 4 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3189,6 +3189,9 @@ INSERT INTO "schema_migrations" (version) VALUES
('20231013110924'),
('20231031092610'),
('20231031122202'),
('20231031122216');
('20231031122216'),
('20231116093310'),
('20231222074427'),
('20231222085647');


5 changes: 2 additions & 3 deletions lib/countries.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: UTF-8
module Countries
ALPHA_2_SELECTION = [{ name: 'Afghanistan', code: 'AF' },
{ name: 'Åland Islands', code: 'AX' },
Expand Down Expand Up @@ -259,7 +258,7 @@ module Countries
{ code: 'CY', rate: BigDecimal('0.19') },
{ code: 'CZ', rate: BigDecimal('0.21') },
{ code: 'DK', rate: BigDecimal('0.25') },
{ code: 'EE', rate: BigDecimal('0.2') },
{ code: 'EE', rate: BigDecimal('0.22') },
{ code: 'FI', rate: BigDecimal('0.24') },
{ code: 'FR', rate: BigDecimal('0.2') },
{ code: 'DE', rate: BigDecimal('0.19') },
Expand All @@ -278,7 +277,7 @@ module Countries
{ code: 'SK', rate: BigDecimal('0.2') },
{ code: 'SI', rate: BigDecimal('0.22') },
{ code: 'ES', rate: BigDecimal('0.21') },
{ code: 'SE', rate: BigDecimal('0.25') },].freeze
{ code: 'SE', rate: BigDecimal('0.25') }].freeze

def self.for_selection
ALPHA_2_SELECTION.collect { |i| [i[:name], i[:code]] }
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,10 @@ auction_iban:
IBAN of the account that should appear in invoice as issuer.
value: 'EE557700771000598731'
value_format: string

estonian_vat_rate:
code: 'estonian_vat_rate'
description: |
Estonian VAT rate in percent. Default: 0.22
value: '0.22'
value_format: decimal
8 changes: 7 additions & 1 deletion test/lib/countries_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
require 'countries'

class CountriesTest < ActiveSupport::TestCase
def setup
super

@setting = settings(:estonian_vat_rate)
end

def test_name_from_country_code
assert_equal('United Kingdom', Countries.name_from_alpha2_code('GB'))
assert_nil(Countries.name_from_alpha2_code('GBSS'))
Expand All @@ -12,7 +18,7 @@ def test_alpha2_code_from_name
end

def test_vat_rate_from_country_code
assert_equal(BigDecimal('0.2'), Countries.vat_rate_from_alpha2_code('EE'))
assert_equal(BigDecimal(Setting.find_by(code: :estonian_vat_rate).retrieve, 2), Countries.vat_rate_from_alpha2_code('EE'))
assert_equal(BigDecimal('0.25'), Countries.vat_rate_from_alpha2_code('SE'))

assert_equal(BigDecimal('0'), Countries.vat_rate_from_alpha2_code('US'))
Expand Down
12 changes: 10 additions & 2 deletions test/models/billing_profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def setup
@billing_profile = billing_profiles(:company)
@orphaned_profile = billing_profiles(:orphaned)

@setting = settings(:estonian_vat_rate)

stub_request(:patch, 'http://eis_billing_system:3000/api/v1/invoice/update_invoice_data')
.to_return(status: 200, body: @message.to_json, headers: {})
end
Expand Down Expand Up @@ -82,14 +84,20 @@ def test_estonian_customer_always_gets_vat_rate_applied
assert_equal(BigDecimal('0'), @billing_profile.vat_rate)

@billing_profile.update(country_code: 'EE')
assert_equal(BigDecimal('0.20'), @billing_profile.vat_rate)

travel_to Time.zone.local(2024, 1, 1, 0, 0, 0) do
assert_equal(BigDecimal(Setting.find_by(code: :estonian_vat_rate).retrieve, 2), @billing_profile.vat_rate)
end
end

def test_customer_from_outside_european_union_has_vat_rate_0
assert_equal(BigDecimal('0'), @billing_profile.vat_rate)

@billing_profile.update(country_code: 'EE')
assert_equal(BigDecimal('0.20'), @billing_profile.vat_rate)

travel_to Time.zone.local(2024, 1, 1, 0, 0, 0) do
assert_equal(BigDecimal(Setting.find_by(code: :estonian_vat_rate).retrieve, 2), @billing_profile.vat_rate)
end

@billing_profile.update(country_code: 'US')
assert_equal(BigDecimal('0'), @billing_profile.vat_rate)
Expand Down
5 changes: 4 additions & 1 deletion test/models/offer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ def test_total_work_as_expected_if_billing_profile_was_deleted_for_UK_profile
assert_equal(offer.total, Money.new('500', 'EUR'))

offer.billing_profile = nil
assert_equal(offer.total, Money.new('600', 'EUR'))

travel_to Time.zone.local(2024, 1, 1, 0, 0, 0) do
assert_equal(offer.total, Money.new('610', 'EUR'))
end
end

def test_offer_must_be_higher_than_minimum_value_allowed_in_settings
Expand Down

0 comments on commit e7d945f

Please sign in to comment.