class ProjectFinanceHandlerImpl implements ProjectFinanceHandler {

    @Override
    @Transactional
    public BigDecimal getResearchParticipationPercentageFromProject(long projectId){
        List<ProjectFinanceResource> applicationFinanceResources = this.getFinanceChecksTotals(projectId);

        BigDecimal totalCosts = applicationFinanceResources.stream()
                .map(ProjectFinanceResource::getTotal)
                .reduce(BigDecimal.ZERO, BigDecimal::add);


        BigDecimal researchCosts = applicationFinanceResources.stream()
                .filter(f ->
                        OrganisationTypeEnum.isResearchParticipationOrganisation(organisationRepository.findById(f.getOrganisation()).get().getOrganisationType().getId())
                )
                .map(ProjectFinanceResource::getTotal)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        BigDecimal researchParticipation = BigDecimal.ZERO;

        if(totalCosts.compareTo(BigDecimal.ZERO)!=0) {
            researchParticipation = researchCosts.divide(totalCosts, 6, BigDecimal.ROUND_HALF_UP);
        }
        researchParticipation = researchParticipation.multiply(BigDecimal.valueOf(100));
        return researchParticipation.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

}