/** import record using X_I_ProductPlanning table */
  private void importRecords() {
    for (X_I_ProductPlanning ipp : getRecords(false, p_IsImportOnlyNoErrors)) {
      if (ipp.getM_Product_ID() > 0 && ipp.getS_Resource_ID() > 0 && ipp.getM_Warehouse_ID() > 0) {
        importProductPlanning(ipp);
      } else if (ipp.getForecastValue() == null || ipp.getM_Forecast_ID() == 0) {
        String error = "";
        if (ipp.getM_Product_ID() == 0) {
          error = error + " @M_Product_ID@ @NotFound@ ,";
        }
        if (ipp.getS_Resource_ID() == 0) {
          error = error + " @S_Resource_ID@ @NotFound@ ,";
        }
        if (ipp.getM_Warehouse_ID() == 0) {
          error = error + " @M_Waehouse_ID@ @NotFound@";
        }
        ipp.setI_ErrorMsg(Msg.parseTranslation(getCtx(), error));
        isImported = false;
        ipp.saveEx();
        return;
      }

      if (ipp.getForecastValue() == null) {
        isImported = true;

      } else if (ipp.getM_Forecast_ID() > 0
          && ipp.getM_Warehouse_ID() > 0
          && ipp.getM_Product_ID() > 0
          && ipp.getQty().signum() > 0) {
        importForecast(ipp);
      } else {
        String error = "";
        if (ipp.getM_Forecast_ID() == 0) {
          error = error + " @M_Forecast_ID@ @NotFound@ ,";
        }
        if (ipp.getM_Warehouse_ID() == 0) {
          error = error + " @M_Warehouse_ID@ @NotFound@ ,";
        }
        if (ipp.getQty().signum() <= 0) {
          error = error + " @Qty@ @Error@";
        }
        ipp.setI_ErrorMsg(Msg.parseTranslation(getCtx(), error));
        isImported = false;
        ipp.saveEx();
        return;
      }

      if (isImported) {
        ipp.setI_IsImported(true);
        ipp.setProcessed(true);
        ipp.saveEx();
      }
    }
  }
  /**
   * Import Forecast Record using X_I_ProductPlanning table
   *
   * @param ipp X_I_ProductPlanning
   */
  private void importForecast(X_I_ProductPlanning ipp) {

    if (ipp.getForecastValue() == null && ipp.getM_Forecast_ID() == 0) {
      ipp.setI_ErrorMsg(Msg.getMsg(getCtx(), "@M_Forecast_ID@ @NotFound@"));
      ipp.saveEx();
      isImported = false;
      return;
    }

    MForecast forecast = new MForecast(getCtx(), ipp.getM_Forecast_ID(), get_TrxName());

    final StringBuffer whereClause = new StringBuffer();
    whereClause
        .append(X_M_ForecastLine.COLUMNNAME_M_Forecast_ID)
        .append("=? AND ")
        .append(X_M_ForecastLine.COLUMNNAME_M_Product_ID)
        .append("=? AND ")
        .append(X_M_ForecastLine.COLUMNNAME_M_Warehouse_ID)
        .append("=? AND ")
        .append(X_M_ForecastLine.COLUMNNAME_DatePromised)
        .append("=? AND ")
        .append(X_M_ForecastLine.COLUMNNAME_SalesRep_ID)
        .append("=?");

    X_M_ForecastLine forecastLine = null;
    if (ipp.getM_ForecastLine_ID() > 0) {
      forecastLine = new X_M_ForecastLine(getCtx(), ipp.getM_ForecastLine_ID(), get_TrxName());
    } else {
      forecastLine =
          new Query(getCtx(), X_M_ForecastLine.Table_Name, whereClause.toString(), get_TrxName())
              .setClient_ID()
              .setParameters(
                  new Object[] {
                    ipp.getM_Forecast_ID(),
                    ipp.getM_Product_ID(),
                    ipp.getM_Warehouse_ID(),
                    ipp.getDatePromised(),
                    ipp.getSalesRep_ID()
                  })
              .first();
    }

    if (forecastLine == null) {
      forecastLine = new X_M_ForecastLine(getCtx(), 0, get_TrxName());
    }

    forecastLine.setM_Forecast_ID(ipp.getM_Forecast_ID());
    forecastLine.setAD_Org_ID(ipp.getAD_Org_ID());
    forecastLine.setM_Product_ID(ipp.getM_Product_ID());
    forecastLine.setM_Warehouse_ID(ipp.getM_Warehouse_ID());
    forecastLine.setC_Period_ID(
        MPeriod.getC_Period_ID(getCtx(), ipp.getDatePromised(), ipp.getAD_Org_ID()));
    forecastLine.setDatePromised(ipp.getDatePromised());
    forecastLine.setSalesRep_ID(ipp.getSalesRep_ID());
    forecastLine.setQty(ipp.getQty());
    forecastLine.saveEx();
    ipp.setM_ForecastLine_ID(forecastLine.getM_ForecastLine_ID());
    ipp.saveEx();
    isImported = true;
  }
  /** fill IDs values based on Search Key */
  private void fillIDValues() {
    for (X_I_ProductPlanning ppi : getRecords(false, p_IsImportOnlyNoErrors)) {
      if (ppi.getC_BPartner_ID() == 0)
        ppi.setC_BPartner_ID(
            getID(
                I_C_BPartner.Table_Name,
                I_C_BPartner.COLUMNNAME_Value + "=?",
                new Object[] {ppi.getBPartner_Value()}));
      if (ppi.getM_Product_ID() == 0)
        ppi.setM_Product_ID(
            getID(
                I_M_Product.Table_Name,
                I_M_Product.COLUMNNAME_Value + "=?",
                new Object[] {ppi.getProductValue()}));
      if (ppi.getM_Warehouse_ID() == 0)
        ppi.setM_Warehouse_ID(
            getID(
                I_M_Warehouse.Table_Name,
                I_M_Warehouse.COLUMNNAME_Value + "=?",
                new Object[] {ppi.getWarehouseValue()}));
      if (ppi.getAD_Org_ID() == 0)
        ppi.setAD_Org_ID(
            getID(
                I_AD_Org.Table_Name,
                I_AD_Org.COLUMNNAME_Value + "=?",
                new Object[] {ppi.getOrgValue()}));
      if (ppi.getDD_NetworkDistribution_ID() == 0)
        ppi.setDD_NetworkDistribution_ID(
            getID(
                I_DD_NetworkDistribution.Table_Name,
                I_DD_NetworkDistribution.COLUMNNAME_Value + "=?",
                new Object[] {ppi.getNetworkDistributionValue()}));
      if (ppi.getPP_Product_BOM_ID() == 0)
        ppi.setPP_Product_BOM_ID(
            getID(
                I_PP_Product_BOM.Table_Name,
                I_PP_Product_BOM.COLUMNNAME_Value + "=?",
                new Object[] {ppi.getProduct_BOM_Value()}));
      if (ppi.getM_Forecast_ID() == 0)
        ppi.setM_Forecast_ID(
            getID(
                I_M_Forecast.Table_Name,
                I_M_Forecast.COLUMNNAME_Name + "=?",
                new Object[] {ppi.getForecastValue()}));
      if (ppi.getS_Resource_ID() == 0)
        ppi.setS_Resource_ID(
            getID(
                I_S_Resource.Table_Name,
                I_S_Resource.COLUMNNAME_Value
                    + "=? AND "
                    + I_S_Resource.COLUMNNAME_ManufacturingResourceType
                    + "=?",
                new Object[] {ppi.getResourceValue(), "PT"}));

      ppi.saveEx();
    }
  }