diff --git a/app/controllers/payment_orders_controller.rb b/app/controllers/payment_orders_controller.rb index 9933cc9e6..234d58dea 100644 --- a/app/controllers/payment_orders_controller.rb +++ b/app/controllers/payment_orders_controller.rb @@ -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' } @@ -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 diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 9dc446b07..889c2ee7c 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -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? @@ -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 diff --git a/lib/tasks/assign_invoices_vat_rate.rake b/lib/tasks/assign_invoices_vat_rate.rake index 6a216d471..2d79a147d 100644 --- a/lib/tasks/assign_invoices_vat_rate.rake +++ b/lib/tasks/assign_invoices_vat_rate.rake @@ -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 diff --git a/test/models/invoice_test.rb b/test/models/invoice_test.rb index 09c279fec..87643f274 100644 --- a/test/models/invoice_test.rb +++ b/test/models/invoice_test.rb @@ -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(:+) diff --git a/test/models/payment_orders/payment_order_every_pay_test.rb b/test/models/payment_orders/payment_order_every_pay_test.rb index d71861595..bb5202e95 100644 --- a/test/models/payment_orders/payment_order_every_pay_test.rb +++ b/test/models/payment_orders/payment_order_every_pay_test.rb @@ -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 @@ -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