Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Made seller transfer settlement columns required

Łukasz Adamczyk256f8f5e53e9
diff --git a/spree/core/app/models/spree/seller_transfer.rb b/spree/core/app/models/spree/seller_transfer.rb
index 1bba42cb69..e56e4dab8f 100644
--- a/spree/core/app/models/spree/seller_transfer.rb
+++ b/spree/core/app/models/spree/seller_transfer.rb
@@ -52,6 +52,8 @@ module Spree
validates :currency, presence: true
validates :kind, presence: true, inclusion: { in: KINDS }
validates :provider, presence: true
+ validates :settled_amount, numericality: true
+ validates :settled_currency, presence: true
# A row settles in the currency it was sold in until a provider says
# otherwise — which is the truth outright for the built-in provider, since
@@ -137,22 +139,21 @@ module Spree
#
# A sale is priced in the customer's currency; an account settles in its
# own, and the provider converts on the way in. Both are recorded facts —
- # Spree holds no exchange rates and converts nothing. The fallback is for
- # rows written before the columns existed; everything since carries them.
+ # Spree holds no exchange rates and converts nothing.
#
# @return [BigDecimal]
def settlement_amount
- settled_amount || amount
+ settled_amount
end
# @return [String]
def settlement_currency
- settled_currency.presence || currency
+ settled_currency
end
# @return [Boolean] whether the provider converted this on the way in
def converted?
- settled_currency.present? && settled_currency != currency
+ settled_currency != currency
end
private
diff --git a/spree/core/db/migrate/20260914000001_add_settlement_to_spree_seller_transfers.rb b/spree/core/db/migrate/20260914000001_add_settlement_to_spree_seller_transfers.rb
index 1a066bc559..87cfa675b4 100644
--- a/spree/core/db/migrate/20260914000001_add_settlement_to_spree_seller_transfers.rb
+++ b/spree/core/db/migrate/20260914000001_add_settlement_to_spree_seller_transfers.rb
@@ -1,14 +1,36 @@
# What a seller actually received, when their account settles in a currency
-# other than the one the sale was priced in. Nullable because nil is meaningful:
-# no provider has reported a settlement, so the earned figures stand in.
+# other than the one the sale was priced in.
#
# This records a conversion the provider performed and reported. Spree still
# holds no exchange rates and converts nothing itself.
+#
+# The columns are NOT NULL, because every query that groups or sums by
+# settlement reads them directly — a null row would fall out of balances and
+# out of payout selection without saying so. A row starts out settling in the
+# currency it was sold in, which is the truth outright for a provider that
+# converts nothing, and a provider that does converts overwrites both.
+#
+# The columns are seeded here rather than by a rake task: they are introduced
+# by this migration, in a release that has not shipped, so this is a new
+# column's initial value rather than a transformation of data anyone holds.
class AddSettlementToSpreeSellerTransfers < ActiveRecord::Migration[8.1]
def change
add_column :spree_seller_transfers, :settled_amount, :decimal, precision: 10, scale: 2
add_column :spree_seller_transfers, :settled_currency, :string
+ reversible do |direction|
+ direction.up do
+ execute(<<~SQL.squish)
+ UPDATE spree_seller_transfers
+ SET settled_amount = amount, settled_currency = currency
+ WHERE settled_amount IS NULL OR settled_currency IS NULL
+ SQL
+ end
+ end
+
+ change_column_null :spree_seller_transfers, :settled_amount, false
+ change_column_null :spree_seller_transfers, :settled_currency, false
+
add_index :spree_seller_transfers, [:seller_id, :settled_currency, :status],
name: 'index_seller_transfers_on_seller_and_settled_currency'
end