Custom script to get customer balance on Sales Invoice

This link might help as well: