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
-
#calculate_actual_insured_value ⇒ BigDecimal, Numeric
Insured value of the goods, using the same logic as calculate_declared_value to ensure declared value matches calculated insured value.
-
#carrier ⇒ String?
The shipping carrier, from the selected shipping option or the first shipment.
-
#carrier_fedex? ⇒ Boolean
Whether the selected shipping option is FedEx on a supported carrier.
-
#delivery_description(include_line_items = false) ⇒ String
Human-readable description of how the order is delivered or picked up.
-
#domestic? ⇒ Boolean
Whether the shipment stays within the origin address's country.
-
#has_supported_carrier? ⇒ Boolean
Whether the chosen carrier is one we support for shipping.
-
#is_goods_shipping? ⇒ Boolean
Whether goods actually ship (not a warehouse pickup and not service-only).
-
#is_onsite_service_only? ⇒ Boolean
Whether this is service-only and fulfilled onsite (ONSITE service SKU).
-
#is_remote_service_only? ⇒ Boolean
Whether this is service-only and fulfilled remotely (REMOTE service SKU).
-
#is_service_only? ⇒ Boolean
Whether the order contains at least one service line item and no goods.
-
#is_warehouse_ca_pickup? ⇒ Boolean
Whether this is a warehouse pickup in Canada.
-
#is_warehouse_pickup? ⇒ Boolean
Whether the destination is our warehouse and the order is picked up there.
-
#is_warehouse_us_pickup? ⇒ Boolean
Whether this is a warehouse pickup in the United States.
-
#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.
-
#must_be_insured? ⇒ Boolean
Whether any line item must be shipped insured.
-
#must_be_signature_confirmation? ⇒ Boolean
Whether any authorized PayPal payment exceeds the signature-required threshold.
-
#search_deliveries_for_equivalent_packaging ⇒ Object
This populates our shipments.
-
#ship_weight ⇒ Float
(also: #line_item_ship_weight)
Total shipping weight of the parent goods line items, with a 0.1 lb floor.
-
#ships_from_text ⇒ String
Short customer-facing "ships from" / fulfillment text.
-
#subtotal ⇒ BigDecimal, Numeric
Discounted subtotal of the active, non-shipping line items.
-
#subtotal_cogs ⇒ BigDecimal, Numeric
Cost-of-goods subtotal of the active, non-shipping leaf line items.
-
#subtotal_for_commercial_invoice ⇒ BigDecimal, Numeric
Subtotal of parent goods line items at their commercial invoice unit value.
-
#subtotal_for_insured_value ⇒ BigDecimal, Numeric
COGS subtotal of the line items that must be shipped insured.
-
#subtotal_for_ltl_threshold ⇒ Float
MSRP-style subtotal for the LTL threshold check; subtotal returns 0 on a before_save, so this computes from price * quantity directly.
-
#subtotal_msrp ⇒ BigDecimal, Numeric
MSRP subtotal of the active, non-shipping line items.
Instance Method Details
#calculate_actual_insured_value ⇒ BigDecimal, Numeric
Insured value of the goods, using the same logic as calculate_declared_value
to ensure declared value matches calculated insured value.
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 |
#carrier ⇒ String?
The shipping carrier, from the selected shipping option or the first shipment.
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.
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.
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.
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.
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).
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).
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).
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.
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.
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.
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.
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.
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.
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.
195 196 197 |
# File 'app/concerns/models/packable.rb', line 195 def must_be_signature_confirmation? order&.payments&.&.any? { |pp| pp.category == Payment::PAYPAL && pp.amount > Payment::PAYPAL_MIN_SIGNATURE_REQUIRED } end |
#search_deliveries_for_equivalent_packaging ⇒ Object
This populates our shipments
24 25 26 |
# File 'app/concerns/models/packable.rb', line 24 def search_deliveries_for_equivalent_packaging Shipping::CreateSuggestedShipment.new.process(self) end |
#ship_weight ⇒ Float Also known as: line_item_ship_weight
Total shipping weight of the parent goods line items, with a 0.1 lb floor.
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_text ⇒ String
Short customer-facing "ships from" / fulfillment text.
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 |
#subtotal ⇒ BigDecimal, Numeric
Discounted subtotal of the active, non-shipping line items.
151 152 153 |
# File 'app/concerns/models/packable.rb', line 151 def subtotal line_items.active_non_shipping_lines.sum(&:discounted_total) end |
#subtotal_cogs ⇒ BigDecimal, Numeric
Cost-of-goods subtotal of the active, non-shipping leaf line items.
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_invoice ⇒ BigDecimal, Numeric
Subtotal of parent goods line items at their commercial invoice unit value.
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_value ⇒ BigDecimal, Numeric
COGS subtotal of the line items that must be shipped insured.
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_threshold ⇒ Float
MSRP-style subtotal for the LTL threshold check; subtotal returns 0 on a
before_save, so this computes from price * quantity directly.
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_msrp ⇒ BigDecimal, Numeric
MSRP subtotal of the active, non-shipping line items.
163 164 165 |
# File 'app/concerns/models/packable.rb', line 163 def subtotal_msrp line_items.active_non_shipping_lines.sum(&:price_total) end |