Class: Spree::Commissions::CalculateLine
- Inherits:
-
Object
- Object
- Spree::Commissions::CalculateLine
- Includes:
- ServiceModule::Base
- Defined in:
- app/services/spree/commissions/calculate_line.rb
Overview
Works out what one commission line is worth, and builds it.
Two amounts, computed independently, because they are two different
supplies. amount is the platform's fee, charged on what the seller sold.
tax_amount is VAT on that fee — the marketplace's own taxable service to
the seller, which is why it is applied on top rather than taken out of
the item's own VAT. Keeping them apart is what lets a marketplace invoice
its commission properly in the EU, and it is the reason the base defaults
to the seller's net price: mixing the consumer's VAT into the fee base
would tax the same money twice under two different regimes.
The returned record is not saved — the caller persists it inside whatever transaction is placing the order.
Swap through Spree.commissions_calculate_line_service.
Class Method Summary collapse
-
.base_for(rate, subject) ⇒ BigDecimal
The seller's own revenue on this row, net of VAT unless the rate opts into a gross base.
Instance Method Summary collapse
Methods included from ServiceModule::Base
Class Method Details
.base_for(rate, subject) ⇒ BigDecimal
The seller's own revenue on this row, net of VAT unless the rate opts
into a gross base. Discounts come off either way — taxable_basis is
already the discounted, never-negative figure both line items and
fulfillments agree on — because commission is charged on what the
customer actually paid, and a promotion is the seller's concession in v1.
Which VAT column matters depends on how the store prices: a tax-inclusive price carries VAT inside it, which a net base removes, while a tax-exclusive one carries none, so only a gross base adds any.
A class method because a value band has to weigh the sale exactly as the fee will (Spree::Commissions::Context#commission_basis) — if the two ever computed it differently, a band would admit sales the fee then charged as though they were worth something else.
86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/services/spree/commissions/calculate_line.rb', line 86 def self.base_for(rate, subject) basis = subject.taxable_basis base = if rate&.tax_inclusive? basis + subject.additional_tax_total.to_d else basis - subject.included_tax_total.to_d end [base, BigDecimal(0)].max end |
Instance Method Details
#call(rate:, seller:, order:, line_item: nil, fulfillment: nil, commission_tax: nil) ⇒ Spree::CommissionLine
Returns unsaved.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/services/spree/commissions/calculate_line.rb', line 31 def call(rate:, seller:, order:, line_item: nil, fulfillment: nil, commission_tax: nil) # Exactly one, checked here rather than left to the record's own # validation: passing both would otherwise quietly commission the item # and drop the delivery, and only fail on save. subjects = [line_item, fulfillment].compact unless subjects.one? return failure(nil, 'a commission line needs exactly one of a line item or a fulfillment') end subject = subjects.first currency = order.currency precision = Spree::Money::Rounding.precision(currency) tax = commission_tax || resolve_tax(rate: rate, seller: seller, order: order) amount = Spree::Money::Rounding.quantize(charge_for(rate, subject, currency), precision) tax_amount = Spree::Money::Rounding.quantize(amount * tax.rate, precision) success( Spree::CommissionLine.new( order: order, seller: seller, line_item: line_item, fulfillment: fulfillment, commission_rate: rate, kind: rate.kind, rate: rate.value, amount: amount, tax_amount: tax_amount, # Summed from the two rounded parts rather than rounded again, so a # marketplace's ledger reconciles with its payouts to the cent. total: amount + tax_amount, currency: currency, **tax.to_line_attributes ) ) end |