How to add custom field calculation?

Example :
I added two custom fields in the purchase invoice item table fat and snf.

if I add 10 qty of milk. that milk fat is 3.2 and snf value is 7.6 note this is 1 qty of fat and snf. now I want to automatically change the rate and amount value. rate value is 24.24 and the amount value is 242.4. The field name is “qty”, “snf”, “fat”, “rate”, and “amount” .

how to write a client script?

If the fat value is 3.3 and snf value is 7.7. now rate value is 24.54. so that is i want matrix

Please give a proper calculation for fat and snf to write a better code. I cant understand your calculation right now.