Search in sources :

Example 1 with TrackingNumber

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

the class StockCorrectionServiceImpl method generateStockMove.

public StockMove generateStockMove(StockCorrection stockCorrection) throws AxelorException {
    StockLocation toStockLocation = stockCorrection.getStockLocation();
    Company company = toStockLocation.getCompany();
    StockLocation fromStockLocation = stockConfigService.getInventoryVirtualStockLocation(stockConfigService.getStockConfig(company));
    StockMoveService stockMoveService = Beans.get(StockMoveService.class);
    StockMoveLineService stockMoveLineService = Beans.get(StockMoveLineService.class);
    StockLocationLine stockLocationLine = null;
    StockLocationLineService stockLocationLineService = Beans.get(StockLocationLineService.class);
    if (stockCorrection.getTrackingNumber() == null) {
        stockLocationLine = stockLocationLineService.getStockLocationLine(stockCorrection.getStockLocation(), stockCorrection.getProduct());
    } else {
        stockLocationLine = stockLocationLineService.getDetailLocationLine(stockCorrection.getStockLocation(), stockCorrection.getProduct(), stockCorrection.getTrackingNumber());
    }
    BigDecimal realQty = stockCorrection.getRealQty();
    Product product = stockCorrection.getProduct();
    TrackingNumber trackingNumber = stockCorrection.getTrackingNumber();
    BigDecimal diff = realQty.subtract(stockLocationLine.getCurrentQty());
    StockMove stockMove = null;
    if (diff.compareTo(BigDecimal.ZERO) == 0) {
        return null;
    } else if (diff.compareTo(BigDecimal.ZERO) > 0) {
        stockMove = this.createStockMoveHeader(company, fromStockLocation, toStockLocation);
    } else {
        stockMove = this.createStockMoveHeader(company, toStockLocation, fromStockLocation);
    }
    stockMove.setOriginTypeSelect(StockMoveRepository.ORIGIN_STOCK_CORRECTION);
    stockMove.setOriginId(stockCorrection.getId());
    stockMove.setStockCorrectionReason(stockCorrection.getStockCorrectionReason());
    BigDecimal productCostPrice = (BigDecimal) productCompanyService.get(product, "costPrice", company);
    StockMoveLine stockMoveLine = stockMoveLineService.createStockMoveLine(product, product.getName(), product.getDescription(), diff.abs(), productCostPrice, productCostPrice, product.getUnit(), stockMove, StockMoveLineService.TYPE_NULL, false, BigDecimal.ZERO);
    if (stockMoveLine == null) {
        throw new AxelorException(stockCorrection, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.STOCK_CORRECTION_1));
    }
    if (trackingNumber != null && stockMoveLine.getTrackingNumber() == null) {
        stockMoveLine.setTrackingNumber(trackingNumber);
    }
    stockMoveService.plan(stockMove);
    stockMoveService.copyQtyToRealQty(stockMove);
    stockMoveService.realize(stockMove, false);
    return stockMove;
}
Also used : AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) StockMove(com.axelor.apps.stock.db.StockMove) TrackingNumber(com.axelor.apps.stock.db.TrackingNumber) StockLocation(com.axelor.apps.stock.db.StockLocation) Product(com.axelor.apps.base.db.Product) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) BigDecimal(java.math.BigDecimal)

Example 2 with TrackingNumber

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

the class StockMoveLineServiceImpl method splitStockMoveLineByTrackingNumber.

@Override
@Transactional
public void splitStockMoveLineByTrackingNumber(StockMoveLine stockMoveLine, List<LinkedHashMap<String, Object>> trackingNumbers) {
    // boolean draft = true;
    // if (stockMoveLine.getStockMove() != null
    // && stockMoveLine.getStockMove().getStatusSelect() ==
    // StockMoveRepository.STATUS_PLANNED) {
    // draft = false;
    // }
    BigDecimal totalSplitQty = BigDecimal.ZERO;
    for (LinkedHashMap<String, Object> trackingNumberItem : trackingNumbers) {
        BigDecimal counter = new BigDecimal(trackingNumberItem.get("counter").toString());
        if (counter.compareTo(BigDecimal.ZERO) == 0) {
            continue;
        }
        totalSplitQty = totalSplitQty.add(counter);
        TrackingNumber trackingNumber = trackingNumberRepo.all().filter("self.product.id = ?1 and self.trackingNumberSeq = ?2", stockMoveLine.getProduct(), trackingNumberItem.get("trackingNumberSeq").toString()).fetchOne();
        if (trackingNumber == null) {
            trackingNumber = new TrackingNumber();
            trackingNumber.setCounter(counter);
            trackingNumber.setTrackingNumberSeq(trackingNumberItem.get("trackingNumberSeq").toString());
            if (trackingNumberItem.get("warrantyExpirationDate") != null) {
                trackingNumber.setWarrantyExpirationDate(LocalDate.parse(trackingNumberItem.get("warrantyExpirationDate").toString()));
            }
            if (trackingNumberItem.get("perishableExpirationDate") != null) {
                trackingNumber.setPerishableExpirationDate(LocalDate.parse(trackingNumberItem.get("perishableExpirationDate").toString()));
            }
            if (trackingNumberItem.get("origin") != null) {
                trackingNumber.setOrigin(trackingNumberItem.get("origin").toString());
            }
            if (trackingNumberItem.get("note") != null) {
                trackingNumber.setNote(trackingNumberItem.get("note").toString());
            }
            trackingNumber.setProduct(stockMoveLine.getProduct());
        }
        StockMoveLine newStockMoveLine = stockMoveLineRepository.copy(stockMoveLine, true);
        // if (draft) {
        newStockMoveLine.setQty(counter);
        // } else {
        newStockMoveLine.setRealQty(counter);
        // }
        newStockMoveLine.setTrackingNumber(trackingNumber);
        newStockMoveLine.setStockMove(stockMoveLine.getStockMove());
        stockMoveLineRepository.save(newStockMoveLine);
    }
    if (totalSplitQty.compareTo(stockMoveLine.getQty()) < 0) {
        BigDecimal remainingQty = stockMoveLine.getQty().subtract(totalSplitQty);
        stockMoveLine.setQty(remainingQty);
        stockMoveLine.setRealQty(remainingQty);
        stockMoveLine.setTrackingNumber(null);
        stockMoveLine.setStockMove(stockMoveLine.getStockMove());
    } else {
        stockMoveLineRepository.remove(stockMoveLine);
    }
}
Also used : TrackingNumber(com.axelor.apps.stock.db.TrackingNumber) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 3 with TrackingNumber

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

the class StockMoveLineServiceImpl method checkExpirationDates.

@Override
public void checkExpirationDates(StockMove stockMove) throws AxelorException {
    List<String> errorList = new ArrayList<>();
    for (StockMoveLine stockMoveLine : stockMove.getStockMoveLineList()) {
        TrackingNumber trackingNumber = stockMoveLine.getTrackingNumber();
        if (trackingNumber == null) {
            continue;
        }
        Product product = trackingNumber.getProduct();
        if (product == null || !product.getCheckExpirationDateAtStockMoveRealization()) {
            continue;
        }
        if (product.getHasWarranty() && trackingNumber.getWarrantyExpirationDate().isBefore(appBaseService.getTodayDate(stockMove.getCompany())) || product.getIsPerishable() && trackingNumber.getPerishableExpirationDate().isBefore(appBaseService.getTodayDate(stockMove.getCompany()))) {
            errorList.add(product.getName());
        }
    }
    if (!errorList.isEmpty()) {
        String errorStr = errorList.stream().collect(Collectors.joining(", "));
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.STOCK_MOVE_LINE_EXPIRED_PRODUCTS), errorStr);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) TrackingNumber(com.axelor.apps.stock.db.TrackingNumber) ArrayList(java.util.ArrayList) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Product(com.axelor.apps.base.db.Product)

Example 4 with TrackingNumber

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

the class InventoryLineService method updateInventoryLine.

public InventoryLine updateInventoryLine(InventoryLine inventoryLine, Inventory inventory) {
    StockLocation stockLocation = inventory.getStockLocation();
    Product product = inventoryLine.getProduct();
    if (product != null) {
        StockLocationLine stockLocationLine = Beans.get(StockLocationLineService.class).getOrCreateStockLocationLine(stockLocation, product);
        if (stockLocationLine != null) {
            inventoryLine.setCurrentQty(stockLocationLine.getCurrentQty());
            inventoryLine.setRack(stockLocationLine.getRack());
            if (inventoryLine.getTrackingNumber() != null) {
                inventoryLine.setCurrentQty(Beans.get(StockLocationLineRepository.class).all().filter("self.product = :product and self.detailsStockLocation = :stockLocation and self.trackingNumber = :trackingNumber").bind("product", inventoryLine.getProduct()).bind("stockLocation", stockLocation).bind("trackingNumber", inventoryLine.getTrackingNumber()).fetchStream().map(it -> it.getCurrentQty()).reduce(BigDecimal.ZERO, (a, b) -> a.add(b)));
            }
        } else {
            inventoryLine.setCurrentQty(null);
            inventoryLine.setRack(null);
        }
    }
    return inventoryLine;
}
Also used : BigDecimal(java.math.BigDecimal) StockLocationLineRepository(com.axelor.apps.stock.db.repo.StockLocationLineRepository) StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) Beans(com.axelor.inject.Beans) Product(com.axelor.apps.base.db.Product) StockLocation(com.axelor.apps.stock.db.StockLocation) StockConfigRepository(com.axelor.apps.stock.db.repo.StockConfigRepository) Inventory(com.axelor.apps.stock.db.Inventory) TrackingNumber(com.axelor.apps.stock.db.TrackingNumber) InventoryLine(com.axelor.apps.stock.db.InventoryLine) RoundingMode(java.math.RoundingMode) StockLocation(com.axelor.apps.stock.db.StockLocation) StockLocationLineRepository(com.axelor.apps.stock.db.repo.StockLocationLineRepository) Product(com.axelor.apps.base.db.Product) StockLocationLine(com.axelor.apps.stock.db.StockLocationLine)

Example 5 with TrackingNumber

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

the class InventoryService method generateStockMoveLines.

/**
 * Generate lines for the given stock move. Depending if we are creating an incoming or outgoing
 * stock move, we only create stock move line with positive quantity.
 *
 * @param inventoryLine an inventory line
 * @param stockMove a stock move being created
 * @param isEnteringStock whether we are creating an incoming or outgoing stock move.
 * @throws AxelorException
 */
protected void generateStockMoveLines(InventoryLine inventoryLine, StockMove stockMove, boolean isEnteringStock) throws AxelorException {
    Product product = inventoryLine.getProduct();
    TrackingNumber trackingNumber = inventoryLine.getTrackingNumber();
    BigDecimal diff = inventoryLine.getRealQty().subtract(inventoryLine.getCurrentQty());
    if (!isEnteringStock) {
        diff = diff.negate();
    }
    if (diff.signum() > 0) {
        BigDecimal avgPrice;
        StockLocationLine stockLocationLine = stockLocationLineService.getStockLocationLine(stockMove.getToStockLocation(), product);
        if (stockLocationLine != null) {
            avgPrice = stockLocationLine.getAvgPrice();
        } else {
            avgPrice = BigDecimal.ZERO;
        }
        StockMoveLine stockMoveLine = stockMoveLineService.createStockMoveLine(product, product.getName(), product.getDescription(), diff, avgPrice, avgPrice, product.getUnit(), stockMove, StockMoveLineService.TYPE_NULL, false, BigDecimal.ZERO);
        if (stockMoveLine == null) {
            throw new AxelorException(inventoryLine.getInventory(), TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVENTORY_7) + " " + inventoryLine.getInventory().getInventorySeq());
        }
        if (trackingNumber != null && stockMoveLine.getTrackingNumber() == null) {
            stockMoveLine.setTrackingNumber(trackingNumber);
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) TrackingNumber(com.axelor.apps.stock.db.TrackingNumber) Product(com.axelor.apps.base.db.Product) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) BigDecimal(java.math.BigDecimal)

Aggregations

TrackingNumber (com.axelor.apps.stock.db.TrackingNumber)13 BigDecimal (java.math.BigDecimal)8 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)6 Product (com.axelor.apps.base.db.Product)5 StockLocationLine (com.axelor.apps.stock.db.StockLocationLine)5 AxelorException (com.axelor.exception.AxelorException)5 StockLocation (com.axelor.apps.stock.db.StockLocation)4 Transactional (com.google.inject.persist.Transactional)4 InventoryLine (com.axelor.apps.stock.db.InventoryLine)3 StockMove (com.axelor.apps.stock.db.StockMove)2 TrackingNumberConfiguration (com.axelor.apps.stock.db.TrackingNumberConfiguration)2 Beans (com.axelor.inject.Beans)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AppSupplychain (com.axelor.apps.base.db.AppSupplychain)1 Company (com.axelor.apps.base.db.Company)1 Sequence (com.axelor.apps.base.db.Sequence)1 Wizard (com.axelor.apps.base.db.Wizard)1 PurchaseOrder (com.axelor.apps.purchase.db.PurchaseOrder)1