Module: Models::Packable

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#calculate_actual_insured_valueObject



124
125
126
127
# File 'app/concerns/models/packable.rb', line 124

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

#carrierObject



104
105
106
# File 'app/concerns/models/packable.rb', line 104

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

#carrier_fedex?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'app/concerns/models/packable.rb', line 96

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

#delivery_description(include_line_items = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/concerns/models/packable.rb', line 62

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

Returns:

  • (Boolean)


91
92
93
94
# File 'app/concerns/models/packable.rb', line 91

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

#has_supported_carrier?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'app/concerns/models/packable.rb', line 100

def has_supported_carrier?
  supported_shipping_carrier?
end

#is_goods_shipping?Boolean

Returns:

  • (Boolean)


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

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

#is_onsite_service_only?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/concerns/models/packable.rb', line 53

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

Returns:

  • (Boolean)


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

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

Returns:

  • (Boolean)


43
44
45
46
# File 'app/concerns/models/packable.rb', line 43

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

Returns:

  • (Boolean)


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

def is_warehouse_ca_pickup? = warehouse_pickup_for_country?('CA')

#is_warehouse_pickup?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'app/concerns/models/packable.rb', line 26

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

Returns:

  • (Boolean)


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

def is_warehouse_us_pickup? = warehouse_pickup_for_country?('US')

#is_zero_charge_dropship?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'app/concerns/models/packable.rb', line 33

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

Returns:

  • (Boolean)


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

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

#must_be_signature_confirmation?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'app/concerns/models/packable.rb', line 137

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



17
18
19
# File 'app/concerns/models/packable.rb', line 17

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

#ship_weightObject Also known as: line_item_ship_weight



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

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

#ships_from_textObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/concerns/models/packable.rb', line 77

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

#subtotalObject



108
109
110
# File 'app/concerns/models/packable.rb', line 108

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

#subtotal_cogsObject



120
121
122
# File 'app/concerns/models/packable.rb', line 120

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

#subtotal_for_commercial_invoiceObject



112
113
114
# File 'app/concerns/models/packable.rb', line 112

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

#subtotal_for_insured_valueObject



129
130
131
# File 'app/concerns/models/packable.rb', line 129

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_thresholdObject



141
142
143
144
# File 'app/concerns/models/packable.rb', line 141

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

#subtotal_msrpObject



116
117
118
# File 'app/concerns/models/packable.rb', line 116

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