Search in sources :

Example 11 with InvoiceGenerator

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

the class StockMoveInvoiceServiceImpl method createInvoiceFromPurchaseOrder.

@Override
@Transactional(rollbackOn = { Exception.class })
public Invoice createInvoiceFromPurchaseOrder(StockMove stockMove, PurchaseOrder purchaseOrder, Map<Long, BigDecimal> qtyToInvoiceMap) throws AxelorException {
    if (!supplyChainConfigService.getSupplyChainConfig(stockMove.getCompany()).getActivateIncStockMovePartialInvoicing() && computeNonCanceledInvoiceQty(stockMove).signum() > 0) {
        throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.STOCK_MOVE_PARTIAL_INVOICE_ERROR));
    }
    InvoiceGenerator invoiceGenerator = purchaseOrderInvoiceService.createInvoiceGenerator(purchaseOrder, stockMove.getIsReversion());
    Invoice invoice = invoiceGenerator.generate();
    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;
        }
        this.extendInternalReference(stockMove, invoice);
        invoice.setAddressStr(Beans.get(AddressService.class).computeAddressStr(invoice.getAddress()));
        if (invoice != null) {
            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)

Example 12 with InvoiceGenerator

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

the class StockMoveInvoiceServiceImpl method createInvoiceFromOrderlessStockMove.

@Override
@Transactional(rollbackOn = { Exception.class })
public Invoice createInvoiceFromOrderlessStockMove(StockMove stockMove, Map<Long, BigDecimal> qtyToInvoiceMap) throws AxelorException {
    int stockMoveType = stockMove.getTypeSelect();
    int invoiceOperationType;
    if (stockMove.getIsReversion()) {
        if (stockMoveType == StockMoveRepository.TYPE_INCOMING) {
            invoiceOperationType = InvoiceRepository.OPERATION_TYPE_CLIENT_REFUND;
        } else if (stockMoveType == StockMoveRepository.TYPE_OUTGOING) {
            invoiceOperationType = InvoiceRepository.OPERATION_TYPE_SUPPLIER_REFUND;
        } else {
            return null;
        }
    } else {
        if (stockMoveType == StockMoveRepository.TYPE_INCOMING) {
            invoiceOperationType = InvoiceRepository.OPERATION_TYPE_SUPPLIER_PURCHASE;
        } else if (stockMoveType == StockMoveRepository.TYPE_OUTGOING) {
            invoiceOperationType = InvoiceRepository.OPERATION_TYPE_CLIENT_SALE;
        } else {
            return null;
        }
    }
    // do not use invoiced partner if the option is disabled
    if (!appSupplychainService.getAppSupplychain().getActivatePartnerRelations()) {
        stockMove.setInvoicedPartner(null);
    }
    InvoiceGenerator invoiceGenerator = new InvoiceGeneratorSupplyChain(stockMove, invoiceOperationType) {

        @Override
        public Invoice generate() throws AxelorException {
            return super.createInvoiceHeader();
        }
    };
    Invoice invoice = invoiceGenerator.generate();
    invoiceGenerator.populate(invoice, this.createInvoiceLines(invoice, stockMove, stockMove.getStockMoveLineList(), qtyToInvoiceMap));
    if (invoice != null) {
        this.extendInternalReference(stockMove, invoice);
        invoice.setAddressStr(Beans.get(AddressService.class).computeAddressStr(invoice.getAddress()));
        if (stockMoveType == StockMoveRepository.TYPE_OUTGOING) {
            invoice.setHeadOfficeAddress(stockMove.getPartner().getHeadOfficeAddress());
        }
        invoiceRepository.save(invoice);
        if (invoice != null) {
            Set<StockMove> stockMoveSet = invoice.getStockMoveSet();
            if (stockMoveSet == null) {
                stockMoveSet = new HashSet<>();
                invoice.setStockMoveSet(stockMoveSet);
            }
            stockMoveSet.add(stockMove);
        }
        invoiceRepository.save(invoice);
    }
    return invoice;
}
Also used : Invoice(com.axelor.apps.account.db.Invoice) StockMove(com.axelor.apps.stock.db.StockMove) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator) InvoiceGeneratorSupplyChain(com.axelor.apps.supplychain.service.invoice.generator.InvoiceGeneratorSupplyChain) Transactional(com.google.inject.persist.Transactional)

Example 13 with InvoiceGenerator

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

the class InvoicingProjectService method generateInvoice.

@Transactional(rollbackOn = { Exception.class })
public Invoice generateInvoice(InvoicingProject invoicingProject) throws AxelorException {
    Project project = invoicingProject.getProject();
    Partner customer = project.getClientPartner();
    Partner customerContact = project.getContactPartner();
    if (invoicingProject.getSaleOrderLineSet().isEmpty() && invoicingProject.getPurchaseOrderLineSet().isEmpty() && invoicingProject.getLogTimesSet().isEmpty() && invoicingProject.getExpenseLineSet().isEmpty() && invoicingProject.getProjectSet().isEmpty() && invoicingProject.getProjectTaskSet().isEmpty()) {
        throw new AxelorException(invoicingProject, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVOICING_PROJECT_EMPTY));
    }
    if (invoicingProject.getProject() == null) {
        throw new AxelorException(invoicingProject, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVOICING_PROJECT_PROJECT));
    }
    if (invoicingProject.getProject().getClientPartner() == null) {
        throw new AxelorException(invoicingProject, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVOICING_PROJECT_PROJECT_PARTNER));
    }
    if (customerContact == null && customer.getContactPartnerSet().size() == 1) {
        customerContact = customer.getContactPartnerSet().iterator().next();
    }
    Company company = this.getRootCompany(project);
    if (company == null) {
        throw new AxelorException(invoicingProject, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVOICING_PROJECT_PROJECT_COMPANY));
    }
    InvoiceGenerator invoiceGenerator = new InvoiceGenerator(InvoiceRepository.OPERATION_TYPE_CLIENT_SALE, company, customer.getPaymentCondition(), customer.getInPaymentMode(), partnerService.getInvoicingAddress(customer), customer, customerContact, customer.getCurrency(), Beans.get(PartnerPriceListService.class).getDefaultPriceList(customer, PriceListRepository.TYPE_SALE), null, null, null, null, null, null) {

        @Override
        public Invoice generate() throws AxelorException {
            Invoice invoice = super.createInvoiceHeader();
            invoice.setProject(project);
            invoice.setPriceList(project.getPriceList());
            return invoice;
        }
    };
    Invoice invoice = invoiceGenerator.generate();
    AccountConfigService accountConfigService = Beans.get(AccountConfigService.class);
    AccountConfig accountConfig = accountConfigService.getAccountConfig(company);
    invoice.setDisplayTimesheetOnPrinting(accountConfig.getDisplayTimesheetOnPrinting());
    invoice.setDisplayExpenseOnPrinting(accountConfig.getDisplayExpenseOnPrinting());
    invoiceGenerator.populate(invoice, this.populate(invoice, invoicingProject));
    Beans.get(InvoiceRepository.class).save(invoice);
    invoicingProject.setInvoice(invoice);
    invoicingProject.setStatusSelect(InvoicingProjectRepository.STATUS_GENERATED);
    invoicingProjectRepo.save(invoicingProject);
    return invoice;
}
Also used : InvoicingProject(com.axelor.apps.businessproject.db.InvoicingProject) Project(com.axelor.apps.project.db.Project) AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) Invoice(com.axelor.apps.account.db.Invoice) InvoiceGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceGenerator) AccountConfigService(com.axelor.apps.account.service.config.AccountConfigService) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) Partner(com.axelor.apps.base.db.Partner) AccountConfig(com.axelor.apps.account.db.AccountConfig) Transactional(com.google.inject.persist.Transactional)

Example 14 with InvoiceGenerator

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

the class InvoiceServiceImpl method compute.

/**
 * Fonction permettant de calculer l'intégralité d'une facture :
 *
 * <ul>
 *   <li>Détermine les taxes;
 *   <li>Détermine la TVA;
 *   <li>Détermine les totaux.
 * </ul>
 *
 * (Transaction)
 *
 * @param invoice Une facture.
 * @throws AxelorException
 */
@Override
public Invoice compute(final Invoice invoice) throws AxelorException {
    log.debug("Calcul de la facture");
    InvoiceGenerator invoiceGenerator = new InvoiceGenerator() {

        @Override
        public Invoice generate() throws AxelorException {
            List<InvoiceLine> invoiceLines = new ArrayList<InvoiceLine>();
            if (invoice.getInvoiceLineList() != null) {
                invoiceLines.addAll(invoice.getInvoiceLineList());
            }
            populate(invoice, invoiceLines);
            return invoice;
        }
    };
    Invoice invoice1 = invoiceGenerator.generate();
    invoice1.setAdvancePaymentInvoiceSet(this.getDefaultAdvancePaymentInvoice(invoice1));
    return invoice1;
}
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) ArrayList(java.util.ArrayList)

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