Skip to content

Commit 37dbd12

Browse files
committed
FINERACT-2181: Refactor trial balance processing to reduce cognitive complexity
1 parent 6204cde commit 37dbd12

File tree

1 file changed

+74
-43
lines changed

1 file changed

+74
-43
lines changed

fineract-accounting/src/main/java/org/apache/fineract/accounting/glaccount/jobs/updatetrialbalancedetails/UpdateTrialBalanceDetailsTasklet.java

Lines changed: 74 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -45,55 +45,86 @@ public class UpdateTrialBalanceDetailsTasklet implements Tasklet {
4545
@Override
4646
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
4747
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSourceServiceFactory.determineDataSourceService().retrieveDataSource());
48-
final StringBuilder tbGapSqlBuilder = new StringBuilder(500);
49-
tbGapSqlBuilder.append("select distinct(je.transaction_date) ").append("from acc_gl_journal_entry je ")
50-
.append("where je.transaction_date > (select coalesce(MAX(created_date),'2010-01-01') from m_trial_balance)");
51-
final List<LocalDate> tbGaps = jdbcTemplate.queryForList(tbGapSqlBuilder.toString(), LocalDate.class);
48+
49+
processTrialBalanceGaps(jdbcTemplate);
50+
updateClosingBalances(jdbcTemplate);
51+
52+
return RepeatStatus.FINISHED;
53+
}
54+
55+
private void processTrialBalanceGaps(JdbcTemplate jdbcTemplate) {
56+
final String tbGapSql = "select distinct(je.transaction_date) from acc_gl_journal_entry je "
57+
+ "where je.transaction_date > (select coalesce(MAX(created_date),'2010-01-01') from m_trial_balance)";
58+
final List<LocalDate> tbGaps = jdbcTemplate.queryForList(tbGapSql, LocalDate.class);
59+
5260
for (LocalDate tbGap : tbGaps) {
53-
int days = DateUtils.getExactDifferenceInDays(tbGap, DateUtils.getBusinessLocalDate());
54-
if (days < 1) {
61+
if (DateUtils.getExactDifferenceInDays(tbGap, DateUtils.getBusinessLocalDate()) < 1) {
5562
continue;
5663
}
57-
final StringBuilder sqlBuilder = new StringBuilder(600);
58-
sqlBuilder.append("Insert Into m_trial_balance(office_id, account_id, Amount, entry_date, created_date,closing_balance) ")
59-
.append("Select je.office_id, je.account_id, SUM(CASE WHEN je.type_enum=1 THEN (-1) * je.amount ELSE je.amount END) ")
60-
.append("as Amount, Date(je.entry_date) as Entry_Date, je.transaction_date as Created_Date,sum(je.amount) as closing_balance ")
61-
.append("from acc_gl_journal_entry je WHERE je.transaction_date = ? ")
62-
.append("group by je.account_id, je.office_id, je.transaction_date, Date(je.entry_date)");
63-
final int result = jdbcTemplate.update(sqlBuilder.toString(), tbGap);
64-
log.debug("{}: Records affected by updateTrialBalanceDetails: {}", ThreadLocalContextUtil.getTenant().getName(), result);
64+
insertTrialBalanceForDate(jdbcTemplate, tbGap);
6565
}
66-
String distinctOfficeQuery = "select distinct(office_id) from m_trial_balance where closing_balance is null group by office_id";
67-
final List<Long> officeIds = jdbcTemplate.queryForList(distinctOfficeQuery, Long.class);
66+
}
67+
68+
private void insertTrialBalanceForDate(JdbcTemplate jdbcTemplate, LocalDate tbGap) {
69+
final String sql = "Insert Into m_trial_balance(office_id, account_id, Amount, entry_date, created_date,closing_balance) "
70+
+ "Select je.office_id, je.account_id, SUM(CASE WHEN je.type_enum=1 THEN (-1) * je.amount ELSE je.amount END) "
71+
+ "as Amount, Date(je.entry_date) as Entry_Date, je.transaction_date as Created_Date,sum(je.amount) as closing_balance "
72+
+ "from acc_gl_journal_entry je WHERE je.transaction_date = ? "
73+
+ "group by je.account_id, je.office_id, je.transaction_date, Date(je.entry_date)";
74+
75+
final int result = jdbcTemplate.update(sql, tbGap);
76+
log.debug("{}: Records affected by updateTrialBalanceDetails: {}", ThreadLocalContextUtil.getTenant().getName(), result);
77+
}
78+
79+
private void updateClosingBalances(JdbcTemplate jdbcTemplate) {
80+
final List<Long> officeIds = getDistinctOfficeIds(jdbcTemplate);
81+
6882
for (Long officeId : officeIds) {
69-
String distinctAccountQuery = "select distinct(account_id) from m_trial_balance where office_id=? and closing_balance is null group by account_id";
70-
final List<Long> accountIds = jdbcTemplate.queryForList(distinctAccountQuery, Long.class, officeId);
71-
for (Long accountId : accountIds) {
72-
final String closingBalanceQuery = "select closing_balance from m_trial_balance where office_id=? and account_id=? and closing_balance "
73-
+ "is not null order by created_date desc, entry_date desc limit 1";
74-
List<BigDecimal> closingBalanceData = jdbcTemplate.queryForList(closingBalanceQuery, BigDecimal.class, officeId, accountId);
75-
List<TrialBalance> tbRows = trialBalanceRepositoryWrapper.findNewByOfficeAndAccount(officeId, accountId);
76-
BigDecimal closingBalance = null;
77-
if (!CollectionUtils.isEmpty(closingBalanceData)) {
78-
closingBalance = closingBalanceData.get(0);
79-
}
80-
if (CollectionUtils.isEmpty(closingBalanceData)) {
81-
closingBalance = BigDecimal.ZERO;
82-
for (TrialBalance row : tbRows) {
83-
closingBalance = closingBalance.add(row.getAmount());
84-
row.setClosingBalance(closingBalance);
85-
}
86-
} else {
87-
for (TrialBalance tbRow : tbRows) {
88-
if (closingBalance != null) {
89-
closingBalance = closingBalance.add(tbRow.getAmount());
90-
}
91-
tbRow.setClosingBalance(closingBalance);
92-
}
93-
}
94-
trialBalanceRepositoryWrapper.save(tbRows);
83+
updateClosingBalancesForOffice(jdbcTemplate, officeId);
84+
}
85+
}
86+
87+
private List<Long> getDistinctOfficeIds(JdbcTemplate jdbcTemplate) {
88+
String distinctOfficeQuery = "select distinct(office_id) from m_trial_balance where closing_balance is null group by office_id";
89+
return jdbcTemplate.queryForList(distinctOfficeQuery, Long.class);
90+
}
91+
92+
private void updateClosingBalancesForOffice(JdbcTemplate jdbcTemplate, Long officeId) {
93+
final List<Long> accountIds = getDistinctAccountIds(jdbcTemplate, officeId);
94+
95+
for (Long accountId : accountIds) {
96+
updateClosingBalanceForAccount(jdbcTemplate, officeId, accountId);
97+
}
98+
}
99+
100+
private List<Long> getDistinctAccountIds(JdbcTemplate jdbcTemplate, Long officeId) {
101+
String distinctAccountQuery = "select distinct(account_id) from m_trial_balance where office_id=? and closing_balance is null group by account_id";
102+
return jdbcTemplate.queryForList(distinctAccountQuery, Long.class, officeId);
103+
}
104+
105+
private void updateClosingBalanceForAccount(JdbcTemplate jdbcTemplate, Long officeId, Long accountId) {
106+
BigDecimal closingBalance = getPreviousClosingBalance(jdbcTemplate, officeId, accountId);
107+
List<TrialBalance> tbRows = trialBalanceRepositoryWrapper.findNewByOfficeAndAccount(officeId, accountId);
108+
109+
updateTrialBalanceRows(tbRows, closingBalance);
110+
}
111+
112+
private BigDecimal getPreviousClosingBalance(JdbcTemplate jdbcTemplate, Long officeId, Long accountId) {
113+
final String closingBalanceQuery = "select closing_balance from m_trial_balance where office_id=? and account_id=? and closing_balance "
114+
+ "is not null order by created_date desc, entry_date desc limit 1";
115+
List<BigDecimal> closingBalanceData = jdbcTemplate.queryForList(closingBalanceQuery, BigDecimal.class, officeId, accountId);
116+
117+
return CollectionUtils.isEmpty(closingBalanceData) ? BigDecimal.ZERO : closingBalanceData.get(0);
118+
}
119+
120+
private void updateTrialBalanceRows(List<TrialBalance> tbRows, BigDecimal initialClosingBalance) {
121+
BigDecimal closingBalance = initialClosingBalance;
122+
123+
for (TrialBalance row : tbRows) {
124+
if (closingBalance != null) {
125+
closingBalance = closingBalance.add(row.getAmount());
95126
}
127+
row.setClosingBalance(closingBalance);
96128
}
97-
return RepeatStatus.FINISHED;
98129
}
99130
}

0 commit comments

Comments
 (0)