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;
}
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;
}
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");
}
}
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;
}
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;
}
Aggregations