Sha256: f0023dcca63257d9816f57eb9da01833c30f7e8edfb0bcf095f66bcad2a9ad45

Contents?: true

Size: 1.35 KB

Versions: 62

Compression:

Stored size: 1.35 KB

Contents

module Workarea
  module Pricing
    class OrderTotals
      def initialize(order, shippings = [])
        @order = order
        @shippings = shippings
      end

      def total
        set_item_totals
        set_subtotal
        set_shipping_totals
        set_tax_totals
        set_total
        set_total_value
      end

      def price_adjustments
        @price_adjustments ||= @order.price_adjustments +
                                @shippings.map(&:price_adjustments).reduce(:+)
      end

      private

      def set_item_totals
        @order.items.each do |item|
          item.total_value = item.price_adjustments.reject do |adjustment|
            adjustment.price.in?(%w(tax shipping))
          end.sum(&:amount)

          item.total_price = item.price_adjustments.adjusting('item').sum
        end
      end

      def set_subtotal
        @order.subtotal_price = price_adjustments.adjusting('item').sum
      end

      def set_shipping_totals
        @order.shipping_total = price_adjustments.adjusting('shipping').sum
      end

      def set_tax_totals
        @order.tax_total = price_adjustments.adjusting('tax').sum
      end

      def set_total
        @order.total_price = price_adjustments.sum
      end

      def set_total_value
        @order.total_value = @order.total_price - @order.shipping_total - @order.tax_total
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/pricing/order_totals.rb
workarea-core-3.5.26 app/models/workarea/pricing/order_totals.rb
workarea-core-3.4.45 app/models/workarea/pricing/order_totals.rb
workarea-core-3.5.25 app/models/workarea/pricing/order_totals.rb
workarea-core-3.5.23 app/models/workarea/pricing/order_totals.rb
workarea-core-3.4.44 app/models/workarea/pricing/order_totals.rb
workarea-core-3.5.22 app/models/workarea/pricing/order_totals.rb
workarea-core-3.4.43 app/models/workarea/pricing/order_totals.rb
workarea-core-3.5.21 app/models/workarea/pricing/order_totals.rb
workarea-core-3.4.42 app/models/workarea/pricing/order_totals.rb
workarea-core-3.5.20 app/models/workarea/pricing/order_totals.rb
workarea-core-3.4.41 app/models/workarea/pricing/order_totals.rb
workarea-core-3.5.19 app/models/workarea/pricing/order_totals.rb
workarea-core-3.4.40 app/models/workarea/pricing/order_totals.rb
workarea-core-3.5.18 app/models/workarea/pricing/order_totals.rb
workarea-core-3.4.39 app/models/workarea/pricing/order_totals.rb
workarea-core-3.5.17 app/models/workarea/pricing/order_totals.rb
workarea-core-3.4.38 app/models/workarea/pricing/order_totals.rb
workarea-core-3.5.16 app/models/workarea/pricing/order_totals.rb
workarea-core-3.4.37 app/models/workarea/pricing/order_totals.rb