Search in sources :

Example 1 with BillDto

use of org.collectiveone.modules.assignations.dto.BillDto in project CollectiveOneWebapp by CollectiveOne.

the class Bill method toDto.

public BillDto toDto() {
    BillDto dto = new BillDto();
    dto.setAssetId(tokenType.getId().toString());
    dto.setAssetName(tokenType.getName());
    dto.setValue(value);
    return dto;
}
Also used : BillDto(org.collectiveone.modules.assignations.dto.BillDto)

Example 2 with BillDto

use of org.collectiveone.modules.assignations.dto.BillDto in project CollectiveOneWebapp by CollectiveOne.

the class AssignationService method createAssignation.

public PostResult createAssignation(UUID initiativeId, AssignationDto assignationDto, UUID creatorId) {
    Initiative initiative = initiativeRepository.findById(initiativeId);
    Assignation assignation = new Assignation();
    assignation.setType(AssignationType.valueOf(assignationDto.getType()));
    assignation.setMotive(assignationDto.getMotive());
    assignation.setNotes(assignationDto.getNotes());
    assignation.setCreator(appUserRepository.findByC1Id(creatorId));
    assignation.setCreationDate(new Timestamp(System.currentTimeMillis()));
    assignation.setState(AssignationState.valueOf(assignationDto.getConfig().getStartState()));
    assignation.setInitiative(initiative);
    assignation.getAlsoInInitiatives().addAll(initiativeService.getParentGenealogyInitiatives(initiative.getId()));
    AssignationConfig config = new AssignationConfig();
    config.setSelfBiasVisible(Boolean.valueOf(assignationDto.getConfig().getSelfBiasVisible()));
    config.setEvaluationsVisible(Boolean.valueOf(assignationDto.getConfig().getEvaluationsVisible()));
    config.setDurationDays(assignationDto.getConfig().getMaxDuration());
    if (assignation.getState() == AssignationState.OPEN) {
        config.setMinClosureDate(new Timestamp(System.currentTimeMillis()));
        config.setMaxClosureDate(new Timestamp(System.currentTimeMillis() + assignationDto.getConfig().getMaxDuration() * DAYS_TO_MS));
    }
    config = assignationConfigRepository.save(config);
    assignation.setConfig(config);
    assignation = assignationRepository.save(assignation);
    for (ReceiverDto receiverDto : assignationDto.getReceivers()) {
        Receiver receiver = new Receiver();
        receiver.setUser(appUserRepository.findByC1Id(UUID.fromString(receiverDto.getUser().getC1Id())));
        receiver.setAssignation(assignation);
        receiver.setAssignedPercent(receiverDto.getPercent());
        receiver.setState(ReceiverState.PENDING);
        if (receiverDto.getIsDonor()) {
            receiver.setType(ReceiverType.DONOR);
        } else {
            receiver.setType(ReceiverType.NORMAL);
        }
        receiver = receiverRepository.save(receiver);
        assignation.getReceivers().add(receiver);
    }
    for (BillDto billDto : assignationDto.getAssets()) {
        TokenType tokenType = tokenService.getTokenType(UUID.fromString(billDto.getAssetId()));
        Bill bill = new Bill();
        bill.setAssignation(assignation);
        bill.setTokenType(tokenType);
        bill.setValue(billDto.getValue());
        bill = billRepository.save(bill);
        assignation.getBills().add(bill);
    }
    switch(assignation.getType()) {
        case DIRECT:
            for (Bill bill : assignation.getBills()) {
                for (Receiver receiver : assignation.getReceivers()) {
                    double value = bill.getValue() * receiver.getAssignedPercent() / 100.0;
                    PostResult result = tokenTransferService.transferFromInitiativeToUser(assignation.getInitiative().getId(), receiver.getUser().getC1Id(), bill.getTokenType().getId(), value);
                    if (result.getResult().equals("success")) {
                        receiver.setState(ReceiverState.RECEIVED);
                        receiver.setTransfer(memberTransferRepository.findById(UUID.fromString(result.getElementId())));
                        receiverRepository.save(receiver);
                    }
                }
            }
            assignation.setState(AssignationState.DONE);
            assignationRepository.save(assignation);
            activityService.directAssignationCreated(assignation, appUserRepository.findByC1Id(creatorId));
            break;
        case PEER_REVIEWED:
            for (EvaluatorDto evaluatorDto : assignationDto.getEvaluators()) {
                Evaluator evaluator = new Evaluator();
                evaluator.setUser(appUserRepository.findByC1Id(UUID.fromString(evaluatorDto.getUser().getC1Id())));
                evaluator.setAssignation(assignation);
                evaluator.setState(EvaluatorState.PENDING);
                /* Weight logic TBD */
                evaluator.setWeight(1.0);
                evaluator = evaluatorRepository.save(evaluator);
                /* Create the grades of all evaluators already */
                for (Receiver receiver : assignation.getReceivers()) {
                    EvaluationGrade grade = new EvaluationGrade();
                    grade.setAssignation(assignation);
                    grade.setEvaluator(evaluator);
                    grade.setReceiver(receiver);
                    grade.setPercent(0.0);
                    grade.setType(EvaluationGradeType.SET);
                    grade.setState(EvaluationGradeState.PENDING);
                    grade = evaluationGradeRepository.save(grade);
                    evaluator.getGrades().add(grade);
                }
                assignation.getEvaluators().add(evaluator);
            }
            activityService.peerReviewedAssignationCreated(assignation, appUserRepository.findByC1Id(creatorId));
            break;
    }
    return new PostResult("success", "success", assignation.getId().toString());
}
Also used : ReceiverDto(org.collectiveone.modules.assignations.dto.ReceiverDto) PeerReviewedAssignation(org.collectiveone.modules.assignations.evaluationlogic.PeerReviewedAssignation) Initiative(org.collectiveone.modules.initiatives.Initiative) Timestamp(java.sql.Timestamp) PostResult(org.collectiveone.common.dto.PostResult) TokenType(org.collectiveone.modules.tokens.TokenType) BillDto(org.collectiveone.modules.assignations.dto.BillDto) EvaluatorDto(org.collectiveone.modules.assignations.dto.EvaluatorDto)

Aggregations

BillDto (org.collectiveone.modules.assignations.dto.BillDto)2 Timestamp (java.sql.Timestamp)1 PostResult (org.collectiveone.common.dto.PostResult)1 EvaluatorDto (org.collectiveone.modules.assignations.dto.EvaluatorDto)1 ReceiverDto (org.collectiveone.modules.assignations.dto.ReceiverDto)1 PeerReviewedAssignation (org.collectiveone.modules.assignations.evaluationlogic.PeerReviewedAssignation)1 Initiative (org.collectiveone.modules.initiatives.Initiative)1 TokenType (org.collectiveone.modules.tokens.TokenType)1