Class: Calculator::AdvancedFlatPercent
- Inherits:
-
Calculator
- Object
- Calculator
- Calculator::AdvancedFlatPercent
- Defined in:
- app/models/calculator/advanced_flat_percent.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.description ⇒ Object
5 6 7 |
# File 'app/models/calculator/advanced_flat_percent.rb', line 5 def self.description I18n.t("advanced_flat_percent") end |
.register ⇒ Object
9 10 11 12 |
# File 'app/models/calculator/advanced_flat_percent.rb', line 9 def self.register super UserGroup.register_calculator(self) end |
Instance Method Details
#compute(object) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/calculator/advanced_flat_percent.rb', line 14 def compute(object) return unless object.present? and object.line_items.present? and object.user.present? part = self.preferred_flat_percent.abs / 100.0 item_total = object.line_items.map(&:amount).sum if self.preferred_based_on_cost_price item_cost_price_total = object.line_items.map do |li| if !li.variant.cost_price.nil? && li.variant.cost_price > 0 li.variant.cost_price * li.quantity else li.amount end end.sum (item_cost_price_total * (1 + part) - item_total).round(2) else (item_total * (-part)).round(2) end end |
#compute_item(variant) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/calculator/advanced_flat_percent.rb', line 34 def compute_item(variant) part = self.preferred_flat_percent.abs / 100.0 if self.preferred_based_on_cost_price if !variant.cost_price.nil? && variant.cost_price > 0 variant.cost_price * (1 + part) else variant.price end else variant.price * (1 - part) end end |