Skip to content

Commit

Permalink
fix: report list and fields
Browse files Browse the repository at this point in the history
  • Loading branch information
M4rc0Russ0 committed Jan 15, 2025
1 parent a34074b commit 3420287
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Set<ReportEntity> findDispatchableTransactions(@Param("organisationId") String o

@Query("""
SELECT r FROM accounting_reporting_core.report.ReportEntity r
LEFT JOIN accounting_reporting_core.report.ReportEntity r2 on r.idControl = r2.idControl and r.ver < r2.ver
WHERE r.organisation.id = :organisationId
AND r2.idControl IS NULL
ORDER BY r.createdAt ASC, r.reportId ASC""")
Set<ReportEntity> findByOrganisationId(@Param("organisationId") String organisationId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.springframework.transaction.annotation.Transactional;
import org.zalando.problem.Problem;

import java.util.Optional;

@Service
@org.jmolecules.ddd.annotation.Service
@Slf4j
Expand Down Expand Up @@ -89,6 +91,13 @@ public ReportView responseView(ReportEntity reportEntity) {
reportResponseView.setPeriod(reportEntity.getPeriod());
reportResponseView.setDate(reportEntity.getDate());
reportResponseView.setPublish(reportEntity.getLedgerDispatchApproved());
reportResponseView.setCanBenPublish(true);
Either<Problem, Boolean> left = reportService.canPublish(reportEntity);
reportResponseView.setError(Optional.empty());
if (left.isLeft()) {
reportResponseView.setCanBenPublish(false);
reportResponseView.setError(Optional.of(left.getLeft()));
}
reportResponseView.setVer(reportEntity.getVer());
//BalanceSheet
reportEntity.getBalanceSheetReportData().flatMap(balanceSheetData -> balanceSheetData.getAssets().flatMap(assets -> assets.getNonCurrentAssets().flatMap(nonCurrentAssets -> nonCurrentAssets.getPropertyPlantEquipment()))).ifPresent(bigDecimal -> reportResponseView.setPropertyPlantEquipment(bigDecimal.toString()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -18,17 +19,22 @@
public class ReportSearchRequest {

@Schema(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94")
@NotNull
private String organisationId;

@Schema(example = "INCOME_STATEMENT")
@NotNull
private ReportType reportType;

@NotNull
private IntervalType intervalType;

@Schema(example = "2024")
@NotNull
private short year;

@Schema(example = "3")
@NotNull
private short period;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Setter;
import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.report.IntervalType;
import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.report.ReportType;
import org.zalando.problem.Problem;

import java.time.LocalDate;
import java.util.Optional;
Expand All @@ -30,6 +31,10 @@ public class ReportView {

private Boolean publish;

private Boolean canBenPublish;

private Optional<Problem> error;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate date;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ public Either<Problem, ReportEntity> storeBalanceSheet(
.build();

reportEntity.setBalanceSheetReportData(Optional.of(balanceSheetReportData));
/**
* Todo: Bug: Always save even when error is triggered.
*/
if (!reportEntity.isValid()) {
return Either.left(Problem.builder()
.withTitle("INVALID_REPORT")
Expand Down Expand Up @@ -514,32 +517,7 @@ public Either<Problem, ReportEntity> store(ReportEntity reportEntity) {
.with("reportId", reportId)
.build());
}
// Validate profitForTheYear consistency between IncomeStatementData and BalanceSheetData
val relatedReportType = reportEntity.getType().equals(INCOME_STATEMENT) ? BALANCE_SHEET : INCOME_STATEMENT;
val relatedReportId = Report.idControl(reportEntity.getOrganisation().getId(), relatedReportType, reportEntity.getIntervalType(), reportEntity.getYear(), reportEntity.getPeriod());
val relatedReportM = reportRepository.findLatestByIdControl(reportEntity.getOrganisation().getId(), relatedReportId);

if (relatedReportM.isPresent()) {
val relatedReport = relatedReportM.orElseThrow();
val relatedProfit = relatedReport.getType().equals(INCOME_STATEMENT)
? relatedReport.getIncomeStatementReportData().flatMap(IncomeStatementData::getProfitForTheYear).orElse(BigDecimal.ZERO)
: relatedReport.getBalanceSheetReportData().flatMap(BalanceSheetData::getCapital).flatMap(BalanceSheetData.Capital::getProfitForTheYear).orElse(BigDecimal.ZERO);


BigDecimal newProfit = reportEntity.getType().equals(INCOME_STATEMENT)
? reportEntity.getIncomeStatementReportData().flatMap(IncomeStatementData::getProfitForTheYear).orElse(BigDecimal.ZERO)
: reportEntity.getBalanceSheetReportData().flatMap(BalanceSheetData::getCapital).flatMap(BalanceSheetData.Capital::getProfitForTheYear).orElse(BigDecimal.ZERO);
if (0 != newProfit.compareTo(relatedProfit)) {
reportRepository.save(reportEntity);
return Either.left(Problem.builder()
.withTitle("PROFIT_FOR_THE_YEAR_MISMATCH")
.withDetail(STR."Profit for the year does not match the related report. \{newProfit} != \{relatedProfit}")
.withStatus(Status.BAD_REQUEST)
.with("reportId", reportId)
.build());
}

}
reportRepository.save(reportEntity);
return Either.right(reportEntity);
}
Expand Down Expand Up @@ -681,7 +659,7 @@ private static Either<Problem, Void> emptyReportData(String reportId) {
.build());
}

private Either<Problem, Boolean> canPublish(ReportEntity reportEntity) {
public Either<Problem, Boolean> canPublish(ReportEntity reportEntity) {

// Validate profitForTheYear consistency between IncomeStatementData and BalanceSheetData
val relatedReportType = reportEntity.getType().equals(INCOME_STATEMENT) ? BALANCE_SHEET : INCOME_STATEMENT;
Expand Down Expand Up @@ -724,13 +702,7 @@ private Either<Problem, Boolean> canPublish(ReportEntity reportEntity) {
.with("reportId", reportEntity.getReportId())
.build());
}
return Either.right(true);
}
return Either.left(Problem.builder()
.withTitle("NO_RELATED_REPORT")
.withDetail(STR."Profit for the year does not match the related report.")
.withStatus(Status.BAD_REQUEST)
.with("reportId", reportEntity.getReportId())
.build());
return Either.right(true);
}
}

0 comments on commit 3420287

Please sign in to comment.