public StockOpnameHeader save( List<StockOpnameDetail> stockOpnameDetails, Timestamp performedBeginTimestamp, Timestamp performedEndTimestamp, Session session) { StockOpnameHeader stockOpnameHeader = new StockOpnameHeader(); stockOpnameHeader.setPerformedBy(Main.getUserLogin()); stockOpnameHeader.setPerformedBeginTimestamp(performedBeginTimestamp); stockOpnameHeader.setPerformedEndTimestamp(performedEndTimestamp); stockOpnameHeader.setLastUpdatedBy(Main.getUserLogin().getId()); stockOpnameHeader.setLastUpdatedTimestamp(CommonUtils.getCurrentTimestamp()); session.saveOrUpdate(stockOpnameHeader); for (StockOpnameDetail stockOpname : stockOpnameDetails) { Item item = stockOpname.getItem(); List<ItemStock> itemStocks = item.getItemStocks(); for (ItemStock itemStock : itemStocks) { session.saveOrUpdate(itemStock); } session.saveOrUpdate(item); stockOpname.setStockOpnameHeader(stockOpnameHeader); session.saveOrUpdate(stockOpname); } return stockOpnameHeader; }
private BigDecimal calculateOverAmount(Item item, int overStock) throws UserException { BigDecimal amount = BigDecimal.valueOf(0); int excess = overStock; List<ItemStock> itemStocks = item.getItemStocks(); for (int i = itemStocks.size() - 1; i >= 0 && excess > 0; --i) { ItemStock itemStock = itemStocks.get(i); int maxStock = itemStock.getPurchaseDetail().getQuantity(); int stock = itemStock.getQuantity(); int taken; if ((excess + stock) > maxStock) { taken = maxStock - stock; } else { taken = excess; } int remain = excess - taken; int newStock = stock + taken; itemStock.setQuantity(newStock); excess = remain; amount = amount.add( BigDecimal.valueOf(taken).multiply(itemStock.getPurchaseDetail().getPricePerUnit())); } if (excess > 0) { int maxStockLimit = ItemFacade.getInstance().calculateMaxStock(item); throw new UserException( "Batas penyesuaian stock untuk item " + item.getCode() + " " + item.getName() + " hanya bisa disesuaikan hingga " + maxStockLimit + " " + item.getUnit() + ".<br/><br/>" + "Kelebihan " + excess + " " + item.getUnit() + " harus dilakukan melalui menu Transaksi Pembelian."); } return amount; }
private BigDecimal calculateLossAmount(Item item, int lossStock) { BigDecimal amount = BigDecimal.valueOf(0); int requirement = lossStock; List<ItemStock> itemStocks = item.getItemStocks(); for (int i = 0; i < itemStocks.size() && requirement > 0; ++i) { ItemStock itemStock = itemStocks.get(i); int stock = itemStock.getQuantity(); int taken; if (requirement > stock) { taken = stock; requirement -= stock; } else { taken = requirement; requirement = 0; } int remain = stock - taken; itemStock.setQuantity(remain); amount = amount.add( BigDecimal.valueOf(taken).multiply(itemStock.getPurchaseDetail().getPricePerUnit())); } return amount; }