Class: Spree::DeliveryRate

Inherits:
Object
  • Object
show all
Extended by:
DisplayMoney
Defined in:
app/models/spree/delivery_rate.rb

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Instance Method Details

#additional_tax_total ⇒ BigDecimal

Returns the additional tax total for the shipping rate

Returns:

  • (BigDecimal)


92
93
94
# File 'app/models/spree/delivery_rate.rb', line 92

def additional_tax_total
  tax_rate && tax_inclusive? ? BigDecimal(0) : tax_amount
end

#delivery_range ⇒ String

Returns the delivery range for the shipping method

Returns:

  • (String)


145
146
147
148
149
# File 'app/models/spree/delivery_rate.rb', line 145

def delivery_range
  return unless delivery_method.delivery_range

  delivery_method.delivery_range
end

#display_delivery_range ⇒ String

Returns the display delivery range for the shipping method

Returns:

  • (String)


154
155
156
157
158
# File 'app/models/spree/delivery_rate.rb', line 154

def display_delivery_range
  return unless delivery_range

  I18n.t('spree.display_delivery_range', delivery_range: delivery_range)
end

#display_final_price(**options) ⇒ String, Spree::Money Also known as: display_total

Every money display on an unpriced rate says the same thing. A storefront that renders the total rather than the cost would otherwise put "$0.00" — free freight — in front of a buyer whose shipment has not been quoted yet.

Keeps the generated method's shape: the same keyword options, and a Spree::Money on every priced path, so callers doing money arithmetic on the total are unaffected.

Returns:



135
136
137
138
139
# File 'app/models/spree/delivery_rate.rb', line 135

def display_final_price(**options)
  return quoted_after_review if unpriced?

  Spree::Money.new(final_price, { currency: currency }.merge(options))
end

#display_price ⇒ Object Also known as: display_cost



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/spree/delivery_rate.rb', line 36

def display_price
  return quoted_after_review if unpriced?

  price = display_base_price.to_s

  return price if tax_rate.nil? || tax_amount.zero? || !tax_rate.show_rate_in_label

  I18n.t(
    "spree.shipping_rates.display_price.#{tax_inclusive? ? :including_tax : :excluding_tax}",
    price: price,
    tax_amount: display_tax_amount,
    tax_rate_name: tax_rate.name
  )
end

#final_price ⇒ Object Also known as: total

returns base price - any available discounts for this Shipment useful when you want to present a list of available shipping rates



116
117
118
119
120
121
122
# File 'app/models/spree/delivery_rate.rb', line 116

def final_price
  if with_free_shipping_promotion? || cost < -discount_amount
    BigDecimal(0)
  else
    cost + discount_amount
  end
end

#free? ⇒ Boolean

Returns true if the shipping rate is free.

An unpriced rate is never free — its cost is zero because nobody has quoted it yet, and saying "free shipping" over a container of goods would be a promise the merchant cannot keep.

Returns:

  • (Boolean)


60
61
62
# File 'app/models/spree/delivery_rate.rb', line 60

def free?
  !unpriced? && final_price.zero?
end

#freight_summary ⇒ Spree::FreightSummary?

The logistics the freight provider computed when it quoted this rate — cartons, pallets, cubic meters, gross weight. Frozen at estimate time and never re-derived, so an order keeps reporting what it actually shipped as.

Returns:



70
71
72
# File 'app/models/spree/delivery_rate.rb', line 70

def freight_summary
  @freight_summary ||= Spree::FreightSummary.(&.dig('freight_summary'))
end

#included_tax_total ⇒ BigDecimal

Returns the included tax total for the shipping rate

Returns:

  • (BigDecimal)


99
100
101
# File 'app/models/spree/delivery_rate.rb', line 99

def included_tax_total
  tax_rate && tax_inclusive? ? tax_amount : BigDecimal(0)
end

#name ⇒ String

Provider-priced rates carry their own display name ("UPS Ground", or a merchant label override) set by the Estimator; calculator rates store nil and read as the delivery method's name, preserving the historic delegate behavior.

Returns:

  • (String)


32
33
34
# File 'app/models/spree/delivery_rate.rb', line 32

def name
  self[:name].presence || delivery_method.name
end

#tax_amount ⇒ BigDecimal Also known as: tax_total

Returns the tax amount for the shipping rate, computed directly from the rate configuration (rates display estimated tax before a fulfillment exists, so this never reads TaxLine rows).

Returns:

  • (BigDecimal)


79
80
81
82
83
84
85
86
87
# File 'app/models/spree/delivery_rate.rb', line 79

def tax_amount
  @tax_amount ||= if tax_rate.nil?
                    BigDecimal(0)
                  elsif tax_inclusive?
                    Spree::Money::Rounding.to_currency(cost / (1 + tax_rate.amount) * tax_rate.amount, currency)
                  else
                    Spree::Money::Rounding.to_currency(cost * tax_rate.amount, currency)
                  end
end

#tax_inclusive? ⇒ Boolean

Whether the cost already includes its tax, which the sale's market decides (Spree::Purchase::Taxation#tax_inclusive?).

Returns:

  • (Boolean)


107
108
109
# File 'app/models/spree/delivery_rate.rb', line 107

def tax_inclusive?
  fulfillment&.owner&.tax_inclusive? || false
end