Class: PurchaseOrderItem
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"
Models::Auditable::ALWAYS_IGNORED
Instance Attribute Summary collapse
#creator, #updater
Class Method Summary
collapse
Instance Method Summary
collapse
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
#publish_event
Instance Attribute Details
#currency ⇒ Object
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
|
#description ⇒ Object
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_id ⇒ Object
65
|
# File 'app/models/purchase_order_item.rb', line 65
validates :item_id, presence: { if: :stock_item? }
|
#quantity ⇒ Object
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
|
#sku ⇒ Object
64
|
# File 'app/models/purchase_order_item.rb', line 64
validates :sku, presence: { if: :stock_item? }
|
#total_cost ⇒ Object
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_weight ⇒ Object
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_cost ⇒ Object
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_quantity ⇒ Object
66
|
# File 'app/models/purchase_order_item.rb', line 66
validates :quantity, :unit_quantity, numericality: { greater_than: 0 }
|
#unit_weight ⇒ Object
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_receive ⇒ ActiveRecord::Relation<PurchaseOrderItem>
A relation of PurchaseOrderItems that are auto receive. Active Record Scope
79
|
# File 'app/models/purchase_order_item.rb', line 79
scope :auto_receive, -> { where(auto_receive: true) }
|
.is_stock_item ⇒ ActiveRecord::Relation<PurchaseOrderItem>
A relation of PurchaseOrderItems that are is stock item. Active Record Scope
78
|
# File 'app/models/purchase_order_item.rb', line 78
scope :is_stock_item, -> { where(stock_item: true) }
|
.order_by_sku ⇒ ActiveRecord::Relation<PurchaseOrderItem>
A relation of PurchaseOrderItems that are order by sku. Active Record Scope
80
|
# File 'app/models/purchase_order_item.rb', line 80
scope :order_by_sku, -> { order('sku ASC') }
|
.removable ⇒ ActiveRecord::Relation<PurchaseOrderItem>
A relation of PurchaseOrderItems that are removable. Active Record Scope
81
|
# File 'app/models/purchase_order_item.rb', line 81
scope :removable, -> { where(state: %w[not_receipted cancelled]) }
|
.with_receipted_items ⇒ ActiveRecord::Relation<PurchaseOrderItem>
A relation of PurchaseOrderItems that are with receipted items. Active Record Scope
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
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
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
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
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
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
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_symbol ⇒ Object
181
182
183
|
# File 'app/models/purchase_order_item.rb', line 181
def currency_symbol
Money::Currency.new(currency).symbol
end
|
54
|
# File 'app/models/purchase_order_item.rb', line 54
belongs_to :item, optional: true
|
#landed_costs ⇒ ActiveRecord::Relation<LandedCost>
58
|
# File 'app/models/purchase_order_item.rb', line 58
has_many :landed_costs
|
55
|
# File 'app/models/purchase_order_item.rb', line 55
belongs_to :line_item, optional: true
|
#no_landed_costs_applied? ⇒ 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_date ⇒ Object
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
|
53
|
# File 'app/models/purchase_order_item.rb', line 53
belongs_to :purchase_order, inverse_of: :purchase_order_items, optional: true
|
#purchase_order_shipments ⇒ ActiveRecord::Relation<PurchaseOrderShipment>
60
|
# File 'app/models/purchase_order_item.rb', line 60
has_many :purchase_order_shipments, through: :shipment_items
|
#shipment_items ⇒ ActiveRecord::Relation<ShipmentItem>
59
|
# File 'app/models/purchase_order_item.rb', line 59
has_many :shipment_items
|
#shipment_receipt_items ⇒ ActiveRecord::Relation<ShipmentReceiptItem>
57
|
# File 'app/models/purchase_order_item.rb', line 57
has_many :shipment_receipt_items
|
#some_items_receipted? ⇒ 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
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_transit ⇒ Object
207
208
209
|
# File 'app/models/purchase_order_item.rb', line 207
def total_in_transit
[total_shipped - total_received, 0].max
end
|
#total_open ⇒ Object
203
204
205
|
# File 'app/models/purchase_order_item.rb', line 203
def total_open
unit_quantity - total_received
end
|
#total_received ⇒ Object
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
|