From c7eed430ecc0d2602573813280741832ffccf2c6 Mon Sep 17 00:00:00 2001 From: Lovro Colic Date: Tue, 28 Jan 2025 19:20:38 +0100 Subject: [PATCH] fix current usage taxes --- .../integrations/aggregator/taxes/invoices/payload.rb | 2 +- app/services/invoices/customer_usage_service.rb | 4 +++- spec/services/invoices/customer_usage_service_spec.rb | 10 ++++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/services/integrations/aggregator/taxes/invoices/payload.rb b/app/services/integrations/aggregator/taxes/invoices/payload.rb index 426316e30fe..3946bf91d01 100644 --- a/app/services/integrations/aggregator/taxes/invoices/payload.rb +++ b/app/services/integrations/aggregator/taxes/invoices/payload.rb @@ -47,7 +47,7 @@ def fee_item(fee) mapped_item ||= empty_struct { - 'item_id' => fee.id, + 'item_id' => fee.id || fee.item_id, 'item_code' => mapped_item.external_id, 'amount_cents' => fee.sub_total_excluding_taxes_amount_cents&.to_i } diff --git a/app/services/invoices/customer_usage_service.rb b/app/services/invoices/customer_usage_service.rb index d90026334f3..bcca53785da 100644 --- a/app/services/invoices/customer_usage_service.rb +++ b/app/services/invoices/customer_usage_service.rb @@ -165,7 +165,9 @@ def compute_amounts_with_provider_taxes result.fees_taxes = taxes_result.fees invoice.fees.each do |fee| - fee_taxes = result.fees_taxes.find { |item| item.item_id == fee.id } + fee_taxes = result.fees_taxes.find do |item| + (item.item_id == fee.item_id) && (item.amount_cents.to_i == fee.sub_total_excluding_taxes_amount_cents&.to_i) + end res = Fees::ApplyProviderTaxesService.call(fee:, fee_taxes:) res.raise_if_error! diff --git a/spec/services/invoices/customer_usage_service_spec.rb b/spec/services/invoices/customer_usage_service_spec.rb index 9dc10c3a2c7..1655fbf64c7 100644 --- a/spec/services/invoices/customer_usage_service_spec.rb +++ b/spec/services/invoices/customer_usage_service_spec.rb @@ -133,7 +133,14 @@ let(:endpoint) { 'https://api.nango.dev/v1/anrok/draft_invoices' } let(:body) do p = Rails.root.join('spec/fixtures/integration_aggregator/taxes/invoices/success_response.json') - File.read(p) + json = File.read(p) + + # setting item_id based on the test example + response = JSON.parse(json) + response['succeededInvoices'].first['fees'].last['item_id'] = charge.billable_metric.id + response['succeededInvoices'].first['fees'].last['amount_cents'] = 2532 + + response.to_json end let(:integration_collection_mapping) do create( @@ -151,7 +158,6 @@ allow(LagoHttpClient::Client).to receive(:new).with(endpoint).and_return(lago_client) allow(lago_client).to receive(:post_with_response).and_return(response) allow(response).to receive(:body).and_return(body) - allow_any_instance_of(Fee).to receive(:id).and_return('lago_fee_id') # rubocop:disable RSpec/AnyInstance end it 'initializes an invoice' do