Sha256: c468fb33ce25002cc685a4afc0272fdaf30afbcffc5bf23fdd11fc4be7d045ce

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

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
            if method(:determine_target_shipment).arity == 1
              quantity = total_parts - existing_parts
              shipment = determine_target_shipment(quantity) unless shipment
              add_to_shipment(shipment, quantity)
            else
              shipment = determine_target_shipment unless shipment
              add_to_shipment(shipment, total_parts - existing_parts)
            end
          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

4 entries across 4 versions & 1 rubygems

Version Path
solidus_product_assembly-1.4.0 app/models/spree/order_inventory_assembly.rb
solidus_product_assembly-1.3.0 app/models/spree/order_inventory_assembly.rb
solidus_product_assembly-1.2.0 app/models/spree/order_inventory_assembly.rb
solidus_product_assembly-1.1.0 app/models/spree/order_inventory_assembly.rb