Class: Spree::Commissions::Context

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/commissions/context.rb

Overview

What a commission rule is asked about: one sale, and everything a rule might want to know about it.

Assembled once per line rather than let each rule reach into the record itself, so a rule stays a predicate and the expensive parts — the category walk especially — are resolved for the whole order up front. Mirrors Spree::Pricing::Context, which serves price rules the same way.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seller:, order:, line_item: nil, fulfillment: nil, currency: nil, categories: nil) ⇒ Context

Returns a new instance of Context.

Parameters:

  • seller (Spree::Seller) —

    the seller being charged

  • order (Spree::Order)
  • line_item (Spree::LineItem, nil) (defaults to: nil) —

    the sale being commissioned…

  • fulfillment (Spree::Fulfillment, nil) (defaults to: nil) —

    …or its delivery

  • currency (String, nil) (defaults to: nil) —

    defaults to the order's

  • categories (Array<Spree::Category>, nil) (defaults to: nil) —

    the product's categories with their ancestors, resolved once for the order — a rate on a parent governs everything filed beneath it



23
24
25
26
27
28
29
30
# File 'app/models/spree/commissions/context.rb', line 23

def initialize(seller:, order:, line_item: nil, fulfillment: nil, currency: nil, categories: nil)
  @seller = seller
  @order = order
  @line_item = line_item
  @fulfillment = fulfillment
  @currency = currency || order&.currency
  @categories = categories
end

Instance Attribute Details

#currency ⇒ Object (readonly)

Returns the value of attribute currency.



13
14
15
# File 'app/models/spree/commissions/context.rb', line 13

def currency
  @currency
end

#fulfillment ⇒ Object (readonly)

Returns the value of attribute fulfillment.



13
14
15
# File 'app/models/spree/commissions/context.rb', line 13

def fulfillment
  @fulfillment
end

#line_item ⇒ Object (readonly)

Returns the value of attribute line_item.



13
14
15
# File 'app/models/spree/commissions/context.rb', line 13

def line_item
  @line_item
end

#order ⇒ Object (readonly)

Returns the value of attribute order.



13
14
15
# File 'app/models/spree/commissions/context.rb', line 13

def order
  @order
end

#seller ⇒ Object (readonly)

Returns the value of attribute seller.



13
14
15
# File 'app/models/spree/commissions/context.rb', line 13

def seller
  @seller
end

Instance Method Details

#categories ⇒ Array<Spree::Category>

The product's categories and every category above them. Empty when commissioning a delivery, which belongs to no category.

Returns:



48
49
50
51
52
# File 'app/models/spree/commissions/context.rb', line 48

def categories
  return @categories if @categories

  @categories = product ? Spree::Commissions::ResolveRate.categories_for([product]).fetch(product.id, []) : []
end

#commission_basis(rate = nil) ⇒ BigDecimal?

The money a rule about value compares against.

Whether that is the tax-inclusive or tax-exclusive figure depends on the rate being considered, which is why the rate is passed in: a band and the fee it gates must weigh the same sale the same way, or two bands meeting at 50 would straddle a number the operator never set.

Parameters:

  • rate (Spree::CommissionRate, nil) (defaults to: nil) —

    nil weighs the sale as it stands, which is all a caller without a rate in hand can mean

Returns:

  • (BigDecimal, nil)


64
65
66
67
68
# File 'app/models/spree/commissions/context.rb', line 64

def commission_basis(rate = nil)
  return nil if subject.nil?

  Spree::Commissions::CalculateLine.base_for(rate, subject)
end

#product ⇒ Spree::Product?

Returns nil when commissioning a delivery.

Returns:



40
41
42
# File 'app/models/spree/commissions/context.rb', line 40

def product
  line_item&.variant&.product
end

#subject ⇒ Spree::LineItem, ...

What is being commissioned — the item, or the delivery.



35
36
37
# File 'app/models/spree/commissions/context.rb', line 35

def subject
  line_item || fulfillment
end