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