Search in sources :

Example 81 with StockMoveLine

use of com.axelor.apps.stock.db.StockMoveLine in project axelor-open-suite by axelor.

the class SaleOrderStockServiceImpl method createStockMoveLine.

@Override
public StockMoveLine createStockMoveLine(StockMove stockMove, SaleOrderLine saleOrderLine, BigDecimal qty) throws AxelorException {
    if (this.isStockMoveProduct(saleOrderLine)) {
        Unit unit = saleOrderLine.getProduct().getUnit();
        BigDecimal priceDiscounted = saleOrderLine.getPriceDiscounted();
        BigDecimal requestedReservedQty = saleOrderLine.getRequestedReservedQty().subtract(saleOrderLine.getDeliveredQty());
        BigDecimal companyUnitPriceUntaxed = (BigDecimal) productCompanyService.get(saleOrderLine.getProduct(), "costPrice", saleOrderLine.getSaleOrder() != null ? saleOrderLine.getSaleOrder().getCompany() : null);
        if (unit != null && !unit.equals(saleOrderLine.getUnit())) {
            qty = unitConversionService.convert(saleOrderLine.getUnit(), unit, qty, qty.scale(), saleOrderLine.getProduct());
            priceDiscounted = unitConversionService.convert(unit, saleOrderLine.getUnit(), priceDiscounted, appBaseService.getNbDecimalDigitForUnitPrice(), saleOrderLine.getProduct());
            requestedReservedQty = unitConversionService.convert(saleOrderLine.getUnit(), unit, requestedReservedQty, requestedReservedQty.scale(), saleOrderLine.getProduct());
        }
        BigDecimal taxRate = BigDecimal.ZERO;
        TaxLine taxLine = saleOrderLine.getTaxLine();
        if (taxLine != null) {
            taxRate = taxLine.getValue();
        }
        if (saleOrderLine.getQty().signum() != 0) {
            companyUnitPriceUntaxed = saleOrderLine.getCompanyExTaxTotal().divide(saleOrderLine.getQty(), Beans.get(AppBaseService.class).getNbDecimalDigitForUnitPrice(), RoundingMode.HALF_UP);
        }
        StockMoveLine stockMoveLine = stockMoveLineSupplychainService.createStockMoveLine(saleOrderLine.getProduct(), saleOrderLine.getProductName(), saleOrderLine.getDescription(), qty, requestedReservedQty, priceDiscounted, companyUnitPriceUntaxed, null, unit, stockMove, StockMoveLineService.TYPE_SALES, saleOrderLine.getSaleOrder().getInAti(), taxRate, saleOrderLine, null);
        if (saleOrderLine.getDeliveryState() == 0) {
            saleOrderLine.setDeliveryState(SaleOrderLineRepository.DELIVERY_STATE_NOT_DELIVERED);
        }
        return stockMoveLine;
    }
    return null;
}
Also used : AppBaseService(com.axelor.apps.base.service.app.AppBaseService) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine)

Example 82 with StockMoveLine

use of com.axelor.apps.stock.db.StockMoveLine in project axelor-open-suite by axelor.

the class DeclarationOfExchangesExporterServices method exportToCSV.

// TODO: factorize code to parent.
@Override
protected String exportToCSV() throws AxelorException {
    Path path = getFilePath();
    Period period = declarationOfExchanges.getPeriod();
    List<StockMoveLine> stockMoveLines = Beans.get(StockMoveLineRepository.class).findForDeclarationOfExchanges(period.getFromDate(), period.getToDate(), declarationOfExchanges.getProductTypeSelect(), declarationOfExchanges.getStockMoveTypeSelect(), declarationOfExchanges.getCountry(), declarationOfExchanges.getCompany()).fetch();
    List<String[]> dataList = new ArrayList<>(stockMoveLines.size());
    int lineNum = 1;
    for (StockMoveLine stockMoveLine : stockMoveLines) {
        String[] data = new String[columnHeadersList.size()];
        StockMove stockMove = stockMoveLine.getStockMove();
        BigDecimal fiscalValue = stockMoveLine.getUnitPriceUntaxed().multiply(stockMoveLine.getRealQty()).setScale(0, RoundingMode.HALF_UP);
        String taxNbr;
        if (stockMove.getTypeSelect() == StockMoveRepository.TYPE_OUTGOING && stockMoveLine.getRegime() != Regime.OTHER_EXPEDITIONS) {
            if (stockMove.getPartner() == null) {
                taxNbr = String.format(I18n.get("Partner is missing on stock move %s."), stockMove.getName());
            }
            if (StringUtils.isBlank(stockMove.getPartner().getTaxNbr())) {
                taxNbr = String.format(I18n.get("Tax number is missing on partner %s."), stockMove.getPartner().getName());
            }
            taxNbr = stockMove.getPartner().getTaxNbr();
        } else {
            taxNbr = "";
        }
        data[columnHeadersList.indexOf(LINE_NUM)] = String.valueOf(lineNum++);
        data[columnHeadersList.indexOf(FISC_VAL)] = String.valueOf(fiscalValue);
        data[columnHeadersList.indexOf(TAKER)] = taxNbr;
        dataList.add(data);
    }
    try {
        MoreFiles.createParentDirectories(path);
        CsvTool.csvWriter(path.getParent().toString(), path.getFileName().toString(), ';', getTranslatedHeaders(), dataList);
    } catch (IOException e) {
        throw new AxelorException(e, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, e.getLocalizedMessage());
    }
    return attach(path.toString());
}
Also used : Path(java.nio.file.Path) AxelorException(com.axelor.exception.AxelorException) StockMove(com.axelor.apps.stock.db.StockMove) ArrayList(java.util.ArrayList) Period(com.axelor.apps.base.db.Period) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine)

Example 83 with StockMoveLine

use of com.axelor.apps.stock.db.StockMoveLine in project axelor-open-suite by axelor.

the class FixedAssetServiceSupplyChainImpl method createFixedAssets.

@Transactional
@Override
public List<FixedAsset> createFixedAssets(Invoice invoice) throws AxelorException {
    List<FixedAsset> fixedAssetList = super.createFixedAssets(invoice);
    if (!Beans.get(AppSupplychainService.class).isApp("supplychain")) {
        return fixedAssetList;
    }
    if (fixedAssetList.isEmpty()) {
        return new ArrayList<>();
    }
    StockLocation stockLocation = invoice.getPurchaseOrder() != null ? invoice.getPurchaseOrder().getStockLocation() : null;
    for (FixedAsset fixedAsset : fixedAssetList) {
        PurchaseOrderLine pol = fixedAsset.getInvoiceLine().getPurchaseOrderLine();
        fixedAsset.setStockLocation(stockLocation);
        if (fixedAsset.getInvoiceLine().getIncomingStockMove() != null && CollectionUtils.isNotEmpty(fixedAsset.getInvoiceLine().getIncomingStockMove().getStockMoveLineList())) {
            fixedAsset.setTrackingNumber(fixedAsset.getInvoiceLine().getIncomingStockMove().getStockMoveLineList().stream().filter(l -> pol.equals(l.getPurchaseOrderLine())).findFirst().map(StockMoveLine::getTrackingNumber).orElse(null));
            fixedAsset.setStockLocation(fixedAsset.getInvoiceLine().getIncomingStockMove().getToStockLocation());
        }
    }
    return fixedAssetList;
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) StockLocation(com.axelor.apps.stock.db.StockLocation) ArrayList(java.util.ArrayList) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) FixedAsset(com.axelor.apps.account.db.FixedAsset) Transactional(com.google.inject.persist.Transactional)

Example 84 with StockMoveLine

use of com.axelor.apps.stock.db.StockMoveLine in project axelor-open-suite by axelor.

the class StockMoveServiceSupplychainImpl method verifyProductStock.

@Override
public void verifyProductStock(StockMove stockMove) throws AxelorException {
    AppSupplychain appSupplychain = appSupplyChainService.getAppSupplychain();
    if (stockMove.getAvailabilityRequest() && stockMove.getStockMoveLineList() != null && appSupplychain.getIsVerifyProductStock() && stockMove.getFromStockLocation() != null) {
        StringJoiner notAvailableProducts = new StringJoiner(",");
        int counter = 1;
        for (StockMoveLine stockMoveLine : stockMove.getStockMoveLineList()) {
            boolean isAvailableProduct = stockMoveLineServiceSupplychain.isAvailableProduct(stockMove, stockMoveLine);
            if (!isAvailableProduct && counter <= 10) {
                notAvailableProducts.add(stockMoveLine.getProduct().getFullName());
                counter++;
            }
        }
        if (!Strings.isNullOrEmpty(notAvailableProducts.toString())) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, String.format(I18n.get(IExceptionMessage.STOCK_MOVE_VERIFY_PRODUCT_STOCK_ERROR), notAvailableProducts.toString()));
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) AppSupplychain(com.axelor.apps.base.db.AppSupplychain) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) StringJoiner(java.util.StringJoiner)

Example 85 with StockMoveLine

use of com.axelor.apps.stock.db.StockMoveLine in project axelor-open-suite by axelor.

the class StockMoveServiceSupplychainImpl method realize.

@Override
@Transactional(rollbackOn = { Exception.class })
public String realize(StockMove stockMove, boolean check) throws AxelorException {
    if (!Beans.get(AppSupplychainService.class).isApp("supplychain")) {
        return super.realize(stockMove, check);
    }
    LOG.debug("RĂ©alisation du mouvement de stock : {} ", stockMove.getStockMoveSeq());
    String newStockSeq = super.realize(stockMove, check);
    AppSupplychain appSupplychain = appSupplyChainService.getAppSupplychain();
    if (StockMoveRepository.ORIGIN_SALE_ORDER.equals(stockMove.getOriginTypeSelect())) {
        updateSaleOrderLinesDeliveryState(stockMove, !stockMove.getIsReversion());
        // Update linked saleOrder delivery state depending on BackOrder's existence
        SaleOrder saleOrder = saleOrderRepo.find(stockMove.getOriginId());
        if (newStockSeq != null) {
            saleOrder.setDeliveryState(SaleOrderRepository.DELIVERY_STATE_PARTIALLY_DELIVERED);
        } else {
            Beans.get(SaleOrderStockService.class).updateDeliveryState(saleOrder);
            if (appSupplychain.getTerminateSaleOrderOnDelivery()) {
                terminateOrConfirmSaleOrderStatus(saleOrder);
            }
        }
        Beans.get(SaleOrderRepository.class).save(saleOrder);
    } else if (StockMoveRepository.ORIGIN_PURCHASE_ORDER.equals(stockMove.getOriginTypeSelect())) {
        updatePurchaseOrderLines(stockMove, !stockMove.getIsReversion());
        // Update linked purchaseOrder receipt state depending on BackOrder's existence
        PurchaseOrder purchaseOrder = purchaseOrderRepo.find(stockMove.getOriginId());
        if (newStockSeq != null) {
            purchaseOrder.setReceiptState(PurchaseOrderRepository.STATE_PARTIALLY_RECEIVED);
        } else {
            Beans.get(PurchaseOrderStockService.class).updateReceiptState(purchaseOrder);
            if (appSupplychain.getTerminatePurchaseOrderOnReceipt()) {
                finishOrValidatePurchaseOrderStatus(purchaseOrder);
            }
        }
        Beans.get(PurchaseOrderRepository.class).save(purchaseOrder);
    }
    if (appSupplyChainService.getAppSupplychain().getManageStockReservation()) {
        Beans.get(ReservedQtyService.class).updateReservedQuantity(stockMove, StockMoveRepository.STATUS_REALIZED);
    }
    detachNonDeliveredStockMoveLines(stockMove);
    List<Long> trackingNumberIds = stockMove.getStockMoveLineList().stream().map(StockMoveLine::getTrackingNumber).filter(Objects::nonNull).map(TrackingNumber::getId).filter(Objects::nonNull).collect(Collectors.toList());
    if (CollectionUtils.isNotEmpty(trackingNumberIds)) {
        Query update = JPA.em().createQuery("UPDATE FixedAsset self SET self.stockLocation = :stockLocation WHERE self.trackingNumber.id IN (:trackingNumber)");
        update.setParameter("stockLocation", stockMove.getToStockLocation());
        update.setParameter("trackingNumber", trackingNumberIds);
        update.executeUpdate();
    }
    return newStockSeq;
}
Also used : AppSupplychain(com.axelor.apps.base.db.AppSupplychain) TrackingNumber(com.axelor.apps.stock.db.TrackingNumber) Query(javax.persistence.Query) SaleOrderRepository(com.axelor.apps.sale.db.repo.SaleOrderRepository) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) SaleOrder(com.axelor.apps.sale.db.SaleOrder) Transactional(com.google.inject.persist.Transactional)

Aggregations

StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)121 BigDecimal (java.math.BigDecimal)59 StockMove (com.axelor.apps.stock.db.StockMove)44 AxelorException (com.axelor.exception.AxelorException)41 Product (com.axelor.apps.base.db.Product)33 Transactional (com.google.inject.persist.Transactional)31 ArrayList (java.util.ArrayList)28 List (java.util.List)18 Company (com.axelor.apps.base.db.Company)16 StockMoveRepository (com.axelor.apps.stock.db.repo.StockMoveRepository)16 StockLocation (com.axelor.apps.stock.db.StockLocation)14 StockMoveLineService (com.axelor.apps.stock.service.StockMoveLineService)14 Unit (com.axelor.apps.base.db.Unit)13 Beans (com.axelor.inject.Beans)13 ProdProduct (com.axelor.apps.production.db.ProdProduct)12 StockMoveService (com.axelor.apps.stock.service.StockMoveService)12 I18n (com.axelor.i18n.I18n)12 Inject (com.google.inject.Inject)12 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)10 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)10