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

solution for wrong vat rate for old invoices #1223

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 0 additions & 38 deletions app/controllers/payment_orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,6 @@ def show
@payment_order.callback_url = callback_payment_order_url(@payment_order.uuid)
end

# # ANY /payment_orders/aa450f1a-45e2-4f22-b2c3-f5f46b5f906b/return
# def return
# @payment_order = PaymentOrder.find_by!(uuid: params[:uuid])

# if @payment_order.paid?
# respond_to do |format|
# format.html do
# redirect_to invoices_path, notice: t('.already_paid') and return
# end

# format.json { render json: @payment_order.errors, status: :unprocessable_entity and return }
# end
# end

# @payment_order.update!(response: params.to_unsafe_h)

# ResultStatusUpdateJob.perform_later

# respond_to do |format|
# if @payment_order.mark_invoice_as_paid
# format.html { redirect_to invoices_path, notice: successful_update_notice }
# format.json { redirect_to invoices_path, notice: successful_update_notice }
# else
# format.html do
# redirect_to invoices_path,
# notice: t('.not_successful')
# end
# format.json { render json: @payment_order.errors, status: :unprocessable_entity }
# end
# end
# end

# POST /payment_orders/aa450f1a-45e2-4f22-b2c3-f5f46b5f906b/callback
def callback
render status: :ok, json: { status: 'ok' }
Expand All @@ -76,12 +44,6 @@ def create_params
params.require(:payment_order).permit(:user_id, :invoice_id, :invoice_ids, :type)
end

# def successful_update_notice
# invoice_ids = @payment_order.invoices.map(&:number).join(', ')
# return t('.bulk_update', ids: invoice_ids) if @payment_order.invoices.count > 1
# t(:updated)
# end

def authorize_user
authorize! :read, PaymentOrder
authorize! :create, PaymentOrder
Expand Down
2 changes: 1 addition & 1 deletion app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def recalculate_vat_rate
end

def assign_vat_rate
return BigDecimal(OLD_EST_RATE_VAT, 2) if created_at < Date.new(2024, 1, 1) && country_code == 'EE'
return BigDecimal(Setting.find_by(code: :estonian_vat_rate).retrieve, 2) if country_code == 'EE'

return BigDecimal('0') if vat_code.present?
Expand Down Expand Up @@ -299,7 +300,6 @@ def clear_linked_ban

def prepare_payment_fields(time)
self.paid_at = time
self.vat_rate = billing_profile.present? ? billing_profile.vat_rate : vat_rate
self.paid_amount = total
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/assign_invoices_vat_rate.rake
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ namespace :invoices do
end
end
end

task assign_values_to_old_invoices: :environment do
Invoice.where('created_at < ?', '2024-01-01').where(country_code: 'EE', vat_rate: 0.22)
.update_all(vat_rate: 0.2, in_directo: false)
end
end
44 changes: 44 additions & 0 deletions test/models/invoice_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,50 @@ def test_if_auction_has_deposit_it_should_be_destracted_from_total
assert_equal @payable_invoice.total.to_f, 5.0
end

def test_billing_profile_change_should_not_effect_to_old_invoices
@payable_invoice.update!(
created_at: Date.new(2023, 1, 1).to_time,
issue_date: Date.new(2023, 1, 2).to_time,
due_date: Date.new(2023, 1, 3).to_time,
vat_rate: 0.2,
)

travel_to Date.new(2024, 1, 10).to_time

assert_equal @payable_invoice.created_at, Date.new(2023, 1, 1).to_time

user = @payable_invoice.user
billing_profile = BillingProfile.create!(name: 'test', country_code: 'EE', user: user, vat_code: nil)

assert_equal 0.2, @payable_invoice.vat_rate

@payable_invoice.update!(billing_profile: billing_profile) && @payable_invoice.reload

assert_equal 0.2, @payable_invoice.vat_rate
assert_equal billing_profile, @payable_invoice.billing_profile
end

def test_after_payment_invoice_data_should_not_be_changed
@payable_invoice.update!(
created_at: Date.new(2023, 1, 1).to_time,
issue_date: Date.new(2023, 1, 2).to_time,
due_date: Date.new(2023, 1, 3).to_time,
vat_rate: 0.2,
) && @payable_invoice.reload

travel_to Date.new(2024, 1, 10).to_time

assert_equal @payable_invoice.created_at, Date.new(2023, 1, 1).to_time
assert_not @payable_invoice.paid?

@payable_invoice.mark_as_paid_at(Time.zone.now) && @payable_invoice.reload

assert_equal @payable_invoice.created_at, Date.new(2023, 1, 1).to_time
assert @payable_invoice.paid?
assert_equal @payable_invoice.paid_at, Time.zone.now
assert_equal @payable_invoice.vat_rate, 0.2
end

def invoices_total(invoices)
invoices.map(&:total)
.reduce(:+)
Expand Down
46 changes: 0 additions & 46 deletions test/models/payment_orders/payment_order_every_pay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,46 +51,6 @@ def teardown
travel_back
end

# def test_form_fields_are_filled_according_to_schema
# @orphaned_invoice.cents = Money.from_amount(1000.00, Setting.find_by(code: 'auction_currency').retrieve).cents
# assert_equal Money.from_amount(1200.00, Setting.find_by(code: 'auction_currency').retrieve), @orphaned_invoice.total

# expected_fields = {
# api_username: 'api_user',
# account_id: 'EUR3D1',
# timestamp: '1522542600',
# amount: '1200.00',
# transaction_type: 'charge',
# hmac_fields: 'account_id,amount,api_username,callback_url,customer_url,hmac_fields,locale,nonce,order_reference,timestamp,transaction_type',
# locale: 'en',
# }
# form_fields = @every_pay.form_fields
# expected_fields.each do |k, v|
# assert_equal(v, form_fields[k])
# end
# end

# def test_form_fields_with_estonian_locale
# @every_pay.user = @user
# @orphaned_invoice.cents = Money.from_amount(1234.56, Setting.find_by(code: 'auction_currency').retrieve).cents
# assert_equal Money.from_amount(1481.47, Setting.find_by(code: 'auction_currency').retrieve), @orphaned_invoice.total
# @user.update!(locale: :et)

# expected_fields = {
# api_username: 'api_user',
# account_id: 'EUR3D1',
# timestamp: '1522542600',
# amount: '1481.47',
# transaction_type: 'charge',
# hmac_fields: 'account_id,amount,api_username,callback_url,customer_url,hmac_fields,locale,nonce,order_reference,timestamp,transaction_type',
# locale: 'et',
# }
# form_fields = @every_pay.form_fields
# expected_fields.each do |k, v|
# assert_equal(v, form_fields[k])
# end
# end

def test_form_url_returns_the_constant
assert_equal(PaymentOrders::EveryPay::URL, @every_pay.form_url)
end
Expand All @@ -99,12 +59,6 @@ def test_channel
assert_equal('EveryPay', @every_pay.channel)
end

# def test_mark_invoice_as_paid_works_when_response_is_valid
# @every_pay.mark_invoice_as_paid

# assert(@every_pay.invoices.all?(&:paid?))
# end

def test_mark_invoice_as_paid_does_not_work_when_response_is_invalid
@fake_every_pay.mark_invoice_as_paid

Expand Down
Loading