Search in sources :

Example 1 with MoveTemplateLine

use of com.axelor.apps.account.db.MoveTemplateLine in project axelor-open-suite by axelor.

the class MoveTemplateService method generateMove.

@Transactional(rollbackOn = { Exception.class })
public List<Long> generateMove(MoveTemplate moveTemplate, List<HashMap<String, Object>> dataList) throws AxelorException {
    List<Long> moveList = new ArrayList<>();
    BigDecimal hundred = new BigDecimal(100);
    for (HashMap<String, Object> data : dataList) {
        LocalDate moveDate = LocalDate.parse(data.get("date").toString(), DateTimeFormatter.ISO_DATE);
        boolean isDebit = false;
        Partner debitPartner = null;
        Partner creditPartner = null;
        BigDecimal moveBalance = new BigDecimal(data.get("moveBalance").toString());
        Partner partner = null;
        if (data.get("debitPartner") != null) {
            debitPartner = partnerRepo.find(Long.parseLong(((HashMap<String, Object>) data.get("debitPartner")).get("id").toString()));
            partner = debitPartner;
        }
        if (data.get("creditPartner") != null) {
            creditPartner = partnerRepo.find(Long.parseLong(((HashMap<String, Object>) data.get("creditPartner")).get("id").toString()));
            partner = creditPartner;
        }
        if (moveTemplate.getJournal().getCompany() != null) {
            Move move = moveService.getMoveCreateService().createMove(moveTemplate.getJournal(), moveTemplate.getJournal().getCompany(), null, partner, moveDate, null, MoveRepository.TECHNICAL_ORIGIN_TEMPLATE, 0);
            int counter = 1;
            for (MoveTemplateLine moveTemplateLine : moveTemplate.getMoveTemplateLineList()) {
                partner = null;
                if (moveTemplateLine.getDebitCreditSelect().equals(MoveTemplateLineRepository.DEBIT)) {
                    isDebit = true;
                    if (moveTemplateLine.getHasPartnerToDebit()) {
                        partner = debitPartner;
                    }
                } else if (moveTemplateLine.getDebitCreditSelect().equals(MoveTemplateLineRepository.CREDIT)) {
                    isDebit = false;
                    if (moveTemplateLine.getHasPartnerToCredit()) {
                        partner = creditPartner;
                    }
                }
                BigDecimal amount = moveBalance.multiply(moveTemplateLine.getPercentage()).divide(hundred, RoundingMode.HALF_UP);
                MoveLine moveLine = moveLineService.createMoveLine(move, partner, moveTemplateLine.getAccount(), amount, isDebit, moveDate, moveDate, counter, moveTemplate.getFullName(), moveTemplateLine.getName());
                move.getMoveLineList().add(moveLine);
                Tax tax = moveTemplateLine.getTax();
                if (tax != null) {
                    TaxLine taxLine = taxService.getTaxLine(tax, moveDate);
                    if (taxLine != null) {
                        moveLine.setTaxLine(taxLine);
                        moveLine.setTaxRate(taxLine.getValue());
                        moveLine.setTaxCode(tax.getCode());
                    }
                }
                moveLine.setAnalyticDistributionTemplate(moveTemplateLine.getAnalyticDistributionTemplate());
                moveLineService.generateAnalyticMoveLines(moveLine);
                counter++;
            }
            if (moveTemplate.getAutomaticallyValidate()) {
                moveValidateService.validate(move);
            }
            moveRepo.save(move);
            moveList.add(move.getId());
        }
    }
    return moveList;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Tax(com.axelor.apps.account.db.Tax) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine) Move(com.axelor.apps.account.db.Move) MoveTemplateLine(com.axelor.apps.account.db.MoveTemplateLine) MoveLine(com.axelor.apps.account.db.MoveLine) Partner(com.axelor.apps.base.db.Partner) Transactional(com.google.inject.persist.Transactional)

Example 2 with MoveTemplateLine

use of com.axelor.apps.account.db.MoveTemplateLine in project axelor-open-suite by axelor.

the class MoveTemplateService method generateMove.

@Transactional(rollbackOn = { Exception.class })
public List<Long> generateMove(LocalDate moveDate, List<HashMap<String, Object>> moveTemplateList) throws AxelorException {
    List<Long> moveList = new ArrayList<>();
    for (HashMap<String, Object> moveTemplateMap : moveTemplateList) {
        MoveTemplate moveTemplate = moveTemplateRepo.find(Long.valueOf((Integer) moveTemplateMap.get("id")));
        if (moveTemplate.getJournal().getCompany() != null) {
            Move move = moveService.getMoveCreateService().createMove(moveTemplate.getJournal(), moveTemplate.getJournal().getCompany(), null, null, moveDate, null, MoveRepository.TECHNICAL_ORIGIN_TEMPLATE, 0);
            int counter = 1;
            for (MoveTemplateLine moveTemplateLine : moveTemplate.getMoveTemplateLineList()) {
                BigDecimal amount = moveTemplateLine.getDebit().add(moveTemplateLine.getCredit());
                MoveLine moveLine = moveLineService.createMoveLine(move, moveTemplateLine.getPartner(), moveTemplateLine.getAccount(), amount, moveTemplateLine.getDebit().compareTo(BigDecimal.ZERO) > 0, moveDate, moveDate, counter, moveTemplate.getFullName(), moveTemplateLine.getName());
                move.getMoveLineList().add(moveLine);
                Tax tax = moveTemplateLine.getTax();
                if (tax != null) {
                    TaxLine taxLine = taxService.getTaxLine(tax, moveDate);
                    if (taxLine != null) {
                        moveLine.setTaxLine(taxLine);
                        moveLine.setTaxRate(taxLine.getValue());
                        moveLine.setTaxCode(tax.getCode());
                    }
                }
                moveLine.setAnalyticDistributionTemplate(moveTemplateLine.getAnalyticDistributionTemplate());
                moveLineService.generateAnalyticMoveLines(moveLine);
                counter++;
            }
            if (moveTemplate.getAutomaticallyValidate()) {
                moveValidateService.validate(move);
            }
            moveRepo.save(move);
            moveList.add(move.getId());
        }
    }
    return moveList;
}
Also used : ArrayList(java.util.ArrayList) Tax(com.axelor.apps.account.db.Tax) MoveTemplate(com.axelor.apps.account.db.MoveTemplate) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine) Move(com.axelor.apps.account.db.Move) MoveTemplateLine(com.axelor.apps.account.db.MoveTemplateLine) MoveLine(com.axelor.apps.account.db.MoveLine) Transactional(com.google.inject.persist.Transactional)

Example 3 with MoveTemplateLine

use of com.axelor.apps.account.db.MoveTemplateLine in project axelor-open-suite by axelor.

the class MoveTemplateService method checkValidityInAmount.

protected boolean checkValidityInAmount(MoveTemplate moveTemplate) {
    BigDecimal debit = BigDecimal.ZERO;
    BigDecimal credit = BigDecimal.ZERO;
    for (MoveTemplateLine line : moveTemplate.getMoveTemplateLineList()) {
        debit = debit.add(line.getDebit());
        credit = credit.add(line.getCredit());
    }
    LOG.debug("Debit : {}, Credit : {}", debit, credit);
    if (debit.compareTo(BigDecimal.ZERO) != 0 && credit.compareTo(BigDecimal.ZERO) != 0 && debit.compareTo(credit) == 0) {
        this.validateMoveTemplateLine(moveTemplate);
        return true;
    } else {
        return false;
    }
}
Also used : MoveTemplateLine(com.axelor.apps.account.db.MoveTemplateLine) BigDecimal(java.math.BigDecimal)

Example 4 with MoveTemplateLine

use of com.axelor.apps.account.db.MoveTemplateLine in project axelor-open-suite by axelor.

the class MoveTemplateService method validateMoveTemplateLine.

@Transactional
public void validateMoveTemplateLine(MoveTemplate moveTemplate) {
    moveTemplate.setIsValid(true);
    for (MoveTemplateLine line : moveTemplate.getMoveTemplateLineList()) {
        line.setIsValid(true);
    }
    moveTemplateRepo.save(moveTemplate);
}
Also used : MoveTemplateLine(com.axelor.apps.account.db.MoveTemplateLine) Transactional(com.google.inject.persist.Transactional)

Example 5 with MoveTemplateLine

use of com.axelor.apps.account.db.MoveTemplateLine in project axelor-open-suite by axelor.

the class MoveTemplateService method checkValidityInPercentage.

protected boolean checkValidityInPercentage(MoveTemplate moveTemplate) {
    BigDecimal debitPercent = BigDecimal.ZERO;
    BigDecimal creditPercent = BigDecimal.ZERO;
    for (MoveTemplateLine line : moveTemplate.getMoveTemplateLineList()) {
        LOG.debug("Adding percent: {}", line.getPercentage());
        if (MoveTemplateLineRepository.DEBIT.equals(line.getDebitCreditSelect())) {
            debitPercent = debitPercent.add(line.getPercentage());
        } else {
            creditPercent = creditPercent.add(line.getPercentage());
        }
    }
    LOG.debug("Debit percent: {}, Credit percent: {}", debitPercent, creditPercent);
    if (debitPercent.compareTo(BigDecimal.ZERO) != 0 && creditPercent.compareTo(BigDecimal.ZERO) != 0 && debitPercent.compareTo(creditPercent) == 0) {
        this.validateMoveTemplateLine(moveTemplate);
        return true;
    } else {
        return false;
    }
}
Also used : MoveTemplateLine(com.axelor.apps.account.db.MoveTemplateLine) BigDecimal(java.math.BigDecimal)

Aggregations

MoveTemplateLine (com.axelor.apps.account.db.MoveTemplateLine)5 BigDecimal (java.math.BigDecimal)4 Transactional (com.google.inject.persist.Transactional)3 Move (com.axelor.apps.account.db.Move)2 MoveLine (com.axelor.apps.account.db.MoveLine)2 Tax (com.axelor.apps.account.db.Tax)2 TaxLine (com.axelor.apps.account.db.TaxLine)2 ArrayList (java.util.ArrayList)2 MoveTemplate (com.axelor.apps.account.db.MoveTemplate)1 Partner (com.axelor.apps.base.db.Partner)1 LocalDate (java.time.LocalDate)1 HashMap (java.util.HashMap)1