Issue #23254:fix gain/loss calculation

cloned https://github.com/xtuple/qt-client/pull/432 for the 4.7 line