-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
WIP MOSS refactoring #747
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
Closed
Closed
WIP MOSS refactoring #747
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
89a0168
Add POROs for creating tax adjustments
mamhoff f7edf46
Replace `TaxRate.adjust` with {Item,Order}Adjuster.adjust!
mamhoff b24c9cb
Add TaxRate.for_zone scope
mamhoff 49b8aa2
Allow passing nil into Spree::Zone.with_shared_members
mamhoff 132f02f
Use Spree::TaxRate.for_zone scope in Spree::TaxRate.match
mamhoff 7a1117e
Extract tax adjuster helpers into module
mamhoff d5e15d0
Move TaxRate.match logic to TaxHelpers
mamhoff c66ee85
Handle default VATs and tax rates for order separately
mamhoff a6b0e4e
Add `initial_price` column to line items
mamhoff 9cdf6db
Change line item prices depending on VAT in order tax zone
mamhoff 98bca71
Remove dead code from tax_rate.rb
mamhoff c6b67a1
Add TaxRate.applicable_for?(item)
mamhoff 4996525
Remove default tax zone validation
mamhoff 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 hidden or 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 hidden or 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 hidden or 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,51 @@ | ||
| module Spree | ||
| module Tax | ||
| class ItemAdjuster | ||
| attr_reader :item, :order | ||
|
|
||
| include TaxHelpers | ||
|
|
||
| def initialize(item, options = {}) | ||
| @item = item | ||
| @order = @item.order | ||
| # set caching instance variables so order-wide calculations are only done | ||
| # once per order | ||
| @rates_for_order_zone = options[:rates_for_order_zone] | ||
| @rates_for_default_zone = options[:rates_for_default_zone] | ||
| @order_tax_zone = options[:order_tax_zone] | ||
| @default_tax_zone = options[:default_tax_zone] | ||
| @outside_default_vat_zone = options[:outside_default_vat_zone] | ||
| end | ||
|
|
||
| def adjust! | ||
| return unless order_tax_zone | ||
| # Using .destroy_all to make sure callbacks fire | ||
| item.adjustments.tax.destroy_all | ||
|
|
||
| if outside_default_vat_zone? | ||
| adjust_item_price | ||
| end | ||
|
|
||
| TaxRate.store_pre_tax_amount(@item, rates_for_item) | ||
|
|
||
| rates_for_item.map { |rate| rate.adjust(order_tax_zone, item) } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def adjust_item_price | ||
| default_vat_amounts = default_rates_for_item.map(&:amount).sum | ||
| included_item_rate_amounts = rates_for_item.select(&:included_in_price).map(&:amount).sum | ||
| item.price = item.initial_price / (1 + default_vat_amounts) * (1 + included_item_rate_amounts) | ||
| end | ||
|
|
||
| def default_rates_for_item | ||
| rates_for_default_zone.included_in_price.select { |rate| rate.applicable_for?(item) } | ||
| end | ||
|
|
||
| def rates_for_item | ||
| @rates_for_item ||= rates_for_order_zone.select { |rate| rate.applicable_for?(item) } | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,33 @@ | ||
| module Spree | ||
| module Tax | ||
| class OrderAdjuster | ||
| attr_reader :order | ||
|
|
||
| include TaxHelpers | ||
|
|
||
| def initialize(order) | ||
| @order = order | ||
| end | ||
|
|
||
| def adjust! | ||
| return unless order_tax_zone | ||
|
|
||
| (order.line_items + order.shipments).each do |item| | ||
| ItemAdjuster.new(item, order_wide_options).adjust! | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def order_wide_options | ||
| { | ||
| rates_for_order_zone: rates_for_order_zone, | ||
| rates_for_default_zone: rates_for_default_zone, | ||
| order_tax_zone: order_tax_zone, | ||
| default_tax_zone: default_tax_zone, | ||
| outside_default_vat_zone: outside_default_vat_zone? | ||
| } | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,33 @@ | ||
| module Spree | ||
| module Tax | ||
| module TaxHelpers | ||
| private | ||
|
|
||
| def rates_for_order_zone | ||
| @rates_for_order_zone ||= Spree::TaxRate.for_zone(order_tax_zone) | ||
| end | ||
|
|
||
| def rates_for_default_zone | ||
| @rates_for_default_zone ||= Spree::TaxRate.for_zone(default_tax_zone) | ||
| end | ||
|
|
||
| def order_tax_zone | ||
| @order_tax_zone ||= order.tax_zone | ||
| end | ||
|
|
||
| def default_tax_zone | ||
| # Memoizing values that are potentially `false` can not use the `||=` shorthand | ||
| @default_tax_zone.nil? ? Spree::Zone.default_tax.presence : @default_tax_zone | ||
| end | ||
|
|
||
| def outside_default_vat_zone? | ||
| # Memoizing booleans can not use the `||=` shorthand | ||
| if @outside_default_vat_zone.nil? | ||
| @outside_default_vat_zone = default_tax_zone && !default_tax_zone.contains?(order_tax_zone) | ||
| else | ||
| @outside_default_vat_zone | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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
5 changes: 5 additions & 0 deletions
5
core/db/migrate/20160126123300_add_initial_price_to_line_items.rb
This file contains hidden or 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,5 @@ | ||
| class AddInitialPriceToLineItems < ActiveRecord::Migration | ||
| def change | ||
| add_column :spree_line_items, :initial_price, :decimal, precision: 8, scale: 2 | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
unnecessary : ) The tests below would fail anyhow.
The BigDecimal one is also superfluous imo