Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed vat rate calculation logic #1194

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 6.4.0'
gem 'rails', '>= 6.0.3.5'
gem 'rails-i18n'
# gem 'rails_semantic_logger'
gem 'recaptcha'
gem 'ruby-openai'
gem 'scenic'
gem 'simpleidn'
gem 'skylight'
gem 'sprockets', '~> 4.0'
# gem 'turbolinks', '~> 5'
gem 'turbo-rails'
gem 'webpacker', '~> 6.0.0.rc.5'
gem 'webpush'
Expand All @@ -49,6 +47,12 @@ group :development, :test do

# https://github.com/rubocop/rubocop-performance
gem 'rubocop-performance', require: false
gem "ruby-lsp"
gem "rubocop"
gem "rubocop-packaging"
gem "rubocop-rspec"
gem "rubocop-shopify"
gem "rubocop-thread_safety"
end

group :development do
Expand Down
26 changes: 26 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ GEM
pg_search (2.3.6)
activerecord (>= 5.2)
activesupport (>= 5.2)
prism (0.19.0)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -346,9 +347,27 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-capybara (2.19.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.24.0)
rubocop (~> 1.33)
rubocop-packaging (0.5.2)
rubocop (>= 1.33, < 2.0)
rubocop-performance (1.18.0)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rspec (2.25.0)
rubocop (~> 1.40)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
rubocop-shopify (2.14.0)
rubocop (~> 1.51)
rubocop-thread_safety (0.5.1)
rubocop (>= 0.90.0)
ruby-lsp (0.13.2)
language_server-protocol (~> 3.17.0)
prism (>= 0.19.0, < 0.20)
sorbet-runtime (>= 0.5.5685)
ruby-openai (4.2.0)
faraday (>= 1)
faraday-multipart (>= 1)
Expand All @@ -372,6 +391,7 @@ GEM
unf (~> 0.1.4)
skylight (5.3.4)
activesupport (>= 5.2.0)
sorbet-runtime (0.5.11164)
sprockets (4.2.0)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand Down Expand Up @@ -474,7 +494,13 @@ DEPENDENCIES
rails-i18n
recaptcha
redis (~> 5.0)
rubocop
rubocop-packaging
rubocop-performance
rubocop-rspec
rubocop-shopify
rubocop-thread_safety
ruby-lsp
ruby-openai
scenic
selenium-webdriver
Expand Down
41 changes: 21 additions & 20 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class Invoice < ApplicationRecord
validates :billing_profile, presence: true, on: :create

validate :user_id_must_be_the_same_as_on_billing_profile_or_nil
before_update :update_billing_info

before_create :set_invoice_number

before_update :update_billing_info
before_update :recalculate_vat_rate

delegate :enable_deposit?, to: :enable_deposit?
delegate :deposit, to: :deposit

attr_accessor :vat_rate

scope :with_search_scope, (lambda do |origin|
if origin.present?
if numeric?(origin)
Expand Down Expand Up @@ -85,7 +85,7 @@ def self.search(params = {})
when 'channel'
query.left_outer_joins(:paid_with_payment_order)
.select("invoices.*, REPLACE(payment_orders.type, 'PaymentOrders::', '') AS payment_order_channel")
.order(Arel.sql("payment_order_channel #{sort_direction} NULLS LAST"))
.order(Arel.sql("payment_order_channel #{sort_direction} NULLS LAST"))
when 'billing_profile_name'
query.left_outer_joins(:billing_profile).order("billing_profiles.name #{sort_direction}")
when 'total'
Expand Down Expand Up @@ -118,6 +118,23 @@ def deposit
result.auction.deposit
end

def recalculate_vat_rate
return if billing_profile_id == billing_profile_id_was
return unless payable?

self.vat_rate = assign_vat_rate

send_updated_invoice_infromation_to_billing_service
end

def assign_vat_rate
return BigDecimal(Setting.find_by(code: :estonian_vat_rate).retrieve, 2) if country_code == 'EE'

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

Countries.vat_rate_from_alpha2_code(country_code)
end

def billing_restrictions_issue
errors.add(:base, I18n.t('cannot get access'))
logger.error('PROBLEM WITH TOKEN')
Expand Down Expand Up @@ -190,22 +207,6 @@ def address
[street, postal_code_with_city, country_name].compact.join(', ')
end

def vat_rate
return tax_fresh_rate if country_code == 'EE'

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

Countries.vat_rate_from_alpha2_code(country_code)
end

def tax_fresh_rate
if created_at.year < 2024
BigDecimal(OLD_EST_RATE_VAT)
else
BigDecimal(Setting.find_by(code: :estonian_vat_rate).retrieve, 2)
end
end

def filename
return unless title

Expand Down
48 changes: 25 additions & 23 deletions app/models/invoice_creator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class InvoiceCreator
attr_reader :result_id
attr_reader :result
attr_reader :invoice
attr_reader :result_id, :result, :invoice

def initialize(result_id)
@result_id = result_id
Expand Down Expand Up @@ -45,7 +43,7 @@ def assign_invoice_associations
end

def send_invoice_to_billing_system(invoice)
response = EisBilling::Invoice.call(invoice: invoice)
response = EisBilling::Invoice.call(invoice:)
if response.result?
link = response.instance['everypay_link']
invoice.update(payment_link: link)
Expand All @@ -56,14 +54,7 @@ def send_invoice_to_billing_system(invoice)

def assign_price
invoice.cents = result_offer.cents
invoice.invoice_items = [
InvoiceItem.new(invoice: invoice,
cents: result_offer.cents,
name: I18n.t('invoice_items.name',
domain_name: result_auction.domain_name,
auction_end: result_auction.ends_at.to_date,
locale: I18n.default_locale)),
]
invoice.invoice_items = [assign_invoice_item]
end

def assign_billing_address
Expand All @@ -86,6 +77,7 @@ def create_invoice

ActiveRecord::Base.transaction do
assign_invoice_associations
invoice.vat_rate = assign_vat_rate
result_auction.enable_deposit ? assign_price_with_deposit : assign_price
set_issue_and_due_date
assign_billing_address
Expand All @@ -95,23 +87,33 @@ def create_invoice
end

def assign_price_with_deposit
# total = result_offer.cents - result_auction.requirement_deposit_in_cents

# invoice.cents = total
invoice.cents = result_offer.cents
invoice.invoice_items = [
InvoiceItem.new(invoice: invoice,
cents: result_offer.cents,
name: I18n.t('invoice_items.name',
domain_name: result_auction.domain_name,
auction_end: result_auction.ends_at.to_date,
locale: I18n.default_locale)),
]
invoice.invoice_items = [assign_invoice_item]
end

def assign_invoice_item
InvoiceItem.new(invoice:,
cents: result_offer.cents,
name: I18n.t('invoice_items.name',
domain_name: result_auction.domain_name,
auction_end: result_auction.ends_at.to_date,
locale: I18n.default_locale))
end

def mark_as_paid_if_sum_is_zero
return unless invoice.total.zero?

invoice.mark_as_paid_at(Time.zone.now)
end

def assign_vat_rate
if invoice.billing_alpha_two_country_code == 'EE'
return BigDecimal(Setting.find_by(code: :estonian_vat_rate).retrieve,
2)
end

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

Countries.vat_rate_from_alpha2_code(invoice.billing_alpha_two_country_code)
end
end
19 changes: 19 additions & 0 deletions lib/tasks/assign_invoices_vat_rate.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace :invoices do
desc 'Assign vat rate to invoices what have vat rate of nil'
task assign_invoices_vat_rate: :environment do
Invoice.where(vat_rate: [nil, 0.0], country_code: 'EE').in_batches do |batch_invoices|
batch_invoices.where('created_at < ?', '2024-01-01').update_all(vat_rate: 0.2)
batch_invoices.where('created_at >= ?', '2024-01-01').update_all(vat_rate: 0.22)
end

Invoice.where(vat_rate: nil).where.not(vat_code: nil).in_batches do |batch_invoices|
batch_invoices.update_all(vat_rate: 0.0)
end

Invoice.where.not(country_code: 'EE').where(vat_rate: nil).in_batches do |batch_invoices|
batch_invoices.each do |invoice|
invoice.update(vat_rate: Countries.vat_rate_from_alpha2_code(invoice.country_code))
end
end
end
end
2 changes: 2 additions & 0 deletions test/fixtures/invoices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ payable:
billing_address: "Baker Street 221B, NW1 6XE London, United Kingdom"
billing_vat_code: "12234567890"
billing_alpha_two_country_code: "GB"
vat_rate: 0.0

orphaned:
result: orphaned
Expand All @@ -39,3 +40,4 @@ orphaned:
billing_name: "Orphan Profile"
billing_address: "Baker Street 221B, NW1 6XE London, United Kingdom"
billing_alpha_two_country_code: "GB"
vat_rate: 0.0
39 changes: 39 additions & 0 deletions test/models/invoice_creator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,43 @@ def test_should_be_assigned_billing_profile_information
assert_equal result.invoice.billing_vat_code, @billing_company.vat_code
assert_equal result.invoice.billing_alpha_two_country_code, @billing_company.country_code
end

def test_should_be_assigned_estonian_vat_rate
offer_bid_value = 10_000

user = users(:participant)
auction = auctions(:english)

mid_day = Time.new(Time.now.year, Time.now.month, Time.now.day, 12, 0, 0)
auction.update(starts_at: mid_day - 1.day, ends_at: Time.zone.now + 10.minutes)
auction.reload
assert auction.offers.empty?

billing_profile = user.billing_profiles.first
billing_profile.update!(country_code: 'EE', alpha_two_country_code: 'EE', vat_code: 'IE6388047V') && billing_profile.reload

Offer.create!(
auction: auction,
user: user,
cents: offer_bid_value,
billing_profile: billing_profile
)

assert auction.offers.present?
auction.update(ends_at: Time.zone.now - 1.minute) && auction.reload

ResultCreationJob.perform_now
auction.reload

result = Result.last
assert_equal result.auction.domain_name, auction.domain_name
assert_equal result.invoice.status, 'issued'

assert_equal billing_profile.country_code, 'EE'
assert_equal result.invoice.billing_vat_code, billing_profile.vat_code
assert_equal result.invoice.billing_alpha_two_country_code, billing_profile.country_code

assert_equal result.invoice.vat_rate, BigDecimal(Setting.find_by(code: :estonian_vat_rate).retrieve, 2)
assert_equal result.invoice.vat_rate, 0.22
end
end
Loading