Skip to content

Commit

Permalink
Feat(anrok): call report credit note job when creating a credit note (#…
Browse files Browse the repository at this point in the history
…2617)

When creating a credit note we should schedule a job to report credit
note creation to tax provider is customer has tax_provider set up;

When reporting credit note, the reported amount should be taken from the
items amounts
  • Loading branch information
annvelents authored Sep 24, 2024
1 parent 1d7ec67 commit b4cd672
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 7 deletions.
10 changes: 10 additions & 0 deletions app/models/credit_note_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class CreditNoteItem < ApplicationRecord
def applied_taxes
credit_note.applied_taxes.where(tax_code: fee.applied_taxes.select('fees_taxes.tax_code'))
end

# This method returns item amount with coupons applied
# coupons are applied proportionally to the way they're applied on corresponding fee
# so knowing the item total proportion to fee total we can calculate item amount with coupons
def sub_total_excluding_taxes_amount_cents
return 0 if amount_cents.zero? || fee.amount_cents.zero?

item_proportion_to_fee = amount_cents.to_f / fee.amount_cents
item_proportion_to_fee * fee.sub_total_excluding_taxes_amount_cents
end
end

# == Schema Information
Expand Down
5 changes: 5 additions & 0 deletions app/services/credit_notes/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def call
deliver_webhook
deliver_email
handle_refund if should_handle_refund?
report_to_tax_provider

if credit_note.should_sync_credit_note?
Integrations::Aggregator::CreditNotes::CreateJob.perform_later(credit_note:)
Expand Down Expand Up @@ -190,6 +191,10 @@ def handle_refund
end
end

def report_to_tax_provider
CreditNotes::ProviderTaxes::ReportJob.perform_later(credit_note:)
end

def compute_amounts_and_taxes
taxes_result = CreditNotes::ApplyTaxesService.call(
invoice:,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def cn_item(item)
{
'item_id' => fee.item_id,
'item_code' => mapped_item.external_id,
'amount_cents' => (fee.sub_total_excluding_taxes_amount_cents&.to_i || 0) * -1
'amount_cents' => item.sub_total_excluding_taxes_amount_cents.to_i * -1
}
end

Expand Down
6 changes: 6 additions & 0 deletions spec/factories/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,11 @@
shipping_state { state }
shipping_country { country }
end

trait :with_tax_integration do
after :create do |customer|
create(:anrok_customer, customer:)
end
end
end
end
26 changes: 26 additions & 0 deletions spec/models/credit_note_item_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe CreditNoteItem, type: :model do
subject(:credit_note_item) { create(:credit_note_item) }

it { is_expected.to belong_to(:credit_note) }
it { is_expected.to belong_to(:fee) }

describe '#sub_total_excluding_taxes_amount_cents' do
let(:credit_note_item) { build(:credit_note_item, amount_cents: 100, fee: fee) }
let(:fee) { build(:fee, amount_cents: 1000, precise_amount_cents: 1000, precise_coupons_amount_cents: 0) }

context 'when there are no coupons applied' do
it 'returns item amount with coupons applied' do
expect(credit_note_item.sub_total_excluding_taxes_amount_cents).to eq(100)
end
end

context 'when there are coupons applied' do
let(:fee) { build(:fee, amount_cents: 1000, precise_amount_cents: 1000, precise_coupons_amount_cents: 20) }

it 'returns item amount with coupons applied' do
expect(credit_note_item.sub_total_excluding_taxes_amount_cents).to eq(98)
end
end
end
end
10 changes: 10 additions & 0 deletions spec/services/credit_notes/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@
let(:service_call) { create_service.call }
end

context 'when customer has tax_provider set up' do
let(:customer) { create(:customer, :with_tax_integration, organization:) }

it 'sync with tax provider' do
expect do
create_service.call
end.to have_enqueued_job(CreditNotes::ProviderTaxes::ReportJob)
end
end

context 'when organization does not have right email settings' do
before { invoice.organization.update!(email_settings: []) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,20 @@
:fee,
invoice:,
add_on:,
created_at: current_time - 3.seconds
created_at: current_time - 3.seconds,
amount_cents: 200,
precise_amount_cents: 200
)
end
let(:fee_add_on_two) do
create(
:fee,
invoice:,
add_on: add_on_two,
created_at: current_time - 2.seconds
created_at: current_time - 2.seconds,
amount_cents: 200,
precise_amount_cents: 200,
precise_coupons_amount_cents: 20
)
end
let(:credit_note) do
Expand All @@ -66,10 +71,10 @@
end

let(:credit_note_item1) do
create(:credit_note_item, credit_note:, fee: fee_add_on, amount_cents: fee_add_on.amount_cents)
create(:credit_note_item, credit_note:, fee: fee_add_on, amount_cents: 190)
end
let(:credit_note_item2) do
create(:credit_note_item, credit_note:, fee: fee_add_on_two, amount_cents: fee_add_on_two.amount_cents)
create(:credit_note_item, credit_note:, fee: fee_add_on_two, amount_cents: 180)
end

let(:body) do
Expand All @@ -92,12 +97,12 @@
{
'item_id' => fee_add_on.item_id,
'item_code' => 'm1',
'amount_cents' => -200
'amount_cents' => -190
},
{
'item_id' => fee_add_on_two.item_id,
'item_code' => '1',
'amount_cents' => -200
'amount_cents' => -162
}
]
}
Expand Down

0 comments on commit b4cd672

Please sign in to comment.