-
-
Notifications
You must be signed in to change notification settings - Fork 731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Include adjustments with negative taxes on Sales Tax reports #5492
Merged
luisramos0
merged 6 commits into
openfoodfoundation:master
from
pacodelaluna:include-negative-tax-adjustments-in-sales-report
Jun 16, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c729f64
Include adjustments without positive taxes
9abe41f
Use OrderTaxAdjustmentsFetcher service
b4952dc
Cosmetics
00c9dd7
Remove N+1 fetching order and li adjustments
sauloperez ff4d7fb
Refactor with Arel to perform a single query
sauloperez 6672cfd
Readd price_adustments after bad rebase
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
# This class will be used to get Tax Adjustments related to an order, | ||
# and proceed basic calcultation over them. | ||
|
||
class OrderTaxAdjustmentsFetcher | ||
def initialize(order) | ||
@order = order | ||
end | ||
|
||
def totals | ||
all.each_with_object({}) do |adjustment, hash| | ||
tax_rates_hash = tax_rates_hash(adjustment) | ||
hash.update(tax_rates_hash) { |_tax_rate, amount1, amount2| amount1 + amount2 } | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :order | ||
|
||
def all | ||
Spree::Adjustment | ||
.with_tax | ||
.where(order_adjustments.or(line_item_adjustments)) | ||
.order('created_at ASC') | ||
end | ||
|
||
def order_adjustments | ||
table[:adjustable_id].eq(order.id) | ||
.and(table[:adjustable_type].eq('Spree::Order')) | ||
end | ||
|
||
def line_item_adjustments | ||
table[:adjustable_id].eq(order.line_item_ids.join(',')) | ||
.and(table[:adjustable_type].eq('Spree::LineItem')) | ||
end | ||
|
||
def table | ||
@table ||= Spree::Adjustment.arel_table | ||
end | ||
|
||
def tax_rates_hash(adjustment) | ||
tax_rates = TaxRateFinder.tax_rates_of(adjustment) | ||
|
||
Hash[tax_rates.collect do |tax_rate| | ||
tax_amount = if tax_rates.one? | ||
adjustment.included_tax | ||
else | ||
tax_rate.compute_tax(adjustment.amount) | ||
end | ||
[tax_rate, tax_amount] | ||
end] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require "spec_helper" | ||
|
||
describe OrderTaxAdjustmentsFetcher do | ||
describe "#totals" do | ||
let(:zone) { create(:zone_with_member) } | ||
let(:coordinator) { create(:distributor_enterprise, charges_sales_tax: true) } | ||
|
||
let(:tax_rate10) { create(:tax_rate, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, amount: 0.1, zone: zone) } | ||
let(:tax_rate15) { create(:tax_rate, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, amount: 0.15, zone: zone) } | ||
let(:tax_rate20) { create(:tax_rate, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, amount: 0.2, zone: zone) } | ||
let(:tax_rate25) { create(:tax_rate, included_in_price: true, calculator: Spree::Calculator::DefaultTax.new, amount: 0.25, zone: zone) } | ||
let(:tax_category10) { create(:tax_category, tax_rates: [tax_rate10]) } | ||
let(:tax_category15) { create(:tax_category, tax_rates: [tax_rate15]) } | ||
let(:tax_category20) { create(:tax_category, tax_rates: [tax_rate20]) } | ||
let(:tax_category25) { create(:tax_category, tax_rates: [tax_rate25]) } | ||
|
||
let(:variant) { create(:variant, product: create(:product, tax_category: tax_category10)) } | ||
let(:enterprise_fee) { create(:enterprise_fee, enterprise: coordinator, tax_category: tax_category20, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 48.0)) } | ||
let(:additional_adjustment) { create(:adjustment, amount: 50.0, included_tax: tax_rate25.compute_tax(50.0)) } | ||
|
||
let(:order_cycle) { create(:simple_order_cycle, coordinator: coordinator, coordinator_fees: [enterprise_fee], distributors: [coordinator], variants: [variant]) } | ||
let(:line_item) { create(:line_item, variant: variant, price: 44.0) } | ||
let(:order) do | ||
create( | ||
:order, | ||
line_items: [line_item], | ||
bill_address: create(:address), | ||
order_cycle: order_cycle, | ||
distributor: coordinator, | ||
adjustments: [additional_adjustment] | ||
) | ||
end | ||
|
||
before do | ||
allow(Spree::Config).to receive(:shipment_inc_vat).and_return(true) | ||
allow(Spree::Config).to receive(:shipping_tax_rate).and_return(tax_rate15.amount) | ||
end | ||
|
||
let(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 46.0)) } | ||
let!(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method, order: order) } | ||
|
||
before do | ||
order.create_tax_charge! | ||
order.update_distribution_charge! | ||
end | ||
|
||
subject { OrderTaxAdjustmentsFetcher.new(order).totals } | ||
|
||
it "returns a hash with all 3 taxes" do | ||
expect(subject.size).to eq(4) | ||
end | ||
|
||
it "contains tax on line_item" do | ||
expect(subject[tax_rate10]).to eq(4.0) | ||
end | ||
|
||
it "contains tax on shipping_fee" do | ||
expect(subject[tax_rate15]).to eq(6.0) | ||
end | ||
|
||
it "contains tax on enterprise_fee" do | ||
expect(subject[tax_rate20]).to eq(8.0) | ||
end | ||
|
||
it "contains tax on order adjustment" do | ||
expect(subject[tax_rate25]).to eq(10.0) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm being picky here but I'd lean towards
#merge
instead of#update
for readability purposes. They are synonyms, right? I didn't even know#update
existed and I had to look it up to tell whether it belonged to AR or Ruby. Feel free to dismiss though.