Reorganize code in EnterpriseFeeApplicator

This commit is contained in:
luisramos0
2018-10-02 16:20:44 +01:00
parent f0183cd17e
commit 1ce431b671

View File

@@ -1,24 +1,27 @@
module OpenFoodNetwork
class EnterpriseFeeApplicator < Struct.new(:enterprise_fee, :variant, :role)
def create_line_item_adjustment(line_item)
a = enterprise_fee.create_adjustment(line_item_adjustment_label, line_item.order, line_item, true)
AdjustmentMetadata.create! adjustment: a, enterprise: enterprise_fee.enterprise, fee_name: enterprise_fee.name, fee_type: enterprise_fee.fee_type, enterprise_role: role
a.set_absolute_included_tax! adjustment_tax(line_item, a)
create_adjustment(line_item_adjustment_label, line_item.order, line_item)
end
def create_order_adjustment(order)
a = enterprise_fee.create_adjustment(order_adjustment_label, order, order, true)
AdjustmentMetadata.create! adjustment: a, enterprise: enterprise_fee.enterprise, fee_name: enterprise_fee.name, fee_type: enterprise_fee.fee_type, enterprise_role: role
a.set_absolute_included_tax! adjustment_tax(order, a)
create_adjustment(order_adjustment_label, order, order)
end
private
def create_adjustment(label, target, calculable)
adjustment = create_enterprise_fee_adjustment(label, target, calculable)
AdjustmentMetadata.create! adjustment: adjustment, enterprise: enterprise_fee.enterprise, fee_name: enterprise_fee.name, fee_type: enterprise_fee.fee_type, enterprise_role: role
adjustment.set_absolute_included_tax! adjustment_tax(adjustment)
end
def create_enterprise_fee_adjustment(label, target, calculable)
adjustment = enterprise_fee.create_adjustment(label, target, calculable, true)
end
def line_item_adjustment_label
"#{variant.product.name} - #{base_adjustment_label}"
end
@@ -31,7 +34,7 @@ module OpenFoodNetwork
I18n.t(:enterprise_fee_by, type: enterprise_fee.fee_type, role: role, enterprise_name: enterprise_fee.enterprise.name)
end
def adjustment_tax(adjustable, adjustment)
def adjustment_tax(adjustment)
tax_rates = adjustment.tax_rates
tax_rates.select(&:included_in_price).sum do |rate|