Sha256: 8f02e5b6c6b32a7ee0c0e5b54e80d82b6e12b44ae769c83a244724baeb579249

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module SolidusPromotions
  class OrderAdjuster
    attr_reader :order, :promotions, :dry_run

    def initialize(order, dry_run_promotion: nil)
      @order = order
      @dry_run = !!dry_run_promotion
      @promotions = LoadPromotions.new(order: order, dry_run_promotion: dry_run_promotion).call
    end

    def call
      order.reset_current_discounts

      return order unless SolidusPromotions::Promotion.order_activatable?(order)

      discounted_order = DiscountOrder.new(order, promotions, dry_run: dry_run).call

      PersistDiscountedOrder.new(discounted_order).call unless dry_run

      order.reset_current_discounts

      unless dry_run
        # Since automations might have added a line item, we need to recalculate item total and item count here.
        order.item_total = order.line_items.sum(&:amount)
        order.item_count = order.line_items.sum(&:quantity)
        order.promo_total = (order.line_items + order.shipments).sum(&:promo_total)
      end
      order
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_promotions-4.5.1 app/models/solidus_promotions/order_adjuster.rb
solidus_promotions-4.5.0 app/models/solidus_promotions/order_adjuster.rb
solidus_promotions-4.4.2 app/models/solidus_promotions/order_adjuster.rb