corporate: Fix next_invoice_date not updated on end_date extension.

Earlier, on extending end_date for legacy plans, next_invoice_date
was not extended which resulted in invoice_plan() run for such
legacy plans before the end_date.

The intended behaviour is that the legacy plan should be invoiced
only once on the end_date to downgrade or switch to a new tier.

This commit adds logic to update next_invoice_date when end_date
is extended via /support.
This commit is contained in:
Prakhar Pratyush 2024-02-16 15:45:08 +05:30 committed by Tim Abbott
parent d2bc94a724
commit 9648256c3c
2 changed files with 27 additions and 5 deletions

View File

@ -1444,7 +1444,12 @@ class BillingSession(ABC):
assert plan.status == CustomerPlan.ACTIVE
old_end_date = plan.end_date
plan.end_date = new_end_date
plan.save(update_fields=["end_date"])
old_next_invoice_date = plan.next_invoice_date
# Legacy plans should be invoiced once on the end_date to
# downgrade or switch to a new tier.
if plan.tier == CustomerPlan.TIER_SELF_HOSTED_LEGACY:
plan.next_invoice_date = new_end_date
plan.save(update_fields=["end_date", "next_invoice_date"])
self.write_to_audit_log(
event_type=AuditLogEventType.CUSTOMER_PLAN_PROPERTY_CHANGED,
event_time=timezone_now(),
@ -1455,6 +1460,17 @@ class BillingSession(ABC):
"property": "end_date",
},
)
if old_next_invoice_date != plan.next_invoice_date:
self.write_to_audit_log(
event_type=AuditLogEventType.CUSTOMER_PLAN_PROPERTY_CHANGED,
event_time=timezone_now(),
extra_data={
"old_value": old_next_invoice_date,
"new_value": new_end_date,
"plan_id": plan.id,
"property": "next_invoice_date",
},
)
return f"Current plan for {self.billing_entity_display_name} updated to end on {end_date_string}."
raise SupportRequestError(
f"No current plan for {self.billing_entity_display_name}."

View File

@ -353,6 +353,7 @@ class TestRemoteServerSupportEndpoint(ZulipTestCase):
assert plan is not None
self.assertEqual(plan.status, CustomerPlan.ACTIVE)
self.assertEqual(plan.end_date, datetime(2050, 2, 1, tzinfo=timezone.utc))
self.assertEqual(plan.next_invoice_date, datetime(2050, 2, 1, tzinfo=timezone.utc))
cordelia = self.example_user("cordelia")
self.login_user(cordelia)
@ -375,17 +376,22 @@ class TestRemoteServerSupportEndpoint(ZulipTestCase):
)
plan.refresh_from_db()
self.assertEqual(plan.end_date, datetime(2040, 1, 1, tzinfo=timezone.utc))
audit_log = RemoteRealmAuditLog.objects.filter(
self.assertEqual(plan.next_invoice_date, datetime(2040, 1, 1, tzinfo=timezone.utc))
audit_logs = RemoteRealmAuditLog.objects.filter(
event_type=RemoteRealmAuditLog.CUSTOMER_PLAN_PROPERTY_CHANGED
).last()
assert audit_log is not None
).order_by("-id")
assert audit_logs.exists()
next_invoice_date_changed_audit_log = audit_logs[0]
end_date_changed_audit_log = audit_logs[1]
expected_extra_data = {
"old_value": "2050-02-01T00:00:00Z",
"new_value": "2040-01-01T00:00:00Z",
"property": "end_date",
"plan_id": plan.id,
}
self.assertEqual(audit_log.extra_data, expected_extra_data)
self.assertEqual(end_date_changed_audit_log.extra_data, expected_extra_data)
expected_extra_data["property"] = "next_invoice_date"
self.assertEqual(next_invoice_date_changed_audit_log.extra_data, expected_extra_data)
result = self.client_post(
"/activity/remote/support",