How I implemented recurring features in Trevidia
Source: Dev.to
Recurring Invoices in Trevidia
- Set an invoice to recur daily, weekly, bi‑weekly, monthly, quarterly, or yearly.
- Automatically calculate the next invoice date.
- Keep invoices linked, allowing users to track payment history over time.
- Edit or stop a recurring invoice at any point.
- No hacks. No duplicate clutter. One source of truth.
Each recurring invoice is linked to the original invoice data, so the recurrence frequency and the next recurring date are derived from that single source.
Implementation Details
I created a Laravel schedule that runs a job called ProcessRecurringInvoices daily. The job:
- Scans the database for invoices whose next recurring date matches the current day.
- Duplicates the relevant values from the original invoice.
- Increments the invoice number based on the previous invoice number.
- Sends the new invoice to the email address used for the original invoice, provided the recipient has not unsubscribed from email notifications.
This approach ensures that recurring invoices are generated automatically without manual duplication, maintaining a clean and consistent data structure.