Module: Spree::Receivable

Extended by:
ActiveSupport::Concern
Included in:
PurchaseOrder, StockTransfer
Defined in:
app/models/concerns/spree/receivable.rb

Overview

What a purchase order and a stock transfer have in common: lines that promise a quantity, deliveries that bring some of it, and a status that says how far along that is.

Constant Summary collapse

OPEN_STATUSES =
%w[draft ready_to_ship ordered in_transit partially_received].freeze
CLOSED_STATUSES =
%w[received over_received canceled].freeze
INCOMING_STATUSES =

The statuses in which a document's awaited units are on their way — and so count toward the destination's incoming_count. A draft or a packed box is not moving yet.

%w[ordered in_transit partially_received].freeze

Instance Method Summary collapse

Instance Method Details

#closed? ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/concerns/spree/receivable.rb', line 83

def closed?
  CLOSED_STATUSES.include?(status)
end

#closed_short? ⇒ Boolean

Closed by the merchant with units still owed, rather than by a delivery.

Returns:

  • (Boolean)


90
91
92
# File 'app/models/concerns/spree/receivable.rb', line 90

def closed_short?
  closed_short_at.present?
end

#fully_received? ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/concerns/spree/receivable.rb', line 56

def fully_received?
  items.any? && !short?
end

#incoming? ⇒ Boolean

Whether the units still awaited are on their way, and so counted as incoming at the destination.

Returns:

  • (Boolean)


79
80
81
# File 'app/models/concerns/spree/receivable.rb', line 79

def incoming?
  INCOMING_STATUSES.include?(status)
end

#items_count ⇒ Object



26
27
28
# File 'app/models/concerns/spree/receivable.rb', line 26

def items_count
  items.size
end

#open? ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/concerns/spree/receivable.rb', line 71

def open?
  OPEN_STATUSES.include?(status)
end

#over? ⇒ Boolean

Whether any line took in more than it expected.

Returns:

  • (Boolean)


52
53
54
# File 'app/models/concerns/spree/receivable.rb', line 52

def over?
  items.any?(&:over_received?)
end

#quantity_expected_total ⇒ Object



30
31
32
# File 'app/models/concerns/spree/receivable.rb', line 30

def quantity_expected_total
  items.sum(&:quantity_expected)
end

#quantity_received_total ⇒ Object



34
35
36
# File 'app/models/concerns/spree/receivable.rb', line 34

def quantity_received_total
  items.sum { |item| item.quantity_received.to_i }
end

#quantity_rejected_total ⇒ Object



38
39
40
# File 'app/models/concerns/spree/receivable.rb', line 38

def quantity_rejected_total
  items.sum { |item| item.quantity_rejected.to_i }
end

#short? ⇒ Boolean

Whether any line is still owed units.

Returns:

  • (Boolean)


45
46
47
# File 'app/models/concerns/spree/receivable.rb', line 45

def short?
  items.any?(&:under_received?)
end

#status_after_receive ⇒ String

Where a delivery leaves the document: over-receipt outranks a shortfall elsewhere, because the extra units are already on the shelf and nothing further can be expected once the supplier has sent more than was asked.

Returns:

  • (String)


65
66
67
68
69
# File 'app/models/concerns/spree/receivable.rb', line 65

def status_after_receive
  return 'over_received' if over?

  fully_received? ? 'received' : 'partially_received'
end