Search in sources :

Example 1 with InventoryLine

use of com.axelor.apps.stock.db.InventoryLine 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 2 with InventoryLine

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

the class InventoryLineService method createInventoryLine.

public InventoryLine createInventoryLine(Inventory inventory, Product product, BigDecimal currentQty, String rack, TrackingNumber trackingNumber) {
    InventoryLine inventoryLine = new InventoryLine();
    inventoryLine.setInventory(inventory);
    inventoryLine.setProduct(product);
    inventoryLine.setRack(rack);
    inventoryLine.setCurrentQty(currentQty);
    inventoryLine.setTrackingNumber(trackingNumber);
    this.compute(inventoryLine, inventory);
    return inventoryLine;
}
Also used : InventoryLine(com.axelor.apps.stock.db.InventoryLine)

Example 3 with InventoryLine

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

the class InventoryService method exportInventoryAsCSV.

@Transactional(rollbackOn = { Exception.class })
public MetaFile exportInventoryAsCSV(Inventory inventory) throws IOException {
    List<String[]> list = new ArrayList<>();
    for (InventoryLine inventoryLine : inventory.getInventoryLineList()) {
        String[] item = new String[9];
        String realQty = "";
        item[0] = (inventoryLine.getProduct() == null) ? "" : inventoryLine.getProduct().getName();
        item[1] = (inventoryLine.getProduct() == null) ? "" : inventoryLine.getProduct().getCode();
        item[2] = (inventoryLine.getProduct() == null) ? "" : ((inventoryLine.getProduct().getProductCategory() == null) ? "" : inventoryLine.getProduct().getProductCategory().getName());
        item[3] = (inventoryLine.getRack() == null) ? "" : inventoryLine.getRack();
        item[4] = (inventoryLine.getTrackingNumber() == null) ? "" : inventoryLine.getTrackingNumber().getTrackingNumberSeq();
        item[5] = inventoryLine.getCurrentQty().toString();
        if (inventoryLine.getRealQty() != null && inventory.getStatusSelect() != InventoryRepository.STATUS_DRAFT && inventory.getStatusSelect() != InventoryRepository.STATUS_PLANNED) {
            realQty = inventoryLine.getRealQty().toString();
        }
        item[6] = realQty;
        item[7] = (inventoryLine.getDescription() == null) ? "" : inventoryLine.getDescription();
        String lastInventoryDateTString = "";
        StockLocationLine stockLocationLine = stockLocationLineService.getStockLocationLine(inventory.getStockLocation(), inventoryLine.getProduct());
        if (stockLocationLine != null) {
            ZonedDateTime lastInventoryDateT = stockLocationLine.getLastInventoryDateT();
            lastInventoryDateTString = lastInventoryDateT == null ? "" : lastInventoryDateT.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
        }
        item[8] = lastInventoryDateTString;
        list.add(item);
    }
    Collections.sort(list, new // sort the list by code product
    Comparator<String[]>() {

        @Override
        public int compare(String[] strings, String[] otherStrings) {
            return strings[1].compareTo(otherStrings[1]);
        }
    });
    String fileName = computeExportFileName(inventory);
    File file = MetaFiles.createTempFile(fileName, ".csv").toFile();
    log.debug("File Located at: {}", file.getPath());
    String[] headers = { PRODUCT_NAME, PRODUCT_CODE, PRODUCT_CATEGORY, RACK, TRACKING_NUMBER, CURRENT_QUANTITY, REAL_QUANTITY, DESCRIPTION, LAST_INVENTORY_DATE };
    CsvTool.csvWriter(file.getParent(), file.getName(), ';', '"', headers, list);
    try (InputStream is = new FileInputStream(file)) {
        return Beans.get(MetaFiles.class).upload(is, fileName + ".csv");
    }
}
Also used : MetaFiles(com.axelor.meta.MetaFiles) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) ZonedDateTime(java.time.ZonedDateTime) StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) InventoryLine(com.axelor.apps.stock.db.InventoryLine) Transactional(com.google.inject.persist.Transactional)

Example 4 with InventoryLine

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

the class ImportInventory method validateInventory.

@Transactional(rollbackOn = { Exception.class })
public Object validateInventory(Object bean, Map<String, Object> values) throws AxelorException {
    assert bean instanceof InventoryLine;
    Inventory inventory = (Inventory) bean;
    inventoryService.validateInventory(inventory);
    return inventory;
}
Also used : Inventory(com.axelor.apps.stock.db.Inventory) InventoryLine(com.axelor.apps.stock.db.InventoryLine) Transactional(com.google.inject.persist.Transactional)

Example 5 with InventoryLine

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

the class InventoryService method importFile.

@Transactional(rollbackOn = { Exception.class })
public Path importFile(Inventory inventory) throws AxelorException {
    List<InventoryLine> inventoryLineList = inventory.getInventoryLineList();
    Path filePath = MetaFiles.getPath(inventory.getImportFile());
    List<String[]> data = this.getDatas(filePath);
    HashMap<String, InventoryLine> inventoryLineMap = this.getInventoryLines(inventory);
    List<String> headers = Arrays.asList(data.get(0));
    data.remove(0);
    for (String[] line : data) {
        if (line.length < 6)
            throw new AxelorException(new Throwable(I18n.get(IExceptionMessage.INVENTORY_3_LINE_LENGHT)), inventory, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVENTORY_3));
        String code = line[headers.indexOf(PRODUCT_CODE)].replace("\"", "");
        String rack = line[headers.indexOf(RACK)].replace("\"", "");
        String trackingNumberSeq = line[headers.indexOf(TRACKING_NUMBER)].replace("\"", "");
        BigDecimal realQty = null;
        try {
            if (!StringUtils.isBlank(line[headers.indexOf(REAL_QUANTITY)]))
                realQty = new BigDecimal(line[headers.indexOf(REAL_QUANTITY)]);
        } catch (NumberFormatException e) {
            throw new AxelorException(new Throwable(I18n.get(IExceptionMessage.INVENTORY_3_REAL_QUANTITY)), inventory, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVENTORY_3));
        }
        String description = line[headers.indexOf(DESCRIPTION)].replace("\"", "");
        int qtyScale = Beans.get(AppBaseService.class).getAppBase().getNbDecimalDigitForQty();
        String key = code + trackingNumberSeq;
        if (inventoryLineMap.containsKey(key)) {
            InventoryLine inventoryLine = inventoryLineMap.get(key);
            if (realQty != null)
                inventoryLine.setRealQty(realQty.setScale(qtyScale, RoundingMode.HALF_UP));
            inventoryLine.setDescription(description);
            if (inventoryLine.getTrackingNumber() != null) {
                inventoryLine.getTrackingNumber().setCounter(realQty);
            }
        } else {
            BigDecimal currentQty;
            try {
                currentQty = new BigDecimal(line[headers.indexOf(CURRENT_QUANTITY)].replace("\"", ""));
            } catch (NumberFormatException e) {
                throw new AxelorException(new Throwable(I18n.get(IExceptionMessage.INVENTORY_3_CURRENT_QUANTITY)), inventory, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVENTORY_3));
            }
            InventoryLine inventoryLine = new InventoryLine();
            List<Product> productList = productRepo.all().filter("self.code = :code AND self.dtype = 'Product'").bind("code", code).fetch();
            if (productList != null && !productList.isEmpty()) {
                if (productList.size() > 1) {
                    throw new AxelorException(inventory, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVENTORY_12) + " " + code);
                }
            }
            Product product = productList.get(0);
            if (product == null || !product.getProductTypeSelect().equals(ProductRepository.PRODUCT_TYPE_STORABLE))
                throw new AxelorException(inventory, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.INVENTORY_4) + " " + code);
            inventoryLine.setProduct(product);
            inventoryLine.setInventory(inventory);
            inventoryLine.setRack(rack);
            inventoryLine.setCurrentQty(currentQty.setScale(qtyScale, RoundingMode.HALF_UP));
            if (realQty != null)
                inventoryLine.setRealQty(realQty.setScale(qtyScale, RoundingMode.HALF_UP));
            inventoryLine.setDescription(description);
            inventoryLine.setTrackingNumber(this.getTrackingNumber(trackingNumberSeq, product, realQty));
            inventoryLineList.add(inventoryLine);
        }
    }
    inventory.setInventoryLineList(inventoryLineList);
    inventoryRepo.save(inventory);
    return filePath;
}
Also used : Path(java.nio.file.Path) AxelorException(com.axelor.exception.AxelorException) Product(com.axelor.apps.base.db.Product) BigDecimal(java.math.BigDecimal) InventoryLine(com.axelor.apps.stock.db.InventoryLine) Transactional(com.google.inject.persist.Transactional)

Aggregations

InventoryLine (com.axelor.apps.stock.db.InventoryLine)12 Product (com.axelor.apps.base.db.Product)4 Inventory (com.axelor.apps.stock.db.Inventory)4 Transactional (com.google.inject.persist.Transactional)4 BigDecimal (java.math.BigDecimal)4 StockLocation (com.axelor.apps.stock.db.StockLocation)3 StockLocationLine (com.axelor.apps.stock.db.StockLocationLine)3 TrackingNumber (com.axelor.apps.stock.db.TrackingNumber)3 AxelorException (com.axelor.exception.AxelorException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Company (com.axelor.apps.base.db.Company)1 StockMove (com.axelor.apps.stock.db.StockMove)1 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)1 TrackingNumberConfiguration (com.axelor.apps.stock.db.TrackingNumberConfiguration)1 InventoryLineRepository (com.axelor.apps.stock.db.repo.InventoryLineRepository)1 StockConfigRepository (com.axelor.apps.stock.db.repo.StockConfigRepository)1 StockLocationLineRepository (com.axelor.apps.stock.db.repo.StockLocationLineRepository)1 Beans (com.axelor.inject.Beans)1 MetaFiles (com.axelor.meta.MetaFiles)1