Search in sources :

Example 81 with Invoice

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

the class StockMoveMultiInvoiceServiceImpl method areFieldsConflictedToGenerateSupplierInvoice.

@Override
public Map<String, Object> areFieldsConflictedToGenerateSupplierInvoice(List<StockMove> stockMoveList) throws AxelorException {
    Map<String, Object> mapResult = new HashMap<>();
    boolean paymentConditionToCheck = false;
    boolean paymentModeToCheck = false;
    boolean contactPartnerToCheck = false;
    checkForAlreadyInvoicedStockMove(stockMoveList);
    List<Invoice> dummyInvoiceList = stockMoveList.stream().map(this::createDummyInInvoice).collect(Collectors.toList());
    checkInStockMoveRequiredFieldsAreTheSame(dummyInvoiceList);
    if (!dummyInvoiceList.isEmpty()) {
        PaymentCondition firstPaymentCondition = dummyInvoiceList.get(0).getPaymentCondition();
        PaymentMode firstPaymentMode = dummyInvoiceList.get(0).getPaymentMode();
        Partner firstContactPartner = dummyInvoiceList.get(0).getContactPartner();
        paymentConditionToCheck = !dummyInvoiceList.stream().map(Invoice::getPaymentCondition).allMatch(paymentCondition -> Objects.equals(paymentCondition, firstPaymentCondition));
        paymentModeToCheck = !dummyInvoiceList.stream().map(Invoice::getPaymentMode).allMatch(paymentMode -> Objects.equals(paymentMode, firstPaymentMode));
        contactPartnerToCheck = !dummyInvoiceList.stream().map(Invoice::getContactPartner).allMatch(contactPartner -> Objects.equals(contactPartner, firstContactPartner));
        mapResult.put("paymentCondition", firstPaymentCondition);
        mapResult.put("paymentMode", firstPaymentMode);
        mapResult.put("contactPartner", firstContactPartner);
    }
    mapResult.put("paymentConditionToCheck", paymentConditionToCheck);
    mapResult.put("paymentModeToCheck", paymentModeToCheck);
    mapResult.put("contactPartnerToCheck", contactPartnerToCheck);
    return mapResult;
}
Also used : PaymentCondition(com.axelor.apps.account.db.PaymentCondition) RefundInvoice(com.axelor.apps.account.service.invoice.generator.invoice.RefundInvoice) Invoice(com.axelor.apps.account.db.Invoice) HashMap(java.util.HashMap) Partner(com.axelor.apps.base.db.Partner) PaymentMode(com.axelor.apps.account.db.PaymentMode)

Example 82 with Invoice

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

the class StockMoveMultiInvoiceServiceImpl method createDummyInInvoice.

/**
 * Create a dummy invoice to hold fields used to generate the invoice which will be saved.
 *
 * @param stockMove an in stock move.
 * @return the created dummy invoice.
 */
protected Invoice createDummyInInvoice(StockMove stockMove) {
    Invoice dummyInvoice = new Invoice();
    if (stockMove.getOriginId() != null && StockMoveRepository.ORIGIN_PURCHASE_ORDER.equals(stockMove.getOriginTypeSelect())) {
        PurchaseOrder purchaseOrder = purchaseOrderRepository.find(stockMove.getOriginId());
        dummyInvoice.setCurrency(purchaseOrder.getCurrency());
        dummyInvoice.setPartner(purchaseOrder.getSupplierPartner());
        dummyInvoice.setCompany(purchaseOrder.getCompany());
        dummyInvoice.setTradingName(purchaseOrder.getTradingName());
        dummyInvoice.setPaymentCondition(purchaseOrder.getPaymentCondition());
        dummyInvoice.setPaymentMode(purchaseOrder.getPaymentMode());
        dummyInvoice.setContactPartner(purchaseOrder.getContactPartner());
        dummyInvoice.setPriceList(purchaseOrder.getPriceList());
        dummyInvoice.setInAti(purchaseOrder.getInAti());
    } else {
        dummyInvoice.setCurrency(stockMove.getCompany().getCurrency());
        dummyInvoice.setPartner(stockMove.getPartner());
        dummyInvoice.setCompany(stockMove.getCompany());
        dummyInvoice.setTradingName(stockMove.getTradingName());
        dummyInvoice.setAddress(stockMove.getFromAddress());
        dummyInvoice.setAddressStr(stockMove.getFromAddressStr());
    }
    return dummyInvoice;
}
Also used : RefundInvoice(com.axelor.apps.account.service.invoice.generator.invoice.RefundInvoice) Invoice(com.axelor.apps.account.db.Invoice) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder)

Example 83 with Invoice

use of com.axelor.apps.account.db.Invoice 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 84 with Invoice

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

the class StockMoveMultiInvoiceServiceImpl method createDummyOutInvoice.

/**
 * Create a dummy invoice to hold fields used to generate the invoice which will be saved.
 *
 * @param stockMove an out stock move.
 * @return the created dummy invoice.
 */
protected Invoice createDummyOutInvoice(StockMove stockMove) {
    Invoice dummyInvoice = new Invoice();
    if (stockMove.getOriginId() != null && StockMoveRepository.ORIGIN_SALE_ORDER.equals(stockMove.getOriginTypeSelect())) {
        SaleOrder saleOrder = saleOrderRepository.find(stockMove.getOriginId());
        dummyInvoice.setCurrency(saleOrder.getCurrency());
        dummyInvoice.setPartner(saleOrder.getClientPartner());
        dummyInvoice.setCompany(saleOrder.getCompany());
        dummyInvoice.setTradingName(saleOrder.getTradingName());
        dummyInvoice.setPaymentCondition(saleOrder.getPaymentCondition());
        dummyInvoice.setPaymentMode(saleOrder.getPaymentMode());
        dummyInvoice.setAddress(saleOrder.getMainInvoicingAddress());
        dummyInvoice.setAddressStr(saleOrder.getMainInvoicingAddressStr());
        dummyInvoice.setContactPartner(saleOrder.getContactPartner());
        dummyInvoice.setPriceList(saleOrder.getPriceList());
        dummyInvoice.setInAti(saleOrder.getInAti());
        dummyInvoice.setGroupProductsOnPrintings(saleOrder.getGroupProductsOnPrintings());
    } else {
        dummyInvoice.setCurrency(stockMove.getCompany().getCurrency());
        dummyInvoice.setPartner(stockMove.getPartner());
        dummyInvoice.setCompany(stockMove.getCompany());
        dummyInvoice.setTradingName(stockMove.getTradingName());
        dummyInvoice.setAddress(stockMove.getToAddress());
        dummyInvoice.setAddressStr(stockMove.getToAddressStr());
        dummyInvoice.setGroupProductsOnPrintings(stockMove.getGroupProductsOnPrintings());
    }
    return dummyInvoice;
}
Also used : RefundInvoice(com.axelor.apps.account.service.invoice.generator.invoice.RefundInvoice) Invoice(com.axelor.apps.account.db.Invoice) SaleOrder(com.axelor.apps.sale.db.SaleOrder)

Example 85 with Invoice

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

the class StockMoveMultiInvoiceServiceImpl method toPositivePriceInvoice.

/**
 * For any invoice, if ex tax total is negative, returns the corresponding refund invoice.
 */
protected Invoice toPositivePriceInvoice(Invoice invoice) throws AxelorException {
    if (invoice.getExTaxTotal().signum() < 0) {
        Invoice refund = transformToRefund(invoice);
        invoiceRepository.remove(invoice);
        return refund;
    } else {
        return invoice;
    }
}
Also used : RefundInvoice(com.axelor.apps.account.service.invoice.generator.invoice.RefundInvoice) Invoice(com.axelor.apps.account.db.Invoice)

Aggregations

Invoice (com.axelor.apps.account.db.Invoice)195 AxelorException (com.axelor.exception.AxelorException)69 ArrayList (java.util.ArrayList)48 Transactional (com.google.inject.persist.Transactional)46 BigDecimal (java.math.BigDecimal)32 Company (com.axelor.apps.base.db.Company)31 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)29 Partner (com.axelor.apps.base.db.Partner)27 InvoiceRepository (com.axelor.apps.account.db.repo.InvoiceRepository)22 InvoiceService (com.axelor.apps.account.service.invoice.InvoiceService)22 PaymentMode (com.axelor.apps.account.db.PaymentMode)21 Map (java.util.Map)21 List (java.util.List)20 StockMove (com.axelor.apps.stock.db.StockMove)19 RefundInvoice (com.axelor.apps.account.service.invoice.generator.invoice.RefundInvoice)18 MoveLine (com.axelor.apps.account.db.MoveLine)17 LocalDate (java.time.LocalDate)17 InvoiceGenerator (com.axelor.apps.account.service.invoice.generator.InvoiceGenerator)16 Context (com.axelor.rpc.Context)15 InvoicePayment (com.axelor.apps.account.db.InvoicePayment)14