this is an old problem, very important, never fixed.
I asked for a help on stackoverflow and github, but without results…
There are others topics about this issue.
The discount should be calculated before all taxes, but it’s applied after taxes. A total discount colculated after the total tax it’s useless! You can read here for more informations.
I hope that somebody can fix this bug, it’s no difficult to do.