How to do it ... Let me extract a few methods to better understand 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 him, this first block
if (adjustment.increaseVATLine) { if (adjustment.vatItem.isSalesType) { a(); } else { b(); } } else { if (adjustment.vatItem.isSalesType) { b(); } else { a(); } }
just means executing a() if adjustment.increaseVATLine has the same meaning as adjustment.vatItem.isSalesType , b() otherwise. Therefore, 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 changing 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 are starting to see the logic. If this increase and increase of VATLine corresponds to isSalesType, then we are a debit, otherwise a loan, but if it is a decrease, then we credit only if they do not match. What a good way to express it? Well, for one, call a () and b () smarter - now that we can see what they do
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. Debit the account when it increases the account and increases the VAT line, as well as the type of sales or when it decreases, and either it decreases the VAT line, or it is the type of sales, but not both. Does this truth table help? First column adjustmentAmount.isIncrease ; the second is adjustment.increaseVATLine ; the third is adjustment.vatItem.isSalesType . The fourth column is D for debit, C for credit; in parentheses indicates 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 understand why @Xavier Ho's solution works; odd totals - all debit, even - all loans.
This is just one research path; Hope this will be helpful.