diff --git a/app/services/credit_notes/create_from_termination.rb b/app/services/credit_notes/create_from_termination.rb index d161174a377..344a0169c36 100644 --- a/app/services/credit_notes/create_from_termination.rb +++ b/app/services/credit_notes/create_from_termination.rb @@ -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 diff --git a/spec/scenarios/invoices/invoices_spec.rb b/spec/scenarios/invoices/invoices_spec.rb index 2f7ad02c945..40f3faf6f4d 100644 --- a/spec/scenarios/invoices/invoices_spec.rb +++ b/spec/scenarios/invoices/invoices_spec.rb @@ -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, @@ -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( @@ -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 diff --git a/spec/services/credit_notes/create_from_termination_spec.rb b/spec/services/credit_notes/create_from_termination_spec.rb index 6adc1c60d0a..902b5b610a3 100644 --- a/spec/services/credit_notes/create_from_termination_spec.rb +++ b/spec/services/credit_notes/create_from_termination_spec.rb @@ -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)