Search in sources :

Example 1 with InvoiceGenerator

use of com.axelor.apps.account.service.invoice.generator.InvoiceGenerator in project axelor-open-suite by axelor.

the class InvoiceServiceImpl method mergeInvoice.

@Override
@Transactional(rollbackOn = { Exception.class })
public Invoice mergeInvoice(List<Invoice> invoiceList, Company company, Currency currency, Partner partner, Partner contactPartner, PriceList priceList, PaymentMode paymentMode, PaymentCondition paymentCondition) throws AxelorException {
    String numSeq = "";
    String externalRef = "";
    for (Invoice invoiceLocal : invoiceList) {
        if (!numSeq.isEmpty()) {
            numSeq += "-";
        }
        if (invoiceLocal.getInternalReference() != null) {
            numSeq += invoiceLocal.getInternalReference();
        }
        if (!externalRef.isEmpty()) {
            externalRef += "|";
        }
        if (invoiceLocal.getExternalReference() != null) {
            externalRef += invoiceLocal.getExternalReference();
        }
    }
    InvoiceGenerator invoiceGenerator = new InvoiceGenerator(InvoiceRepository.OPERATION_TYPE_CLIENT_SALE, company, paymentCondition, paymentMode, partnerService.getInvoicingAddress(partner), partner, contactPartner, currency, priceList, numSeq, externalRef, null, company.getDefaultBankDetails(), null, null) {

        @Override
        public Invoice generate() throws AxelorException {
            return super.createInvoiceHeader();
        }
    };
    Invoice invoiceMerged = invoiceGenerator.generate();
    List<InvoiceLine> invoiceLines = this.getInvoiceLinesFromInvoiceList(invoiceList);
    invoiceGenerator.populate(invoiceMerged, invoiceLines);
    this.setInvoiceForInvoiceLines(invoiceLines, invoiceMerged);
    invoiceRepo.save(invoiceMerged);
    return invoiceMerged;
}
Also used : RefundInvoice(com.axelor.apps.account.service.invoice.generator.invoice.RefundInvoice) Invoice(com.axelor.apps.account.db.Invoice) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator) Transactional(com.google.inject.persist.Transactional)

Example 2 with InvoiceGenerator

use of com.axelor.apps.account.service.invoice.generator.InvoiceGenerator in project axelor-open-suite by axelor.

the class SaleOrderInvoiceServiceImpl method createInvoiceAndLines.

public Invoice createInvoiceAndLines(SaleOrder saleOrder, List<SaleOrderLineTax> taxLineList, Product invoicingProduct, BigDecimal percentToInvoice, int operationSubTypeSelect, Account partnerAccount) throws AxelorException {
    InvoiceGenerator invoiceGenerator = this.createInvoiceGenerator(saleOrder);
    Invoice invoice = invoiceGenerator.generate();
    List<InvoiceLine> invoiceLinesList = (taxLineList != null && !taxLineList.isEmpty()) ? this.createInvoiceLinesFromTax(invoice, taxLineList, invoicingProduct, percentToInvoice) : this.createInvoiceLinesFromSO(invoice, saleOrder, invoicingProduct, percentToInvoice);
    invoiceGenerator.populate(invoice, invoiceLinesList);
    invoice.setAddressStr(saleOrder.getMainInvoicingAddressStr());
    invoice.setOperationSubTypeSelect(operationSubTypeSelect);
    if (partnerAccount != null) {
        Partner partner = invoice.getPartner();
        if (partner != null) {
            partnerAccount = Beans.get(FiscalPositionAccountService.class).getAccount(partner.getFiscalPosition(), partnerAccount);
        }
        invoice.setPartnerAccount(partnerAccount);
    }
    return invoice;
}
Also used : Invoice(com.axelor.apps.account.db.Invoice) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator) Partner(com.axelor.apps.base.db.Partner)

Example 3 with InvoiceGenerator

use of com.axelor.apps.account.service.invoice.generator.InvoiceGenerator in project axelor-open-suite by axelor.

the class SaleOrderInvoiceServiceImpl method createInvoice.

@Override
public Invoice createInvoice(SaleOrder saleOrder, List<SaleOrderLine> saleOrderLineList, Map<Long, BigDecimal> qtyToInvoiceMap) throws AxelorException {
    InvoiceGenerator invoiceGenerator = this.createInvoiceGenerator(saleOrder);
    Invoice invoice = invoiceGenerator.generate();
    invoiceGenerator.populate(invoice, this.createInvoiceLines(invoice, saleOrderLineList, qtyToInvoiceMap));
    invoice.setAddressStr(saleOrder.getMainInvoicingAddressStr());
    return invoice;
}
Also used : Invoice(com.axelor.apps.account.db.Invoice) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator)

Example 4 with InvoiceGenerator

use of com.axelor.apps.account.service.invoice.generator.InvoiceGenerator in project axelor-open-suite by axelor.

the class StockMoveMultiInvoiceServiceImpl method createInvoiceFromMultiIncomingStockMove.

@Override
@Transactional(rollbackOn = { Exception.class })
public Optional<Invoice> createInvoiceFromMultiIncomingStockMove(List<StockMove> stockMoveList) throws AxelorException {
    if (stockMoveList == null || stockMoveList.isEmpty()) {
        return Optional.empty();
    }
    // create dummy invoice from the first stock move
    Invoice dummyInvoice = createDummyInInvoice(stockMoveList.get(0));
    // Check if field constraints are respected
    for (StockMove stockMove : stockMoveList) {
        completeInvoiceInMultiIncomingStockMove(dummyInvoice, stockMove);
    }
    /*  check if some other fields are different and assign a default value */
    if (dummyInvoice.getAddress() == null) {
        dummyInvoice.setAddress(Beans.get(PartnerService.class).getInvoicingAddress(dummyInvoice.getPartner()));
        dummyInvoice.setAddressStr(Beans.get(AddressService.class).computeAddressStr(dummyInvoice.getAddress()));
    }
    fillReferenceInvoiceFromMultiInStockMove(stockMoveList, dummyInvoice);
    InvoiceGenerator invoiceGenerator = new InvoiceGenerator(InvoiceRepository.OPERATION_TYPE_SUPPLIER_PURCHASE, dummyInvoice.getCompany(), dummyInvoice.getPaymentCondition(), dummyInvoice.getPaymentMode(), dummyInvoice.getAddress(), dummyInvoice.getPartner(), dummyInvoice.getContactPartner(), dummyInvoice.getCurrency(), dummyInvoice.getPriceList(), dummyInvoice.getInternalReference(), dummyInvoice.getExternalReference(), dummyInvoice.getInAti(), null, dummyInvoice.getTradingName(), null) {

        @Override
        public Invoice generate() throws AxelorException {
            return super.createInvoiceHeader();
        }
    };
    Invoice invoice = invoiceGenerator.generate();
    invoice.setAddressStr(dummyInvoice.getAddressStr());
    List<InvoiceLine> invoiceLineList = new ArrayList<>();
    for (StockMove stockMoveLocal : stockMoveList) {
        List<InvoiceLine> createdInvoiceLines = stockMoveInvoiceService.createInvoiceLines(invoice, stockMoveLocal, stockMoveLocal.getStockMoveLineList(), null);
        if (stockMoveLocal.getTypeSelect() == StockMoveRepository.TYPE_OUTGOING) {
            createdInvoiceLines.forEach(this::negateInvoiceLinePrice);
        }
        invoiceLineList.addAll(createdInvoiceLines);
    }
    invoiceGenerator.populate(invoice, invoiceLineList);
    invoiceRepository.save(invoice);
    invoice = toPositivePriceInvoice(invoice);
    if (invoice.getExTaxTotal().signum() == 0 && stockMoveList.stream().allMatch(StockMove::getIsReversion)) {
        invoice.setOperationTypeSelect(InvoiceRepository.OPERATION_TYPE_SUPPLIER_REFUND);
    }
    stockMoveList.forEach(invoice::addStockMoveSetItem);
    return Optional.of(invoice);
}
Also used : RefundInvoice(com.axelor.apps.account.service.invoice.generator.invoice.RefundInvoice) Invoice(com.axelor.apps.account.db.Invoice) StockMove(com.axelor.apps.stock.db.StockMove) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator) ArrayList(java.util.ArrayList) Transactional(com.google.inject.persist.Transactional)

Example 5 with InvoiceGenerator

use of com.axelor.apps.account.service.invoice.generator.InvoiceGenerator in project axelor-open-suite by axelor.

the class StockMoveInvoiceServiceImpl method createInvoiceFromSaleOrder.

@Override
@Transactional(rollbackOn = { Exception.class })
public Invoice createInvoiceFromSaleOrder(StockMove stockMove, SaleOrder saleOrder, Map<Long, BigDecimal> qtyToInvoiceMap) throws AxelorException {
    // we block if we are trying to invoice partially if config is deactivated
    if (!supplyChainConfigService.getSupplyChainConfig(stockMove.getCompany()).getActivateOutStockMovePartialInvoicing() && computeNonCanceledInvoiceQty(stockMove).signum() > 0) {
        throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.STOCK_MOVE_PARTIAL_INVOICE_ERROR), stockMove.getStockMoveSeq());
    }
    InvoiceGenerator invoiceGenerator = saleOrderInvoiceService.createInvoiceGenerator(saleOrder, stockMove.getIsReversion());
    Invoice invoice = invoiceGenerator.generate();
    checkSplitSalePartiallyInvoicedStockMoveLines(stockMove, stockMove.getStockMoveLineList());
    invoiceGenerator.populate(invoice, this.createInvoiceLines(invoice, stockMove, stockMove.getStockMoveLineList(), qtyToInvoiceMap));
    if (invoice != null) {
        // do not create empty invoices
        if (invoice.getInvoiceLineList() == null || invoice.getInvoiceLineList().isEmpty()) {
            return null;
        }
        invoice.setSaleOrder(saleOrder);
        this.extendInternalReference(stockMove, invoice);
        invoice.setDeliveryAddress(stockMove.getToAddress());
        invoice.setDeliveryAddressStr(stockMove.getToAddressStr());
        invoice.setAddressStr(saleOrder.getMainInvoicingAddressStr());
        // fill default advance payment invoice
        if (invoice.getOperationSubTypeSelect() != InvoiceRepository.OPERATION_SUB_TYPE_ADVANCE) {
            invoice.setAdvancePaymentInvoiceSet(Beans.get(InvoiceService.class).getDefaultAdvancePaymentInvoice(invoice));
        }
        invoice.setPartnerTaxNbr(saleOrder.getClientPartner().getTaxNbr());
        if (!Strings.isNullOrEmpty(saleOrder.getInvoiceComments())) {
            invoice.setNote(saleOrder.getInvoiceComments());
        }
        if (ObjectUtils.isEmpty(invoice.getProformaComments()) && !Strings.isNullOrEmpty(saleOrder.getProformaComments())) {
            invoice.setProformaComments(saleOrder.getProformaComments());
        }
        Set<StockMove> stockMoveSet = invoice.getStockMoveSet();
        if (stockMoveSet == null) {
            stockMoveSet = new HashSet<>();
            invoice.setStockMoveSet(stockMoveSet);
        }
        stockMoveSet.add(stockMove);
        invoiceRepository.save(invoice);
    }
    return invoice;
}
Also used : AxelorException(com.axelor.exception.AxelorException) Invoice(com.axelor.apps.account.db.Invoice) StockMove(com.axelor.apps.stock.db.StockMove) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator) Transactional(com.google.inject.persist.Transactional)

Aggregations

Invoice (com.axelor.apps.account.db.Invoice)14 InvoiceGenerator (com.axelor.apps.account.service.invoice.generator.InvoiceGenerator)14 Transactional (com.google.inject.persist.Transactional)8 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)7 StockMove (com.axelor.apps.stock.db.StockMove)5 RefundInvoice (com.axelor.apps.account.service.invoice.generator.invoice.RefundInvoice)4 AxelorException (com.axelor.exception.AxelorException)3 ArrayList (java.util.ArrayList)3 Partner (com.axelor.apps.base.db.Partner)2 AccountConfig (com.axelor.apps.account.db.AccountConfig)1 InvoiceRepository (com.axelor.apps.account.db.repo.InvoiceRepository)1 AccountConfigService (com.axelor.apps.account.service.config.AccountConfigService)1 Address (com.axelor.apps.base.db.Address)1 Company (com.axelor.apps.base.db.Company)1 AddressService (com.axelor.apps.base.service.AddressService)1 InvoicingProject (com.axelor.apps.businessproject.db.InvoicingProject)1 InvoiceGeneratorContract (com.axelor.apps.contract.generator.InvoiceGeneratorContract)1 Project (com.axelor.apps.project.db.Project)1 InvoiceGeneratorSupplyChain (com.axelor.apps.supplychain.service.invoice.generator.InvoiceGeneratorSupplyChain)1 StringTool.getIdListString (com.axelor.apps.tool.StringTool.getIdListString)1