-
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5613 from luisramos0/calculators
Move all Calculators from spree to OFN and out of the Spree namespace
- Loading branch information
Showing
49 changed files
with
387 additions
and
205 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: false | ||
|
||
require_dependency 'spree/calculator' | ||
require 'open_food_network/enterprise_fee_calculator' | ||
|
||
module Calculator | ||
class DefaultTax < Spree::Calculator | ||
def self.description | ||
Spree.t(:default_tax) | ||
end | ||
|
||
def compute(computable) | ||
case computable | ||
when Spree::Order | ||
compute_order(computable) | ||
when Spree::LineItem | ||
compute_line_item(computable) | ||
end | ||
end | ||
|
||
private | ||
|
||
def rate | ||
calculable | ||
end | ||
|
||
# Enable calculation of tax for enterprise fees with tax rates where included_in_price = false | ||
def compute_order(order) | ||
calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(order.distributor, | ||
order.order_cycle) | ||
|
||
[ | ||
line_items_total(order), | ||
per_item_fees_total(order, calculator), | ||
per_order_fees_total(order, calculator) | ||
].sum do |total| | ||
round_to_two_places(total * rate.amount) | ||
end | ||
end | ||
|
||
def line_items_total(order) | ||
matched_line_items = order.line_items.select do |line_item| | ||
line_item.product.tax_category == rate.tax_category | ||
end | ||
|
||
matched_line_items.sum(&:total) | ||
end | ||
|
||
# Finds relevant fees for each line_item, | ||
# calculates the tax on them, and returns the total tax | ||
def per_item_fees_total(order, calculator) | ||
order.line_items.sum do |line_item| | ||
calculator.per_item_enterprise_fee_applicators_for(line_item.variant) | ||
.select { |applicator| applicable_rate?(applicator, line_item) } | ||
.sum { |applicator| applicator.enterprise_fee.compute_amount(line_item) } | ||
end | ||
end | ||
|
||
def applicable_rate?(applicator, line_item) | ||
fee = applicator.enterprise_fee | ||
(!fee.inherits_tax_category && fee.tax_category == rate.tax_category) || | ||
(fee.inherits_tax_category && line_item.product.tax_category == rate.tax_category) | ||
end | ||
|
||
# Finds relevant fees for whole order, | ||
# calculates the tax on them, and returns the total tax | ||
def per_order_fees_total(order, calculator) | ||
calculator.per_order_enterprise_fee_applicators_for(order) | ||
.select { |applicator| applicator.enterprise_fee.tax_category == rate.tax_category } | ||
.sum { |applicator| applicator.enterprise_fee.compute_amount(order) } | ||
end | ||
|
||
def compute_line_item(line_item) | ||
if line_item.tax_category == rate.tax_category | ||
if rate.included_in_price | ||
deduced_total_by_rate(line_item.total, rate) | ||
else | ||
round_to_two_places(line_item.total * rate.amount) | ||
end | ||
else | ||
0 | ||
end | ||
end | ||
|
||
def round_to_two_places(amount) | ||
BigDecimal(amount.to_s).round(2, BigDecimal::ROUND_HALF_UP) | ||
end | ||
|
||
def deduced_total_by_rate(total, rate) | ||
round_to_two_places(total - ( total / (1 + rate.amount) ) ) | ||
end | ||
end | ||
end |
13 changes: 11 additions & 2 deletions
13
...ator/flat_percent_item_total_decorator.rb → ...els/calculator/flat_percent_item_total.rb
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,23 @@ | ||
# frozen_string_literal: false | ||
|
||
require_dependency 'spree/calculator' | ||
require 'spree/localized_number' | ||
|
||
module Calculator | ||
class FlatRate < Spree::Calculator | ||
extend Spree::LocalizedNumber | ||
|
||
preference :amount, :decimal, default: 0 | ||
preference :currency, :string, default: Spree::Config[:currency] | ||
|
||
localize_number :preferred_amount | ||
|
||
def self.description | ||
I18n.t(:flat_rate_per_order) | ||
end | ||
|
||
def compute(_object = nil) | ||
preferred_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: false | ||
|
||
require_dependency 'spree/calculator' | ||
require 'spree/localized_number' | ||
|
||
module Calculator | ||
class FlexiRate < Spree::Calculator | ||
extend Spree::LocalizedNumber | ||
|
||
preference :first_item, :decimal, default: 0.0 | ||
preference :additional_item, :decimal, default: 0.0 | ||
preference :max_items, :integer, default: 0 | ||
preference :currency, :string, default: Spree::Config[:currency] | ||
|
||
localize_number :preferred_first_item, | ||
:preferred_additional_item | ||
|
||
def self.description | ||
I18n.t(:flexible_rate) | ||
end | ||
|
||
def self.available?(_object) | ||
true | ||
end | ||
|
||
def compute(object) | ||
max = preferred_max_items.to_i | ||
items_count = line_items_for(object).map(&:quantity).sum | ||
|
||
# check max value to avoid divide by 0 errors | ||
return 0 if max.zero? | ||
|
||
if items_count > max | ||
compute_for(max - 1) | ||
elsif items_count <= max | ||
compute_for(items_count - 1) | ||
end | ||
end | ||
|
||
private | ||
|
||
def compute_for(count) | ||
count * preferred_additional_item.to_f + preferred_first_item.to_f | ||
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
14 changes: 12 additions & 2 deletions
14
.../spree/calculator/price_sack_decorator.rb → app/models/calculator/price_sack.rb
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.