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);
}
}
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;
}
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));
}
}
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);
}
Aggregations