mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user