Class: SpreeCmCommissioner::PricingModels::Preview

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/pricing_models/preview.rb

Instance Method Summary collapse

Instance Method Details

#call(order_context:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/spree_cm_commissioner/pricing_models/preview.rb', line 6

def call(order_context:)
  pricing_adjustments = grouped_line_item_contexts(order_context).flat_map do |group_contexts|
    find_pricing_models(group_contexts).flat_map do |pricing_model|
      pricing_model.preview(
        order_context: order_context,
        line_item_contexts: group_contexts
      )
    end
  end

  pricing_adjustment_total = pricing_adjustments.sum(&:amount)
  item_total = order_context.item_total
  existing_pricing_adjustment_total = order_context.pricing_adjustment_total || 0
  base_total = order_context.total - existing_pricing_adjustment_total
  total = base_total + pricing_adjustment_total

  success(
    pricing_preview: PricingPreview.new(
      id: order_context.id,
      item_total: item_total,
      total: total,
      pricing_adjustment_total: pricing_adjustment_total,
      pricing_adjustments: pricing_adjustments
    )
  )
end