Class: Spree::CommissionRate

Inherits:
Object
  • Object
show all
Includes:
CanonicalDecimals, HasListPosition, Metadata, SingleStoreResource, TypedAssociations
Defined in:
app/models/spree/commission_rate.rb

Overview

What a marketplace charges a seller, as configuration.

Rates are resolved per line item by walking a store's enabled rates in list order and taking the first whose rules match (Spree::Commissions::ResolveRate). A rate with no rules matches everything, which is how a marketplace expresses a single default without configuring anything.

The list IS the precedence, so what an operator sees in the table is what resolution does. Seeded rates arrive in the order merchants asked for — product, then category, then seller, then the marketplace default — and a marketplace that wants a different answer drags a row instead of reasoning about which rule type is 'narrower'.

Mutable, unlike the Spree::CommissionLine rows it produces: editing a rate changes what the next sale is charged and never what a past one was.

Constant Summary collapse

KINDS =
%w[percentage fixed].freeze

Instance Method Summary collapse

Methods included from Metadata

#private_metadata, #private_metadata=

Instance Method Details

#amount_for(order_currency) ⇒ BigDecimal?

The flat fee in one currency, or nil where none is set.

Parameters:

  • order_currency (String)

Returns:

  • (BigDecimal, nil)


153
154
155
# File 'app/models/spree/commission_rate.rb', line 153

def amount_for(order_currency)
  value_for(order_currency)&.amount
end

#amounts ⇒ Hash{String=>BigDecimal}

The flat fee amounts as the API reads and writes them: currency => amount.

Returns:

  • (Hash{String=>BigDecimal})


176
177
178
# File 'app/models/spree/commission_rate.rb', line 176

def amounts
  commission_rate_values.to_h { |value| [value.currency, value.amount] }
end

#amounts=(values) ⇒ void

This method returns an undefined value.

Replaces the flat fee amounts wholesale. A currency left out is retired, which is how a marketplace stops charging this rate there.

Parameters:

  • values (Hash, nil) —

    currency => amount



198
199
200
# File 'app/models/spree/commission_rate.rb', line 198

def amounts=(values)
  @pending_amounts = (values || {}).to_h.transform_keys { |key| key.to_s.upcase }
end

#applies_to_currency?(order_currency) ⇒ Boolean

Whether this rate can be charged in the given currency.

Parameters:

  • order_currency (String)

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/spree/commission_rate.rb', line 126

def applies_to_currency?(order_currency)
  # A flat fee applies only where it has stated an amount. Falling through
  # to the next rate is deliberate: converting one currency's figure into
  # another would invent a fee nobody set.
  return amount_for(order_currency).present? if fixed?

  # A percentage is a ratio, so it travels everywhere. Its floor and cap
  # are amounts and do not: each applies only in the currency it was
  # written in, and a sale in another currency is simply charged unbounded
  # rather than skipped. Skipping would be the harsher reading — a
  # marketplace that capped its dollar fees has not thereby said it wants
  # no commission at all on euro sales.
  true
end

#bounds ⇒ Hash{String=>Hash}

The floors and caps as the API reads and writes them: currency => { min_amount:, max_amount: }. Currencies carrying neither are left out, so a rate with no bounds anywhere reads as an empty hash.

Returns:

  • (Hash{String=>Hash})


185
186
187
188
189
190
191
# File 'app/models/spree/commission_rate.rb', line 185

def bounds
  commission_rate_values.each_with_object({}) do |value, result|
    next if value.min_amount.nil? && value.max_amount.nil?

    result[value.currency] = { 'min_amount' => value.min_amount, 'max_amount' => value.max_amount }
  end
end

#bounds=(values) ⇒ void

This method returns an undefined value.

Replaces the floors and caps wholesale, on the same rows the flat fee amounts live on. A currency left out keeps its amount and loses only its bounds — writing one is never a way to delete the other.

Parameters:

  • values (Hash, nil) —

    currency => { min_amount:, max_amount: }



208
209
210
# File 'app/models/spree/commission_rate.rb', line 208

def bounds=(values)
  @pending_bounds = (values || {}).to_h.transform_keys { |key| key.to_s.upcase }
end

#fixed? ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'app/models/spree/commission_rate.rb', line 118

def fixed?
  kind == 'fixed'
end

#global? ⇒ Boolean

Whether this rate charges every sale, having named nothing to narrow it.

Load-bearing for ordering, not just description: resolution stops at the first match, so a global rate makes every rate below it unreachable. It belongs at the bottom of the list, and an operator moving one up is turning off everything beneath it.

Returns:

  • (Boolean)


220
221
222
# File 'app/models/spree/commission_rate.rb', line 220

def global?
  commission_rules.empty?
end

#matches?(context) ⇒ Boolean

Whether this rate's rules admit the sale.

Every rule must say yes. A rule naming several records means any of them, so "(Cameras OR Audio) AND that seller" is a category rule holding two ids beside a seller rule holding one — the OR lives inside a rule, which is why there is no match-policy setting to get wrong. A rate with no rules charges every sale.

Parameters:

Returns:

  • (Boolean)


234
235
236
# File 'app/models/spree/commission_rate.rb', line 234

def matches?(context)
  commission_rules.all? { |rule| rule.applicable?(context) }
end

#max_amount_for(order_currency) ⇒ BigDecimal?

The cap in one currency, or nil where none is set.

Parameters:

  • order_currency (String)

Returns:

  • (BigDecimal, nil)


169
170
171
# File 'app/models/spree/commission_rate.rb', line 169

def max_amount_for(order_currency)
  value_for(order_currency)&.max_amount
end

#min_amount_for(order_currency) ⇒ BigDecimal?

The floor in one currency, or nil where none is set.

Parameters:

  • order_currency (String)

Returns:

  • (BigDecimal, nil)


161
162
163
# File 'app/models/spree/commission_rate.rb', line 161

def min_amount_for(order_currency)
  value_for(order_currency)&.min_amount
end

#percentage? ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/spree/commission_rate.rb', line 113

def percentage?
  kind == 'percentage'
end

#rules ⇒ Array<Spree::CommissionRule>

The rate's targeting, under the name the API reads and writes it by.

Returns:



241
242
243
# File 'app/models/spree/commission_rate.rb', line 241

def rules
  commission_rules
end

#rules=(rows) ⇒ void

This method returns an undefined value.

The flat payload the admin API writes: [{type:, preferences: {...}}], replacing the rate's rules wholesale. Rows update by id, new ones resolve their class through the registry, and omitted ones are removed — the same shape a price list's rules use.

Ids inside a rule's preferences are scope-checked against this rate's own store as they are written (see of: :id, scope:), so a rule can never be pointed at another marketplace's records.

Parameters:

  • rows (Array<Hash>, nil)


256
257
258
259
260
261
262
# File 'app/models/spree/commission_rate.rb', line 256

def rules=(rows)
  # Destroyed rather than assigned away, so clearing retires the rules
  # instead of trying to null a non-null column.
  return commission_rules.destroy_all if persisted? && Array(rows).empty?

  assign_typed_association(:commission_rules, identify_existing_rules(rows))
end

#value_for(order_currency) ⇒ Spree::CommissionRateValue?

The row holding this rate's amounts for one currency.

Parameters:

  • order_currency (String)

Returns:



145
146
147
# File 'app/models/spree/commission_rate.rb', line 145

def value_for(order_currency)
  commission_rate_values.find { |value| value.currency.to_s.casecmp?(order_currency.to_s) }
end