WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@yashranaway
Copy link
Contributor

fixes #1410

Previous PR #2075 & #2237

What

Stores offer code discount terms directly in InstallmentPlanSnapshot so installment payments retain their original discount even if the offer code is deleted, expired, or modified.

Changes

  • Store offer code data directly in snapshot (amount, percentage, code string, currency, duration)
  • Removed belongs_to :original_offer_code association - no longer relies on live offer code
  • set_price_and_rate prioritizes snapshot data for installment payments
  • Updated Onetime::BackfillPaymentOptionInstallmentSnapshots to include offer code data when creating snapshots
  • Added Onetime::BackfillOfferCodeFieldsToInstallmentSnapshots to update existing snapshots missing offer code data

Tests

  • Discount preserved after offer code deletion
  • Discount preserved after offer code expiration
  • Discount preserved after offer code amount changes
  • Snapshot stores code, currency, duration for display
  • Usage limits don't affect subsequent installments
  • Both one-time backfill services tested

AI disclosure

Used Claude Opus 4.5 via Cursor for implementation.
All code was manually reviewed and verified.

Notes

I’ve watched all the Antiwork livestreams

Comment on lines +19 to +51

def has_original_offer_code?
original_offer_code_id.present? && (original_offer_code_amount_cents.present? || original_offer_code_amount_percentage.present?)
end

def original_offer_code_amount_off(price_cents)
return 0 if !has_original_offer_code?
return original_offer_code_amount_cents if !original_offer_code_is_percent?

(price_cents * (original_offer_code_amount_percentage / 100.0)).round
end

def original_offer_code_display_code
original_offer_code_code
end

def displayed_amount_off(currency_type, with_symbol: false)
return nil unless has_original_offer_code?

if with_symbol
if original_offer_code_is_percent?
"#{original_offer_code_amount_percentage}%"
else
Money.new(original_offer_code_amount_cents, currency_type).format(no_cents_if_whole: true, symbol: true)
end
else
if original_offer_code_is_percent?
original_offer_code_amount_percentage
else
Money.new(original_offer_code_amount_cents, currency_type).format(no_cents_if_whole: true, symbol: false)
end
end
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed association, added display methods

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated set_price_and_rate and original_offer_code

Comment on lines +61 to +75
}

if purchase.offer_code.present?
snapshot_attrs.merge!(
original_offer_code_id: purchase.offer_code.id,
original_offer_code_amount_cents: purchase.offer_code.amount_cents,
original_offer_code_amount_percentage: purchase.offer_code.amount_percentage,
original_offer_code_is_percent: purchase.offer_code.is_percent?,
original_offer_code_duration_in_months: purchase.offer_code.duration_in_months,
original_offer_code_code: purchase.offer_code.code,
original_offer_code_currency: purchase.offer_code.currency_type
)
end

payment_option.build_installment_plan_snapshot(snapshot_attrs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snapshot now includes offer code fields

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new service

@yashranaway
Copy link
Contributor Author

@EmCousin _a added updates as per #2237 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Offer code not applied to non-first installments when max usage limit is reached

2 participants