Sha256: 421af98149f7e7a0ae232adfacae9448ff0a92f8c268867a06e9415d039e6c5f

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module Spree
  # This class has basically the same functionality of Spree core OrderInventory
  # except that it takes account of bundle parts and properly creates and removes
  # inventory unit for each parts of a bundle
  class OrderInventoryAssembly < OrderInventory
    attr_reader :product

    def initialize(line_item)
      @order = line_item.order
      @line_item = line_item
      @product = line_item.product
    end

    def verify(shipment = nil)
      if order.completed? || shipment.present?
        line_item.quantity_by_variant.each do |part, total_parts|
          existing_parts = line_item.inventory_units.where(variant: part).count

          self.variant = part

          if existing_parts < total_parts
            shipment = determine_target_shipment unless shipment
            add_to_shipment(shipment, total_parts - existing_parts)
          elsif existing_parts > total_parts
            quantity = existing_parts - total_parts
            if shipment.present?
              remove_from_shipment(shipment, quantity)
            else
              order.shipments.each do |shipment|
                break if quantity == 0
                quantity -= remove_from_shipment(shipment, quantity)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_product_assembly-1.0.0 app/models/spree/order_inventory_assembly.rb