Search in sources :

Example 11 with Contract

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

the class ContractServiceImpl method checkInvoicedConsumptionLines.

protected void checkInvoicedConsumptionLines(Contract contract) throws AxelorException {
    Contract origin = find(contract.getId());
    List<ConsumptionLine> lineInvoiced = origin.getConsumptionLineList().stream().filter(ConsumptionLine::getIsInvoiced).collect(Collectors.toList());
    for (ConsumptionLine line : contract.getConsumptionLineList()) {
        if (lineInvoiced.contains(line)) {
            lineInvoiced.remove(line);
        }
    }
    if (!lineInvoiced.isEmpty()) {
        throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.CONTRACT_CANT_REMOVE_INVOICED_LINE));
    }
}
Also used : ConsumptionLine(com.axelor.apps.contract.db.ConsumptionLine) AxelorException(com.axelor.exception.AxelorException) InvoiceGeneratorContract(com.axelor.apps.contract.generator.InvoiceGeneratorContract) Contract(com.axelor.apps.contract.db.Contract)

Example 12 with Contract

use of com.axelor.apps.contract.db.Contract 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 Contract

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

the class ContractVersionServiceImpl method waiting.

@Override
@Transactional(rollbackOn = { Exception.class })
public void waiting(ContractVersion version, LocalDate date) throws AxelorException {
    Contract contract = Stream.of(version.getContract(), version.getNextContract()).filter(Objects::nonNull).findFirst().orElseThrow(() -> new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.CONTRACT_MISSING_FROM_VERSION)));
    if (contract.getIsInvoicingManagement() && version.getIsPeriodicInvoicing() && (contract.getFirstPeriodEndDate() == null || version.getInvoicingDuration() == null)) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CONTRACT_MISSING_FIRST_PERIOD));
    }
    version.setStatusSelect(WAITING_VERSION);
}
Also used : AxelorException(com.axelor.exception.AxelorException) Objects(java.util.Objects) Contract(com.axelor.apps.contract.db.Contract) Transactional(com.google.inject.persist.Transactional)

Example 14 with Contract

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

the class ContractController method terminated.

public void terminated(ActionRequest request, ActionResponse response) {
    Contract contract = Beans.get(ContractRepository.class).find(request.getContext().asType(Contract.class).getId());
    try {
        ContractService service = Beans.get(ContractService.class);
        service.checkCanTerminateContract(contract);
        service.terminateContract(contract, true, contract.getTerminatedDate());
        response.setReload(true);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : ContractRepository(com.axelor.apps.contract.db.repo.ContractRepository) ContractService(com.axelor.apps.contract.service.ContractService) Contract(com.axelor.apps.contract.db.Contract)

Example 15 with Contract

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

the class ContractController method copyFromTemplate.

public void copyFromTemplate(ActionRequest request, ActionResponse response) {
    try {
        ContractTemplate template = ModelTool.toBean(ContractTemplate.class, request.getContext().get("contractTemplate"));
        template = Beans.get(ContractTemplateRepository.class).find(template.getId());
        Contract contract = Beans.get(ContractRepository.class).find(request.getContext().asType(Contract.class).getId());
        Beans.get(ContractService.class).copyFromTemplate(contract, template);
        response.setReload(true);
    } catch (Exception e) {
        TraceBackService.trace(response, e, ResponseMessageType.ERROR);
    }
}
Also used : ContractTemplate(com.axelor.apps.contract.db.ContractTemplate) ContractRepository(com.axelor.apps.contract.db.repo.ContractRepository) ContractService(com.axelor.apps.contract.service.ContractService) Contract(com.axelor.apps.contract.db.Contract)

Aggregations

Contract (com.axelor.apps.contract.db.Contract)21 ContractRepository (com.axelor.apps.contract.db.repo.ContractRepository)10 ContractVersion (com.axelor.apps.contract.db.ContractVersion)9 ContractService (com.axelor.apps.contract.service.ContractService)7 ContractLine (com.axelor.apps.contract.db.ContractLine)6 AxelorException (com.axelor.exception.AxelorException)5 InvoiceGeneratorContract (com.axelor.apps.contract.generator.InvoiceGeneratorContract)4 Invoice (com.axelor.apps.account.db.Invoice)3 ConsumptionLine (com.axelor.apps.contract.db.ConsumptionLine)3 ContractTemplate (com.axelor.apps.contract.db.ContractTemplate)3 ContractVersionRepository (com.axelor.apps.contract.db.repo.ContractVersionRepository)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