This is just code cleanup, no behavior is changing. It deduplicates getbalance code and makes it comprehsible so it is not doing bizarre things like subtracting negative fees (https://github.com/bitcoin/bitcoin/pull/9167).
For each wallet transaction, the balance is updated as follows:
+ CAmount debit = wtx.GetDebit(filter);
+ const bool outgoing = debit > 0;
+ for (const CTxOut& out : wtx.tx->vout) {
+ if (outgoing && IsChange(out)) {
+ debit -= out.nValue;
+ } else if (IsMine(out) & filter && depth >= minDepth && (!account || *account == GetAccountName(out.scriptPubKey))) {
+ balance += out.nValue;
+ }
+ }
+
+ if (outgoing && (!account || *account == wtx.strFromAccount)) {
+ balance -= debit;
+ }