Search in sources :

Example 11 with ContractVersion

use of com.axelor.apps.contract.db.ContractVersion in project axelor-open-suite by axelor.

the class ContractServiceImpl method waitingNextVersion.

@Override
@Transactional(rollbackOn = { Exception.class })
public void waitingNextVersion(Contract contract, LocalDate date) throws AxelorException {
    ContractVersion version = contract.getNextVersion();
    versionService.waiting(version, date);
    save(contract);
}
Also used : ContractVersion(com.axelor.apps.contract.db.ContractVersion) Transactional(com.google.inject.persist.Transactional)

Example 12 with ContractVersion

use of com.axelor.apps.contract.db.ContractVersion in project axelor-open-suite by axelor.

the class ContractServiceImpl method invoicingContract.

@Override
@Transactional(rollbackOn = { Exception.class })
public Invoice invoicingContract(Contract contract) throws AxelorException {
    Invoice invoice = generateInvoice(contract);
    InvoiceRepository invoiceRepository = Beans.get(InvoiceRepository.class);
    invoiceRepository.save(invoice);
    // Compute all additional lines
    List<ContractLine> additionalLines = contract.getAdditionalBenefitContractLineList().stream().filter(contractLine -> !contractLine.getIsInvoiced()).peek(contractLine -> contractLine.setIsInvoiced(true)).collect(Collectors.toList());
    for (ContractLine line : additionalLines) {
        InvoiceLine invLine = generate(invoice, line);
        invLine.setContractLine(line);
        contractLineRepo.save(line);
    }
    // Compute all classic contract lines
    for (ContractVersion version : getVersions(contract)) {
        BigDecimal ratio = BigDecimal.ONE;
        if (contract.getCurrentContractVersion().getIsTimeProratedInvoice()) {
            if (isFullProrated(contract) && !DateTool.isProrata(contract.getInvoicePeriodStartDate(), contract.getInvoicePeriodEndDate(), version.getActivationDate(), version.getEndDate())) {
                continue;
            }
            LocalDate start = version.getActivationDate().isBefore(contract.getInvoicePeriodStartDate()) ? contract.getInvoicePeriodStartDate() : version.getActivationDate();
            LocalDate end = version.getEndDate() == null || (version.getEndDate() != null && contract.getInvoicePeriodEndDate().isBefore(version.getEndDate())) ? contract.getInvoicePeriodEndDate() : version.getEndDate();
            ratio = durationService.computeRatio(start, end, contract.getCurrentContractVersion().getInvoicingDuration());
        }
        List<ContractLine> lines = version.getContractLineList().stream().filter(contractLine -> !contractLine.getIsConsumptionLine()).collect(Collectors.toList());
        for (ContractLine line : lines) {
            ContractLine tmp = contractLineRepo.copy(line, false);
            tmp.setAnalyticMoveLineList(line.getAnalyticMoveLineList());
            tmp.setQty(tmp.getQty().multiply(ratio).setScale(appBaseService.getNbDecimalDigitForQty(), RoundingMode.HALF_UP));
            tmp = this.contractLineService.computeTotal(tmp);
            InvoiceLine invLine = generate(invoice, tmp);
            invLine.setContractLine(line);
        }
    }
    // Compute all consumption lines
    Multimap<ContractLine, ConsumptionLine> consLines = mergeConsumptionLines(contract);
    for (Entry<ContractLine, Collection<ConsumptionLine>> entries : consLines.asMap().entrySet()) {
        ContractLine line = entries.getKey();
        InvoiceLine invoiceLine = generate(invoice, line);
        invoiceLine.setContractLine(line);
        entries.getValue().stream().peek(cons -> cons.setInvoiceLine(invoiceLine)).forEach(cons -> cons.setIsInvoiced(true));
        line.setQty(BigDecimal.ZERO);
        contractLineService.computeTotal(line);
    }
    // Compute invoice
    if (invoice.getInvoiceLineList() != null && !invoice.getInvoiceLineList().isEmpty()) {
        Beans.get(InvoiceServiceImpl.class).compute(invoice);
    }
    // Increase invoice period date
    increaseInvoiceDates(contract);
    return invoiceRepository.save(invoice);
}
Also used : InvoiceLineService(com.axelor.apps.account.service.invoice.InvoiceLineService) ConsumptionLine(com.axelor.apps.contract.db.ConsumptionLine) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) ContractLine(com.axelor.apps.contract.db.ContractLine) InvoiceGeneratorContract(com.axelor.apps.contract.generator.InvoiceGeneratorContract) ContractVersion(com.axelor.apps.contract.db.ContractVersion) Transactional(com.google.inject.persist.Transactional) BigDecimal(java.math.BigDecimal) HashMultimap(com.google.common.collect.HashMultimap) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) TaxLine(com.axelor.apps.account.db.TaxLine) FiscalPositionAccountService(com.axelor.apps.account.service.FiscalPositionAccountService) RoundingMode(java.math.RoundingMode) AnalyticMoveLineRepository(com.axelor.apps.account.db.repo.AnalyticMoveLineRepository) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) InvoiceService(com.axelor.apps.account.service.invoice.InvoiceService) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) InvoiceLineGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceLineGenerator) DurationService(com.axelor.apps.base.service.DurationService) LocalDate(java.time.LocalDate) Entry(java.util.Map.Entry) ContractVersionRepository(com.axelor.apps.contract.db.repo.ContractVersionRepository) FiscalPosition(com.axelor.apps.account.db.FiscalPosition) PriceListRepository(com.axelor.apps.base.db.repo.PriceListRepository) ContractRepository(com.axelor.apps.contract.db.repo.ContractRepository) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) IExceptionMessage(com.axelor.apps.contract.exception.IExceptionMessage) AxelorException(com.axelor.exception.AxelorException) ConsumptionLineRepository(com.axelor.apps.contract.db.repo.ConsumptionLineRepository) I18n(com.axelor.i18n.I18n) ContractTemplate(com.axelor.apps.contract.db.ContractTemplate) DateTool(com.axelor.apps.tool.date.DateTool) PriceListLineRepository(com.axelor.apps.base.db.repo.PriceListLineRepository) InvoiceServiceImpl(com.axelor.apps.account.service.invoice.InvoiceServiceImpl) AccountManagementService(com.axelor.apps.base.service.tax.AccountManagementService) Logger(org.slf4j.Logger) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) InvoiceLineRepository(com.axelor.apps.account.db.repo.InvoiceLineRepository) ContractLineRepository(com.axelor.apps.contract.db.repo.ContractLineRepository) Invoice(com.axelor.apps.account.db.Invoice) Account(com.axelor.apps.account.db.Account) Contract(com.axelor.apps.contract.db.Contract) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) Beans(com.axelor.inject.Beans) Collections(java.util.Collections) AuthUtils(com.axelor.auth.AuthUtils) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) Invoice(com.axelor.apps.account.db.Invoice) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) InvoiceServiceImpl(com.axelor.apps.account.service.invoice.InvoiceServiceImpl) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) ConsumptionLine(com.axelor.apps.contract.db.ConsumptionLine) ContractLine(com.axelor.apps.contract.db.ContractLine) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) Collection(java.util.Collection) ContractVersion(com.axelor.apps.contract.db.ContractVersion) Transactional(com.google.inject.persist.Transactional)

Example 13 with ContractVersion

use of com.axelor.apps.contract.db.ContractVersion in project axelor-open-suite by axelor.

the class ContractServiceImpl method renewContract.

@Override
@Transactional(rollbackOn = { Exception.class })
public void renewContract(Contract contract, LocalDate date) throws AxelorException {
    ContractVersion currentVersion = contract.getCurrentContractVersion();
    ContractVersion nextVersion = Beans.get(ContractVersionRepository.class).copy(currentVersion, true);
    versionService.terminate(currentVersion, date.minusDays(1));
    contract.addVersionHistory(currentVersion);
    currentVersion.setContract(null);
    contract.setCurrentContractVersion(nextVersion);
    nextVersion.setNextContract(null);
    nextVersion.setContract(contract);
    if (nextVersion.getIsTacitRenewal()) {
        nextVersion.setSupposedEndDate(durationService.computeDuration(nextVersion.getRenewalDuration(), date));
    }
    if (nextVersion.getIsAutoEnableVersionOnRenew()) {
        versionService.ongoing(nextVersion, date);
    } else {
        versionService.waiting(nextVersion, date);
    }
    contract.setLastRenewalDate(date);
    contract.setRenewalNumber(contract.getRenewalNumber() + 1);
    save(contract);
}
Also used : ContractVersion(com.axelor.apps.contract.db.ContractVersion) ContractVersionRepository(com.axelor.apps.contract.db.repo.ContractVersionRepository) Transactional(com.google.inject.persist.Transactional)

Example 14 with ContractVersion

use of com.axelor.apps.contract.db.ContractVersion in project axelor-open-suite by axelor.

the class ContractServiceImpl method close.

@Override
@Transactional
public void close(Contract contract, LocalDate terminationDate) {
    LocalDate today = appBaseService.getTodayDate(contract.getCompany());
    ContractVersion currentVersion = contract.getCurrentContractVersion();
    if (terminationDate.isBefore(today) || terminationDate.equals(today)) {
        versionService.terminate(currentVersion, terminationDate);
        contract.setStatusSelect(CLOSED_CONTRACT);
    }
    save(contract);
}
Also used : LocalDate(java.time.LocalDate) ContractVersion(com.axelor.apps.contract.db.ContractVersion) Transactional(com.google.inject.persist.Transactional)

Example 15 with ContractVersion

use of com.axelor.apps.contract.db.ContractVersion in project axelor-open-suite by axelor.

the class ContractServiceImpl method ongoingCurrentVersion.

@Override
@Transactional(rollbackOn = { Exception.class })
public Invoice ongoingCurrentVersion(Contract contract, LocalDate date) throws AxelorException {
    ContractVersion currentVersion = contract.getCurrentContractVersion();
    if (currentVersion.getSupposedActivationDate() != null) {
        date = currentVersion.getSupposedActivationDate();
    }
    Invoice invoice = null;
    if (currentVersion.getIsWithEngagement() && contract.getStatusSelect() != ContractRepository.ACTIVE_CONTRACT || currentVersion.getEngagementStartFromVersion()) {
        contract.setEngagementStartDate(date);
    }
    if (contract.getStatusSelect() != ContractRepository.ACTIVE_CONTRACT) {
        activeContract(contract, date);
    }
    versionService.ongoing(currentVersion, date);
    contract.setVersionNumber(contract.getVersionNumber() + 1);
    if (currentVersion.getIsPeriodicInvoicing() && contract.getVersionNumber() == 0) {
        contract.setInvoicePeriodStartDate(currentVersion.getActivationDate());
        contract.setInvoicePeriodEndDate(contract.getFirstPeriodEndDate());
    }
    if (contract.getCurrentContractVersion().getAutomaticInvoicing()) {
        if (contract.getCurrentContractVersion().getInvoicingMomentSelect() == ContractVersionRepository.BEGIN_INVOICING_MOMENT) {
            invoice = invoicingContract(contract);
        } else {
            fillInvoicingDateByInvoicingMoment(contract);
        }
    }
    return invoice;
}
Also used : Invoice(com.axelor.apps.account.db.Invoice) ContractVersion(com.axelor.apps.contract.db.ContractVersion) Transactional(com.google.inject.persist.Transactional)

Aggregations

ContractVersion (com.axelor.apps.contract.db.ContractVersion)23 Transactional (com.google.inject.persist.Transactional)12 Contract (com.axelor.apps.contract.db.Contract)9 ContractLine (com.axelor.apps.contract.db.ContractLine)5 ContractVersionRepository (com.axelor.apps.contract.db.repo.ContractVersionRepository)5 Invoice (com.axelor.apps.account.db.Invoice)4 ContractRepository (com.axelor.apps.contract.db.repo.ContractRepository)4 AxelorException (com.axelor.exception.AxelorException)3 Account (com.axelor.apps.account.db.Account)2 AnalyticMoveLine (com.axelor.apps.account.db.AnalyticMoveLine)2 FiscalPosition (com.axelor.apps.account.db.FiscalPosition)2 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)2 TaxLine (com.axelor.apps.account.db.TaxLine)2 AnalyticMoveLineRepository (com.axelor.apps.account.db.repo.AnalyticMoveLineRepository)2 InvoiceLineRepository (com.axelor.apps.account.db.repo.InvoiceLineRepository)2 InvoiceRepository (com.axelor.apps.account.db.repo.InvoiceRepository)2 FiscalPositionAccountService (com.axelor.apps.account.service.FiscalPositionAccountService)2 InvoiceLineService (com.axelor.apps.account.service.invoice.InvoiceLineService)2 InvoiceService (com.axelor.apps.account.service.invoice.InvoiceService)2 InvoiceServiceImpl (com.axelor.apps.account.service.invoice.InvoiceServiceImpl)2