Module: Models::Packable

Extended by:
ActiveSupport::Concern
Includes:
Memery
Included in:
Delivery
Defined in:
app/concerns/models/packable.rb

Overview

ActiveSupport::Concern mixin: packable.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#calculate_actual_insured_valueBigDecimal, Numeric

Insured value of the goods, using the same logic as calculate_declared_value
to ensure declared value matches calculated insured value.

Returns:

  • (BigDecimal, Numeric)


176
177
178
179
# File 'app/concerns/models/packable.rb', line 176

def calculate_actual_insured_value
  # Use the same logic as calculate_declared_value to ensure declared value matches calculated insured value
  line_items.goods.without_children.sum { |li| li.quantity * li.unit_value_for_commercial_invoice }
end

#carrierString?

The shipping carrier, from the selected shipping option or the first shipment.

Returns:

  • (String, nil)


145
146
147
# File 'app/concerns/models/packable.rb', line 145

def carrier
  shipping_option&.carrier || shipments.first&.sanitized_carrier
end

#carrier_fedex?Boolean

Whether the selected shipping option is FedEx on a supported carrier.

Returns:

  • (Boolean)


133
134
135
# File 'app/concerns/models/packable.rb', line 133

def carrier_fedex?
  shipping_option&.carrier == 'FedEx' && has_supported_carrier?
end

#delivery_description(include_line_items = false) ⇒ String

Human-readable description of how the order is delivered or picked up.

Parameters:

  • include_line_items (Boolean) (defaults to: false)

    prepend the line item names and ship weight

Returns:

  • (String)

    delivery description



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/concerns/models/packable.rb', line 93

def delivery_description(include_line_items = false)
  return 'service fulfilled remotely'      if is_remote_service_only?
  return 'service fulfilled onsite'        if is_onsite_service_only?
  return 'service fulfilled electronically' if is_service_only?

  line_items_desc = include_line_items ? "#{line_items.grouped_by_category.map { |_c, lis| lis.map(&:name).join(',') }.join(',')}, #{ship_weight} lbs of goods, " : ''

  if is_warehouse_pickup?
    "#{line_items_desc}to pick up from our warehouse in #{origin_address.city}, #{origin_address.state_code}, #{origin_address.country_printable_name}"
  else
    "#{line_items_desc}shipped from #{origin_address.city}, #{origin_address.state_code}, #{origin_address.country_printable_name}"
  end
end

#domestic?Boolean

Whether the shipment stays within the origin address's country.

Returns:

  • (Boolean)


126
127
128
129
# File 'app/concerns/models/packable.rb', line 126

def domestic?
  country_iso3 = destination_address&.country_iso3 || resource.try(:installation_country_iso3)
  origin_address.country_iso3 == country_iso3
end

#has_supported_carrier?Boolean

Whether the chosen carrier is one we support for shipping.

Returns:

  • (Boolean)


139
140
141
# File 'app/concerns/models/packable.rb', line 139

def has_supported_carrier?
  supported_shipping_carrier?
end

#is_goods_shipping?Boolean

Whether goods actually ship (not a warehouse pickup and not service-only).

Returns:

  • (Boolean)


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

def is_goods_shipping?
  !(is_warehouse_pickup? || is_service_only?)
end

#is_onsite_service_only?Boolean

Whether this is service-only and fulfilled onsite (ONSITE service SKU).

Returns:

  • (Boolean)


79
80
81
# File 'app/concerns/models/packable.rb', line 79

def is_onsite_service_only?
  is_service_only? && line_items.reject(&:marked_for_destruction?).any? { |li| li.sku.include?('ONSITE') }
end

#is_remote_service_only?Boolean

Whether this is service-only and fulfilled remotely (REMOTE service SKU).

Returns:

  • (Boolean)


73
74
75
# File 'app/concerns/models/packable.rb', line 73

def is_remote_service_only?
  is_service_only? && line_items.reject(&:marked_for_destruction?).any? { |li| li.sku.include?('REMOTE') }
end

#is_service_only?Boolean

Whether the order contains at least one service line item and no goods.

Returns:

  • (Boolean)


65
66
67
68
# File 'app/concerns/models/packable.rb', line 65

def is_service_only?
  items = line_items.reject(&:marked_for_destruction?)
  items.any?(&:is_service?) && items.none?(&:is_goods?)
end

#is_warehouse_ca_pickup?Boolean

Whether this is a warehouse pickup in Canada.

Returns:

  • (Boolean)


61
# File 'app/concerns/models/packable.rb', line 61

def is_warehouse_ca_pickup? = warehouse_pickup_for_country?('CA')

#is_warehouse_pickup?Boolean

Whether the destination is our warehouse and the order is picked up there.
STs and returns are not warehouse pickups because we want to adjust shipping method if necessary.

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'app/concerns/models/packable.rb', line 38

def is_warehouse_pickup?
  return false unless (dest_addr = destination_address)

  # STs and returns are not warehouse pickups because we want to adjust shipping method if necessary
  dest_addr.is_warehouse && !store_transfer? && !is_rma_return?
end

#is_warehouse_us_pickup?Boolean

Whether this is a warehouse pickup in the United States.

Returns:

  • (Boolean)


57
# File 'app/concerns/models/packable.rb', line 57

def is_warehouse_us_pickup? = warehouse_pickup_for_country?('US')

#is_zero_charge_dropship?Boolean

Whether every goods line item dropships at zero cost for the customer and
the order/quote does not ship from a single origin.

Returns:

  • (Boolean)


48
49
50
51
52
# File 'app/concerns/models/packable.rb', line 48

def is_zero_charge_dropship?
  has_dropship_items? &&
    !(order || quote)&.single_origin &&
    line_items.goods.includes(item: :supplier_item).all? { |li| li.item.supplier_item.dropships_cost_for_customer_is_zero? }
end

#must_be_insured?Boolean

Whether any line item must be shipped insured.

Returns:

  • (Boolean)


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

def must_be_insured?
  line_items.without_children.must_be_shipped_insured.active_non_shipping_lines.any?
end

#must_be_signature_confirmation?Boolean

Whether any authorized PayPal payment exceeds the signature-required threshold.

Returns:

  • (Boolean)


195
196
197
# File 'app/concerns/models/packable.rb', line 195

def must_be_signature_confirmation?
  order&.payments&.all_authorized&.any? { |pp| pp.category == Payment::PAYPAL && pp.amount > Payment::PAYPAL_MIN_SIGNATURE_REQUIRED }
end

#search_deliveries_for_equivalent_packagingObject

This populates our shipments

Returns:

  • (Object)

    result of Shipping::CreateSuggestedShipment processing



24
25
26
# File 'app/concerns/models/packable.rb', line 24

def search_deliveries_for_equivalent_packaging
  Shipping::CreateSuggestedShipment.new.process(self)
end

#ship_weightFloat Also known as: line_item_ship_weight

Total shipping weight of the parent goods line items, with a 0.1 lb floor.

Returns:

  • (Float)

    shipping weight in pounds



30
31
32
# File 'app/concerns/models/packable.rb', line 30

def ship_weight
  @ship_weight ||= [0.1, line_items.includes(:item).non_shipping.parents_only.sum(&:total_shipping_weight).round(1)].max
end

#ships_from_textString

Short customer-facing "ships from" / fulfillment text.

Returns:

  • (String)


110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/concerns/models/packable.rb', line 110

def ships_from_text
  if is_remote_service_only?
    'service fulfilled remotely'
  elsif is_onsite_service_only?
    'service fulfilled onsite'
  elsif is_service_only?
    'Services fulfilled electronically'
  elsif is_warehouse_pickup?
    "Pickup from our warehouse in #{origin_address.city}, #{origin_address.state_code}, #{origin_address.country_printable_name}"
  else
    "Ships from #{origin_address.city}, #{origin_address.state_code}. #{origin_address.country_printable_name}"
  end
end

#subtotalBigDecimal, Numeric

Discounted subtotal of the active, non-shipping line items.

Returns:

  • (BigDecimal, Numeric)


151
152
153
# File 'app/concerns/models/packable.rb', line 151

def subtotal
  line_items.active_non_shipping_lines.sum(&:discounted_total)
end

#subtotal_cogsBigDecimal, Numeric

Cost-of-goods subtotal of the active, non-shipping leaf line items.

Returns:

  • (BigDecimal, Numeric)


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

def subtotal_cogs
  line_items.without_children.active_non_shipping_lines.sum(&:current_line_cogs)
end

#subtotal_for_commercial_invoiceBigDecimal, Numeric

Subtotal of parent goods line items at their commercial invoice unit value.

Returns:

  • (BigDecimal, Numeric)


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

def subtotal_for_commercial_invoice
  line_items.parents_only.goods.sum { |li| li.quantity * li.unit_value_for_commercial_invoice }
end

#subtotal_for_insured_valueBigDecimal, Numeric

COGS subtotal of the line items that must be shipped insured.

Returns:

  • (BigDecimal, Numeric)


183
184
185
# File 'app/concerns/models/packable.rb', line 183

def subtotal_for_insured_value
  line_items.without_children.must_be_shipped_insured.active_non_shipping_lines.sum(&:current_line_cogs)
end

#subtotal_for_ltl_thresholdFloat

MSRP-style subtotal for the LTL threshold check; subtotal returns 0 on a
before_save, so this computes from price * quantity directly.

Returns:

  • (Float)


202
203
204
205
# File 'app/concerns/models/packable.rb', line 202

def subtotal_for_ltl_threshold
  # subtotal returns 0 on a before_save, this will return an actual msrp subtotal
  line_items.non_shipping.sum { |li| li.price * li.quantity }.round(1)
end

#subtotal_msrpBigDecimal, Numeric

MSRP subtotal of the active, non-shipping line items.

Returns:

  • (BigDecimal, Numeric)


163
164
165
# File 'app/concerns/models/packable.rb', line 163

def subtotal_msrp
  line_items.active_non_shipping_lines.sum(&:price_total)
end