Sales Invoice taking rate with margin randomly

In Sales Invoice sometime on submit rate_with_margin is setting automatically. This is not happening always. Only sometimes randomly. Why it is taking and how to resolve this?
Referring screenshot where values changed after submit