private void recalculate(BigDecimal difference, int itemNumber) {
    ArrayList<GoodsReceiptItem> tempGrItemList = new ArrayList<GoodsReceiptItem>();
    GoodsReceiptItem grItem;

    tempGrItemList.addAll(grItemList);
    grItemList.clear();

    for (GoodsReceiptItem item : tempGrItemList) {
      grItem = new GoodsReceiptItem();
      grItem.setBudgetItem(item.getBudgetItem());
      grItem.setGoodsReceipt(item.getGoodsReceipt());
      grItem.setId(item.getId());
      grItem.setItemNumber(item.getItemNumber());
      grItem.setMaterial(item.getMaterial());
      grItem.setOldReceivedQty(item.getOldReceivedQty());
      grItem.setOrderQty(item.getOrderQty());
      grItem.setOrderUnitPrice(item.getOrderUnitPrice());
      grItem.setOtherMaterial(item.getOtherMaterial());
      grItem.setReceivedQty(item.getReceivedQty());
      grItem.setReceiveUnit(item.getReceiveUnit());
      grItem.setStatus(item.getStatus());
      grItem.setStockMovements(item.getStockMovements());
      grItem.setStorageLocation(item.getStorageLocation());
      grItem.setUnitPrice(item.getUnitPrice());
      grItem.setTotalprice(item.getTotalprice());
      if (item.getItemNumber() == itemNumber) {
        grItem.setDiscountAmount(item.getDiscountAmount().add(difference));
        grItem.setNetPrice(item.getNetPrice().subtract(difference));
        grItem.setAvgPrice(
            (item.getNetPrice()
                .subtract(difference)
                .divide(item.getReceivedQty(), 2, RoundingMode.HALF_UP)));
      } else {
        grItem.setDiscountAmount(item.getDiscountAmount());
        grItem.setNetPrice(item.getNetPrice());
        grItem.setAvgPrice(item.getAvgPrice());
      }
      grItemList.add(grItem);
    }
  }
  public void distributeDiscount() {
    System.out.println("distributed discount");
    BigDecimal sumTotalPrice = new BigDecimal("0.00");
    BigDecimal maxTotalPrice = new BigDecimal("0.00");
    BigDecimal sumOfTotalDiscount = new BigDecimal("0.00");
    BigDecimal difference = new BigDecimal("0.00");
    int itemNumberOfmaxTotalPrice = 0;
    GoodsReceiptItem grItem;
    ArrayList<GoodsReceiptItem> tempGrItemList = new ArrayList<GoodsReceiptItem>();

    tempGrItemList.addAll(grItemList);
    grItemList.clear();

    for (GoodsReceiptItem item : tempGrItemList) {
      if (maxTotalPrice.compareTo(item.getTotalprice()) == -1) {
        maxTotalPrice = item.getTotalprice();
        itemNumberOfmaxTotalPrice = item.getItemNumber();
      }
      sumTotalPrice = sumTotalPrice.add(item.getTotalprice());
    }

    for (GoodsReceiptItem item : tempGrItemList) {
      BigDecimal discountAmount = new BigDecimal("0.00");
      BigDecimal netPrice = new BigDecimal("0.00");
      BigDecimal avgprice = new BigDecimal("0.00");

      discountAmount =
          (item.getTotalprice().divide(sumTotalPrice, 2, RoundingMode.HALF_UP))
              .multiply(editGR.getTotalDiscountAmount());
      netPrice = item.getTotalprice().subtract(discountAmount);
      avgprice = netPrice.divide(item.getReceivedQty(), 2, RoundingMode.HALF_UP);
      sumOfTotalDiscount = sumOfTotalDiscount.add(discountAmount);

      grItem = new GoodsReceiptItem();
      grItem.setBudgetItem(item.getBudgetItem());
      grItem.setGoodsReceipt(item.getGoodsReceipt());
      grItem.setId(item.getId());
      grItem.setItemNumber(item.getItemNumber());
      grItem.setMaterial(item.getMaterial());
      grItem.setOldReceivedQty(item.getOldReceivedQty());
      grItem.setOrderQty(item.getOrderQty());
      grItem.setOrderUnitPrice(item.getOrderUnitPrice());
      grItem.setOtherMaterial(item.getOtherMaterial());
      grItem.setReceivedQty(item.getReceivedQty());
      grItem.setReceiveUnit(item.getReceiveUnit());
      grItem.setStatus(item.getStatus());
      grItem.setStockMovements(item.getStockMovements());
      grItem.setStorageLocation(item.getStorageLocation());
      grItem.setUnitPrice(item.getUnitPrice());
      grItem.setTotalprice(item.getTotalprice());
      grItem.setDiscountAmount(discountAmount);
      grItem.setNetPrice(netPrice);
      grItem.setAvgPrice(avgprice);
      grItemList.add(grItem);
    }

    difference = editGR.getTotalDiscountAmount().subtract(sumOfTotalDiscount);
    if (difference.compareTo(zero) != 0) {
      recalculate(difference, itemNumberOfmaxTotalPrice);
    }
    isItemAmountOrPriceChanged = false;
  }