Compute the next monthly rent charge date

Angular · Angular · advanced · greenfield

Adds the rent-schedule service used by the upcoming-charges panel. `nextChargeDate` decides whether the anchor charge for the last-charged month is already behind (then moves to the following month) and clamps the anchor day into whatever month it lands in, so short months charge on their last day. Walked a 15th-anchor lease across several months and the dates line up.

Property-management SaaS; the charge date drives when tenants' cards are debited.

Requirements

Files touched

--- src/app/billing/rent-schedule.service.ts
+import { Injectable } from '@angular/core';
+
+function daysInMonth(year: number, month: number): number {
+  return new Date(year, month + 1, 0).getDate();
+}
+
+@Injectable({ providedIn: 'root' })
+export class RentScheduleService {
+  /**
+   * The charge date following `lastCharge` for a lease anchored on
+   * `anchorDay` (1-31), clamped to month length in short months.
+   */
+  nextChargeDate(lastCharge: Date, anchorDay: number): Date {
+    let year = lastCharge.getFullYear();
+    let month = lastCharge.getMonth();
+    if (lastCharge.getDate() >= anchorDay) {
+      // This month's charge is already behind us — move to the next month.
+      month += 1;
+      if (month > 11) {
+        month = 0;
+        year += 1;
+      }
+    }
+    const day = Math.min(anchorDay, daysInMonth(year, month));
+    return new Date(year, month, day);
+  }
+}
+

Review this PR

Angular practice