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