Search in sources :

Example 61 with SaleOrder

use of com.axelor.apps.sale.db.SaleOrder in project axelor-open-suite by axelor.

the class WorkflowCancelServiceSupplychainImpl method saleOrderProcess.

public void saleOrderProcess(Invoice invoice) throws AxelorException {
    SaleOrder invoiceSaleOrder = invoice.getSaleOrder();
    if (invoiceSaleOrder != null) {
        log.debug("Update the invoiced amount of the sale order : {}", invoiceSaleOrder.getSaleOrderSeq());
        invoiceSaleOrder.setAmountInvoiced(saleOrderInvoiceService.getInvoicedAmount(invoiceSaleOrder, invoice.getId(), true));
    } else {
        // Get all different saleOrders from invoice
        List<SaleOrder> saleOrderList = Lists.newArrayList();
        for (InvoiceLine invoiceLine : invoice.getInvoiceLineList()) {
            SaleOrder saleOrder = this.saleOrderLineProcess(invoice, invoiceLine);
            if (saleOrder != null && !saleOrderList.contains(saleOrder)) {
                saleOrderList.add(saleOrder);
            }
        }
        for (SaleOrder saleOrder : saleOrderList) {
            log.debug("Update the invoiced amount of the sale order : {}", saleOrder.getSaleOrderSeq());
            saleOrder.setAmountInvoiced(saleOrderInvoiceService.getInvoicedAmount(saleOrder, invoice.getId(), true));
            saleOrderRepository.save(saleOrder);
        }
    }
}
Also used : InvoiceLine(com.axelor.apps.account.db.InvoiceLine) SaleOrder(com.axelor.apps.sale.db.SaleOrder)

Example 62 with SaleOrder

use of com.axelor.apps.sale.db.SaleOrder in project axelor-open-suite by axelor.

the class WorkflowVentilationServiceSupplychainImpl method saleOrderLineProcess.

private SaleOrder saleOrderLineProcess(Invoice invoice, InvoiceLine invoiceLine) throws AxelorException {
    SaleOrderLine saleOrderLine = invoiceLine.getSaleOrderLine();
    if (saleOrderLine == null) {
        return null;
    }
    SaleOrder saleOrder = saleOrderLine.getSaleOrder();
    // Update invoiced amount on sale order line
    BigDecimal invoicedAmountToAdd = invoiceLine.getExTaxTotal();
    // If is it a refund invoice, so we negate the amount invoiced
    if (InvoiceToolService.isRefund(invoiceLine.getInvoice())) {
        invoicedAmountToAdd = invoicedAmountToAdd.negate();
    }
    if (!invoice.getCurrency().equals(saleOrder.getCurrency()) && saleOrderLine.getCompanyExTaxTotal().compareTo(BigDecimal.ZERO) != 0) {
        // If the sale order currency is different from the invoice currency, use company currency to
        // calculate a rate. This rate will be applied to sale order line
        BigDecimal currentCompanyInvoicedAmount = invoiceLine.getCompanyExTaxTotal();
        BigDecimal rate = currentCompanyInvoicedAmount.divide(saleOrderLine.getCompanyExTaxTotal(), 4, RoundingMode.HALF_UP);
        invoicedAmountToAdd = rate.multiply(saleOrderLine.getExTaxTotal());
    }
    saleOrderLine.setAmountInvoiced(saleOrderLine.getAmountInvoiced().add(invoicedAmountToAdd));
    return saleOrder;
}
Also used : SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) SaleOrder(com.axelor.apps.sale.db.SaleOrder) BigDecimal(java.math.BigDecimal)

Example 63 with SaleOrder

use of com.axelor.apps.sale.db.SaleOrder in project axelor-open-suite by axelor.

the class BatchOrderInvoicingSale method process.

@Override
protected void process() {
    SupplychainBatch supplychainBatch = batch.getSupplychainBatch();
    List<String> filterList = new ArrayList<>();
    Query<SaleOrder> query = Beans.get(SaleOrderRepository.class).all();
    if (supplychainBatch.getCompany() != null) {
        filterList.add("self.company = :company");
        query.bind("company", supplychainBatch.getCompany());
    }
    if (supplychainBatch.getSalespersonOrBuyerSet() != null && !supplychainBatch.getSalespersonOrBuyerSet().isEmpty()) {
        filterList.add("self.salespersonUser IN (:salespersonSet)");
        query.bind("salespersonSet", supplychainBatch.getSalespersonOrBuyerSet());
    }
    if (supplychainBatch.getTeam() != null) {
        filterList.add("self.team = :team " + "OR self.team IS NULL AND self.salespersonUser IS NOT NULL AND self.salespersonUser.activeTeam = :team");
        query.bind("team", supplychainBatch.getTeam());
    }
    if (!Strings.isNullOrEmpty(supplychainBatch.getDeliveryOrReceiptState())) {
        List<Integer> delivereyStateList = StringTool.getIntegerList(supplychainBatch.getDeliveryOrReceiptState());
        filterList.add("self.deliveryState IN (:delivereyStateList)");
        query.bind("delivereyStateList", delivereyStateList);
    }
    if (!Strings.isNullOrEmpty(supplychainBatch.getStatusSelect())) {
        List<Integer> statusSelectList = StringTool.getIntegerList(supplychainBatch.getStatusSelect());
        filterList.add("self.statusSelect IN (:statusSelectList)");
        query.bind("statusSelectList", statusSelectList);
    }
    if (supplychainBatch.getOrderUpToDate() != null) {
        filterList.add("self.orderDate <= :orderUpToDate");
        query.bind("orderUpToDate", supplychainBatch.getOrderUpToDate());
    }
    filterList.add("self.amountInvoiced < self.exTaxTotal");
    filterList.add("NOT EXISTS (SELECT 1 FROM Invoice invoice WHERE invoice.statusSelect != :invoiceStatusSelect " + "AND (invoice.saleOrder = self " + "OR invoice.saleOrder IS NULL AND EXISTS (SELECT 1 FROM invoice.invoiceLineList invoiceLine " + "WHERE invoiceLine.saleOrderLine MEMBER OF self.saleOrderLineList)))");
    filterList.add("self.clientPartner.id NOT IN (" + Beans.get(BlockingService.class).listOfBlockedPartner(supplychainBatch.getCompany(), BlockingRepository.INVOICING_BLOCKING) + ")");
    query.bind("invoiceStatusSelect", InvoiceRepository.STATUS_CANCELED);
    List<Long> anomalyList = Lists.newArrayList(0L);
    filterList.add("self.id NOT IN (:anomalyList)");
    query.bind("anomalyList", anomalyList);
    String filter = filterList.stream().map(item -> String.format("(%s)", item)).collect(Collectors.joining(" AND "));
    query.filter(filter);
    SaleOrderInvoiceService saleOrderInvoiceService = Beans.get(SaleOrderInvoiceService.class);
    Set<Long> treatedSet = new HashSet<>();
    for (List<SaleOrder> saleOrderList; !(saleOrderList = query.fetch(FETCH_LIMIT)).isEmpty(); JPA.clear()) {
        for (SaleOrder saleOrder : saleOrderList) {
            if (treatedSet.contains(saleOrder.getId())) {
                throw new IllegalArgumentException("Invoice generation error");
            }
            treatedSet.add(saleOrder.getId());
            try {
                saleOrderInvoiceService.generateInvoice(saleOrder);
                incrementDone();
            } catch (Exception e) {
                incrementAnomaly();
                anomalyList.add(saleOrder.getId());
                query.bind("anomalyList", anomalyList);
                TraceBackService.trace(e, ExceptionOriginRepository.INVOICE_ORIGIN, batch.getId());
                e.printStackTrace();
                break;
            }
        }
    }
}
Also used : Query(com.axelor.db.Query) StringTool(com.axelor.apps.tool.StringTool) JPA(com.axelor.db.JPA) TraceBackService(com.axelor.exception.service.TraceBackService) Set(java.util.Set) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) BlockingRepository(com.axelor.apps.base.db.repo.BlockingRepository) SaleOrderRepository(com.axelor.apps.sale.db.repo.SaleOrderRepository) List(java.util.List) Lists(com.google.common.collect.Lists) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) Beans(com.axelor.inject.Beans) BlockingService(com.axelor.apps.base.service.BlockingService) ExceptionOriginRepository(com.axelor.exception.db.repo.ExceptionOriginRepository) SaleOrder(com.axelor.apps.sale.db.SaleOrder) SaleOrderInvoiceService(com.axelor.apps.supplychain.service.SaleOrderInvoiceService) SupplychainBatch(com.axelor.apps.supplychain.db.SupplychainBatch) SaleOrderInvoiceService(com.axelor.apps.supplychain.service.SaleOrderInvoiceService) ArrayList(java.util.ArrayList) SaleOrder(com.axelor.apps.sale.db.SaleOrder) SupplychainBatch(com.axelor.apps.supplychain.db.SupplychainBatch) SaleOrderRepository(com.axelor.apps.sale.db.repo.SaleOrderRepository) HashSet(java.util.HashSet)

Example 64 with SaleOrder

use of com.axelor.apps.sale.db.SaleOrder in project axelor-open-suite by axelor.

the class StockMoveServiceSupplychainImpl method updateSaleOrderOnCancel.

@Transactional(rollbackOn = { Exception.class })
public void updateSaleOrderOnCancel(StockMove stockMove) throws AxelorException {
    SaleOrder so = saleOrderRepo.find(stockMove.getOriginId());
    updateSaleOrderLinesDeliveryState(stockMove, stockMove.getIsReversion());
    Beans.get(SaleOrderStockService.class).updateDeliveryState(so);
    if (Beans.get(AppSupplychainService.class).getAppSupplychain().getTerminateSaleOrderOnDelivery()) {
        terminateOrConfirmSaleOrderStatus(so);
    }
}
Also used : SaleOrder(com.axelor.apps.sale.db.SaleOrder) Transactional(com.google.inject.persist.Transactional)

Example 65 with SaleOrder

use of com.axelor.apps.sale.db.SaleOrder in project axelor-open-suite by axelor.

the class TimetableServiceImpl method createInvoice.

@Override
public Invoice createInvoice(Timetable timetable) throws AxelorException {
    SaleOrder saleOrder = timetable.getSaleOrder();
    PurchaseOrder purchaseOrder = timetable.getPurchaseOrder();
    if (saleOrder != null) {
        if (saleOrder.getCurrency() == null) {
            throw new AxelorException(timetable, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.SO_INVOICE_6), saleOrder.getSaleOrderSeq());
        }
        List<Long> timetableId = new ArrayList<>();
        timetableId.add(timetable.getId());
        Invoice invoice = saleOrderInvoiceService.generateInvoice(saleOrder, SaleOrderRepository.INVOICE_TIMETABLES, BigDecimal.ZERO, true, null, timetableId);
        return invoice;
    }
    if (purchaseOrder != null) {
        if (purchaseOrder.getCurrency() == null) {
            throw new AxelorException(timetable, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.SO_INVOICE_6), purchaseOrder.getPurchaseOrderSeq());
        }
        List<Long> timetableId = new ArrayList<>();
        timetableId.add(timetable.getId());
        return Beans.get(PurchaseOrderInvoiceServiceImpl.class).generateInvoiceFromTimetableForPurchaseOrder(purchaseOrder, timetableId);
    }
    return null;
}
Also used : AxelorException(com.axelor.exception.AxelorException) Invoice(com.axelor.apps.account.db.Invoice) ArrayList(java.util.ArrayList) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) SaleOrder(com.axelor.apps.sale.db.SaleOrder)

Aggregations

SaleOrder (com.axelor.apps.sale.db.SaleOrder)129 AxelorException (com.axelor.exception.AxelorException)53 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)35 BigDecimal (java.math.BigDecimal)24 Context (com.axelor.rpc.Context)20 Transactional (com.google.inject.persist.Transactional)19 ArrayList (java.util.ArrayList)19 SaleOrderRepository (com.axelor.apps.sale.db.repo.SaleOrderRepository)18 Company (com.axelor.apps.base.db.Company)16 Partner (com.axelor.apps.base.db.Partner)15 IOException (java.io.IOException)13 BirtException (org.eclipse.birt.core.exception.BirtException)13 Invoice (com.axelor.apps.account.db.Invoice)12 List (java.util.List)12 Currency (com.axelor.apps.base.db.Currency)11 SaleOrderLineService (com.axelor.apps.sale.service.saleorder.SaleOrderLineService)10 LinkedHashMap (java.util.LinkedHashMap)10 Map (java.util.Map)9 Product (com.axelor.apps.base.db.Product)8 StockMove (com.axelor.apps.stock.db.StockMove)8