Skip to content

Commit 917afaf

Browse files
committed
Do not change issuing_date on finalization if keep_anchor is set
1 parent b0151a3 commit 917afaf

File tree

5 files changed

+111
-17
lines changed

5 files changed

+111
-17
lines changed

app/services/invoices/provider_taxes/pull_taxes_and_apply_service.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,17 @@ def create_applied_prepaid_credit
115115
end
116116

117117
def issuing_date
118-
@issuing_date ||= Time.current.in_time_zone(customer.applicable_timezone).to_date
118+
@issuing_date ||=
119+
if issuing_date_keep_anchor?
120+
invoice.issuing_date
121+
else
122+
Time.current.in_time_zone(customer.applicable_timezone).to_date
123+
end
124+
end
125+
126+
def issuing_date_keep_anchor?
127+
invoice.invoice_subscriptions.first&.recurring? &&
128+
customer.applicable_subscription_invoice_issuing_date_adjustment == "keep_anchor"
119129
end
120130

121131
def payment_due_date

app/services/invoices/refresh_draft_and_finalize_service.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def call
1717
invoice.issuing_date = issuing_date
1818
refresh_result = Invoices::RefreshDraftService.call(invoice:, context: :finalize)
1919
if invoice.tax_pending?
20+
# When we need to fetch taxes, the invoice isn't finalized until taxes are pulled.
21+
# So we can't show the final issuing/payment due dates yet.
22+
# We'll set those in Inovoices::ProviderTaxes::PullTaxesAndApplyService
23+
# once the taxes are successfully pulled.
2024
invoice.update!(issuing_date: drafted_issuing_date)
2125
# rubocop:disable Rails/TransactionExitStatement
2226
return refresh_result
@@ -60,7 +64,17 @@ def call
6064
attr_accessor :invoice, :result
6165

6266
def issuing_date
63-
@issuing_date ||= Time.current.in_time_zone(invoice.customer.applicable_timezone).to_date
67+
@issuing_date ||=
68+
if issuing_date_keep_anchor?
69+
invoice.issuing_date
70+
else
71+
Time.current.in_time_zone(invoice.customer.applicable_timezone).to_date
72+
end
73+
end
74+
75+
def issuing_date_keep_anchor?
76+
invoice.invoice_subscriptions.first&.recurring? &&
77+
invoice.customer.applicable_subscription_invoice_issuing_date_adjustment == "keep_anchor"
6478
end
6579

6680
def payment_due_date

db/structure.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10653,10 +10653,10 @@ ALTER TABLE ONLY public.wallet_transactions_invoice_custom_sections
1065310653
SET search_path TO "$user", public;
1065410654

1065510655
INSERT INTO "schema_migrations" (version) VALUES
10656-
('20251201084648'),
1065710656
('20251204142205'),
1065810657
('20251202141759'),
1065910658
('20251201094057'),
10659+
('20251201084648'),
1066010660
('20251128102055'),
1066110661
('20251127145819'),
1066210662
('20251127123135'),

spec/services/invoices/provider_taxes/pull_taxes_and_apply_service_spec.rb

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,50 @@
152152
.to change(invoice.error_details.tax_error, :count).from(1).to(0)
153153
end
154154

155-
it "updates the issuing date and payment due date" do
156-
invoice.customer.update(timezone: "America/New_York")
155+
context "with a non-recurring invoice" do
156+
let(:billing_entity) { create(:billing_entity, organization:, subscription_invoice_issuing_date_adjustment: "keep_anchor") }
157157

158-
freeze_time do
159-
current_date = Time.current.in_time_zone("America/New_York").to_date
158+
it "updates the issuing date and payment due date" do
159+
invoice.customer.update(timezone: "America/New_York")
160160

161-
expect { pull_taxes_service.call }
162-
.to change { invoice.reload.issuing_date }.to(current_date)
163-
.and change { invoice.reload.payment_due_date }.to(current_date)
161+
freeze_time do
162+
current_date = Time.current.in_time_zone("America/New_York").to_date
163+
164+
expect { pull_taxes_service.call }
165+
.to change { invoice.reload.issuing_date }.to(current_date)
166+
.and change { invoice.reload.payment_due_date }.to(current_date)
167+
end
168+
end
169+
end
170+
171+
context "with a recurring invoice" do
172+
let(:billing_entity) { create(:billing_entity, organization:, subscription_invoice_issuing_date_adjustment:) }
173+
174+
before do
175+
invoice.invoice_subscriptions.first.update(recurring: true)
176+
invoice.customer.update(timezone: "America/New_York")
177+
end
178+
179+
context "with issuing date adjustment set to keep_anchor" do
180+
let(:subscription_invoice_issuing_date_adjustment) { "keep_anchor" }
181+
182+
it "does not update the issuing date" do
183+
expect { pull_taxes_service.call }.not_to change { invoice.reload.issuing_date }
184+
end
185+
end
186+
187+
context "with issuing date adjustment set to align_with_finalization_date" do
188+
let(:subscription_invoice_issuing_date_adjustment) { "align_with_finalization_date" }
189+
190+
it "updates the issuing date and payment due date" do
191+
freeze_time do
192+
current_date = Time.current.in_time_zone("America/New_York").to_date
193+
194+
expect { pull_taxes_service.call }
195+
.to change { invoice.reload.issuing_date }.to(current_date)
196+
.and change { invoice.reload.payment_due_date }.to(current_date)
197+
end
198+
end
164199
end
165200
end
166201

spec/services/invoices/refresh_draft_and_finalize_service_spec.rb

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,50 @@
8989
expect(Invoices::TransitionToFinalStatusService).to have_received(:call).with(invoice:)
9090
end
9191

92-
it "updates the issuing date" do
93-
invoice.customer.update(timezone: "America/New_York")
92+
context "with a non-recurring invoice" do
93+
let(:billing_entity) { create(:billing_entity, organization:, subscription_invoice_issuing_date_adjustment: "keep_anchor") }
9494

95-
freeze_time do
96-
current_date = Time.current.in_time_zone("America/New_York").to_date
95+
it "updates the issuing date" do
96+
invoice.customer.update(timezone: "America/New_York")
9797

98-
expect { finalize_service.call }
99-
.to change { invoice.reload.issuing_date }.to(current_date)
100-
.and change { invoice.reload.payment_due_date }.to(current_date)
98+
freeze_time do
99+
current_date = Time.current.in_time_zone("America/New_York").to_date
100+
101+
expect { finalize_service.call }
102+
.to change { invoice.reload.issuing_date }.to(current_date)
103+
.and change { invoice.reload.payment_due_date }.to(current_date)
104+
end
105+
end
106+
end
107+
108+
context "with a recurring invoice" do
109+
let(:billing_entity) { create(:billing_entity, organization:, subscription_invoice_issuing_date_adjustment:) }
110+
111+
before do
112+
invoice.invoice_subscriptions.first.update(recurring: true)
113+
invoice.customer.update(timezone: "America/New_York")
114+
end
115+
116+
context "with issuing date adjustment set to keep_anchor" do
117+
let(:subscription_invoice_issuing_date_adjustment) { "keep_anchor" }
118+
119+
it "does not update the issuing date" do
120+
expect { finalize_service.call }.not_to change { invoice.reload.issuing_date }
121+
end
122+
end
123+
124+
context "with issuing date adjustment set to align_with_finalization_date" do
125+
let(:subscription_invoice_issuing_date_adjustment) { "align_with_finalization_date" }
126+
127+
it "updates the issuing date" do
128+
freeze_time do
129+
current_date = Time.current.in_time_zone("America/New_York").to_date
130+
131+
expect { finalize_service.call }
132+
.to change { invoice.reload.issuing_date }.to(current_date)
133+
.and change { invoice.reload.payment_due_date }.to(current_date)
134+
end
135+
end
101136
end
102137
end
103138

0 commit comments

Comments
 (0)