From fab6a79dae4d25f55606f7ee91f7b8b6a7970dce Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 23 Jan 2024 10:26:22 +1100 Subject: [PATCH] Fix various method to remove or add enterprise fee tax --- .../invoice/data_presenter/line_item.rb | 12 +- .../invoice/data_presenter/line_item_spec.rb | 144 ++++++++++++++++++ 2 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 spec/models/invoice/data_presenter/line_item_spec.rb diff --git a/app/models/invoice/data_presenter/line_item.rb b/app/models/invoice/data_presenter/line_item.rb index a364bf2d611..a1177bf9908 100644 --- a/app/models/invoice/data_presenter/line_item.rb +++ b/app/models/invoice/data_presenter/line_item.rb @@ -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 LineItem < Invoice::DataPresenter::Base 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 @@ def display_amount_with_adjustments_and_with_taxes 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:) diff --git a/spec/models/invoice/data_presenter/line_item_spec.rb b/spec/models/invoice/data_presenter/line_item_spec.rb new file mode 100644 index 00000000000..14d3df8c9ff --- /dev/null +++ b/spec/models/invoice/data_presenter/line_item_spec.rb @@ -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