Search in sources :

Example 1 with ConsumptionLine

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

the class ConsumptionLineController method changeProduct.

public void changeProduct(ActionRequest request, ActionResponse response) {
    ConsumptionLine line = request.getContext().asType(ConsumptionLine.class);
    try {
        Beans.get(ConsumptionLineService.class).fill(line, line.getProduct());
        response.setValues(line);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : ConsumptionLine(com.axelor.apps.contract.db.ConsumptionLine) ConsumptionLineService(com.axelor.apps.contract.service.ConsumptionLineService)

Example 2 with ConsumptionLine

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

the class ContractServiceImpl method mergeConsumptionLines.

@Override
public Multimap<ContractLine, ConsumptionLine> mergeConsumptionLines(Contract contract) {
    Multimap<ContractLine, ConsumptionLine> mergedLines = HashMultimap.create();
    Stream<ConsumptionLine> lineStream = contract.getConsumptionLineList().stream().filter(c -> !c.getIsInvoiced());
    if (contract.getCurrentContractVersion().getIsConsumptionBeforeEndDate()) {
        lineStream = lineStream.filter(line -> line.getLineDate().isBefore(contract.getInvoicePeriodEndDate()));
    }
    lineStream.forEach(line -> {
        ContractVersion version = contract.getCurrentContractVersion();
        if (isFullProrated(contract)) {
            version = versionService.getContractVersion(contract, line.getLineDate());
        }
        if (version == null) {
            line.setIsError(true);
        } else {
            ContractLine matchLine = contractLineRepo.findOneBy(version, line.getProduct(), line.getReference(), true);
            if (matchLine == null) {
                line.setIsError(true);
            } else {
                matchLine.setQty(matchLine.getQty().add(line.getQty()));
                contractLineService.computeTotal(matchLine);
                line.setIsError(false);
                line.setContractLine(matchLine);
                mergedLines.put(matchLine, line);
            }
        }
    });
    return mergedLines;
}
Also used : ConsumptionLine(com.axelor.apps.contract.db.ConsumptionLine) 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) ContractLine(com.axelor.apps.contract.db.ContractLine) ContractVersion(com.axelor.apps.contract.db.ContractVersion)

Example 3 with ConsumptionLine

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

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

Aggregations

ConsumptionLine (com.axelor.apps.contract.db.ConsumptionLine)4 Contract (com.axelor.apps.contract.db.Contract)3 InvoiceGeneratorContract (com.axelor.apps.contract.generator.InvoiceGeneratorContract)3 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 Invoice (com.axelor.apps.account.db.Invoice)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 InvoiceGenerator (com.axelor.apps.account.service.invoice.generator.InvoiceGenerator)2 InvoiceLineGenerator (com.axelor.apps.account.service.invoice.generator.InvoiceLineGenerator)2 PriceListLineRepository (com.axelor.apps.base.db.repo.PriceListLineRepository)2