Invoice Service
The Invoice service is responsible for generating the recurring invoices for a lease as well as exposing an API call to create Ad-Hoc once off invoices. It contains business logic for various scenarios
- When to generate the recurring invoices
- What status to put the invoice in
- What inoice number is to be assigned to the invoice
Listeners
There are 3 Listeners.
- Party Listener handles the
PartyCreated
andPartyUpdated
events by inserting parties directly into the database. - Property Listener handles the
PropertyCreated
event and inserts directly into the database - Lease Listener listens to all events that takes place on a lease, slowly building up all the invoice template rules as well as ad hoc invoice it will need to generate once the lease is active.
Invoice workflow from the listener and UI.
NB Our code refers to the lease as a portfolio currently, they are synonymous.
sequenceDiagram
participant A as UI
participant L as Listener
participant E as Entity
participant R as Readside
participant C as Connector
participant T as Topic
note over A,T: PortfolioOpened
L->>C: Insert draft portfolio
note over A,T: Lease Terms Added
L->>C: Insert draft portfolio
note over A,T: Invoice Template Added
L->>C: Insert invoice template
note over A,T: Invoice Template Updated
L->>C: Upsert invoice template
note over A,T: PortfolioApproved
L->>C: Update portfolio status
L->>+C: Fetch invoice templates for portfolio
C-->>-L: Seq(InvoiceTemplates)
loop Every Invoice Template
L->>+E: CreateInvoice
E->>-L: DONE
E-->>R: InvoiceCreated
E-->>R: InvoiceTemplateGenerated
R->>C: Insert Invoice
R->>C: Insert Invoice Template
end
note over A,T: User creates a once off invoice
alt Create Invoice
A->>+E: CreateInvoice
E->>-L: DONE
E-->>R: InvoiceCreated
R->>C: Insert Invoice
end
note over A,T: User issues an invoice
alt Open Invoice
A->>+E: Open Invoice
E-->>E: Reserve Invoice No
E->>-A: Done
E-->>R: InvoiceOpened
R->>C: Update invoice status
R->>C: Update invoice number
E-->>T: Emit InvoiceOpened event
end
Schedulers
There are 2 primary schedulers.
- Invoice Generator: Generates invoices 15 days before due dates
- Invoice Opener: Automatically delivers invoices 7 days before due date
sequenceDiagram
participant S as Scheduler
participant E as Entity
participant R as Readside
participant C as Connector
participant T as Topic
note over S,T: Invoice Generator runs every 4 hours
S->>+C: Fetch approved portfolios
C->>-S: Seq(ApprovedPortfolios)
S->>+C: Fetch recurring invoice templates for portfolio
C->>-S: Seq(InvoiceTemplate)
S-->>S: Filter invoices we should open
loop Every Invoice Template
S->>+E: CreateInvoice
E->>-S: DONE
E-->>R: InvoiceCreated
E-->>R: InvoiceTemplateGenerated
R->>C: Insert Invoice
R->>C: Insert Invoice Template
end
note over S,T: Invoice Opener Scheduler
S->>+C: Draft invoices where due date < 7 days
C->>-S: Seq(Invoice)
loop Every Invoice
S->>+E: Open Invoice
E-->>E: Reserve Invoice No
E->>-S: Done
E-->>R: InvoiceOpened
R->>C: Update invoice status
R->>C: Update invoice number
E-->>T: Emit InvoiceOpened event
end