Fix various method to remove or add enterprise fee tax

This commit is contained in:
Gaetan Craig-Riou
2024-01-23 10:26:22 +11:00
parent 8181f3b09a
commit fab6a79dae
2 changed files with 152 additions and 4 deletions

View File

@@ -4,7 +4,8 @@ class Invoice
class DataPresenter
class LineItem < Invoice::DataPresenter::Base
attributes :added_tax, :currency, :included_tax, :price_with_adjustments, :quantity,
:variant_id, :unit_price_price_and_unit, :unit_presentation, :enterprise_fee_tax
:variant_id, :unit_price_price_and_unit, :unit_presentation,
:enterprise_fee_additional_tax, :enterprise_fee_included_tax
attributes_with_presenter :variant
array_attribute :tax_rates, class_name: 'TaxRate'
invoice_generation_attributes :added_tax, :included_tax, :price_with_adjustments,
@@ -13,11 +14,12 @@ class Invoice
delegate :name_to_display, :options_text, to: :variant
def amount_with_adjustments_without_taxes
(price_with_adjustments * quantity) - included_tax
fee_tax = enterprise_fee_included_tax || 0.0
(price_with_adjustments * quantity) - included_tax - fee_tax
end
def amount_with_adjustments_and_with_taxes
fee_tax = enterprise_fee_tax || 0.00
fee_tax = enterprise_fee_additional_tax || 0.0
( price_with_adjustments * quantity) + added_tax + fee_tax
end
@@ -30,9 +32,11 @@ class Invoice
end
def single_display_amount_with_adjustments
Spree::Money.new(price_with_adjustments - (included_tax / quantity), currency:)
fee_tax = enterprise_fee_included_tax || 0.0
Spree::Money.new(price_with_adjustments - ((included_tax + fee_tax) / quantity), currency:)
end
# TODO seems useless
def display_line_items_taxes(display_zero: true)
if included_tax.positive?
Spree::Money.new( included_tax, currency:)

View File

@@ -0,0 +1,144 @@
# frozen_string_literal: true
require 'spec_helper'
describe Invoice::DataPresenter::LineItem do
subject(:presenter) { described_class.new(data) }
describe "#amount_with_adjustments_without_taxes" do
let(:data) do
{
price_with_adjustments: 10.0,
quantity: 2,
included_tax: 0.0,
enterprise_fee_included_tax: nil
}
end
it "calculated line item price" do
expect(presenter.amount_with_adjustments_without_taxes).to eq(20.00)
end
context "with tax included in price" do
let(:data) do
{
price_with_adjustments: 10.0,
quantity: 2,
included_tax: 1.0,
enterprise_fee_included_tax: nil
}
end
it "removes the included tax" do
expect(presenter.amount_with_adjustments_without_taxes).to eq(19)
end
context "with enterprise fee" do
let(:data) do
{
price_with_adjustments: 10.0,
quantity: 2,
included_tax: 0.0,
enterprise_fee_included_tax: 0.5
}
end
it "removes the enterpise fee tax" do
expect(presenter.amount_with_adjustments_without_taxes).to eq(19.5)
end
end
end
end
describe "#amount_with_adjustments_and_with_taxes" do
let(:data) do
{
price_with_adjustments: 10.0,
quantity: 2,
added_tax: 0.0,
enterprise_fee_additional_tax: nil
}
end
it "cacluated the line item price with tax" do
expect(presenter.amount_with_adjustments_and_with_taxes).to eq(20.00)
end
context "with tax excluded from price" do
let(:data) do
{
price_with_adjustments: 10.0,
quantity: 2,
added_tax: 1.0,
enterprise_fee_additional_tax: nil
}
end
it "includes the added tax" do
expect(presenter.amount_with_adjustments_and_with_taxes).to eq(21.00)
end
context "with enterprise fee" do
let(:data) do
{
price_with_adjustments: 10.0,
quantity: 2,
added_tax: 0.0,
enterprise_fee_additional_tax: 0.5
}
end
it "adds the enterpise fee tax" do
expect(presenter.amount_with_adjustments_and_with_taxes).to eq(20.50)
end
end
end
end
# TODO
describe "#single_display_amount_with_adjustments" do
let(:data) do
{
price_with_adjustments: 10.0,
quantity: 2,
included_tax: 0.0,
enterprise_fee_included_tax: nil,
currency: "AUD"
}
end
it "displays single price with adjustments" do
expect(presenter.single_display_amount_with_adjustments).to eq(Spree::Money.new(10.0))
end
context "with included tax" do
let(:data) do
{
price_with_adjustments: 10.0,
quantity: 2,
included_tax: 1.0,
enterprise_fee_included_tax: nil
}
end
it "excludes the included tax" do
expect(presenter.single_display_amount_with_adjustments).to eq(Spree::Money.new(9.5))
end
context "with enterpise fee" do
let(:data) do
{
price_with_adjustments: 10.0,
quantity: 2,
included_tax: 1.0,
enterprise_fee_included_tax: 0.5
}
end
it "includes fee but remove tax portion of the fee" do
expect(presenter.single_display_amount_with_adjustments).to eq(Spree::Money.new(9.25))
end
end
end
end
end