Search in sources :

Example 1 with InvoiceRepository

use of com.axelor.apps.account.db.repo.InvoiceRepository in project axelor-open-suite by axelor.

the class InvoicePaymentCreateServiceImpl method createMassInvoicePayment.

@Override
@Transactional(rollbackOn = { Exception.class })
public List<InvoicePayment> createMassInvoicePayment(List<Long> invoiceList, PaymentMode paymentMode, BankDetails companyBankDetails, LocalDate paymentDate, LocalDate bankDepositDate, String chequeNumber) throws AxelorException {
    List<InvoicePayment> invoicePaymentList = new ArrayList<>();
    InvoiceRepository invoiceRepository = Beans.get(InvoiceRepository.class);
    for (Long invoiceId : invoiceList) {
        Invoice invoice = invoiceRepository.find(invoiceId);
        InvoicePayment invoicePayment = this.createInvoicePayment(invoice, paymentMode, companyBankDetails, paymentDate, bankDepositDate, chequeNumber);
        invoicePaymentList.add(invoicePayment);
        invoice.addInvoicePaymentListItem(invoicePayment);
        invoicePaymentToolService.updateAmountPaid(invoice);
    }
    return invoicePaymentList;
}
Also used : InvoicePayment(com.axelor.apps.account.db.InvoicePayment) Invoice(com.axelor.apps.account.db.Invoice) ArrayList(java.util.ArrayList) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) Transactional(com.google.inject.persist.Transactional)

Example 2 with InvoiceRepository

use of com.axelor.apps.account.db.repo.InvoiceRepository in project axelor-open-suite by axelor.

the class IntercoServiceImpl method generateIntercoInvoice.

@Override
public Invoice generateIntercoInvoice(Invoice invoice) throws AxelorException {
    PartnerService partnerService = Beans.get(PartnerService.class);
    InvoiceRepository invoiceRepository = Beans.get(InvoiceRepository.class);
    InvoiceService invoiceService = Beans.get(InvoiceService.class);
    boolean isPurchase;
    // set the status
    int generatedOperationTypeSelect;
    int priceListRepositoryType;
    switch(invoice.getOperationTypeSelect()) {
        case InvoiceRepository.OPERATION_TYPE_SUPPLIER_PURCHASE:
            generatedOperationTypeSelect = InvoiceRepository.OPERATION_TYPE_CLIENT_SALE;
            priceListRepositoryType = PriceListRepository.TYPE_SALE;
            isPurchase = false;
            break;
        case InvoiceRepository.OPERATION_TYPE_SUPPLIER_REFUND:
            generatedOperationTypeSelect = InvoiceRepository.OPERATION_TYPE_CLIENT_REFUND;
            priceListRepositoryType = PriceListRepository.TYPE_SALE;
            isPurchase = false;
            break;
        case InvoiceRepository.OPERATION_TYPE_CLIENT_SALE:
            generatedOperationTypeSelect = InvoiceRepository.OPERATION_TYPE_SUPPLIER_PURCHASE;
            priceListRepositoryType = PriceListRepository.TYPE_PURCHASE;
            isPurchase = true;
            break;
        case InvoiceRepository.OPERATION_TYPE_CLIENT_REFUND:
            generatedOperationTypeSelect = InvoiceRepository.OPERATION_TYPE_SUPPLIER_REFUND;
            priceListRepositoryType = PriceListRepository.TYPE_PURCHASE;
            isPurchase = true;
            break;
        default:
            throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, I18n.get(IExceptionMessage.INVOICE_MISSING_TYPE), invoice);
    }
    Company intercoCompany = findIntercoCompany(invoice.getPartner());
    Partner intercoPartner = invoice.getCompany().getPartner();
    PaymentMode intercoPaymentMode = Beans.get(PaymentModeService.class).reverseInOut(invoice.getPaymentMode());
    Address intercoAddress = partnerService.getInvoicingAddress(intercoPartner);
    BankDetails intercoBankDetails = partnerService.getDefaultBankDetails(intercoPartner);
    AccountingSituation accountingSituation = Beans.get(AccountingSituationService.class).getAccountingSituation(intercoPartner, intercoCompany);
    PriceList intercoPriceList = Beans.get(PartnerPriceListService.class).getDefaultPriceList(intercoPartner, priceListRepositoryType);
    Invoice intercoInvoice = invoiceRepository.copy(invoice, true);
    intercoInvoice.setOperationTypeSelect(generatedOperationTypeSelect);
    intercoInvoice.setCompany(intercoCompany);
    intercoInvoice.setPartner(intercoPartner);
    intercoInvoice.setAddress(intercoAddress);
    intercoInvoice.setAddressStr(Beans.get(AddressService.class).computeAddressStr(intercoAddress));
    intercoInvoice.setPaymentMode(intercoPaymentMode);
    intercoInvoice.setBankDetails(intercoBankDetails);
    Set<Invoice> invoices = invoiceService.getDefaultAdvancePaymentInvoice(intercoInvoice);
    intercoInvoice.setAdvancePaymentInvoiceSet(invoices);
    if (accountingSituation != null) {
        intercoInvoice.setInvoiceAutomaticMail(accountingSituation.getInvoiceAutomaticMail());
        intercoInvoice.setInvoiceMessageTemplate(accountingSituation.getInvoiceMessageTemplate());
        intercoInvoice.setPfpValidatorUser(accountingSituation.getPfpValidatorUser());
    }
    intercoInvoice.setPriceList(intercoPriceList);
    intercoInvoice.setInvoicesCopySelect((intercoPartner.getInvoicesCopySelect() == 0) ? DEFAULT_INVOICE_COPY : intercoPartner.getInvoicesCopySelect());
    intercoInvoice.setCreatedByInterco(true);
    intercoInvoice.setInterco(false);
    intercoInvoice.setPrintingSettings(intercoCompany.getPrintingSettings());
    if (intercoInvoice.getInvoiceLineList() != null) {
        for (InvoiceLine invoiceLine : intercoInvoice.getInvoiceLineList()) {
            invoiceLine.setInvoice(intercoInvoice);
            createIntercoInvoiceLine(invoiceLine, isPurchase);
        }
    }
    invoiceService.compute(intercoInvoice);
    intercoInvoice.setExternalReference(invoice.getInvoiceId());
    intercoInvoice = invoiceRepository.save(intercoInvoice);
    if (Beans.get(AppSupplychainService.class).getAppSupplychain().getIntercoInvoiceCreateValidated()) {
        Beans.get(InvoiceService.class).validate(intercoInvoice);
    }
    invoice.setExternalReference(intercoInvoice.getInvoiceId());
    return intercoInvoice;
}
Also used : AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) PaymentModeService(com.axelor.apps.account.service.payment.PaymentModeService) Invoice(com.axelor.apps.account.db.Invoice) Address(com.axelor.apps.base.db.Address) PartnerPriceListService(com.axelor.apps.base.service.PartnerPriceListService) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) BankDetails(com.axelor.apps.base.db.BankDetails) InvoiceService(com.axelor.apps.account.service.invoice.InvoiceService) PartnerService(com.axelor.apps.base.service.PartnerService) AccountingSituation(com.axelor.apps.account.db.AccountingSituation) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) Partner(com.axelor.apps.base.db.Partner) PriceList(com.axelor.apps.base.db.PriceList) AccountingSituationService(com.axelor.apps.account.service.AccountingSituationService) PaymentMode(com.axelor.apps.account.db.PaymentMode)

Example 3 with InvoiceRepository

use of com.axelor.apps.account.db.repo.InvoiceRepository 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

Invoice (com.axelor.apps.account.db.Invoice)3 InvoiceRepository (com.axelor.apps.account.db.repo.InvoiceRepository)3 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)2 InvoiceService (com.axelor.apps.account.service.invoice.InvoiceService)2 AxelorException (com.axelor.exception.AxelorException)2 Transactional (com.google.inject.persist.Transactional)2 ArrayList (java.util.ArrayList)2 Account (com.axelor.apps.account.db.Account)1 AccountingSituation (com.axelor.apps.account.db.AccountingSituation)1 AnalyticMoveLine (com.axelor.apps.account.db.AnalyticMoveLine)1 FiscalPosition (com.axelor.apps.account.db.FiscalPosition)1 InvoicePayment (com.axelor.apps.account.db.InvoicePayment)1 PaymentMode (com.axelor.apps.account.db.PaymentMode)1 TaxLine (com.axelor.apps.account.db.TaxLine)1 AnalyticMoveLineRepository (com.axelor.apps.account.db.repo.AnalyticMoveLineRepository)1 InvoiceLineRepository (com.axelor.apps.account.db.repo.InvoiceLineRepository)1 AccountingSituationService (com.axelor.apps.account.service.AccountingSituationService)1 FiscalPositionAccountService (com.axelor.apps.account.service.FiscalPositionAccountService)1 InvoiceLineService (com.axelor.apps.account.service.invoice.InvoiceLineService)1 InvoiceServiceImpl (com.axelor.apps.account.service.invoice.InvoiceServiceImpl)1