Hi have you tried doing this in Doc Events?
try making a python script that calculates and then put it here.
doc_events = {
"Doctype": {
"after_insert": "path.to.your.python.script.method"
},
"Another Doctype": {
"after_insert": "path.to.your.other.python.script.method"
}
}