Skip to content

Commit

Permalink
Merge branch 'feat/LOB-833-Impelement-first-defined-metrics' into fea…
Browse files Browse the repository at this point in the history
…t/LOB-752-BE-Save-Dashboards
  • Loading branch information
Kammerlo committed Jan 16, 2025
2 parents ef80875 + 418e440 commit 2a2d20c
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -45,11 +44,21 @@ Set<ReportEntity> findDispatchableTransactions(@Param("organisationId") String o

@Query("""
SELECT r FROM accounting_reporting_core.report.ReportEntity r
JOIN (
SELECT MAX(r2.ver) AS ver, r2.reportId as id FROM accounting_reporting_core.report.ReportEntity r2
WHERE r2.organisation.id = :organisationId
AND (CAST(:startDate AS date) IS NULL OR r2.date >= :startDate)
AND (CAST(:endDate AS date) IS NULL OR r2.date <= :endDate)
AND r2.ledgerDispatchStatus = 'DISPATCHED'
GROUP BY r2.reportId
) AS latest
ON r.ver = latest.ver AND r.reportId = latest.id
WHERE r.organisation.id = :organisationId
AND (CAST(:startDate AS date) IS NULL OR r.date >= :startDate)
AND (CAST(:endDate AS date) IS NULL OR r.date <= :endDate)
AND r.ledgerDispatchStatus = 'DISPATCHED'
""")
List<ReportEntity> getReportEntitiesByDateBetween(@Param("organisationId") String organisationId,
@Param("startDate") LocalDate startDate,
@Param("endDate") LocalDate endDate);
List<ReportEntity> getNewestReportsInRange(@Param("organisationId") String organisationId,
@Param("startDate") LocalDate startDate,
@Param("endDate") LocalDate endDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public Either<Problem, ReportEntity> storeIncomeStatement(
) {
log.info("Income Statement::Saving report example...");

val orgM = organisationPublicApi.findByOrganisationId(organisationId);
Optional<org.cardanofoundation.lob.app.organisation.domain.entity.Organisation> orgM = organisationPublicApi.findByOrganisationId(organisationId);
if (orgM.isEmpty()) {
return Either.left(Problem.builder()
.withTitle("ORGANISATION_NOT_FOUND")
Expand All @@ -387,7 +387,7 @@ public Either<Problem, ReportEntity> storeIncomeStatement(
.with("organisationId", organisationId)
.build());
}
val org = orgM.orElseThrow();
org.cardanofoundation.lob.app.organisation.domain.entity.Organisation org = orgM.orElseThrow();

val reportEntityE = exist(organisationId, reportType, intervalType, year, period);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
import org.cardanofoundation.lob.app.accounting_reporting_core.service.internal.metrics.MetricExecutor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import static org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.metric.MetricEnum.BALANCE_SHEET;

Expand All @@ -36,134 +35,110 @@ public void init() {
}

private Map<BalanceSheetCategories, Integer> getAssetCategories(String organisationID, Optional<LocalDate> startDate, Optional<LocalDate> endDate) {
List<ReportEntity> reportEntities = reportRepository.getReportEntitiesByDateBetween(organisationID,
List<ReportEntity> reportEntities = reportRepository.getNewestReportsInRange(organisationID,
startDate.orElse(null),
endDate.orElse(null));

Map<BalanceSheetCategories, Integer> assetCategories = new HashMap<>();
Map<BalanceSheetCategories, Integer> assetCategories = new EnumMap<>(BalanceSheetCategories.class);

reportEntities.forEach(reportEntity ->
reportEntity.getBalanceSheetReportData().flatMap(BalanceSheetData::getAssets).ifPresent(assets -> {
assets.getCurrentAssets().ifPresent(currentAssets -> {
currentAssets.getCashAndCashEquivalents().ifPresent(cash -> {
assetCategories.merge(BalanceSheetCategories.CASH, cash.intValue(), Integer::sum);
});
currentAssets.getCryptoAssets().ifPresent(cryptoAssets -> {
assetCategories.merge(BalanceSheetCategories.CRYPTO_ASSETS, cryptoAssets.intValue(), Integer::sum);
});
currentAssets.getOtherReceivables().ifPresent(otherReceivables -> {
assetCategories.merge(BalanceSheetCategories.OTHER, otherReceivables.intValue(), Integer::sum);
});
currentAssets.getPrepaymentsAndOtherShortTermAssets().ifPresent(prepayments -> {
assetCategories.merge(BalanceSheetCategories.OTHER, prepayments.intValue(), Integer::sum);
});
currentAssets.getCashAndCashEquivalents().ifPresent(
cash -> assetCategories.merge(BalanceSheetCategories.CASH, cash.intValue(), Integer::sum));
currentAssets.getCryptoAssets().ifPresent(
cryptoAssets -> assetCategories.merge(BalanceSheetCategories.CRYPTO_ASSETS, cryptoAssets.intValue(), Integer::sum));
currentAssets.getOtherReceivables().ifPresent(
otherReceivables -> assetCategories.merge(BalanceSheetCategories.OTHER, otherReceivables.intValue(), Integer::sum));
currentAssets.getPrepaymentsAndOtherShortTermAssets().ifPresent(
prepayments -> assetCategories.merge(BalanceSheetCategories.OTHER, prepayments.intValue(), Integer::sum));
});

assets.getNonCurrentAssets().ifPresent(nonCurrentAssets -> {
nonCurrentAssets.getFinancialAssets().ifPresent(financialAssets -> {
assetCategories.merge(BalanceSheetCategories.FINANCIAL_ASSETS, financialAssets.intValue(), Integer::sum);
});
nonCurrentAssets.getIntangibleAssets().ifPresent(intangibleAssets -> {
assetCategories.merge(BalanceSheetCategories.OTHER, intangibleAssets.intValue(), Integer::sum);
});
nonCurrentAssets.getInvestments().ifPresent(investments -> {
assetCategories.merge(BalanceSheetCategories.OTHER, investments.intValue(), Integer::sum);
});
nonCurrentAssets.getPropertyPlantEquipment().ifPresent(propertyPlantEquipment -> {
assetCategories.merge(BalanceSheetCategories.OTHER, propertyPlantEquipment.intValue(), Integer::sum);
});
nonCurrentAssets.getFinancialAssets().ifPresent(
financialAssets -> assetCategories.merge(BalanceSheetCategories.FINANCIAL_ASSETS, financialAssets.intValue(), Integer::sum));
nonCurrentAssets.getIntangibleAssets().ifPresent(
intangibleAssets -> assetCategories.merge(BalanceSheetCategories.OTHER, intangibleAssets.intValue(), Integer::sum));
nonCurrentAssets.getInvestments().ifPresent(
investments -> assetCategories.merge(BalanceSheetCategories.OTHER, investments.intValue(), Integer::sum));
nonCurrentAssets.getPropertyPlantEquipment().ifPresent(
propertyPlantEquipment -> assetCategories.merge(BalanceSheetCategories.OTHER, propertyPlantEquipment.intValue(), Integer::sum));
});
}));

return assetCategories;
}

private Map<BalanceSheetCategories, Map<BalanceSheetCategories, Integer>> getBalanceSheetOverview(String organisationID, Optional<LocalDate> startDate, Optional<LocalDate> endDate) {
List<ReportEntity> reportEntities = reportRepository.getReportEntitiesByDateBetween(organisationID,
List<ReportEntity> reportEntities = reportRepository.getNewestReportsInRange(organisationID,
startDate.orElse(null),
endDate.orElse(null));

Map<BalanceSheetCategories, Map<BalanceSheetCategories, Integer>> balanceSheetOverview = new HashMap<>();

reportEntities.forEach(reportEntity -> {
reportEntity.getBalanceSheetReportData().ifPresent(balanceSheetData -> {
balanceSheetData.getAssets().ifPresent(assets -> {
Map<BalanceSheetCategories, Integer> assetMap = balanceSheetOverview.getOrDefault(BalanceSheetCategories.ASSETS, new HashMap<>());
assets.getCurrentAssets().ifPresent(currentAssets -> {
currentAssets.getCryptoAssets().ifPresent(cryptoAssets -> {
assetMap.merge(BalanceSheetCategories.CRYPTO_ASSETS, cryptoAssets.intValue(), Integer::sum);
});
currentAssets.getCashAndCashEquivalents().ifPresent(cash -> {
assetMap.merge(BalanceSheetCategories.CASH, cash.intValue(), Integer::sum);
});
currentAssets.getOtherReceivables().ifPresent(otherReceivables -> {
assetMap.merge(BalanceSheetCategories.OTHER, otherReceivables.intValue(), Integer::sum);
});
currentAssets.getPrepaymentsAndOtherShortTermAssets().ifPresent(prepayments -> {
assetMap.merge(BalanceSheetCategories.OTHER, prepayments.intValue(), Integer::sum);
});
});
assets.getNonCurrentAssets().ifPresent(nonCurrentAssets -> {
nonCurrentAssets.getFinancialAssets().ifPresent(financialAssets -> {
assetMap.merge(BalanceSheetCategories.FINANCIAL_ASSETS, financialAssets.intValue(), Integer::sum);
});
nonCurrentAssets.getIntangibleAssets().ifPresent(intangibleAssets -> {
assetMap.merge(BalanceSheetCategories.OTHER, intangibleAssets.intValue(), Integer::sum);
});
nonCurrentAssets.getInvestments().ifPresent(investments -> {
assetMap.merge(BalanceSheetCategories.OTHER, investments.intValue(), Integer::sum);
});
nonCurrentAssets.getPropertyPlantEquipment().ifPresent(propertyPlantEquipment -> {
assetMap.merge(BalanceSheetCategories.OTHER, propertyPlantEquipment.intValue(), Integer::sum);
});
});
balanceSheetOverview.put(BalanceSheetCategories.ASSETS, assetMap);
});

balanceSheetData.getLiabilities().ifPresent(liabilities -> {
Map<BalanceSheetCategories, Integer> liabilityMap = balanceSheetOverview.getOrDefault(BalanceSheetCategories.LIABILITIES, new HashMap<>());
liabilities.getCurrentLiabilities().ifPresent(currentLiabilities -> {
currentLiabilities.getAccrualsAndShortTermProvisions().ifPresent(accruals -> {
liabilityMap.merge(BalanceSheetCategories.ACCRUSAL_AND_SHORT_TERM_PROVISIONS, accruals.intValue(), Integer::sum);
});
currentLiabilities.getTradeAccountsPayables().ifPresent(tradeAccountsPayables -> {
liabilityMap.merge(BalanceSheetCategories.TRADE_ACCOUNTS_PAYABLE, tradeAccountsPayables.intValue(), Integer::sum);
});
currentLiabilities.getOtherCurrentLiabilities().ifPresent(otherCurrentLiabilities -> {
liabilityMap.merge(BalanceSheetCategories.OTHER, otherCurrentLiabilities.intValue(), Integer::sum);
});
});
liabilities.getNonCurrentLiabilities().ifPresent(nonCurrentLiabilities -> {
nonCurrentLiabilities.getProvisions().ifPresent(provisions -> {
liabilityMap.merge(BalanceSheetCategories.PROVISIONS, provisions.intValue(), Integer::sum);
});
});
balanceSheetOverview.put(BalanceSheetCategories.LIABILITIES, liabilityMap);
});
balanceSheetData.getCapital().ifPresent(capital -> {
Map<BalanceSheetCategories, Integer> liabilityMap = balanceSheetOverview.getOrDefault(BalanceSheetCategories.LIABILITIES, new HashMap<>());
capital.getCapital().ifPresent(capitalValue -> {
liabilityMap.merge(BalanceSheetCategories.CAPITAL, capitalValue.intValue(), Integer::sum);
});
capital.getProfitForTheYear().ifPresent(profitForTheYear -> {
liabilityMap.merge(BalanceSheetCategories.PROFIT_OF_THE_YEAR, profitForTheYear.intValue(), Integer::sum);
});
capital.getResultsCarriedForward().ifPresent(resultsCarriedForward -> {
liabilityMap.merge(BalanceSheetCategories.RESULTS_CARRIED_FORWARD, resultsCarriedForward.intValue(), Integer::sum);
});
balanceSheetOverview.put(BalanceSheetCategories.LIABILITIES, liabilityMap);
});
Map<BalanceSheetCategories, Map<BalanceSheetCategories, Integer>> balanceSheetOverview = new EnumMap<>(BalanceSheetCategories.class);

reportEntities.forEach(reportEntity -> reportEntity.getBalanceSheetReportData().ifPresent(balanceSheetData -> {
balanceSheetData.getAssets().ifPresent(assets -> {
Map<BalanceSheetCategories, Integer> assetMap = balanceSheetOverview.getOrDefault(BalanceSheetCategories.ASSETS, new HashMap<>());
processAssets(assets, assetMap);
balanceSheetOverview.put(BalanceSheetCategories.ASSETS, assetMap);
});
});

balanceSheetData.getLiabilities().ifPresent(liabilities -> {
Map<BalanceSheetCategories, Integer> liabilityMap = balanceSheetOverview.getOrDefault(BalanceSheetCategories.LIABILITIES, new HashMap<>());
processLiabilities(liabilities, liabilityMap);
balanceSheetOverview.put(BalanceSheetCategories.LIABILITIES, liabilityMap);
});
balanceSheetData.getCapital().ifPresent(capital -> {
Map<BalanceSheetCategories, Integer> liabilityMap = balanceSheetOverview.getOrDefault(BalanceSheetCategories.LIABILITIES, new HashMap<>());
processCapital(capital, liabilityMap);
balanceSheetOverview.put(BalanceSheetCategories.LIABILITIES, liabilityMap);
});
}));
return balanceSheetOverview;
}

@SafeVarargs
private int sumUpOptionalFields(Optional<BigDecimal>... fields) {
return Stream.of(fields)
.map(field -> field.orElse(BigDecimal.ZERO))
.map(BigDecimal::intValue)
.reduce(Integer::sum)
.orElse(0);
private static void processCapital(BalanceSheetData.Capital capital, Map<BalanceSheetCategories, Integer> liabilityMap) {
capital.getCapital().ifPresent(
capitalValue -> liabilityMap.merge(BalanceSheetCategories.CAPITAL, capitalValue.intValue(), Integer::sum));
capital.getProfitForTheYear().ifPresent(
profitForTheYear -> liabilityMap.merge(BalanceSheetCategories.PROFIT_OF_THE_YEAR, profitForTheYear.intValue(), Integer::sum));
capital.getResultsCarriedForward().ifPresent(
resultsCarriedForward -> liabilityMap.merge(BalanceSheetCategories.RESULTS_CARRIED_FORWARD, resultsCarriedForward.intValue(), Integer::sum));
}

private static void processLiabilities(BalanceSheetData.Liabilities liabilities, Map<BalanceSheetCategories, Integer> liabilityMap) {
liabilities.getCurrentLiabilities().ifPresent(currentLiabilities -> {
currentLiabilities.getAccrualsAndShortTermProvisions().ifPresent(
accruals -> liabilityMap.merge(BalanceSheetCategories.ACCRUSAL_AND_SHORT_TERM_PROVISIONS, accruals.intValue(), Integer::sum));
currentLiabilities.getTradeAccountsPayables().ifPresent(
tradeAccountsPayables -> liabilityMap.merge(BalanceSheetCategories.TRADE_ACCOUNTS_PAYABLE, tradeAccountsPayables.intValue(), Integer::sum));
currentLiabilities.getOtherCurrentLiabilities().ifPresent(
otherCurrentLiabilities -> liabilityMap.merge(BalanceSheetCategories.OTHER, otherCurrentLiabilities.intValue(), Integer::sum));
});
liabilities.getNonCurrentLiabilities().flatMap(BalanceSheetData.Liabilities.NonCurrentLiabilities::getProvisions).ifPresent(
provisions -> liabilityMap.merge(BalanceSheetCategories.PROVISIONS, provisions.intValue(), Integer::sum));
}

private static void processAssets(BalanceSheetData.Assets assets, Map<BalanceSheetCategories, Integer> assetMap) {
assets.getCurrentAssets().ifPresent(currentAssets -> {
currentAssets.getCryptoAssets().ifPresent(
cryptoAssets -> assetMap.merge(BalanceSheetCategories.CRYPTO_ASSETS, cryptoAssets.intValue(), Integer::sum));
currentAssets.getCashAndCashEquivalents().ifPresent(
cash -> assetMap.merge(BalanceSheetCategories.CASH, cash.intValue(), Integer::sum));
currentAssets.getOtherReceivables().ifPresent(
otherReceivables -> assetMap.merge(BalanceSheetCategories.OTHER, otherReceivables.intValue(), Integer::sum));
currentAssets.getPrepaymentsAndOtherShortTermAssets().ifPresent(
prepayments -> assetMap.merge(BalanceSheetCategories.OTHER, prepayments.intValue(), Integer::sum));
});
assets.getNonCurrentAssets().ifPresent(nonCurrentAssets -> {
nonCurrentAssets.getFinancialAssets().ifPresent(
financialAssets -> assetMap.merge(BalanceSheetCategories.FINANCIAL_ASSETS, financialAssets.intValue(), Integer::sum));
nonCurrentAssets.getIntangibleAssets().ifPresent(
intangibleAssets -> assetMap.merge(BalanceSheetCategories.OTHER, intangibleAssets.intValue(), Integer::sum));
nonCurrentAssets.getInvestments().ifPresent(
investments -> assetMap.merge(BalanceSheetCategories.OTHER, investments.intValue(), Integer::sum));
nonCurrentAssets.getPropertyPlantEquipment().ifPresent(
propertyPlantEquipment -> assetMap.merge(BalanceSheetCategories.OTHER, propertyPlantEquipment.intValue(), Integer::sum));
});
}
}
Loading

0 comments on commit 2a2d20c

Please sign in to comment.