Module: Models::InventoryCommittable

Extended by:
ActiveSupport::Concern
Included in:
LineItem, Order
Defined in:
app/concerns/models/inventory_committable.rb

Constant Summary collapse

STATES_WITH_NO_EXPIRATION =
%i[in_cr_hold crm_back_order pending_release_authorization processing_deliveries in_management_hold pre_pack request_carrier_assignment awaiting_carrier_assignment].freeze

Instance Method Summary collapse

Instance Method Details

#can_be_committed?(expires_on = nil) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/concerns/models/inventory_committable.rb', line 17

def can_be_committed?(expires_on = nil)
  if instance_of?(Order)
    %w[SO TO MO].include?(order_type) &&
      %w[cart awaiting_deliveries shipped invoiced cancelled fraudulent].exclude?(state)
  elsif instance_of?(Quote)
    state != 'cancelled' &&
      orders.empty?
  elsif instance_of?(LineItem)
    # if there are no commits or any of the commits have a different expiry date
    # and the resource (Order/Quote) can be committed
    inventory_commits.where(expires_on:).empty? && resource.can_be_committed?
  end
end

#can_be_uncommitted?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'app/concerns/models/inventory_committable.rb', line 31

def can_be_uncommitted?
  if instance_of?(Order) || instance_of?(Quote)
    InventoryCommit.where(line_item_id: line_items.pluck(:id)).any?
  elsif instance_of?(LineItem)
    inventory_commits.any?
  end
end

#commit_line_items(expires_on = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'app/concerns/models/inventory_committable.rb', line 39

def commit_line_items(expires_on = nil)
  return false unless can_be_committed?(expires_on)

  if instance_of?(Order) || instance_of?(Quote)
    Item::InventoryCommitter.crm_commit(line_items, expires_on:)
  elsif instance_of?(LineItem)
    commit_line_item(self, expires_on)
  end
end

#determine_commit_expiration_dateObject



11
12
13
14
15
# File 'app/concerns/models/inventory_committable.rb', line 11

def determine_commit_expiration_date
  return itemizable.commit_expiration_date if instance_of?(LineItem)

  STATES_WITH_NO_EXPIRATION.include?(state.to_sym) ? nil : 3.working.days.since(Date.current)
end

#has_committed_line_items?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/concerns/models/inventory_committable.rb', line 59

def has_committed_line_items?
  if instance_of?(Order) || instance_of?(Quote)
    InventoryCommit.joins(:line_item).merge(line_items).exists?
  elsif instance_of?(LineItem)
    # When inventory_commits is already preloaded (e.g. via includes in a collection
    # render), use the in-memory array to avoid a per-row EXISTS query.
    # The kit_line_item_id branch only applies to kit parent items which are rare,
    # so the occasional miss is acceptable in exchange for eliminating the N+1.
    if inventory_commits.loaded?
      inventory_commits.any?
    else
      InventoryCommit.where('line_item_id = :id or kit_line_item_id = :id', { id: }).any?
    end
  end
end

#uncommit_line_itemsObject



49
50
51
52
53
54
55
56
57
# File 'app/concerns/models/inventory_committable.rb', line 49

def uncommit_line_items
  return false unless can_be_uncommitted?

  if instance_of?(Order) || instance_of?(Quote)
    Item::InventoryCommitter.crm_uncommit(line_items)
  elsif instance_of?(LineItem)
    uncommit_line_item(self)
  end
end