Class: PurchaseOrderShipment

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

Overview

== Schema Information

Table name: purchase_order_shipments
Database name: primary

id :integer not null, primary key
actual_shipping_cost :decimal(8, 2)
bill_of_lading :string(255)
container_number :string(255)
container_type :integer default("carton"), not null
currency :string(255)
date_shipped :date
drop_ship_carrier :string(255)
estimated_landed_cost :decimal(10, 2)
height :float
landed_cost :decimal(10, 2)
length :float
notes :text
promised_delivery_date :date
state :string(255)
tracking_number :string(255)
weight :float
width :float
created_at :datetime
updated_at :datetime
carrier_id :integer
creator_id :integer
updater_id :integer

Indexes

index_purchase_order_shipments_on_carrier_id (carrier_id)

Foreign Keys

fk_rails_... (carrier_id => parties.id)

Constant Summary

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

#actual_shipping_costObject (readonly)



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

validates :weight, :width, :length, :height, :actual_shipping_cost,
numericality: { greater_than: 0.0, if: :is_drop_ship_goods_delivery }

#currencyObject (readonly)



63
# File 'app/models/purchase_order_shipment.rb', line 63

validates :currency, presence: true

#drop_ship_carrierObject (readonly)

validates_associated :shipment_items, :on => :create

Validations:

  • Presence ({ if: :is_drop_ship_goods_delivery })


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

validates :drop_ship_carrier, presence: { if: :is_drop_ship_goods_delivery }

#heightObject (readonly)



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

validates :weight, :width, :length, :height, :actual_shipping_cost,
numericality: { greater_than: 0.0, if: :is_drop_ship_goods_delivery }

#lengthObject (readonly)



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

validates :weight, :width, :length, :height, :actual_shipping_cost,
numericality: { greater_than: 0.0, if: :is_drop_ship_goods_delivery }

#linked_posObject

Returns the value of attribute linked_pos.



56
57
58
# File 'app/models/purchase_order_shipment.rb', line 56

def linked_pos
  @linked_pos
end

#require_drop_ship_po_package_infoObject

Returns the value of attribute require_drop_ship_po_package_info.



56
57
58
# File 'app/models/purchase_order_shipment.rb', line 56

def require_drop_ship_po_package_info
  @require_drop_ship_po_package_info
end

#tracking_numberObject (readonly)



62
# File 'app/models/purchase_order_shipment.rb', line 62

validates :tracking_number, presence: { if: :is_drop_ship_goods_delivery }

#weightObject (readonly)



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

validates :weight, :width, :length, :height, :actual_shipping_cost,
numericality: { greater_than: 0.0, if: :is_drop_ship_goods_delivery }

#widthObject (readonly)



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

validates :weight, :width, :length, :height, :actual_shipping_cost,
numericality: { greater_than: 0.0, if: :is_drop_ship_goods_delivery }

Class Method Details

.open_shipmentsActiveRecord::Relation<PurchaseOrderShipment>

A relation of PurchaseOrderShipments that are open shipments. Active Record Scope

Returns:

See Also:



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

scope :open_shipments, -> { where(state: %w(preparing shipped partially_receipted)) }

Instance Method Details

#all_items_receipted?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'app/models/purchase_order_shipment.rb', line 123

def all_items_receipted?
  shipment_items.all?(&:fully_receipted?)
end

#carrierSupplier

Returns:

See Also:



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

belongs_to :carrier, class_name: 'Supplier', optional: true

#currency_symbolObject



176
177
178
# File 'app/models/purchase_order_shipment.rb', line 176

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

#drop_ship_deliveryObject



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

def drop_ship_delivery
  purchase_orders.first.drop_ship_delivery
end

#enter_shipment_items(params) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/purchase_order_shipment.rb', line 139

def enter_shipment_items(params)
  # do this as a transaction so if any part fails it will roll back
  PurchaseOrderShipment.transaction do
    # loop through the parameters
    params.each do |id, attrs|
      # check if it's been marked as received
      next if attrs['qty_shipped'].blank?

      # find the matching purchase order item
      poi = PurchaseOrderItem.find(id)
      qty_shipped = attrs['qty_shipped'].to_i

      next unless !poi.nil? && qty_shipped.positive?

      uom_to_ea_qty = poi.unit_quantity / poi.quantity
      shipment_items.build(purchase_order: poi.purchase_order,
                           purchase_order_item: poi,
                           quantity: qty_shipped,
                           uom: poi.uom,
                           ea_quantity: uom_to_ea_qty * qty_shipped)
    end
  end
end

#events_for_selectObject



119
120
121
# File 'app/models/purchase_order_shipment.rb', line 119

def events_for_select
  [["#{human_state_name.titleize} (Current)", '']] + possible_events_for_select
end

#fully_landed_costed?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/models/purchase_order_shipment.rb', line 131

def fully_landed_costed?
  shipment_receipts.all? { |sr| sr.landed_cost.present? }
end

#is_drop_ship_goods_deliveryObject



185
186
187
188
189
190
191
# File 'app/models/purchase_order_shipment.rb', line 185

def is_drop_ship_goods_delivery
  require_drop_ship_po_package_info or (purchase_orders.present? and purchase_orders.all? do |po|
                                          po.drop_ship_delivery.present?
                                        end and shipment_items.present? and shipment_items.all? do |si|
                                                                              si.purchase_order_item.item.is_goods?
                                                                            end)
end

#possible_eventsObject



111
112
113
# File 'app/models/purchase_order_shipment.rb', line 111

def possible_events
  state_transitions.map(&:event).sort
end

#possible_events_for_selectObject



115
116
117
# File 'app/models/purchase_order_shipment.rb', line 115

def possible_events_for_select
  possible_events.map { |evt| [evt.to_s.titleize, evt] }
end

#purchase_order_itemsActiveRecord::Relation<PurchaseOrderItem>

Returns:

See Also:



51
# File 'app/models/purchase_order_shipment.rb', line 51

has_many :purchase_order_items, through: :shipment_items

#purchase_ordersActiveRecord::Relation<PurchaseOrder>

Returns:

See Also:



50
# File 'app/models/purchase_order_shipment.rb', line 50

has_many :purchase_orders, through: :shipment_items

#set_estimated_landed_costObject



163
164
165
166
167
168
169
# File 'app/models/purchase_order_shipment.rb', line 163

def set_estimated_landed_cost
  supplier = Supplier.find(self.purchase_orders.map(&:supplier_id).uniq.first)
  if supplier.estimated_landed_cost_ptg.present?
    estimated_landed_cost_calc = self.shipment_items.map{|si| si.total_cost }.sum * (supplier.estimated_landed_cost_ptg / 100)
    self.update(estimated_landed_cost: estimated_landed_cost_calc)
  end
end

#shipment_itemsActiveRecord::Relation<ShipmentItem>

Returns:

See Also:



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

has_many :shipment_items, dependent: :destroy

#shipment_items_grouped_by_poObject



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

def shipment_items_grouped_by_po
  shipment_items.with_receipted_quantity.includes(:shipment_receipt_items,
                                                  { purchase_order: :order }).order_by_po_and_sku.group_by(&:purchase_order)
end

#shipment_items_with_receipted_quantityActiveRecord::Relation<ShipmentItem>

Returns:

See Also:



47
48
49
# File 'app/models/purchase_order_shipment.rb', line 47

has_many :shipment_items_with_receipted_quantity, lambda {
  with_receipted_quantity
}, class_name: 'ShipmentItem'

#shipment_receiptsActiveRecord::Relation<ShipmentReceipt>

Returns:

See Also:



52
# File 'app/models/purchase_order_shipment.rb', line 52

has_many :shipment_receipts

#some_items_receipted?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'app/models/purchase_order_shipment.rb', line 127

def some_items_receipted?
  shipment_items.any? { |si| si.partially_receipted? or si.fully_receipted? }
end

#to_sObject



135
136
137
# File 'app/models/purchase_order_shipment.rb', line 135

def to_s
  "PO Shipment #{carrier.try(:full_name)} #{date_shipped} "
end

#update_statesObject



171
172
173
174
# File 'app/models/purchase_order_shipment.rb', line 171

def update_states
  shipment_items.each(&:receipt_items)
  receipt_items
end