Search in sources :

Example 76 with SaleOrderLine

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

the class SaleOrderMarginServiceImpl method computeMarginSaleOrder.

@Override
public void computeMarginSaleOrder(SaleOrder saleOrder) {
    BigDecimal accountedRevenue = BigDecimal.ZERO;
    BigDecimal totalCostPrice = BigDecimal.ZERO;
    BigDecimal totalGrossProfit = BigDecimal.ZERO;
    BigDecimal marginRate = BigDecimal.ZERO;
    BigDecimal markup = BigDecimal.ZERO;
    if (saleOrder.getSaleOrderLineList() != null && !saleOrder.getSaleOrderLineList().isEmpty()) {
        for (SaleOrderLine saleOrderLineList : saleOrder.getSaleOrderLineList()) {
            if (saleOrderLineList.getProduct() == null || saleOrderLineList.getSubTotalCostPrice().compareTo(BigDecimal.ZERO) == 0 || saleOrderLineList.getExTaxTotal().compareTo(BigDecimal.ZERO) == 0) {
            } else {
                accountedRevenue = accountedRevenue.add(saleOrderLineList.getCompanyExTaxTotal());
                totalCostPrice = totalCostPrice.add(saleOrderLineList.getSubTotalCostPrice());
                totalGrossProfit = totalGrossProfit.add(saleOrderLineList.getSubTotalGrossMargin());
                marginRate = totalGrossProfit.multiply(new BigDecimal(100)).divide(accountedRevenue, 2, RoundingMode.HALF_UP);
                markup = totalGrossProfit.multiply(new BigDecimal(100)).divide(totalCostPrice, 2, RoundingMode.HALF_UP);
            }
        }
    }
    saleOrder.setAccountedRevenue(accountedRevenue);
    saleOrder.setTotalCostPrice(totalCostPrice);
    saleOrder.setTotalGrossMargin(totalGrossProfit);
    saleOrder.setMarginRate(marginRate);
    saleOrder.setMarkup(markup);
}
Also used : SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) BigDecimal(java.math.BigDecimal)

Example 77 with SaleOrderLine

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

the class SaleOrderServiceImpl method handleComplementaryProducts.

@Override
public List<SaleOrderLine> handleComplementaryProducts(SaleOrder saleOrder) throws AxelorException {
    List<SaleOrderLine> saleOrderLineList = saleOrder.getSaleOrderLineList();
    if (saleOrderLineList == null) {
        saleOrderLineList = new ArrayList<>();
    }
    SaleOrderLine originSoLine = null;
    for (SaleOrderLine soLine : saleOrderLineList) {
        if (soLine.getIsComplementaryProductsUnhandledYet()) {
            originSoLine = soLine;
            if (originSoLine.getManualId() == null || originSoLine.getManualId().equals("")) {
                this.setNewManualId(originSoLine);
            }
            break;
        }
    }
    if (originSoLine != null && originSoLine.getProduct() != null && originSoLine.getSelectedComplementaryProductList() != null) {
        SaleOrderLineService saleOrderLineService = Beans.get(SaleOrderLineService.class);
        AppBaseService appBaseService = Beans.get(AppBaseService.class);
        for (ComplementaryProductSelected compProductSelected : originSoLine.getSelectedComplementaryProductList()) {
            // Search if there is already a line for this product to modify or remove
            SaleOrderLine newSoLine = null;
            for (SaleOrderLine soLine : saleOrderLineList) {
                if (originSoLine.getManualId().equals(soLine.getParentId()) && soLine.getProduct() != null && soLine.getProduct().equals(compProductSelected.getProduct())) {
                    // exists and is no longer selected
                    if (compProductSelected.getIsSelected()) {
                        newSoLine = soLine;
                    } else {
                        saleOrderLineList.remove(soLine);
                    }
                    break;
                }
            }
            if (newSoLine == null) {
                if (compProductSelected.getIsSelected()) {
                    newSoLine = new SaleOrderLine();
                    newSoLine.setProduct(compProductSelected.getProduct());
                    newSoLine.setSaleOrder(saleOrder);
                    newSoLine.setQty(originSoLine.getQty().multiply(compProductSelected.getQty()).setScale(appBaseService.getNbDecimalDigitForQty(), RoundingMode.HALF_EVEN));
                    saleOrderLineService.computeProductInformation(newSoLine, newSoLine.getSaleOrder());
                    saleOrderLineService.computeValues(newSoLine.getSaleOrder(), newSoLine);
                    newSoLine.setParentId(originSoLine.getManualId());
                    int targetIndex = saleOrderLineList.indexOf(originSoLine) + 1;
                    saleOrderLineList.add(targetIndex, newSoLine);
                }
            } else {
                newSoLine.setQty(originSoLine.getQty().multiply(compProductSelected.getQty()).setScale(appBaseService.getNbDecimalDigitForQty(), RoundingMode.HALF_EVEN));
                saleOrderLineService.computeProductInformation(newSoLine, newSoLine.getSaleOrder());
                saleOrderLineService.computeValues(newSoLine.getSaleOrder(), newSoLine);
            }
        }
        originSoLine.setIsComplementaryProductsUnhandledYet(false);
    }
    for (int i = 0; i < saleOrderLineList.size(); i++) {
        saleOrderLineList.get(i).setSequence(i);
    }
    return saleOrderLineList;
}
Also used : ComplementaryProductSelected(com.axelor.apps.sale.db.ComplementaryProductSelected) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine)

Example 78 with SaleOrderLine

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

the class SaleOrderServiceImpl method addPack.

@Override
@Transactional
public SaleOrder addPack(SaleOrder saleOrder, Pack pack, BigDecimal packQty) {
    List<PackLine> packLineList = pack.getComponents();
    if (ObjectUtils.isEmpty(packLineList)) {
        return saleOrder;
    }
    packLineList.sort(Comparator.comparing(PackLine::getSequence));
    Integer sequence = -1;
    List<SaleOrderLine> soLines = saleOrder.getSaleOrderLineList();
    if (soLines != null && !soLines.isEmpty()) {
        sequence = soLines.stream().mapToInt(SaleOrderLine::getSequence).max().getAsInt();
    }
    BigDecimal conversionRate = new BigDecimal(1.00);
    if (pack.getCurrency() != null && !pack.getCurrency().getCode().equals(saleOrder.getCurrency().getCode())) {
        try {
            conversionRate = Beans.get(CurrencyConversionFactory.class).getCurrencyConversionService().convert(pack.getCurrency(), saleOrder.getCurrency());
        } catch (MalformedURLException | JSONException | AxelorException e) {
            TraceBackService.trace(e);
        }
    }
    if (Boolean.FALSE.equals(pack.getDoNotDisplayHeaderAndEndPack())) {
        if (saleOrderLineService.getPackLineTypes(packLineList) == null || !saleOrderLineService.getPackLineTypes(packLineList).contains(PackLineRepository.TYPE_START_OF_PACK)) {
            sequence++;
        }
        soLines = saleOrderLineService.createNonStandardSOLineFromPack(pack, saleOrder, packQty, soLines, sequence);
    }
    SaleOrderLine soLine;
    for (PackLine packLine : packLineList) {
        if (packLine.getTypeSelect() != PackLineRepository.TYPE_NORMAL && Boolean.TRUE.equals(pack.getDoNotDisplayHeaderAndEndPack())) {
            continue;
        }
        soLine = saleOrderLineService.createSaleOrderLine(packLine, saleOrder, packQty, conversionRate, ++sequence);
        if (soLine != null) {
            soLine.setSaleOrder(saleOrder);
            soLines.add(soLine);
        }
    }
    if (soLines != null && !soLines.isEmpty()) {
        try {
            saleOrder = Beans.get(SaleOrderComputeService.class).computeSaleOrder(saleOrder);
            Beans.get(SaleOrderMarginService.class).computeMarginSaleOrder(saleOrder);
        } catch (AxelorException e) {
            TraceBackService.trace(e);
        }
        Beans.get(SaleOrderRepository.class).save(saleOrder);
    }
    return saleOrder;
}
Also used : AxelorException(com.axelor.exception.AxelorException) MalformedURLException(java.net.MalformedURLException) JSONException(wslite.json.JSONException) CurrencyConversionFactory(com.axelor.apps.base.service.currency.CurrencyConversionFactory) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) BigDecimal(java.math.BigDecimal) SaleOrderRepository(com.axelor.apps.sale.db.repo.SaleOrderRepository) PackLine(com.axelor.apps.sale.db.PackLine) Transactional(com.google.inject.persist.Transactional)

Example 79 with SaleOrderLine

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

the class SaleOrderServiceImpl method manageComplementaryProductSOLines.

@Override
public void manageComplementaryProductSOLines(SaleOrder saleOrder) throws AxelorException {
    if (saleOrder.getClientPartner() == null || CollectionUtils.isEmpty(saleOrder.getSaleOrderLineList()) || CollectionUtils.isEmpty(saleOrder.getClientPartner().getComplementaryProductList())) {
        return;
    }
    List<SaleOrderLine> newComplementarySOLines = new ArrayList<>();
    List<ComplementaryProduct> complementaryProducts = saleOrder.getClientPartner().getComplementaryProductList();
    for (SaleOrderLine saleOrderLine : saleOrder.getSaleOrderLineList()) {
        if (saleOrderLine.getMainSaleOrderLine() != null) {
            continue;
        }
        newComplementarySOLines.addAll(saleOrderLineService.manageComplementaryProductSaleOrderLine(saleOrderLine, saleOrder, complementaryProducts));
    }
    newComplementarySOLines.stream().forEach(line -> saleOrder.addSaleOrderLineListItem(line));
    Beans.get(SaleOrderComputeService.class).computeSaleOrder(saleOrder);
}
Also used : ComplementaryProduct(com.axelor.apps.sale.db.ComplementaryProduct) ArrayList(java.util.ArrayList) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine)

Example 80 with SaleOrderLine

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

the class SaleOrderComputeServiceImpl method resetPackTotal.

@Override
public void resetPackTotal(SaleOrder saleOrder) {
    List<SaleOrderLine> saleOrderLineList = saleOrder.getSaleOrderLineList();
    if (ObjectUtils.isEmpty(saleOrderLineList)) {
        return;
    }
    for (SaleOrderLine saleOrderLine : saleOrderLineList) {
        if (saleOrderLine.getTypeSelect() == SaleOrderLineRepository.TYPE_END_OF_PACK) {
            saleOrderLine.setIsHideUnitAmounts(Boolean.FALSE);
            saleOrderLine.setIsShowTotal(Boolean.FALSE);
            saleOrderLine.setExTaxTotal(BigDecimal.ZERO);
            saleOrderLine.setInTaxTotal(BigDecimal.ZERO);
        }
    }
    saleOrder.setSaleOrderLineList(saleOrderLineList);
}
Also used : SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine)

Aggregations

SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)116 AxelorException (com.axelor.exception.AxelorException)41 BigDecimal (java.math.BigDecimal)39 SaleOrder (com.axelor.apps.sale.db.SaleOrder)33 ArrayList (java.util.ArrayList)24 Transactional (com.google.inject.persist.Transactional)23 Product (com.axelor.apps.base.db.Product)21 Context (com.axelor.rpc.Context)16 SaleOrderLineService (com.axelor.apps.sale.service.saleorder.SaleOrderLineService)14 PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)9 Partner (com.axelor.apps.base.db.Partner)8 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)8 TaxLine (com.axelor.apps.account.db.TaxLine)7 Unit (com.axelor.apps.base.db.Unit)7 HashMap (java.util.HashMap)7 AnalyticMoveLine (com.axelor.apps.account.db.AnalyticMoveLine)6 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)6 SaleOrderLineRepository (com.axelor.apps.sale.db.repo.SaleOrderLineRepository)6 ReservedQtyService (com.axelor.apps.supplychain.service.ReservedQtyService)6 List (java.util.List)6