Skip to content

Commit

Permalink
Bug/SU-480: Fix bulk import Cupo validation (#1368)
Browse files Browse the repository at this point in the history
  • Loading branch information
fiter-julius-oketayot authored Dec 10, 2024
1 parent 77f06f7 commit 1325016
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.fineract.infrastructure.bulkimport.constants.ClientCupoDecrementConstants;
import org.apache.fineract.infrastructure.bulkimport.constants.ClientCupoIncrementConstants;
import org.apache.fineract.infrastructure.bulkimport.constants.TemplatePopulateImportConstants;
import org.apache.fineract.infrastructure.bulkimport.data.Count;
import org.apache.fineract.infrastructure.bulkimport.importhandler.ImportHandler;
Expand Down Expand Up @@ -202,14 +203,25 @@ private Count importEntity(final Workbook workbook, final List<ClientCupoDecreme
final String clientName = client.getClientName();
final EnumOptionData clientStatus = client.getStatus();
final BigDecimal previousMaximumCupoAmount = client.getCupo();
final BigDecimal totalOutstandingPrincipalAmount = client.getTotalOutstandingPrincipalAmount();
clientCupDecrementSheet.getRow(clientCupoDecrementData.getRowIndex())
.createCell(ClientCupoDecrementConstants.CLIENT_NAME_COL).setCellValue(clientName);
clientCupDecrementSheet.getRow(clientCupoDecrementData.getRowIndex())
.createCell(ClientCupoDecrementConstants.PREVIOUS_MAXIMUM_CUPO_AMOUNT_COL)
.setCellValue(previousMaximumCupoAmount.doubleValue());
if (maximumCupoAmount.compareTo(previousMaximumCupoAmount) > 0) {
if (maximumCupoAmount.compareTo(totalOutstandingPrincipalAmount) < 0) {
errorCount++;
errorMessage = "No puede modificarse ya que el nuevo cupo que sugiere es mayor al actual";
errorMessage = "No se puede modificar ya que la nueva cuota sugerida es menor al capital pendiente actual: "
+ totalOutstandingPrincipalAmount;
ImportHandlerUtils.writeErrorMessage(clientCupDecrementSheet, clientCupoDecrementData.getRowIndex(), errorMessage,
ClientCupoIncrementConstants.STATUS_COL);
clientCupDecrementSheet.setColumnWidth(ClientCupoIncrementConstants.STATUS_COL,
TemplatePopulateImportConstants.EXTRALARGE_COL_SIZE);
continue;
}
if (maximumCupoAmount.compareTo(previousMaximumCupoAmount) >= 0) {
errorCount++;
errorMessage = "No se puede modificar ya que la nueva cuota sugerida es mayor o igual a la actual.";
ImportHandlerUtils.writeErrorMessage(clientCupDecrementSheet, clientCupoDecrementData.getRowIndex(), errorMessage,
ClientCupoDecrementConstants.STATUS_COL);
clientCupDecrementSheet.setColumnWidth(ClientCupoDecrementConstants.STATUS_COL,
Expand Down Expand Up @@ -281,15 +293,17 @@ private Count importEntity(final Workbook workbook, final List<ClientCupoDecreme
} else {
sql = "UPDATE campos_cliente_empresas SET \"Cupo\" = ? WHERE client_id = ? AND \"NIT\" = ?";
}
final int affectedRows = jdbcTemplate.update(sql, maximumCupoAmount, clientId, documentNumber);
if (affectedRows == 0) {
errorCount++;
errorMessage = "No se pudo modificar el cupo";
ImportHandlerUtils.writeErrorMessage(clientCupDecrementSheet, clientCupoDecrementData.getRowIndex(), errorMessage,
ClientCupoDecrementConstants.STATUS_COL);
clientCupDecrementSheet.setColumnWidth(ClientCupoDecrementConstants.STATUS_COL,
TemplatePopulateImportConstants.EXTRALARGE_COL_SIZE);
continue;
if (maximumCupoAmount.compareTo(previousMaximumCupoAmount) < 0) {
final int affectedRows = jdbcTemplate.update(sql, maximumCupoAmount, clientId, documentNumber);
if (affectedRows == 0) {
errorCount++;
errorMessage = "No se pudo modificar el cupo";
ImportHandlerUtils.writeErrorMessage(clientCupDecrementSheet, clientCupoDecrementData.getRowIndex(),
errorMessage, ClientCupoDecrementConstants.STATUS_COL);
clientCupDecrementSheet.setColumnWidth(ClientCupoDecrementConstants.STATUS_COL,
TemplatePopulateImportConstants.EXTRALARGE_COL_SIZE);
continue;
}
}
}
final Cell statusCell = clientCupDecrementSheet.getRow(clientCupoDecrementData.getRowIndex())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ private Count importEntity(final Workbook workbook, final List<ClientCupoIncreme
final Sheet clientCupoIncrementSheet = workbook.getSheet(TemplatePopulateImportConstants.CLIENT_CUPO_INCREMENT_SHEET_NAME);
int successCount = 0;
int errorCount = 0;
ArrayList<String> errMesg;
String errorMessage;
final GsonBuilder gsonBuilder = GoogleGsonSerializerHelper.createGsonBuilder();
gsonBuilder.registerTypeAdapter(LocalDate.class, new DateSerializer(dateFormat, locale));
Expand Down Expand Up @@ -202,35 +201,27 @@ private Count importEntity(final Workbook workbook, final List<ClientCupoIncreme
final Long clientId = client.getClientId();
final String clientName = client.getClientName();
final EnumOptionData clientStatus = client.getStatus();
BigDecimal previousMaximumCupoAmount = client.getCupo();
BigDecimal totalOutstandingPrincipalAmount = client.getTotalOutstandingPrincipalAmount();
final BigDecimal previousMaximumCupoAmount = client.getCupo();
final BigDecimal totalOutstandingPrincipalAmount = client.getTotalOutstandingPrincipalAmount();

clientCupoIncrementSheet.getRow(clientCupoIncrementData.getRowIndex())
.createCell(ClientCupoIncrementConstants.CLIENT_NAME_COL).setCellValue(clientName);
clientCupoIncrementSheet.getRow(clientCupoIncrementData.getRowIndex())
.createCell(ClientCupoIncrementConstants.PREVIOUS_MAXIMUM_CUPO_AMOUNT_COL)
.setCellValue(previousMaximumCupoAmount.doubleValue());

if (totalOutstandingPrincipalAmount.compareTo(BigDecimal.ZERO) < 0) {
totalOutstandingPrincipalAmount = totalOutstandingPrincipalAmount.abs();
previousMaximumCupoAmount = previousMaximumCupoAmount.subtract(totalOutstandingPrincipalAmount);
}
if (totalOutstandingPrincipalAmount.compareTo(previousMaximumCupoAmount) > 0) {
if (maximumCupoAmount.compareTo(totalOutstandingPrincipalAmount) < 0) {
errorCount++;
errorMessage = "No puede modificarse ya que el nuevo cupo que sugiere es menor al actual "
+ totalOutstandingPrincipalAmount;
ImportHandlerUtils.writeErrorMessage(clientCupoIncrementSheet, clientCupoIncrementData.getRowIndex(), errorMessage,
ClientCupoIncrementConstants.STATUS_COL);
clientCupoIncrementSheet.setColumnWidth(ClientCupoIncrementConstants.STATUS_COL,
TemplatePopulateImportConstants.EXTRALARGE_COL_SIZE);
continue;
}
if (maximumCupoAmount.compareTo(totalOutstandingPrincipalAmount) < 0) {
errorCount++;
errorMessage = "No se puede modificar ya que la nueva cuota sugerida es menor al capital pendiente actual: "
+ totalOutstandingPrincipalAmount;
ImportHandlerUtils.writeErrorMessage(clientCupoIncrementSheet, clientCupoIncrementData.getRowIndex(), errorMessage,
ClientCupoIncrementConstants.STATUS_COL);
clientCupoIncrementSheet.setColumnWidth(ClientCupoIncrementConstants.STATUS_COL,
TemplatePopulateImportConstants.EXTRALARGE_COL_SIZE);
continue;
}

if (maximumCupoAmount.compareTo(previousMaximumCupoAmount) < 0) {
if (maximumCupoAmount.compareTo(previousMaximumCupoAmount) <= 0) {
errorCount++;
errorMessage = "No puede modificarse ya que el nuevo cupo que sugiere es menor al actual";
errorMessage = "No se puede modificar ya que la nueva cuota sugerida es menor o igual a la actual.";
ImportHandlerUtils.writeErrorMessage(clientCupoIncrementSheet, clientCupoIncrementData.getRowIndex(), errorMessage,
ClientCupoIncrementConstants.STATUS_COL);
clientCupoIncrementSheet.setColumnWidth(ClientCupoIncrementConstants.STATUS_COL,
Expand Down Expand Up @@ -303,7 +294,7 @@ private Count importEntity(final Workbook workbook, final List<ClientCupoIncreme
} else {
sql = "UPDATE campos_cliente_empresas SET \"Cupo\" = ? WHERE client_id = ? AND \"NIT\" = ?";
}
if (maximumCupoAmount.compareTo(previousMaximumCupoAmount) < 0) {
if (maximumCupoAmount.compareTo(previousMaximumCupoAmount) > 0) {
final int affectedRows = jdbcTemplate.update(sql, maximumCupoAmount, clientId, documentNumber);
if (affectedRows == 0) {
errorCount++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ClientAdditionalFieldsData {
private String clientName;
private Integer legalForm;
private BigDecimal totalOutstandingPrincipalAmount;
private BigDecimal otherTotalOutstandingPrincipalAmount;

public boolean isPerson() {
LegalForm legalForm = LegalForm.fromInt(this.legalForm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public String schema() {
COALESCE(ccp."Cupo otros prestamos", cce."Cupo otros prestamos") AS otherLoansCupo,
mc.legal_form_enum AS legalForm,
mc.blocking_reason_id AS blockingReasonId,
(select COALESCE(SUM(ml.principal_outstanding_derived), 0) FROM m_loan ml WHERE ml.loan_status_id = 300 and client_id =mc.id) AS totalOutstandingPrincipalAmount
(SELECT COALESCE(SUM(ml.principal_outstanding_derived), 0) FROM m_loan ml INNER JOIN m_product_loan mpl ON mpl.id = ml.product_id WHERE ml.loan_status_id = 300 and ml.client_id = mc.id AND mpl.use_other_loans_cupo = FALSE) AS totalOutstandingPrincipalAmount,
(SELECT COALESCE(SUM(ml.principal_outstanding_derived), 0) FROM m_loan ml INNER JOIN m_product_loan mpl ON mpl.id = ml.product_id WHERE ml.loan_status_id = 300 and ml.client_id = mc.id AND mpl.use_other_loans_cupo = TRUE) AS otherTotalOutstandingPrincipalAmount
FROM m_client mc
LEFT JOIN campos_cliente_empresas cce ON cce.client_id = mc.id
LEFT JOIN m_code_value tipo ON tipo.id = cce."Tipo ID_cd_Tipo ID"
Expand All @@ -63,6 +64,7 @@ public ClientAdditionalFieldsData mapRow(@NotNull ResultSet rs, int rowNum) thro
final int statusInt = rs.getInt("status");
final Long blockingReasonId = JdbcSupport.getLong(rs, "blockingReasonId");
final BigDecimal totalOutstandingPrincipalAmount = rs.getBigDecimal("totalOutstandingPrincipalAmount");
final BigDecimal otherTotalOutstandingPrincipalAmount = rs.getBigDecimal("otherTotalOutstandingPrincipalAmount");
EnumOptionData statusData;
if (blockingReasonId != null) {
statusData = ClientEnumerations.status(ClientStatus.BLOCKED);
Expand All @@ -72,6 +74,6 @@ public ClientAdditionalFieldsData mapRow(@NotNull ResultSet rs, int rowNum) thro
final String clientName = rs.getString("clientName");
final Integer legalForm = JdbcSupport.getInteger(rs, "legalForm");
return new ClientAdditionalFieldsData(clientId, tipo, nit, cedula, cupo, otherLoansCupo, statusData, clientName, legalForm,
totalOutstandingPrincipalAmount);
totalOutstandingPrincipalAmount, otherTotalOutstandingPrincipalAmount);
}
}

0 comments on commit 1325016

Please sign in to comment.