Search in sources :

Example 6 with InvoiceGenerator

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

the class SaleOrderInvoiceServiceImpl method mergeInvoice.

@Override
@Transactional
public Invoice mergeInvoice(List<Invoice> invoiceList, Company company, Currency currency, Partner partner, Partner contactPartner, PriceList priceList, PaymentMode paymentMode, PaymentCondition paymentCondition, SaleOrder saleOrder) throws AxelorException {
    log.debug("service supplychain 1 (saleOrder) {}", saleOrder);
    if (saleOrder != null) {
        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 = this.createInvoiceGenerator(saleOrder);
        Invoice invoiceMerged = invoiceGenerator.generate();
        invoiceMerged.setExternalReference(externalRef);
        invoiceMerged.setInternalReference(numSeq);
        if (paymentMode != null)
            invoiceMerged.setPaymentMode(paymentMode);
        if (paymentCondition != null)
            invoiceMerged.setPaymentCondition(paymentCondition);
        List<InvoiceLine> invoiceLines = invoiceService.getInvoiceLinesFromInvoiceList(invoiceList);
        invoiceGenerator.populate(invoiceMerged, invoiceLines);
        invoiceService.setInvoiceForInvoiceLines(invoiceLines, invoiceMerged);
        invoiceMerged.setSaleOrder(null);
        invoiceRepo.save(invoiceMerged);
        swapStockMoveInvoices(invoiceList, invoiceMerged);
        invoiceService.deleteOldInvoices(invoiceList);
        return invoiceMerged;
    } else {
        Invoice invoiceMerged = invoiceService.mergeInvoice(invoiceList, company, currency, partner, contactPartner, priceList, paymentMode, paymentCondition);
        swapStockMoveInvoices(invoiceList, invoiceMerged);
        invoiceService.deleteOldInvoices(invoiceList);
        return invoiceMerged;
    }
}
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) StringTool.getIdListString(com.axelor.apps.tool.StringTool.getIdListString) Transactional(com.google.inject.persist.Transactional)

Example 7 with InvoiceGenerator

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

the class PurchaseOrderInvoiceServiceImpl method createInvoice.

public Invoice createInvoice(PurchaseOrder purchaseOrder, List<PurchaseOrderLine> purchaseOrderLineList, Map<Long, BigDecimal> qtyToInvoiceMap) throws AxelorException {
    InvoiceGenerator invoiceGenerator = this.createInvoiceGenerator(purchaseOrder);
    Invoice invoice = invoiceGenerator.generate();
    invoiceGenerator.populate(invoice, this.createInvoiceLines(invoice, purchaseOrderLineList, qtyToInvoiceMap));
    return invoice;
}
Also used : Invoice(com.axelor.apps.account.db.Invoice) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator)

Example 8 with InvoiceGenerator

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

the class PurchaseOrderInvoiceServiceImpl method createInvoice.

@Override
public Invoice createInvoice(PurchaseOrder purchaseOrder) throws AxelorException {
    InvoiceGenerator invoiceGenerator = this.createInvoiceGenerator(purchaseOrder);
    Invoice invoice = invoiceGenerator.generate();
    List<InvoiceLine> invoiceLineList = this.createInvoiceLines(invoice, purchaseOrder.getPurchaseOrderLineList());
    invoiceGenerator.populate(invoice, invoiceLineList);
    invoice.setPurchaseOrder(purchaseOrder);
    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)

Example 9 with InvoiceGenerator

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

the class ContractServiceImpl method generateInvoice.

public Invoice generateInvoice(Contract contract) throws AxelorException {
    InvoiceGenerator invoiceGenerator = new InvoiceGeneratorContract(contract);
    Invoice invoice = invoiceGenerator.generate();
    return invoice;
}
Also used : Invoice(com.axelor.apps.account.db.Invoice) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator) InvoiceGeneratorContract(com.axelor.apps.contract.generator.InvoiceGeneratorContract)

Example 10 with InvoiceGenerator

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

the class StockMoveMultiInvoiceServiceImpl method createInvoiceFromMultiOutgoingStockMove.

@Override
@Transactional(rollbackOn = { Exception.class })
public Optional<Invoice> createInvoiceFromMultiOutgoingStockMove(List<StockMove> stockMoveList) throws AxelorException {
    if (stockMoveList == null || stockMoveList.isEmpty()) {
        return Optional.empty();
    }
    Set<Address> deliveryAddressSet = new HashSet<>();
    // create dummy invoice from the first stock move
    Invoice dummyInvoice = createDummyOutInvoice(stockMoveList.get(0));
    // Check if field constraints are respected
    for (StockMove stockMove : stockMoveList) {
        completeInvoiceInMultiOutgoingStockMove(dummyInvoice, stockMove);
        if (stockMove.getToAddressStr() != null) {
            deliveryAddressSet.add(stockMove.getToAddress());
        }
    }
    /*  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()));
    }
    fillReferenceInvoiceFromMultiOutStockMove(stockMoveList, dummyInvoice);
    InvoiceGenerator invoiceGenerator = new InvoiceGenerator(InvoiceRepository.OPERATION_TYPE_CLIENT_SALE, 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(), dummyInvoice.getGroupProductsOnPrintings()) {

        @Override
        public Invoice generate() throws AxelorException {
            Invoice invoice = super.createInvoiceHeader();
            invoice.setPartnerTaxNbr(partner.getTaxNbr());
            return invoice;
        }
    };
    Invoice invoice = invoiceGenerator.generate();
    invoice.setAddressStr(dummyInvoice.getAddressStr());
    StringBuilder deliveryAddressStr = new StringBuilder();
    AddressService addressService = Beans.get(AddressService.class);
    for (Address address : deliveryAddressSet) {
        deliveryAddressStr.append(addressService.computeAddressStr(address).replaceAll("\n", ", ") + "\n");
    }
    invoice.setDeliveryAddressStr(deliveryAddressStr.toString());
    List<InvoiceLine> invoiceLineList = new ArrayList<>();
    for (StockMove stockMoveLocal : stockMoveList) {
        stockMoveInvoiceService.checkSplitSalePartiallyInvoicedStockMoveLines(stockMoveLocal, stockMoveLocal.getStockMoveLineList());
        List<InvoiceLine> createdInvoiceLines = stockMoveInvoiceService.createInvoiceLines(invoice, stockMoveLocal, stockMoveLocal.getStockMoveLineList(), null);
        if (stockMoveLocal.getTypeSelect() == StockMoveRepository.TYPE_INCOMING) {
            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_CLIENT_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) Address(com.axelor.apps.base.db.Address) AddressService(com.axelor.apps.base.service.AddressService) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) 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