Set-off
You are really interested in Netting.
At its simplest, a right of set-off exists where there are cross-claims for money between a creditor and a debtor. The effect of a set-off is that both claims are discharged to the extent that they are of an equal amount, and the balance becomes owing to the party who was owed the larger amount.
But, really, it's all about netting. Go on. Be on your way now.