Class: PurchaseOrderItem

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/purchase_order_item.rb

Overview

== Schema Information

Table name: purchase_order_items
Database name: primary

id :integer not null, primary key
auto_receive :boolean
currency :string(255)
description :string(255)
landed_cost_state :string(255)
position :integer
quantity :integer
sku :string(255)
state :string(255)
stock_item :boolean default(TRUE), not null
supplier_description :string(255)
supplier_sku :string(255)
total_cost :decimal(8, 2)
total_weight :float
unit_cost :decimal(8, 2)
unit_quantity :integer
unit_weight :float
uom :string(255)
created_at :datetime
updated_at :datetime
creator_id :integer
item_id :integer
line_item_id :integer
purchase_order_id :integer
rma_item_id :integer
updater_id :integer

Indexes

by_poid_ar (purchase_order_id,auto_receive)
idx_item_id_purchase_order_id (item_id,purchase_order_id)
idx_po_id_state (purchase_order_id,state)
index_purchase_order_items_on_line_item_id (line_item_id)
purchase_order_items_purchase_order_id_position_idx (purchase_order_id,position)
purchase_order_items_state_idx (state)

Foreign Keys

fk_rails_... (item_id => items.id)
purchase_order_items_purchase_order_id_fk (purchase_order_id => purchase_orders.id) ON DELETE => cascade

Constant Summary collapse

WITH_RECEIPTED_ITEMS_SQ_SQL =
"COALESCE((select sum(sri.quantity) from shipment_receipt_items sri where sri.purchase_order_item_id = purchase_order_items.id and sri.state = 'receipted'),0) as shipment_receipt_items_received_count"

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#currencyObject (readonly)



62
63
# File 'app/models/purchase_order_item.rb', line 62

validates :description, :quantity, :unit_weight, :total_weight, :unit_cost,
:total_cost, :currency, presence: true

#descriptionObject (readonly)



62
63
# File 'app/models/purchase_order_item.rb', line 62

validates :description, :quantity, :unit_weight, :total_weight, :unit_cost,
:total_cost, :currency, presence: true

#item_idObject (readonly)



65
# File 'app/models/purchase_order_item.rb', line 65

validates :item_id, presence: { if: :stock_item? }

#quantityObject (readonly)



62
63
# File 'app/models/purchase_order_item.rb', line 62

validates :description, :quantity, :unit_weight, :total_weight, :unit_cost,
:total_cost, :currency, presence: true

#skuObject (readonly)



64
# File 'app/models/purchase_order_item.rb', line 64

validates :sku, presence: { if: :stock_item? }

#total_costObject (readonly)



62
63
# File 'app/models/purchase_order_item.rb', line 62

validates :description, :quantity, :unit_weight, :total_weight, :unit_cost,
:total_cost, :currency, presence: true

#total_weightObject (readonly)



62
63
# File 'app/models/purchase_order_item.rb', line 62

validates :description, :quantity, :unit_weight, :total_weight, :unit_cost,
:total_cost, :currency, presence: true

#unit_costObject (readonly)



62
63
# File 'app/models/purchase_order_item.rb', line 62

validates :description, :quantity, :unit_weight, :total_weight, :unit_cost,
:total_cost, :currency, presence: true

#unit_quantityObject (readonly)



66
# File 'app/models/purchase_order_item.rb', line 66

validates :quantity, :unit_quantity, numericality: { greater_than: 0 }

#unit_weightObject (readonly)



62
63
# File 'app/models/purchase_order_item.rb', line 62

validates :description, :quantity, :unit_weight, :total_weight, :unit_cost,
:total_cost, :currency, presence: true

Class Method Details

.auto_receiveActiveRecord::Relation<PurchaseOrderItem>

A relation of PurchaseOrderItems that are auto receive. Active Record Scope

Returns:

See Also:



79
# File 'app/models/purchase_order_item.rb', line 79

scope :auto_receive, -> { where(auto_receive: true) }

.is_stock_itemActiveRecord::Relation<PurchaseOrderItem>

A relation of PurchaseOrderItems that are is stock item. Active Record Scope

Returns:

See Also:



78
# File 'app/models/purchase_order_item.rb', line 78

scope :is_stock_item, -> { where(stock_item: true) }

.order_by_skuActiveRecord::Relation<PurchaseOrderItem>

A relation of PurchaseOrderItems that are order by sku. Active Record Scope

Returns:

See Also:



80
# File 'app/models/purchase_order_item.rb', line 80

scope :order_by_sku, -> { order('sku ASC') }

.removableActiveRecord::Relation<PurchaseOrderItem>

A relation of PurchaseOrderItems that are removable. Active Record Scope

Returns:

See Also:



81
# File 'app/models/purchase_order_item.rb', line 81

scope :removable, -> { where(state: %w[not_receipted cancelled]) }

.with_receipted_itemsActiveRecord::Relation<PurchaseOrderItem>

A relation of PurchaseOrderItems that are with receipted items. Active Record Scope

Returns:

See Also:



82
83
84
# File 'app/models/purchase_order_item.rb', line 82

scope :with_receipted_items, -> {
  select_append(WITH_RECEIPTED_ITEMS_SQ_SQL)
}

Instance Method Details

#all_items_receipted?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'app/models/purchase_order_item.rb', line 161

def all_items_receipted?
  shipment_receipt_items.receipted.sum(:quantity) == unit_quantity
end

#all_landed_costs_applied?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'app/models/purchase_order_item.rb', line 169

def all_landed_costs_applied?
  landed_costs.reload.actual.sum(:quantity) == unit_quantity
end

#can_be_edited?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'app/models/purchase_order_item.rb', line 157

def can_be_edited?
  %w[not_receipted partially_receipted].include?(state) and shipment_items.empty?
end

#can_enter_landed_cost?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'app/models/purchase_order_item.rb', line 189

def can_enter_landed_cost?
  stock_item? and (not_applied? or partially_applied?)
end

#can_enter_receipt?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'app/models/purchase_order_item.rb', line 185

def can_enter_receipt?
  stock_item? and (not_receipted? or partially_receipted?)
end

#can_enter_shipment?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'app/models/purchase_order_item.rb', line 193

def can_enter_shipment?
  stock_item? and shipment_items.sum(:quantity) < quantity
end

#currency_symbolObject



181
182
183
# File 'app/models/purchase_order_item.rb', line 181

def currency_symbol
  Money::Currency.new(currency).symbol
end

#itemItem

Returns:

See Also:



54
# File 'app/models/purchase_order_item.rb', line 54

belongs_to :item, optional: true

#landed_costsActiveRecord::Relation<LandedCost>

Returns:

See Also:



58
# File 'app/models/purchase_order_item.rb', line 58

has_many :landed_costs

#line_itemLineItem

Returns:

See Also:



55
# File 'app/models/purchase_order_item.rb', line 55

belongs_to :line_item, optional: true

#no_landed_costs_applied?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'app/models/purchase_order_item.rb', line 177

def no_landed_costs_applied?
  landed_costs.actual.empty?
end

#promised_delivery_dateObject



153
154
155
# File 'app/models/purchase_order_item.rb', line 153

def promised_delivery_date
  purchase_order_shipments.open_shipments.map(&:promised_delivery_date).compact.min
end

#purchase_orderPurchaseOrder



53
# File 'app/models/purchase_order_item.rb', line 53

belongs_to :purchase_order, inverse_of: :purchase_order_items, optional: true

#purchase_order_shipmentsActiveRecord::Relation<PurchaseOrderShipment>

Returns:

See Also:



60
# File 'app/models/purchase_order_item.rb', line 60

has_many :purchase_order_shipments, through: :shipment_items

#shipment_itemsActiveRecord::Relation<ShipmentItem>

Returns:

See Also:



59
# File 'app/models/purchase_order_item.rb', line 59

has_many :shipment_items

#shipment_receipt_itemsActiveRecord::Relation<ShipmentReceiptItem>

Returns:

See Also:



57
# File 'app/models/purchase_order_item.rb', line 57

has_many :shipment_receipt_items

#some_items_receipted?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'app/models/purchase_order_item.rb', line 165

def some_items_receipted?
  shipment_receipt_items.receipted.sum(:quantity).positive?
end

#some_landed_costs_applied?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'app/models/purchase_order_item.rb', line 173

def some_landed_costs_applied?
  landed_costs.actual.sum(:quantity).positive?
end

#total_in_transitObject



207
208
209
# File 'app/models/purchase_order_item.rb', line 207

def total_in_transit
  [total_shipped - total_received, 0].max
end

#total_openObject



203
204
205
# File 'app/models/purchase_order_item.rb', line 203

def total_open
  unit_quantity - total_received
end

#total_receivedObject



211
212
213
# File 'app/models/purchase_order_item.rb', line 211

def total_received
  shipment_receipt_items.receipted.sum(:quantity)
end

#total_shipped(exclude_id = nil) ⇒ Object



197
198
199
200
201
# File 'app/models/purchase_order_item.rb', line 197

def total_shipped(exclude_id = nil)
  si = shipment_items
  si = si.where.not(id: exclude_id) if exclude_id
  si.sum(:quantity)
end