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