How to go about this... Let's extract a couple of methods, so we can better see the logic.
private void a() {
entry2.setDebit(adjustment.total);
entry2.setCredit(0d);
}
private void b() {
entry2.setCredit(adjustment.total);
entry2.setDebit(0d);
}
if (adjustment.adjustmentAccount.isIncrease) {
if (adjustment.increaseVATLine) {
if (adjustment.vatItem.isSalesType) {
a();
} else {
b();
}
} else {
if (adjustment.vatItem.isSalesType) {
b();
} else {
a();
}
}
} else {
if (adjustment.increaseVATLine) {
if (adjustment.vatItem.isSalesType) {
b();
} else {
a();
}
} else {
if (adjustment.vatItem.isSalesType) {
a();
} else {
b();
}
}
So now, looking at it, that first block
if (adjustment.increaseVATLine) {
if (adjustment.vatItem.isSalesType) {
a();
} else {
b();
}
} else {
if (adjustment.vatItem.isSalesType) {
b();
} else {
a();
}
}
just amounts to doing a()
if adjustment.increaseVATLine
has the same value as adjustment.vatItem.isSalesType
, b()
otherwise. So we can reduce it:
if (adjustment.adjustmentAccount.isIncrease) {
if (adjustment.increaseVATLine == adjustment.vatItem.isSalesType) {
a();
} else {
b();
}
} else {
if (adjustment.increaseVATLine) {
if (adjustment.vatItem.isSalesType) {
b();
} else {
a();
}
} else {
if (adjustment.vatItem.isSalesType) {
a();
} else {
b();
}
}
}
And the remaining block is the same, just reversing a()
and b()
:
if (adjustment.adjustmentAccount.isIncrease) {
if (adjustment.increaseVATLine == adjustment.vatItem.isSalesType) {
a();
} else {
b();
}
} else {
if (adjustment.increaseVATLine == adjustment.vatItem.isSalesType) {
b();
} else {
a();
}
}
So we begin to see the logic. If it's an increase, and the increaseVATLine matches the isSalesType, then we debit, otherwise credit, but if it's a decrease, then we credit only if they don't match. What's a good way of expressing this? Well, for one, name a() and b() smarter - now that we can see what they're doing
if (adjustment.adjustmentAccount.isIncrease) {
if (adjustment.increaseVATLine == adjustment.vatItem.isSalesType) {
debitEntry();
} else {
creditEntry();
}
} else {
if (adjustment.increaseVATLine == adjustment.vatItem.isSalesType) {
creditEntry();
} else {
debitEntry();
}
}
And now it's a little clearer still. Debit the account when it's an increase account and an increase VAT line, and a sales type, or when it's a decrease and either it's a decrease VAT line OR it's a sales type, but not both. Does this truth table help? First column is adjustmentAmount.isIncrease
; second is adjustment.increaseVATLine
; third is adjustment.vatItem.isSalesType
. Fourth column is D for debit, C for credit; in parentheses are the number of TRUE values among the flags.
TTT -> D (3)
TFF -> D (1)
TTF -> C (2)
TFT -> C (2)
FTT -> C (2)
FFF -> C (0)
FTF -> D (1)
FFT -> D (1)
Now you can see why @Xavier Ho's solution works; the odd totals are all debits, the even ones all credits.
This is just one exploratory path; I hope it's helpful.