Skip to content

Commit

Permalink
feat(manual-payments): Fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Feb 7, 2025
1 parent 58a5c07 commit e8448a4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
6 changes: 0 additions & 6 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,6 @@ def refundable_amount_cents
return 0 if version_number < CREDIT_NOTES_MIN_VERSION || draft?
return 0 if !payment_succeeded? && total_paid_amount_cents == total_amount_cents

# base_amount = if credit?
# available_to_credit_amount_cents
# else
# total_paid_amount_cents - credit_notes.sum("refund_amount_cents + credit_amount_cents")
# end

amount = total_paid_amount_cents - credit_notes.sum(:refund_amount_cents) -
credits.where(before_taxes: false).sum(:amount_cents) -
prepaid_credit_amount_cents
Expand Down
1 change: 0 additions & 1 deletion app/services/invoices/advance_charges_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def call

if invoice && !invoice.closed?
SendWebhookJob.perform_later("invoice.created", invoice)
SendWebhookJob.perform_later('invoice.created', invoice)
create_manual_payment(invoice)
Invoices::GeneratePdfAndNotifyJob.perform_later(invoice:, email: false)
Integrations::Aggregator::Invoices::CreateJob.perform_later(invoice:) if invoice.should_sync_invoice?
Expand Down
47 changes: 38 additions & 9 deletions spec/scenarios/credit_note_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@
expect(fee2.precise_coupons_amount_cents).to eq(25_000)

travel_to(DateTime.new(2023, 10, 23)) do
update_invoice(invoice, payment_status: :succeeded)
Payments::ManualCreateService.call(
organization:,
params: {invoice_id: invoice.id, amount_cents: 1_200, reference: 'ref1'}
)

# Estimate the credit notes amount on full fees
estimate_credit_note(
Expand All @@ -123,7 +126,7 @@
expect(estimate[:taxes_amount_cents]).to eq(4_770)
expect(estimate[:sub_total_excluding_taxes_amount_cents]).to eq(32_800)
expect(estimate[:max_creditable_amount_cents]).to eq(37_570)
expect(estimate[:max_refundable_amount_cents]).to eq(37_570)
expect(estimate[:max_refundable_amount_cents]).to eq(1_200)
expect(estimate[:coupons_adjustment_amount_cents]).to eq(250_00)
expect(estimate[:taxes_rate]).to eq(14.54268)

Expand All @@ -142,7 +145,7 @@
expect(estimate[:taxes_amount_cents]).to eq(1961)
expect(estimate[:sub_total_excluding_taxes_amount_cents]).to eq(9_806)
expect(estimate[:max_creditable_amount_cents]).to eq(11_768)
expect(estimate[:max_refundable_amount_cents]).to eq(11_768)
expect(estimate[:max_refundable_amount_cents]).to eq(1_200)
expect(estimate[:coupons_adjustment_amount_cents]).to eq(16_454)
expect(estimate[:taxes_rate]).to eq(20)

Expand Down Expand Up @@ -296,7 +299,10 @@
expect(fee6.precise_coupons_amount_cents).to eq(9.66216)

travel_to(DateTime.new(2023, 10, 23)) do
update_invoice(invoice, payment_status: :succeeded)
Payments::ManualCreateService.call(
organization:,
params: {invoice_id: invoice.id, amount_cents: 40, reference: 'ref2'}
)

estimate_credit_note(
invoice_id: invoice.id,
Expand Down Expand Up @@ -329,7 +335,7 @@
expect(estimate[:sub_total_excluding_taxes_amount_cents]).to eq(498)
expect(estimate[:taxes_amount_cents]).to eq(50)
expect(estimate[:max_creditable_amount_cents]).to eq(547)
expect(estimate[:max_refundable_amount_cents]).to eq(547)
expect(estimate[:max_refundable_amount_cents]).to eq(40)
expect(estimate[:taxes_rate]).to eq(10)
end
end
Expand All @@ -350,7 +356,11 @@
expect(invoice.total_amount_cents).to eq(163_99)
expect(invoice.taxes_amount_cents).to eq(27_33)
fees = invoice.fees
invoice.update(payment_status: "succeeded")

Payments::ManualCreateService.call(
organization:,
params: {invoice_id: invoice.id, amount_cents: 500, reference: 'ref3'}
)

# estimate and create credit notes for first item - full refund; the taxes are rounded to higher number
estimate_credit_note(
Expand All @@ -370,7 +380,7 @@
precise_taxes_amount_cents: "1366.6",
sub_total_excluding_taxes_amount_cents: 6833,
max_creditable_amount_cents: 8200,
max_refundable_amount_cents: 8200,
max_refundable_amount_cents: 500,
taxes_rate: 20.0
)

Expand Down Expand Up @@ -398,6 +408,11 @@
expect(credit_note.precise_total).to eq(8199.6)
expect(credit_note.taxes_rounding_adjustment).to eq(0.4)

Payments::ManualCreateService.call(
organization:,
params: {invoice_id: invoice.id, amount_cents: 8_000, reference: 'ref3'}
)

# when issuing second credit note, it should be rounded to lower number
estimate_credit_note(
invoice_id: invoice.id,
Expand All @@ -410,12 +425,13 @@
)

estimate = json[:estimated_credit_note]

expect(estimate).to include(
taxes_amount_cents: 13_66,
precise_taxes_amount_cents: "1366.2",
sub_total_excluding_taxes_amount_cents: 6833,
max_creditable_amount_cents: 8199,
max_refundable_amount_cents: 8199,
max_refundable_amount_cents: 300,
taxes_rate: 20.0
)

Expand Down Expand Up @@ -458,6 +474,12 @@
fees = invoice.fees
invoice.update(payment_status: "succeeded")

Payments::ManualCreateService.call(
organization:,
params: {invoice_id: invoice.id, amount_cents: 300_00, reference: 'ref3'}
)
invoice.reload

# estimate and create credit notes for first three items - full refund; the taxes are rounded to higher number
3.times do |i|
estimate_credit_note(
Expand Down Expand Up @@ -625,7 +647,7 @@
precise_taxes_amount_cents: "645.4",
sub_total_excluding_taxes_amount_cents: 3233,
max_creditable_amount_cents: 3878,
max_refundable_amount_cents: 3878,
max_refundable_amount_cents: 1080, # invoice.total_paid_amount_cents - invoice.credit_notes.sum(:refund_amount_cents)
taxes_rate: 20.0
)

Expand Down Expand Up @@ -938,6 +960,13 @@
wallet.reload
expect(wallet.balance_cents).to eq 1500

Payments::ManualCreateService.call(
organization:,
params: {invoice_id: invoice.id, amount_cents: 1500, reference: 'ref3'}
)

invoice.reload

# it allows to estimate a credit notes on credit invoices with payment status succeeded
estimate_credit_note(
invoice_id: invoice.id,
Expand Down
12 changes: 10 additions & 2 deletions spec/services/credit_notes/estimate_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

RSpec.describe CreditNotes::EstimateService, type: :service do
subject(:estimate_service) { described_class.new(invoice:, items:) }
subject(:estimate_service) { described_class.new(invoice: invoice&.reload, items:) }

let(:organization) { create(:organization) }
let(:customer) { create(:customer, organization:) }
Expand Down Expand Up @@ -47,12 +47,15 @@
]
end

let(:params) { {invoice_id: invoice&.id, amount_cents: 9, reference: 'ref1'} }

around { |test| lago_premium!(&test) }

before do
create(:fee_applied_tax, tax:, fee: fee1)
create(:fee_applied_tax, tax:, fee: fee2)
create(:invoice_applied_tax, tax:, invoice:) if invoice
Payments::ManualCreateService.call(organization:, params:)
end

it 'estimates the credit and refund amount' do
Expand Down Expand Up @@ -198,6 +201,8 @@
let(:wallet) { create(:wallet, customer:, balance_cents: 10) }
let(:wallet_transaction) { create(:wallet_transaction, wallet:) }
let(:credit_fee) { create(:fee, fee_type: :credit, invoice:, invoiceable: wallet_transaction) }
let(:params) { {invoice_id: invoice.id, amount_cents: 12, reference: 'ref2'} }

let(:items) do
[
{
Expand All @@ -207,7 +212,10 @@
]
end

before { credit_fee }
before do
credit_fee
Payments::ManualCreateService.call(organization:, params:)
end

context 'when wallet for the credits is active' do
it 'estimates the credit and refund amount not higher than wallet.balance_cents' do
Expand Down
1 change: 0 additions & 1 deletion spec/services/invoices/advance_charges_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def fee_boundaries

expect(SendWebhookJob).to have_been_enqueued.with("invoice.created", result.invoice)
expect(Invoices::GeneratePdfAndNotifyJob).to have_been_enqueued.with(invoice: result.invoice, email: false)
expect(SendWebhookJob).to have_been_enqueued.with("invoice.created", result.invoice)
expect(SegmentTrackJob).to have_been_enqueued.once
expect(Invoices::TransitionToFinalStatusService).to have_received(:call).with(invoice: result.invoice)

Expand Down

0 comments on commit e8448a4

Please sign in to comment.