/**
   * Gets {@link HUItemExpectation} for given <code>piItem</code>.
   *
   * @param piItem
   * @return
   * @return {@link HUItemExpectation}; never return null
   */
  public HUItemExpectation<HUExpectation<ParentExpectationType>> huItemExpectation(
      final I_M_HU_PI_Item piItem) {
    Check.assumeNotNull(piItem, "piItem not null");
    Check.assumeNotNull(huItemExpectations, "huItemExpectations not null");

    final List<HUItemExpectation<HUExpectation<ParentExpectationType>>> result = new ArrayList<>();
    for (final HUItemExpectation<HUExpectation<ParentExpectationType>> huItemExpectation :
        huItemExpectations) {
      final I_M_HU_PI_Item current_piItem = huItemExpectation.getM_HU_PI_Item();
      if (current_piItem == null) {
        continue;
      }

      if (current_piItem.getM_HU_PI_Item_ID() != piItem.getM_HU_PI_Item_ID()) {
        continue;
      }

      result.add(huItemExpectation);
    }

    if (result.isEmpty()) {
      throw new IllegalArgumentException("No HU Item Expectation found for " + piItem);
    } else if (result.size() > 1) {
      throw new IllegalArgumentException(
          "More then one HU Item Expectation found for " + piItem + "\n\n" + result);
    }

    return result.get(0);
  }
  @Override
  public ILUTUProducerAllocationDestination createLUTUProducerAllocationDestination(
      final I_M_HU_LUTU_Configuration lutuConfiguration) {
    Check.assumeNotNull(lutuConfiguration, "lutuConfiguration not null");

    final IHandlingUnitsBL handlingUnitsBL = Services.get(IHandlingUnitsBL.class);

    final ILUTUProducerAllocationDestination luProducerDestination = new LUTUProducerDestination();
    luProducerDestination.setM_HU_LUTU_Configuration(lutuConfiguration);

    //
    // LU Configuration
    final I_M_HU_PI_Item luPIItem = lutuConfiguration.getM_LU_HU_PI_Item();
    final int qtyLU = lutuConfiguration.getQtyLU().intValueExact();
    final boolean qtyLUInfinite = lutuConfiguration.isInfiniteQtyLU();
    final int qtyTU = lutuConfiguration.getQtyTU().intValueExact();
    final boolean qtyTUInfinite = lutuConfiguration.isInfiniteQtyTU();
    if (!handlingUnitsBL.isNoPI(luPIItem)) {
      final I_M_HU_PI luPI = luPIItem.getM_HU_PI_Version().getM_HU_PI();
      luProducerDestination.setLUItemPI(luPIItem);
      luProducerDestination.setLUPI(luPI);
      if (qtyLUInfinite) {
        luProducerDestination.setMaxLUsInfinite();

        //
        // 07378: Fix behavior when max LUs are infinite, created max TUs are the ones we specify
        // (otherwise we end up creating infinite HUs for 3 x Tomatoes)
        luProducerDestination.setMaxTUsForRemainingQty(qtyTU);
      } else {
        luProducerDestination.setMaxLUs(qtyLU);
      }

      // TU configuration
      Check.assume(!qtyTUInfinite, "qtyTUInfinite shall be false when dealing with concrete LUs");
      luProducerDestination.setMaxTUsPerLU(qtyTU);
      luProducerDestination.setCreateTUsForRemainingQty(false);
    } else {
      luProducerDestination.setLUItemPI(null);
      luProducerDestination.setLUPI(null);
      luProducerDestination.setMaxLUs(0);

      // TU configuration
      // luProducerDestination.setMaxTUsPerLU(0); // no need to set
      luProducerDestination.setCreateTUsForRemainingQty(true); // we will create only TUs

      if (qtyTUInfinite) {
        luProducerDestination.setMaxTUsForRemainingQtyInfinite();
      } else {
        luProducerDestination.setMaxTUsForRemainingQty(qtyTU);
      }
    }

    //
    // TU Configuration
    final I_M_HU_PI tuPI = lutuConfiguration.getM_TU_HU_PI();
    luProducerDestination.setTUPI(tuPI);
    // TU Capacity
    final I_M_Product cuProduct = lutuConfiguration.getM_Product();
    final I_C_UOM cuUOM = lutuConfiguration.getC_UOM();
    final boolean qtyCUInfinite = lutuConfiguration.isInfiniteQtyCU();
    final BigDecimal qtyCUPerTU =
        qtyCUInfinite ? IHUCapacityDefinition.INFINITY : lutuConfiguration.getQtyCU();
    luProducerDestination.addTUCapacity(cuProduct, qtyCUPerTU, cuUOM);

    //
    // Misc configuration
    luProducerDestination.setC_BPartner(lutuConfiguration.getC_BPartner());
    luProducerDestination.setC_BPartner_Location_ID(lutuConfiguration.getC_BPartner_Location_ID());
    luProducerDestination.setHUStatus(lutuConfiguration.getHUStatus());
    luProducerDestination.setM_Locator(lutuConfiguration.getM_Locator());

    //
    // Return it
    return luProducerDestination;
  }
  @Override
  public I_M_HU_LUTU_Configuration createLUTUConfiguration(
      final I_M_HU_PI_Item_Product tuPIItemProduct,
      final I_M_Product cuProduct,
      final I_C_UOM cuUOM,
      final org.compiere.model.I_C_BPartner bpartner) {
    Check.assumeNotNull(tuPIItemProduct, "tuPIItemProduct not null");
    Check.assumeNotNull(cuProduct, "cuProduct not null");
    Check.assumeNotNull(cuUOM, "cuUOM not null");

    // Services used:
    final ITrxManager trxManager = Services.get(ITrxManager.class);
    final IHandlingUnitsDAO handlingUnitsDAO = Services.get(IHandlingUnitsDAO.class);
    final IHUCapacityBL huCapacityBL = Services.get(IHUCapacityBL.class);

    //
    // Context
    final Properties ctx = InterfaceWrapperHelper.getCtx(tuPIItemProduct);
    final IContextAware contextProvider;
    final String threadTrxName = trxManager.getThreadInheritedTrxName();
    if (trxManager.isNull(threadTrxName)) {
      contextProvider = new PlainContextAware(ctx, ITrx.TRXNAME_None);
    } else {
      contextProvider = trxManager.createThreadContextAware(ctx);
    }

    //
    // LU/TU configuration (draft)
    final I_M_HU_LUTU_Configuration lutuConfiguration =
        InterfaceWrapperHelper.newInstance(I_M_HU_LUTU_Configuration.class, contextProvider);
    lutuConfiguration.setC_BPartner(bpartner);
    lutuConfiguration.setIsActive(true);

    //
    // TU Configuration
    final I_M_HU_PI tuPI = tuPIItemProduct.getM_HU_PI_Item().getM_HU_PI_Version().getM_HU_PI();
    final IHUCapacityDefinition tuCapacity =
        huCapacityBL.getCapacity(tuPIItemProduct, cuProduct, cuUOM);
    //
    lutuConfiguration.setM_HU_PI_Item_Product(tuPIItemProduct);
    lutuConfiguration.setM_TU_HU_PI(tuPI);
    lutuConfiguration.setM_Product(cuProduct);
    lutuConfiguration.setC_UOM(cuUOM);
    if (tuCapacity.isInfiniteCapacity()) {
      lutuConfiguration.setIsInfiniteQtyCU(true);
      lutuConfiguration.setQtyCU(BigDecimal.ZERO);
    } else {
      lutuConfiguration.setIsInfiniteQtyCU(false);
      lutuConfiguration.setQtyCU(tuCapacity.getCapacity());
    }

    //
    // LU Configuration
    final I_M_HU_PI_Item luPIItem =
        handlingUnitsDAO.retrieveDefaultParentPIItem(
            tuPI, X_M_HU_PI_Version.HU_UNITTYPE_LoadLogistiqueUnit, bpartner);
    if (luPIItem != null) {
      final I_M_HU_PI luPI = luPIItem.getM_HU_PI_Version().getM_HU_PI();
      lutuConfiguration.setM_LU_HU_PI(luPI);
      lutuConfiguration.setM_LU_HU_PI_Item(luPIItem);

      lutuConfiguration.setIsInfiniteQtyLU(true); // we produce as many as needed
      lutuConfiguration.setQtyLU(BigDecimal.ZERO);

      final int qtyTU = luPIItem.getQty().intValueExact();
      lutuConfiguration.setIsInfiniteQtyTU(false);
      lutuConfiguration.setQtyTU(BigDecimal.valueOf(qtyTU));
    } else {
      lutuConfiguration.setM_LU_HU_PI(null);
      lutuConfiguration.setM_LU_HU_PI_Item(null);

      lutuConfiguration.setIsInfiniteQtyLU(false);
      lutuConfiguration.setQtyLU(BigDecimal.ZERO);

      lutuConfiguration.setIsInfiniteQtyTU(true); // as many as needed
      lutuConfiguration.setQtyTU(BigDecimal.ZERO);
    }

    return lutuConfiguration;
  }