diff --git a/app/models/enterprise_fee_adjustments.rb b/app/models/enterprise_fee_adjustments.rb index a31c768d6d8..96798091914 100644 --- a/app/models/enterprise_fee_adjustments.rb +++ b/app/models/enterprise_fee_adjustments.rb @@ -8,18 +8,10 @@ def initialize(adjustments) end def total_additional_tax - @adjustments.reduce(0.0) do |sum, enterprise_fee| - sum += enterprise_fee.additional_tax_total if enterprise_fee&.adjustments - - sum - end + @adjustments.sum { |enterprise_fee| enterprise_fee.additional_tax_total.to_f } 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 + @adjustments.sum { |enterprise_fee| enterprise_fee.included_tax_total.to_f } end end diff --git a/spec/models/enterprise_fee_adjustments_spec.rb b/spec/models/enterprise_fee_adjustments_spec.rb index 3e2a5f6fb24..8e00118b110 100644 --- a/spec/models/enterprise_fee_adjustments_spec.rb +++ b/spec/models/enterprise_fee_adjustments_spec.rb @@ -5,10 +5,14 @@ describe EnterpriseFeeAdjustments do let(:tax_rate) { create(:tax_rate, amount: 0.1) } let(:line_item) { create(:line_item) } + let(:line_item2) { 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(:fee_adjustment2) { + create( :adjustment, originator: enterprise_fee, adjustable: line_item, state: "closed") + } describe "#total_additional_tax" do it "calculates total tax" do @@ -20,10 +24,26 @@ state: "closed", included: false ) + create( + :adjustment, + originator: tax_rate, + adjustable: fee_adjustment2, + amount: 5.0, + state: "closed", + included: false + ) + + enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment, fee_adjustment2]) - enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment]) + expect(enterprise_fee_adjustments.total_additional_tax).to eq(15.0) + end - expect(enterprise_fee_adjustments.total_additional_tax).to eq(10.0) + context "with no tax adjustment" do + it "returns 0.0" do + enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment]) + + expect(enterprise_fee_adjustments.total_additional_tax).to eq(0.0) + end end context "with tax included in price" do @@ -54,10 +74,26 @@ state: "closed", included: true ) + create( + :adjustment, + originator: tax_rate, + adjustable: fee_adjustment2, + amount: 5.0, + state: "closed", + included: true + ) - enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment]) + enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment, fee_adjustment2]) - expect(enterprise_fee_adjustments.total_included_tax).to eq(10.0) + expect(enterprise_fee_adjustments.total_included_tax).to eq(15.0) + end + + context "with no tax adjustment" do + it "returns 0.0" do + enterprise_fee_adjustments = EnterpriseFeeAdjustments.new([fee_adjustment]) + + expect(enterprise_fee_adjustments.total_additional_tax).to eq(0.0) + end end context "with tax excluded from price" do