Module: Models::ShipMeasurable

Extended by:
ActiveSupport::Concern
Includes:
Memery
Included in:
Delivery, ShipQuotable, StandaloneDelivery
Defined in:
app/concerns/models/ship_measurable.rb

Overview

ActiveSupport::Concern mixin: ship measurable.

Instance Method Summary collapse

Instance Method Details

#cartons_totalObject



55
56
57
# File 'app/concerns/models/ship_measurable.rb', line 55

def cartons_total
  shipment_set.cartons.size
end

#crates_totalObject



63
64
65
# File 'app/concerns/models/ship_measurable.rb', line 63

def crates_total
  shipment_set.crates.size
end

#pallets_totalObject



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

def pallets_total
  shipment_set.where(container_type: %i[pallet palleted_crate]).count
end

#ship_freight_class_from_shipmentsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/concerns/models/ship_measurable.rb', line 40

def ship_freight_class_from_shipments
  return unless (swfs = ship_weight_from_shipments)
  return unless (svs = ship_volume_from_shipments_in_cubic_feet)

  pcf = swfs / svs
  freight_class = nil
  Shipping::UpsFreight::FREIGHT_CLASS_BY_PCF.each do |fc, pcf_limits|
    if pcf >= pcf_limits[:lower] && pcf < pcf_limits[:upper]
      freight_class = fc.to_i
      break
    end
  end
  freight_class
end

#ship_volume_from_shipmentsObject



30
31
32
33
34
# File 'app/concerns/models/ship_measurable.rb', line 30

def ship_volume_from_shipments
  vol = nil
  vol = shipments_for_measure.sum(&:volume) if shipments_for_measure.present? && shipments_for_measure.all?(&:volume)
  vol
end

#ship_volume_from_shipments_in_cubic_feetObject



36
37
38
# File 'app/concerns/models/ship_measurable.rb', line 36

def ship_volume_from_shipments_in_cubic_feet
  (svs = ship_volume_from_shipments) ? svs / 1728.0 : nil
end

#ship_weight_from_shipmentsObject



26
27
28
# File 'app/concerns/models/ship_measurable.rb', line 26

def ship_weight_from_shipments
  shipments_for_measure.sum(:weight)
end

#shipment_setObject

Shipments in complete/awaiting_label stage take precedence over packed shipments which take precedence over suggested shipments



11
12
13
14
15
# File 'app/concerns/models/ship_measurable.rb', line 11

def shipment_set
  shipments.where(state: %w[awaiting_label label_complete manually_complete]).presence ||
    shipments.packed.presence ||
    shipments.suggested
end

#shipments_for_measureObject

Returns the shipments to use for measurements. Cartons are used by default, unless none are defined then we'll use the pallet
TODO: once pallets are defined with boxes, use the pallet weight and volume



20
21
22
23
# File 'app/concerns/models/ship_measurable.rb', line 20

def shipments_for_measure
  # We only use top level shipments, ie shipments that are not contained in other shipments/pallets
  shipment_set.top_level
end