Skip to content

Commit

Permalink
feat: report endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
M4rc0Russ0 committed Dec 20, 2024
1 parent c375953 commit 599dc6f
Show file tree
Hide file tree
Showing 23 changed files with 1,347 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class Report {

private String reportId;

private String idReport;

private Organisation organisation;

private ReportType type;
Expand All @@ -27,6 +29,8 @@ public class Report {

private short year;

private Integer ver;

@Builder.Default
private Optional<Short> period = Optional.empty();

Expand All @@ -51,6 +55,19 @@ public class Report {
private LedgerDispatchStatus ledgerDispatchStatus = LedgerDispatchStatus.NOT_DISPATCHED;

public static String id(String organisationId,
ReportType reportType,
IntervalType intervalType,
short year,
Integer ver,
Optional<Short> period) {
return period.map(p -> {
return digestAsHex(STR."\{organisationId}::\{reportType}::\{intervalType}::\{year}::\{ver}::\{p}");
}).orElseGet(() -> {
return digestAsHex(STR."\{organisationId}::\{reportType}::\{intervalType}::\{year}::\{ver}");
});
}

public static String idControl(String organisationId,
ReportType reportType,
IntervalType intervalType,
short year,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.Embeddable;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.Validable;
import org.cardanofoundation.lob.app.support.calc.BigDecimals;
import org.cardanofoundation.lob.app.support.calc.Summable;
Expand All @@ -10,6 +11,7 @@
import java.math.BigDecimal;
import java.util.Optional;

@Slf4j
@AllArgsConstructor
@Builder(toBuilder = true)
@EqualsAndHashCode
Expand All @@ -30,7 +32,7 @@ public class BalanceSheetData implements Validable, Summable {
@Override
public boolean isValid() {
return getAssets().isPresent() && getLiabilities().isPresent() && getCapital().isPresent()
&& assets.sumOf().equals(BigDecimals.sum(liabilities, capital));
&& 0 == assets.sumOf().compareTo(BigDecimals.sum(liabilities, capital));
}

public Optional<Assets> getAssets() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public class ReportEntity extends CommonEntity implements Persistable<String>, V
@Setter
private String reportId;

@Column(name = "id_control", nullable = false, length = 64)
@NotBlank
@Getter
@Setter
private String idControl;

@Column(name = "ver", nullable = false)
@Getter
@Setter
private Integer ver = 1;

@Override
public String getId() {
return reportId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;
import java.util.Set;

public interface ReportRepository extends JpaRepository<ReportEntity, String> {
Expand All @@ -19,4 +20,24 @@ public interface ReportRepository extends JpaRepository<ReportEntity, String> {
Set<ReportEntity> findDispatchableTransactions(@Param("organisationId") String organisationId,
Limit limit);


@Query("""
SELECT r FROM accounting_reporting_core.report.ReportEntity r
WHERE r.organisation.id = :organisationId
ORDER BY r.createdAt ASC, r.reportId ASC""")
Set<ReportEntity> findByOrganisationId(@Param("organisationId") String organisationId);

@Query("""
SELECT r FROM accounting_reporting_core.report.ReportEntity r
WHERE r.organisation.id = :organisationId
AND r.idControl = :idControl""")
Optional<ReportEntity> findByIdControl(@Param("organisationId") String organisationId, @Param("idControl") String idControl);

@Query("""
SELECT r FROM accounting_reporting_core.report.ReportEntity r
WHERE r.organisation.id = :organisationId
AND r.idControl = :idControl
ORDER BY r.ver DESC, r.ledgerDispatchApproved ASC
LIMIT 1""")
Optional<ReportEntity> findLatestByIdControl(@Param("organisationId") String organisationId, @Param("idControl") String idControl);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.cardanofoundation.lob.app.accounting_reporting_core.resource;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.lob.app.accounting_reporting_core.domain.core.report.ReportType;
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.presentation_layer_service.ReportViewService;
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.requests.*;
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.ReportResponseView;
import org.cardanofoundation.lob.app.accounting_reporting_core.resource.views.ReportingParametersView;
import org.cardanofoundation.lob.app.accounting_reporting_core.service.internal.ReportService;
import org.cardanofoundation.lob.app.organisation.service.OrganisationCurrencyService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:3000")
@RequiredArgsConstructor
@Slf4j
public class ReportController {
private final ReportViewService reportViewService;
private final OrganisationCurrencyService organisationCurrencyService;
private final ReportService reportService;


@Tag(name = "Reporting", description = "Report Parameters")
@GetMapping(value = "/report-parameters/{orgId}", produces = "application/json")
public ResponseEntity<?> reportParameters(@PathVariable("orgId") @Parameter(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94") String orgId) {

HashMap<String, String> currencyOrg = new HashMap<>();
/** Todo: Get the value from organisation */
organisationCurrencyService.findByOrganisationIdAndCode(orgId, "CHF")
.stream()
.map(organisationCurrency -> {
return currencyOrg.put(organisationCurrency.getCurrencyId(), organisationCurrency.getId().getCustomerCode());
}).collect(Collectors.toSet());
return ResponseEntity.ok().body(
new ReportingParametersView(
Arrays.stream(ReportType.values()).collect(Collectors.toSet()),
currencyOrg,
"2023"
)
);
}


@Tag(name = "Reporting", description = "Create Balance Sheet")
@PostMapping(value = "/report-create", produces = "application/json")
public ResponseEntity<?> reportCreate(@Valid @RequestBody ReportRequest reportSaveRequest) {

return reportViewService.reportCreate(reportSaveRequest)
.fold(problem -> {
return ResponseEntity.status(problem.getStatus().getStatusCode()).body(ReportResponseView.createFail(problem));
}, success -> {
return ResponseEntity.ok().body(
ReportResponseView.createSuccess(Set.of(reportViewService.responseView(success)))
);
});
}


@Tag(name = "Reporting", description = "Create Income Statement")
@PostMapping(value = "/report-search", produces = "application/json")
public ResponseEntity<?> reportSearch(@Valid @RequestBody ReportSearchRequest reportSearchRequest) {

return reportService.exist(
reportSearchRequest.getOrganisationId(),
reportSearchRequest.getReportType(),
reportSearchRequest.getIntervalType(),
reportSearchRequest.getYear(),
reportSearchRequest.getPeriod()
).fold(problem -> {
return ResponseEntity.status(problem.getStatus().getStatusCode()).body(ReportResponseView.createFail(problem));
}, success -> {
return ResponseEntity.ok().body(
ReportResponseView.createSuccess(Set.of(reportViewService.responseView(success)))
);
});
}


@Tag(name = "Reporting", description = "Report list")
@GetMapping(value = "/report-list/{orgId}", produces = "application/json")
public ResponseEntity<?> reportList(@PathVariable("orgId") @Parameter(example = "75f95560c1d883ee7628993da5adf725a5d97a13929fd4f477be0faf5020ca94") String orgId) {

return ResponseEntity.ok().body(ReportResponseView.createSuccess(reportService.findByOrgId(
orgId
).stream().map(reportViewService::responseView).collect(Collectors.toSet()))
);

}


@Tag(name = "Reporting", description = "Report publish")
@PostMapping(value = "/report-publish", produces = "application/json")
public ResponseEntity<?> reportPublish(@Valid @RequestBody ReportPublishRequest reportPublishRequest) {

return reportViewService.reportPublish(reportPublishRequest).fold(
problem -> {
return ResponseEntity.status(problem.getStatus().getStatusCode()).body(ReportResponseView.createFail(problem));
}, success -> {
return ResponseEntity.ok().body(
ReportResponseView.createSuccess(Set.of(reportViewService.responseView(success)))
);
}
);
}

}
Loading

0 comments on commit 599dc6f

Please sign in to comment.