Problem Calculations in Microflow

Hi all, I'm quite new to Mendix so I hope you can easily help me with the following problem: I am building in application in which a group of people can keep track of the payments they do for each other. So for example: Person A pays a dinner for person B for €20,00. Person A now has a balance of +20 and Person B a balance of -20. This part I have working correctly and I created an overview page with the balances of all users in the app. Form here i want to create a datagrid (or something else) that would show, to who every user has to pay to get his balance to zero, in the least amount of transactions. To do this I have created 3 entities: User (Name & CurrentBalance), Payment (amount, description), report (giver, receiver, amount). The last one is currently not used and was created for the problem described above.   I'm a little stuck here so I hope you can help me on my way!  
1 answers

Do until done:

  • Check if any two-person's balance match, one negative, one positive and make the first transaction by making the debtor paying the creditor.
  • Then sort the person from lowest to highest balance and make one transaction where the lowest balance person pays the highest person an amount which sets one of their balances on 0.

This will result in the least amount of transactions.