app/models/spree/calculator/copayment_discount_item.rb
# object is a Spree::LineItem
object is a Spree::LineItem
object is a Spree :: LineItem
def compute(object = nil)
      if eligible?(object)
        @line_item = object
        @order     = @line_item.order
        relatable_lines  = relatables_from_order
        relatables       = Spree::Variant.where(id: relatable_lines.pluck(:variant_id))
        relatables_quant = relatable_lines.sum(:quantity)
        all_relations      = get_order_relations(relatables)
        current_relations = all_relations.where(related_to: @line_item.variant)
        total_discount   = 0
        all_relations.each do |relation|
          break total_discount if relatables_quant <= 0
          copayments_quant = copayments_quantity(relation)
          relatable_quant  = relatable_lines.where(variant_id: relation.relatable_id).sum(:quantity)
          discount = relation.discount_amount
          factor = get_factor(relation, relatable_quant, copayments_quant)
          final_factor = relatables_quant < factor && relatables_quant < copayments_quant ? relatables_quant : factor
          relatables_quant -= final_factor
          total_discount += (discount * final_factor) if relation.related_to.id == @line_item.variant.id
        end
        total_discount
      else
        0
      end
    end
def compute ( object = nil ) if eligible? ( object ) @line_item = object @order = @line_item . order relatable_lines = relatables_from_order relatables = Spree :: Variant . where ( id : relatable_lines . pluck ( :variant_id ) ) relatables_quant = relatable_lines . sum ( :quantity ) all_relations = get_order_relations ( relatables ) current_relations = all_relations . where ( related_to : @line_item . variant ) total_discount = 0 all_relations . each do | relation | break total_discount if relatables_quant <= 0 copayments_quant = copayments_quantity ( relation ) relatable_quant = relatable_lines . where ( variant_id : relation . relatable_id ) . sum ( :quantity ) discount = relation . discount_amount factor = get_factor ( relation , relatable_quant , copayments_quant ) final_factor = relatables_quant < factor && relatables_quant < copayments_quant ? relatables_quant : factor relatables_quant -= final_factor total_discount += ( discount * final_factor ) if relation . related_to . id == @line_item . variant . id end total_discount else 0 end end
object is a Spree::LineItem
object is a Spree :: LineItem
object
<not_specific>