Skip to content

Commit

Permalink
fix: Credit note calculation with trial period
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Mar 7, 2024
1 parent abe4605 commit 31a528a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/services/credit_notes/create_from_termination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def remaining_duration
billed_from = if subscription.trial_end_date > to_date
to_date
else
subscription.trial_end_date
subscription.trial_end_date - 1.day
end
end

Expand Down
16 changes: 9 additions & 7 deletions spec/scenarios/invoices/invoices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
let(:plan) { create(:plan, organization:, amount_cents: 3500, pay_in_advance: true, trial_period: 7) }

it 'creates an invoice for the expected period' do
travel_to(DateTime.new(2024, 3, 2)) do
travel_to(DateTime.new(2024, 3, 4, 21)) do
create_subscription(
{
external_customer_id: customer.external_id,
Expand All @@ -37,14 +37,17 @@

subscription = customer.subscriptions.first
invoice = subscription.invoices.first
expect(invoice.total_amount_cents).to eq(2597) # (31 - 1 - 7) * 35 / 31
expect(invoice.total_amount_cents).to eq(2371) # (31 - 3 - 7) * 35 / 31

travel_to(DateTime.new(2024, 3, 3)) do
travel_to(DateTime.new(2024, 3, 5, 3)) do
terminate_subscription(subscription)
end

invoice = subscription.invoices.order(created_at: :desc).first
expect(invoice.total_amount_cents).to eq(0)
term_invoice = subscription.invoices.order(created_at: :desc).first
expect(term_invoice.total_amount_cents).to eq(0)

expect(invoice.reload.credit_notes.count).to eq(1)
expect(invoice.credit_notes.first.total_amount_cents).to eq(2371)

travel_to(DateTime.new(2024, 3, 5, 4)) do
create_subscription(
Expand All @@ -57,8 +60,7 @@

subscription = customer.subscriptions.active.first
invoice = subscription.invoices.first
# NOTE: 4 days of trial left
expect(invoice.fees_amount_cents).to eq(2597) # (31 - 4 - 4) * 35 / 31
expect(invoice.fees_amount_cents).to eq(2371) # (31 - 4 - 6) * 35 / 31
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/services/credit_notes/create_from_termination_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@
credit_note = result.credit_note
expect(credit_note).to be_available
expect(credit_note).to be_order_change
expect(credit_note.total_amount_cents).to eq(17)
expect(credit_note.total_amount_cents).to eq(18) # 15 * 1.2
expect(credit_note.total_amount_currency).to eq('EUR')
expect(credit_note.credit_amount_cents).to eq(17)
expect(credit_note.credit_amount_cents).to eq(18)
expect(credit_note.credit_amount_currency).to eq('EUR')
expect(credit_note.balance_amount_cents).to eq(17)
expect(credit_note.balance_amount_cents).to eq(18)
expect(credit_note.balance_amount_currency).to eq('EUR')

expect(credit_note.items.count).to eq(1)
Expand Down

0 comments on commit 31a528a

Please sign in to comment.