There are many questions here, I will try to solve as many as I can. Many problems are a business model, not a data model.
Firstly, you are correct that you need an immutable invoice. After you create an invoice, you cannot change it; the standard way to handle invoice changes is to issue a credit note and a new invoice.
Think about the relationships between tables: Order does not need to hold lineItem , because they are referenced in a different way, i.e.
Order ===== orderId customerId status OrderLineItem ============= orderLineItemId orderId product price quantity
So, to view the order you attach to the tables on orderId. There is also no need to store total as calculated from the connection.
Try not to duplicate your data: your account will refer to orderLineItem s, but not necessarily the same in the order. For example, a customer orders A and B, but B is absent. You send A and create an invoice that refers to orderLineItemId for A. Thus, your invoice tables may look like this:
invoice ======= invoiceId status invoiceLineItem =============== invoiceId orderLineItemId quantity
In other words, there is no need to have item details. You might be wondering why you are not just adding invoiceId to the orderLineItem table - the reason is that the customer can order 10 items A, but you only send 8 of them, and the other two will go to the next account.
Payments are not made against orders, they are against billing. Therefore, your payment table should reference invoiceId.
Then you touch reconciliation. If everything is perfect, the client will make a payment against a certain account, even if a partial payment. In fact, this will be your biggest headache. Suppose a customer has several outstanding invoices for amounts x, y, and z. If they make payment p, to which do you distribute it? Perhaps in order of dates, so if p> x the remainder will be allocated against y. What if p = z? Perhaps the client intends to pay z now, but disputes y and lost x? How you deal with such things is up to you, but I can say that most billing systems do this very poorly.