Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,55 +45,86 @@ public class UpdateTrialBalanceDetailsTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSourceServiceFactory.determineDataSourceService().retrieveDataSource());
final StringBuilder tbGapSqlBuilder = new StringBuilder(500);
tbGapSqlBuilder.append("select distinct(je.transaction_date) ").append("from acc_gl_journal_entry je ")
.append("where je.transaction_date > (select coalesce(MAX(created_date),'2010-01-01') from m_trial_balance)");
final List<LocalDate> tbGaps = jdbcTemplate.queryForList(tbGapSqlBuilder.toString(), LocalDate.class);

processTrialBalanceGaps(jdbcTemplate);
updateClosingBalances(jdbcTemplate);

return RepeatStatus.FINISHED;
}

private void processTrialBalanceGaps(JdbcTemplate jdbcTemplate) {
final String tbGapSql = "select distinct(je.transaction_date) from acc_gl_journal_entry je "
+ "where je.transaction_date > (select coalesce(MAX(created_date),'2010-01-01') from m_trial_balance)";
final List<LocalDate> tbGaps = jdbcTemplate.queryForList(tbGapSql, LocalDate.class);

for (LocalDate tbGap : tbGaps) {
int days = DateUtils.getExactDifferenceInDays(tbGap, DateUtils.getBusinessLocalDate());
if (days < 1) {
if (DateUtils.getExactDifferenceInDays(tbGap, DateUtils.getBusinessLocalDate()) < 1) {
continue;
}
final StringBuilder sqlBuilder = new StringBuilder(600);
sqlBuilder.append("Insert Into m_trial_balance(office_id, account_id, Amount, entry_date, created_date,closing_balance) ")
.append("Select je.office_id, je.account_id, SUM(CASE WHEN je.type_enum=1 THEN (-1) * je.amount ELSE je.amount END) ")
.append("as Amount, Date(je.entry_date) as Entry_Date, je.transaction_date as Created_Date,sum(je.amount) as closing_balance ")
.append("from acc_gl_journal_entry je WHERE je.transaction_date = ? ")
.append("group by je.account_id, je.office_id, je.transaction_date, Date(je.entry_date)");
final int result = jdbcTemplate.update(sqlBuilder.toString(), tbGap);
log.debug("{}: Records affected by updateTrialBalanceDetails: {}", ThreadLocalContextUtil.getTenant().getName(), result);
insertTrialBalanceForDate(jdbcTemplate, tbGap);
}
String distinctOfficeQuery = "select distinct(office_id) from m_trial_balance where closing_balance is null group by office_id";
final List<Long> officeIds = jdbcTemplate.queryForList(distinctOfficeQuery, Long.class);
}

private void insertTrialBalanceForDate(JdbcTemplate jdbcTemplate, LocalDate tbGap) {
final String sql = "Insert Into m_trial_balance(office_id, account_id, Amount, entry_date, created_date,closing_balance) "
+ "Select je.office_id, je.account_id, SUM(CASE WHEN je.type_enum=1 THEN (-1) * je.amount ELSE je.amount END) "
+ "as Amount, Date(je.entry_date) as Entry_Date, je.transaction_date as Created_Date,sum(je.amount) as closing_balance "
+ "from acc_gl_journal_entry je WHERE je.transaction_date = ? "
+ "group by je.account_id, je.office_id, je.transaction_date, Date(je.entry_date)";

final int result = jdbcTemplate.update(sql, tbGap);
log.debug("{}: Records affected by updateTrialBalanceDetails: {}", ThreadLocalContextUtil.getTenant().getName(), result);
}

private void updateClosingBalances(JdbcTemplate jdbcTemplate) {
final List<Long> officeIds = getDistinctOfficeIds(jdbcTemplate);

for (Long officeId : officeIds) {
String distinctAccountQuery = "select distinct(account_id) from m_trial_balance where office_id=? and closing_balance is null group by account_id";
final List<Long> accountIds = jdbcTemplate.queryForList(distinctAccountQuery, Long.class, officeId);
for (Long accountId : accountIds) {
final String closingBalanceQuery = "select closing_balance from m_trial_balance where office_id=? and account_id=? and closing_balance "
+ "is not null order by created_date desc, entry_date desc limit 1";
List<BigDecimal> closingBalanceData = jdbcTemplate.queryForList(closingBalanceQuery, BigDecimal.class, officeId, accountId);
List<TrialBalance> tbRows = trialBalanceRepositoryWrapper.findNewByOfficeAndAccount(officeId, accountId);
BigDecimal closingBalance = null;
if (!CollectionUtils.isEmpty(closingBalanceData)) {
closingBalance = closingBalanceData.get(0);
}
if (CollectionUtils.isEmpty(closingBalanceData)) {
closingBalance = BigDecimal.ZERO;
for (TrialBalance row : tbRows) {
closingBalance = closingBalance.add(row.getAmount());
row.setClosingBalance(closingBalance);
}
} else {
for (TrialBalance tbRow : tbRows) {
if (closingBalance != null) {
closingBalance = closingBalance.add(tbRow.getAmount());
}
tbRow.setClosingBalance(closingBalance);
}
}
trialBalanceRepositoryWrapper.save(tbRows);
updateClosingBalancesForOffice(jdbcTemplate, officeId);
}
}

private List<Long> getDistinctOfficeIds(JdbcTemplate jdbcTemplate) {
String distinctOfficeQuery = "select distinct(office_id) from m_trial_balance where closing_balance is null group by office_id";
return jdbcTemplate.queryForList(distinctOfficeQuery, Long.class);
}

private void updateClosingBalancesForOffice(JdbcTemplate jdbcTemplate, Long officeId) {
final List<Long> accountIds = getDistinctAccountIds(jdbcTemplate, officeId);

for (Long accountId : accountIds) {
updateClosingBalanceForAccount(jdbcTemplate, officeId, accountId);
}
}

private List<Long> getDistinctAccountIds(JdbcTemplate jdbcTemplate, Long officeId) {
String distinctAccountQuery = "select distinct(account_id) from m_trial_balance where office_id=? and closing_balance is null group by account_id";
return jdbcTemplate.queryForList(distinctAccountQuery, Long.class, officeId);
}

private void updateClosingBalanceForAccount(JdbcTemplate jdbcTemplate, Long officeId, Long accountId) {
BigDecimal closingBalance = getPreviousClosingBalance(jdbcTemplate, officeId, accountId);
List<TrialBalance> tbRows = trialBalanceRepositoryWrapper.findNewByOfficeAndAccount(officeId, accountId);

updateTrialBalanceRows(tbRows, closingBalance);
}

private BigDecimal getPreviousClosingBalance(JdbcTemplate jdbcTemplate, Long officeId, Long accountId) {
final String closingBalanceQuery = "select closing_balance from m_trial_balance where office_id=? and account_id=? and closing_balance "
+ "is not null order by created_date desc, entry_date desc limit 1";
List<BigDecimal> closingBalanceData = jdbcTemplate.queryForList(closingBalanceQuery, BigDecimal.class, officeId, accountId);

return CollectionUtils.isEmpty(closingBalanceData) ? BigDecimal.ZERO : closingBalanceData.get(0);
}

private void updateTrialBalanceRows(List<TrialBalance> tbRows, BigDecimal initialClosingBalance) {
BigDecimal closingBalance = initialClosingBalance;

for (TrialBalance row : tbRows) {
if (closingBalance != null) {
closingBalance = closingBalance.add(row.getAmount());
}
row.setClosingBalance(closingBalance);
}
return RepeatStatus.FINISHED;
}
}