Skip to content

Commit

Permalink
Fix various method to remove or add enterprise fee tax
Browse files Browse the repository at this point in the history
  • Loading branch information
rioug committed Jan 22, 2024
1 parent 8181f3b commit fab6a79
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/models/invoice/data_presenter/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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:)
Expand Down
144 changes: 144 additions & 0 deletions spec/models/invoice/data_presenter/line_item_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fab6a79

Please sign in to comment.