I'll try to explain this, that for me is very complicated way to register, because it is simple when you have 1 invoice = 1 payment but, when you have 1 invoice, several payments and/or several credit notes, it is a madness.
1. Simple case. 1 invoice = 1 payment or credit note
You have to look into Cust__Ledger_Entry for Customer movements, I did this in two steps, first to identify one payment, and that payment which invoice was closed by.
| Document type |
Entry No. |
Closed by Entry No. |
Amonunt |
| Payment |
1 |
2 |
1,000 |
| Invoice |
2 |
1 |
1,000 |
This is the easiest case.
2. Mix case 1 invoice = several payments and/or credit notes
You have to look into Cust__Ledger_Entry for Customer movements, I did this in two steps, first to identify one payment, and that payment which invoice was closed by, then look for another payment(s) or credit note (s)
| Document type |
Entry No. |
Closed by Entry No. |
Amonunt |
Invoice paid |
| Payment |
1 |
2 |
-500 |
2 |
| Invoice |
2 |
1 |
1,000 |
2 |
| Credit Note |
3 |
2 |
-500 |
2 |
What I did after I was able to identify proper relationships was to put right after each movement wich invoice has been paid, this way I could identify all documents related to an invoice, also with this data I was able to sort by customer and invoices with its payments.
My natural thinking was to start by invoice, but right after I started with this approach I realized it was very difficult when I had several related documents either payments or credit notes.
I will be happy to share my code for this if you want it. I have been so lonely and with bad service from my partner that I don't want anyone else going through the same. Besides no one wants to explain or is not documented at all.
Hope this can help you.