How can I loop through the list of numbers (invoice and credit note values) to find all the combinations of invoices / credits that match a payment amount? I don't mind doing this in PHP, or directly in MySQL, whichever makes it most viable.
I started looking at this question but can't use python , otherwise this question would answer it : Find all combinations of a list of numbers with a given sum
See example data below (EDIT: sorry about the images at first. Is this OK now?)
invoice value
INV-1 1
INV-2 3
INV-3 1
INV-4 2
INV-5 4
INV-6 7
CRED-1 -1
CRED-2 -1
CRED-3 -1
CRED-4 -1
.
payment
5
.
possible combinations to match payment
invoice value set
INV-1 1 a
INV-2 3 a
INV-3 1 a
INV-2 3 b
INV-4 2 b
INV-1 1 c
INV-5 4 c
INV-4 2 d
INV-6 7 d
CRED-1 -1 d
CRED-2 -1 d
CRED-3 -1 d
CRED-4 -1 d