We need to have each payment record followed by the invoices to which it was applied. Something like this:
P_Payment
R-Remittance
C-Control
P|CHK|PayerName.Payables|123|1000.50|7654|NW Forest Supplies|123 Broadway|New York|NY|10019
R|123|INV501|20190522|100.00|110.00|-10.00|#ACC3212|Payment for Office Supplies|PO789
R|123|INV608|20190513|800.00|800.00|0|||
R|123|INV712|20190524|100.50|100.50|0||| P|ACH|PayerName.Payables|124|55.00|7600|Dynamic Data LLC||||
R|124|INV551|20190525|55.00|55.00|0|||
P|CRD|PayerName.Payables|456|1000.50|7654|Company Widgets|||| R|456|INV501|20191008|100.00|110.00|-10.00|#ACC3212|Payment for widgets|PO789|||
R|456|INV608|20191013|800.00|800.00|0|||
R|456|INV712|20191024|100.50|100.50|0|||
C|2056.00|3|7