Class: ShipmentReceiptItem

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

Overview

== Schema Information

Table name: shipment_receipt_items
Database name: primary

id :integer not null, primary key
currency :string(255)
effective_date :date
exclude_lead_time :boolean
lead_time :integer
quantity :integer
state :string(255)
total_cost :decimal(8, 2)
unit_cost :decimal(8, 2)
created_at :datetime
updated_at :datetime
creator_id :integer
last_gl_transaction_id :integer
purchase_order_id :integer
purchase_order_item_id :integer
shipment_item_id :integer
shipment_receipt_id :integer
updater_id :integer

Indexes

idx_po_id_sr_id (purchase_order_id,shipment_receipt_id)
index_shipment_receipt_items_on_state (state)
poi_id_state (purchase_order_item_id,state)
shipment_item_id_state (shipment_item_id,state)
sr_id_po_id (shipment_receipt_id,purchase_order_id)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Delegated Instance Attributes 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::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#quantityObject (readonly)



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

validates :quantity, numericality: { greater_than: 0 }

#validate_serial_numbersObject

Returns the value of attribute validate_serial_numbers.



59
60
61
# File 'app/models/shipment_receipt_item.rb', line 59

def validate_serial_numbers
  @validate_serial_numbers
end

Class Method Details

.receiptedActiveRecord::Relation<ShipmentReceiptItem>

A relation of ShipmentReceiptItems that are receipted. Active Record Scope

Returns:

See Also:



61
# File 'app/models/shipment_receipt_item.rb', line 61

scope :receipted, -> { where(state: "receipted") }

Instance Method Details

#actual_landed_costObject



85
86
87
# File 'app/models/shipment_receipt_item.rb', line 85

def actual_landed_cost
  actual_landed_costs.first
end

#actual_landed_costsActiveRecord::Relation<LandedCost>

Returns:

See Also:



44
# File 'app/models/shipment_receipt_item.rb', line 44

has_many :actual_landed_costs, -> { actual }, class_name: 'LandedCost'

#build_serial_numbersObject



126
127
128
129
130
131
132
# File 'app/models/shipment_receipt_item.rb', line 126

def build_serial_numbers
  if purchase_order_item.line_item&.serial_numbers&.any?
    purchase_order_item.line_item.serial_numbers.each { |sn| serial_numbers.build(qty: sn.qty, number: sn.number) }
  else
    serial_numbers.build(qty: qty_without_serial_number)
  end
end

#currency_symbolObject



77
78
79
# File 'app/models/shipment_receipt_item.rb', line 77

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

#estimated_landed_costObject



81
82
83
# File 'app/models/shipment_receipt_item.rb', line 81

def estimated_landed_cost
  estimated_landed_costs.first
end

#estimated_landed_costsActiveRecord::Relation<LandedCost>

Returns:

See Also:



43
# File 'app/models/shipment_receipt_item.rb', line 43

has_many :estimated_landed_costs, -> { estimated }, class_name: 'LandedCost'

#has_stock?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'app/models/shipment_receipt_item.rb', line 89

def has_stock?
  ile = item_ledger_entries.first
  store_item = StoreItem.where(store_id: ile.store_id, item_id: ile.item_id, location: ile.location).first
  !store_item.nil? and (store_item.qty_available >= quantity)
end

#itemObject

Alias for Purchase_order_item#item

Returns:

  • (Object)

    Purchase_order_item#item

See Also:



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

delegate :item, to: :purchase_order_item

#item_ledger_entriesActiveRecord::Relation<ItemLedgerEntry>

Returns:

See Also:



41
# File 'app/models/shipment_receipt_item.rb', line 41

has_many :item_ledger_entries

#landed_costsActiveRecord::Relation<LandedCost>

Returns:

See Also:



42
# File 'app/models/shipment_receipt_item.rb', line 42

has_many :landed_costs

#link_serial_numbers_to_item_ledger_entryObject



117
118
119
120
# File 'app/models/shipment_receipt_item.rb', line 117

def link_serial_numbers_to_item_ledger_entry
  sn_ids = serial_numbers.ids
  item_ledger_entries.each { |ile| ile.update(serial_number_ids: sn_ids) } if sn_ids.present?
end

#purchase_orderPurchaseOrder



38
# File 'app/models/shipment_receipt_item.rb', line 38

belongs_to :purchase_order, optional: true

#purchase_order_itemPurchaseOrderItem



37
# File 'app/models/shipment_receipt_item.rb', line 37

belongs_to :purchase_order_item, optional: true

#qty_without_serial_numberObject



122
123
124
# File 'app/models/shipment_receipt_item.rb', line 122

def qty_without_serial_number
  quantity - serial_numbers.sum(:qty)
end

#reverse_item_ledger(reversed_gl_transaction) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/shipment_receipt_item.rb', line 95

def reverse_item_ledger(reversed_gl_transaction)
  existing_ile = item_ledger_entries.where(category: "PO_RECEIPT").first
  raise "Unable to find existing item ledger entry for shipment receipt item #{id}" if existing_ile.nil?

  ItemLedgerEntry.create!(category: "PO_RECEIPT_VOID",
                         store: existing_ile.store,
                         item: existing_ile.item,
                         gl_date: Date.current,
                         quantity: -quantity,
                         currency: existing_ile.currency,
                         unit_cost: existing_ile.unit_cost,
                         total_cost: -existing_ile.total_cost,
                         shipment_receipt_item: self,
                         location: existing_ile.location,
                         ledger_transaction: reversed_gl_transaction)
  void!
end

#serial_numbersActiveRecord::Relation<SerialNumber>

Returns:

See Also:



46
# File 'app/models/shipment_receipt_item.rb', line 46

has_many :serial_numbers

#shipment_itemShipmentItem



39
# File 'app/models/shipment_receipt_item.rb', line 39

belongs_to :shipment_item, optional: true

#shipment_receiptShipmentReceipt



36
# File 'app/models/shipment_receipt_item.rb', line 36

belongs_to :shipment_receipt, optional: true

#store_itemObject



113
114
115
# File 'app/models/shipment_receipt_item.rb', line 113

def store_item
  StoreItem.where(store_id: purchase_order.store_id, item_id: purchase_order_item.item_id, location: shipment_receipt.location).first
end