Skip to content

Commit

Permalink
Remove deprecated offset_payments
Browse files Browse the repository at this point in the history
This was a leftover for a feature that has been removed long time ago.
Please use Refunds now.

Ref solidusio#5008
  • Loading branch information
kennyadsl committed Apr 18, 2023
1 parent 4a95bbf commit 6f8581d
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 64 deletions.
3 changes: 1 addition & 2 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,7 @@ def quantity
end

def has_non_reimbursement_related_refunds?
refunds.non_reimbursement.exists? ||
payments.where("source_type = 'Spree::Payment' AND amount < 0 AND state = 'completed'").exists? # how old versions of spree stored refunds
refunds.non_reimbursement.exists?
end

def tax_total
Expand Down
29 changes: 2 additions & 27 deletions core/app/models/spree/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Payment < Spree::Base
belongs_to :source, polymorphic: true, optional: true
belongs_to :payment_method, -> { with_discarded }, class_name: 'Spree::PaymentMethod', inverse_of: :payments, optional: true

has_many :offsets, -> { offset_payment }, class_name: "Spree::Payment", foreign_key: :source_id
has_many :log_entries, as: :source
has_many :state_changes, as: :stateful
has_many :capture_events, class_name: 'Spree::PaymentCaptureEvent'
Expand Down Expand Up @@ -45,19 +44,6 @@ class Payment < Spree::Base

scope :from_credit_card, -> { where(source_type: 'Spree::CreditCard') }
scope :with_state, ->(state) { where(state: state.to_s) }
# "offset" is reserved by activerecord
# TODO: When removing the method we can also:
# - Remove the `.offsets` association
# - Remove the `#offsets_total` method
# - Remove offsets count from the `#credit_allowed` method
# - Remove offsets check from `Spree::Order#has_non_reimbursement_related_refunds?
def self.offset_payment
Spree::Deprecation.warn <<~MSG
`Spree::Payment offsets` are deprecated. Use the refund system (`Spree::Refund`) instead.
MSG

where("source_type = 'Spree::Payment' AND amount < 0 AND state = 'completed'")
end

scope :checkout, -> { with_state('checkout') }
scope :completed, -> { with_state('completed') }
Expand Down Expand Up @@ -100,22 +86,11 @@ def amount=(amount)
end || amount
end

# The total amount of the offsets (for old-style refunds) for this payment.
#
# @return [BigDecimal] the total amount of this payment's offsets
def offsets_total
offsets.pluck(:amount).sum
end

# The total amount this payment can be credited.
#
# @return [BigDecimal] the amount of this payment minus the offsets
# (old-style refunds) and refunds
# @return [BigDecimal] the amount of this payment minus refunds
def credit_allowed
amount - (
(self.class.where("source_type = 'Spree::Payment' AND amount < 0 AND state = 'completed' AND source_id = ?", id).sum(:amount)).abs +
refunds.sum(:amount)
)
amount - refunds.sum(:amount)
end

# @return [Boolean] true when this payment can be credited
Expand Down
13 changes: 0 additions & 13 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1122,19 +1122,6 @@ def generate
it { is_expected.to eq true }
end

context 'an old-style refund exists' do
let(:order) { create(:order_ready_to_ship) }
let(:payment) { order.payments.first.tap { |p| allow(p).to receive_messages(profiles_supported?: false) } }
let!(:refund_payment) {
build(:payment, amount: -1, order: order, state: 'completed', source: payment).tap do |p|
allow(p).to receive_messages(profiles_supported?: false)
p.save!
end
}

it { is_expected.to eq true }
end

context 'a reimbursement related refund exists' do
let(:order) { refund.payment.order }
let(:refund) { create(:refund, reimbursement_id: 123, amount: 5, payment_amount: 14) }
Expand Down
22 changes: 0 additions & 22 deletions core/spec/models/spree/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -668,14 +668,6 @@
end

describe "#credit_allowed" do
# Regression test for https://github.com/spree/spree/issues/4403 and https://github.com/spree/spree/issues/4407
it "is the difference between offsets total and payment amount" do
payment.amount = 100
expect(payment.credit_allowed).to eq(100)
create(:payment, amount: -80, source: payment, source_type: 'Spree::Payment', state: 'completed', payment_method: create(:check_payment_method))
expect(payment.credit_allowed).to eq(20)
end

it "is the difference between refunds total and payment amount" do
payment.amount = 100

Expand Down Expand Up @@ -1339,18 +1331,4 @@
expect(described_class.valid).to be_empty
end
end

describe '::offset_payment' do
it 'is deprecated' do
expect(Spree::Deprecation).to receive(:warn).with(/offsets` are deprecated/)

described_class.offset_payment
end

it 'still calls the original method' do
Spree::Deprecation.silence do
expect(described_class.offset_payment.class.name).to eq('ActiveRecord::Relation')
end
end
end
end

0 comments on commit 6f8581d

Please sign in to comment.