How to get and calculate data from a secondary table in real time?

I am new to customizing doctypes and I have a problem obtaining and calculating data in a secondary table.

What I want to achieve is that after selecting a tax template it fills the Isv% field with the tax_rate of the tax template and likewise after filling the Base Isv field perform a calculation in real time that fills the total field isv.
I know that this can be done through a custom script but I have few ideas of how to start doing it and make it work as I want as the secondary table of the items in the sales invoice does.
It will help me a lot if you show me examples of how to perform this query.