Refator, add total_additional_tax and total_included_tax

They are used to add/remove enterprise fee tax as needed
This commit is contained in:
Gaetan Craig-Riou
2024-01-23 10:32:09 +11:00
parent 3cfa7e29d1
commit bfe8f08910
2 changed files with 59 additions and 13 deletions

View File

@@ -7,10 +7,17 @@ class EnterpriseFeeAdjustments
@adjustments = adjustments
end
# Calculate the tax portion of enterprise fee when tax excluded from price
def total_tax
def total_additional_tax
@adjustments.reduce(0.0) do |sum, enterprise_fee|
sum += enterprise_fee.adjustments.tax.additional.sum(:amount) if enterprise_fee&.adjustments
sum += enterprise_fee.additional_tax_total if enterprise_fee&.adjustments
sum
end
end
def total_included_tax
@adjustments.reduce(0.0) do |sum, enterprise_fee|
sum += enterprise_fee.included_tax_total if enterprise_fee&.adjustments
sum
end

View File

@@ -3,22 +3,27 @@
require 'spec_helper'
describe EnterpriseFeeAdjustments do
describe "#tax" do
let(:tax_rate) { create(:tax_rate, amount: 0.1) }
let(:line_item) { create(:line_item) }
let(:enterprise_fee) { create(:enterprise_fee, tax_category: tax_rate.tax_category) }
let(:fee_adjustment) {
create( :adjustment, originator: enterprise_fee, adjustable: line_item, state: "closed")
}
let(:tax_rate) { create(:tax_rate, amount: 0.1) }
let(:line_item) { create(:line_item) }
let(:enterprise_fee) { create(:enterprise_fee, tax_category: tax_rate.tax_category) }
let(:fee_adjustment) {
create( :adjustment, originator: enterprise_fee, adjustable: line_item, state: "closed")
}
describe "#total_additional_tax" do
it "calculates total tax" do
create(
:adjustment, originator: tax_rate, adjustable: fee_adjustment, amount: 10.0, state: "closed"
:adjustment,
originator: tax_rate,
adjustable: fee_adjustment,
amount: 10.0,
state: "closed",
included: false
)
enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment])
expect(enterprise_fee_adjustments.total_tax).to eq(10.0)
expect(enterprise_fee_adjustments.total_additional_tax).to eq(10.0)
end
context "with tax included in price" do
@@ -34,7 +39,41 @@ describe EnterpriseFeeAdjustments do
enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment])
expect(enterprise_fee_adjustments.total_tax).to eq(0.0)
expect(enterprise_fee_adjustments.total_additional_tax).to eq(0.0)
end
end
end
describe "total_included_tax" do
it "calculates total tax" do
create(
:adjustment,
originator: tax_rate,
adjustable: fee_adjustment,
amount: 10.0,
state: "closed",
included: true
)
enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment])
expect(enterprise_fee_adjustments.total_included_tax).to eq(10.0)
end
context "with tax excluded from price" do
it "returns 0.0" do
create(
:adjustment,
originator: tax_rate,
adjustable: fee_adjustment,
amount: 10.0,
state: "closed",
included: false
)
enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment])
expect(enterprise_fee_adjustments.total_included_tax).to eq(0.0)
end
end
end