Skip to content

Commit

Permalink
update calculation of item amount reported to anrok to apply coupons
Browse files Browse the repository at this point in the history
  • Loading branch information
annvelents committed Sep 24, 2024
1 parent 4c79f92 commit bce2887
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,14 @@ def cn_item(item)
{
'item_id' => fee.item_id,
'item_code' => mapped_item.external_id,
'amount_cents' => calculate_amount_with_coupons(item, fee)(item.amount_cents&.to_i || 0) * -1
'amount_cents' => item.sub_total_excluding_taxes_amount_cents * -1
}
end

private

attr_reader :customer, :integration_customer, :credit_note

def calculate_amount_with_coupons(item, fee)
item_proportion_to_fee = fee.amount_cents / item.amount_cents.to_f
item_proportion_to_fee * fee.sub_total_excluding_taxes_amount_cents
end
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/models/credit_note_item_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
# 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

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
invoice:,
add_on:,
created_at: current_time - 3.seconds,
amount_cents: 200
amount_cents: 200,
precise_amount_cents: 200
)
end
let(:fee_add_on_two) do
Expand All @@ -54,7 +55,9 @@
invoice:,
add_on: add_on_two,
created_at: current_time - 2.seconds,
amount_cents: 200
amount_cents: 200,
precise_amount_cents: 200,
precise_coupons_amount_cents: 20
)
end
let(:credit_note) do
Expand Down Expand Up @@ -99,7 +102,7 @@
{
'item_id' => fee_add_on_two.item_id,
'item_code' => '1',
'amount_cents' => -180
'amount_cents' => -162
}
]
}
Expand Down

0 comments on commit bce2887

Please sign in to comment.