Class: Spree::PurchaseOrder

Inherits:
Object
  • Object
show all
Includes:
HasCustomFields, HasStatus, Metadata, NumberIdentifier, Receivable, SingleStoreResource
Defined in:
app/models/spree/purchase_order.rb

Overview

Goods bought from a supplier and expected at one of the merchant's own warehouses (docs/plans/6.0-inventory-operations.md).

Replaces the 5.x "external receive" — a StockTransfer with no source location — with a record that has the things a purchase actually has: a supplier, a unit cost, an expected date and a number to quote back.

Ordered units never count toward availability. Nothing here touches count_on_hand until Spree::PurchaseOrders::Receive runs, because the merchant only has the goods once they land.

Every status change is a workflow — Spree::PurchaseOrders::Receive, never purchase_order.receive! — so receiving can take the quantities the warehouse counted rather than assuming the order arrived intact.

Constant Summary

Constants included from Receivable

Receivable::CLOSED_STATUSES, Receivable::INCOMING_STATUSES, Receivable::OPEN_STATUSES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Metadata

#private_metadata, #private_metadata=

Methods included from Receivable

#closed?, #closed_short?, #fully_received?, #incoming?, #items_count, #open?, #over?, #quantity_expected_total, #quantity_received_total, #quantity_rejected_total, #short?, #status_after_receive

Class Method Details

.open_by_date(column, late) ⇒ ActiveRecord::Relation

Parameters:

  • column (Symbol) —

    the date the supplier promised against

  • late (Boolean, nil) —

    true for open orders past it, false for open orders not past it (or without one), nil for every order

Returns:

  • (ActiveRecord::Relation)


75
76
77
78
79
80
# File 'app/models/spree/purchase_order.rb', line 75

def self.open_by_date(column, late)
  return all if late.nil?

  date = arel_table[column]
  late ? open.where(date.lt(Date.current)) : open.where(date.gteq(Date.current).or(date.eq(nil)))
end

.ransackable_scopes_skip_sanitize_args ⇒ Object



67
68
69
# File 'app/models/spree/purchase_order.rb', line 67

def self.ransackable_scopes_skip_sanitize_args
  %i[overdue past_cancel_by]
end

Instance Method Details

#display_subtotal ⇒ Spree::Money

Returns:



91
92
93
# File 'app/models/spree/purchase_order.rb', line 91

def display_subtotal
  Spree::Money.new(subtotal, currency: currency)
end

#editable? ⇒ Boolean

Whether the merchant may still edit the lines. Once the order is placed with the supplier, what was ordered is a matter of record.

Returns:

  • (Boolean)


99
100
101
# File 'app/models/spree/purchase_order.rb', line 99

def editable?
  draft?
end

#event_serializer_class ⇒ Object



103
104
105
# File 'app/models/spree/purchase_order.rb', line 103

def event_serializer_class
  'Spree::Api::V3::PurchaseOrderEventSerializer'.safe_constantize
end

#subtotal ⇒ BigDecimal

What the supplier will invoice for everything ordered.

Returns:

  • (BigDecimal)


86
87
88
# File 'app/models/spree/purchase_order.rb', line 86

def subtotal
  items.sum(&:total_cost)
end