Skip to content

Commit

Permalink
Fbr 412 fix cheque reassignment (#282)
Browse files Browse the repository at this point in the history
* Bug/FBR-412: Fix Cheque Reassignment Issues

* Fix reassignment of cheques

---------

Co-authored-by: Julius Peter Oketayot <[email protected]>
  • Loading branch information
fiter-julius-oketayot and Julius Peter Oketayot authored Nov 17, 2023
1 parent d10489b commit 784653b
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,7 @@ public Collection<AgencyData> retrieveByOfficeHierarchy(final String hierarchy)
m_office mo
INNER JOIN m_office office_under ON
office_under.hierarchy LIKE CONCAT(mo.hierarchy, '%')AND office_under.hierarchy LIKE CONCAT(?, '%')
INNER JOIN m_appuser agency_responsible_user ON agency_responsible_user.office_id = office_under.id
INNER JOIN m_office agency_responsible_user_office ON agency_responsible_user_office.id = agency_responsible_user.office_id
INNER JOIN m_agency ma ON ma.responsible_user_id = agency_responsible_user.id
INNER JOIN m_agency ma ON ma.office_id = office_under.id
GROUP BY ma.id
""";
return this.jdbcTemplate.query(sql, (rs, rowNum) -> AgencyData.instance(rs.getLong("id"), rs.getString("name")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -248,6 +250,9 @@ public CommandProcessingResult authorizedChequeVoidance(final Long chequeId, Jso
cheque.setVoidAuthorizedDate(localDate);
cheque.setVoidAuthorizedBy(currentUser);
cheque.setStatus(BankChequeStatus.VOIDED.getValue());
final Collection<Loan> loans = this.loanRepositoryWrapper.findLoanByChequeId(chequeId);
loans.forEach(l -> l.setCheque(null));
this.loanRepositoryWrapper.save(new ArrayList<>(loans));
chequeJpaRepository.saveAndFlush(cheque);
return new CommandProcessingResultBuilder().withCommandId(command.commandId())
.withResourceIdAsString(String.valueOf(command.entityId())).withEntityId(command.entityId()).build();
Expand Down Expand Up @@ -361,6 +366,14 @@ public CommandProcessingResult printCheques(JsonCommand command) {
BigDecimal chequeAmount = chequeData.getGuaranteeAmount();
final Long loanAccId = chequeData.getLoanAccId();
if (loanAccId != null && chequeData.getLoanAmount() != null) {
final Loan loan = this.loanRepositoryWrapper.findOneWithNotFoundDetection(loanAccId);
if (loan.getCheque() != null) {
throw new BankChequeException("print.cheques", "Loan: " + loan.getAccountNumber() + " has a cheque assigned already");
}
if (!loan.isPendingDisbursementAuthorization()) {
throw new BankChequeException("print.cheques",
"Loan: " + loan.getAccountNumber() + " is.not.in.disbursement.authorization.status");
}
CommandProcessingResult result = this.loanWritePlatformService.disburseLoan(loanAccId, command, false);
if (result.getLoanId() == null) {
throw new BankChequeException("print.cheques", "failed.to.disburse.loan " + loanAccId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,22 +515,14 @@ public Collection<CenterData> retrieveAllForDropdown(final Long officeId) {
public Collection<CenterData> retrieveByOfficeHierarchy(final String hierarchy, final Long agencyId) {
String sql = """
SELECT
center.id AS id,
center.display_name AS name
FROM
m_office mo
INNER JOIN m_office office_under ON office_under.hierarchy LIKE CONCAT(mo.hierarchy, '%')AND office_under.hierarchy LIKE CONCAT(?, '%')
INNER JOIN m_appuser agency_responsible_user ON agency_responsible_user.office_id = office_under.id
INNER JOIN m_office agency_responsible_user_office ON agency_responsible_user_office.id = agency_responsible_user.office_id
INNER JOIN m_agency ma ON ma.responsible_user_id = agency_responsible_user.id
INNER JOIN m_office agency_office_under ON agency_office_under.hierarchy LIKE CONCAT(agency_responsible_user_office.hierarchy, '%')
INNER JOIN m_appuser supervison_responsible_user ON agency_responsible_user.office_id = agency_office_under.id
INNER JOIN m_office supervision_responsible_user_office ON supervision_responsible_user_office.id = supervison_responsible_user.office_id
INNER JOIN m_supervision ms ON ms.responsible_user_id = supervison_responsible_user.id
INNER JOIN m_office supervison_office_under ON supervison_office_under.hierarchy LIKE CONCAT(supervision_responsible_user_office.hierarchy, '%')
INNER JOIN m_appuser portfolio_responsible_user ON portfolio_responsible_user.office_id = supervison_office_under.id
INNER JOIN m_portfolio mp ON mp.responsible_user_id = portfolio_responsible_user.id
INNER JOIN m_group center ON center.portfolio_id = mp.id
center.id AS id,
center.display_name AS name
FROM m_office mo
INNER JOIN m_office office_under ON (office_under.hierarchy LIKE CONCAT(mo.hierarchy, '%') AND office_under.hierarchy LIKE CONCAT(?, '%'))
INNER JOIN m_group center ON center.office_id = office_under.id
LEFT JOIN m_portfolio mp ON mp.id = center.portfolio_id
LEFT JOIN m_supervision ms ON ms.id = mp.supervision_id
LEFT JOIN m_agency ma ON ma.id = ms.agency_id
WHERE center.parent_id IS NULL AND center.level_id = 1
""";
List<Object> params = new ArrayList<>(List.of(hierarchy));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3991,6 +3991,10 @@ public boolean isApproved() {
return status().isApproved();
}

public boolean isPendingDisbursementAuthorization() {
return status().isPendingDisbursementAuthorization();
}

private boolean isNotDisbursed() {
return !isDisbursed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ List<Loan> findByClientIdAndGroupIdAndLoanStatus(@Param("clientId") Long clientI
@Query("select loan from Loan loan where loan.client.id = :clientId")
List<Loan> findLoanByClientId(@Param("clientId") Long clientId);

@Query("select loan from Loan loan where loan.cheque.id = :chequeId")
List<Loan> findLoanByChequeId(@Param("chequeId") Long chequeId);

@Query("select loan from Loan loan where loan.group.id = :groupId and loan.client.id is null")
List<Loan> findByGroupId(@Param("groupId") Long groupId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public Collection<Loan> findActiveLoansByLoanIdAndGroupId(Long clientId, Long gr
return loans;
}

public Collection<Loan> findLoanByChequeId(Long chequeId) {
return this.repository.findLoanByChequeId(chequeId);
}

public Loan saveAndFlush(final Loan loan) {
return this.repository.saveAndFlush(loan);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public boolean isApproved() {
return this.value.equals(LoanStatus.APPROVED.getValue()) || this.value.equals(LoanStatus.DISBURSE_AUTHORIZATION_PENDING.getValue());
}

public boolean isPendingDisbursementAuthorization() {
return this.value.equals(LoanStatus.DISBURSE_AUTHORIZATION_PENDING.getValue());
}

public boolean isActive() {
return this.value.equals(LoanStatus.ACTIVE.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,11 @@ public CommandProcessingResult undoLoanDisbursal(final Long loanId, final JsonCo
Cheque cheque = loan.getCheque();
cheque.setStatus(BankChequeStatus.VOIDED.getValue());
cheque.stampAudit(userId, localDateTime);
cheque.setDescription("Voided by undoing loan disbursal");
cheque.setVoidedBy(currentUser);
cheque.setVoidedDate(localDate);
this.chequeJpaRepository.saveAndFlush(cheque);
loan.setCheque(null);
}
saveAndFlushLoanWithDataIntegrityViolationChecks(loan);
this.accountTransfersWritePlatformService.reverseAllTransactions(loanId, PortfolioAccountType.LOAN);
Expand Down

0 comments on commit 784653b

Please sign in to comment.