private void createUpdateMRPRecord(
      final I_DD_OrderLine_Alternative ddOrderLineAlt, final I_PP_MRP ddOrderLineMRPRecord) {
    Check.assumeNotNull(ddOrderLineAlt, "ddOrderLineAlt not null");
    Check.assumeNotNull(ddOrderLineMRPRecord, "ddOrderLineMRPRecord not null");

    //
    // Get existing MRP Alternative
    I_PP_MRP_Alternative mrpAlternative =
        retrieveMRPAlternative(ddOrderLineAlt, ddOrderLineMRPRecord);

    //
    // If no MRP alternative found (or it was deleted)
    // we need to create a new one
    if (mrpAlternative == null) {
      mrpAlternative = mrpBL.createMRPAlternative(ddOrderLineMRPRecord);
      mrpAlternative.setPP_MRP(ddOrderLineMRPRecord);
      mrpAlternative.setDD_OrderLine_Alternative(ddOrderLineAlt);
    }

    //
    // Get MRP Quantity
    final String typeMRP = ddOrderLineMRPRecord.getTypeMRP();
    final BigDecimal qty;
    if (X_PP_MRP.TYPEMRP_Supply.equals(typeMRP)) {
      qty = ddOrderBL.getQtyToReceive(ddOrderLineAlt);
    } else
    // if (X_PP_MRP.TYPEMRP_Demand.equals(typeMRP))
    {
      qty = ddOrderBL.getQtyToShip(ddOrderLineAlt);
    }

    //
    // Update MRP alternative record
    mrpAlternative.setAD_Org_ID(ddOrderLineAlt.getAD_Org_ID());
    mrpAlternative.setM_Product_ID(ddOrderLineAlt.getM_Product_ID());
    mrpAlternative.setQty(qty);
    mrpAlternative.setIsActive(true);
    InterfaceWrapperHelper.save(mrpAlternative);
  }
  private I_PP_MRP createUpdateMRPRecord(final I_DD_OrderLine ddOrderLine, final String typeMRP) {
    final I_DD_Order ddOrder = ddOrderLine.getDD_Order();
    final String docStatus = ddOrder.getDocStatus();
    final BigDecimal qtyTarget = ddOrderLine.getQtyOrdered();

    final int ppPlantId;
    BigDecimal qty;
    final I_M_Locator locator;
    final int attributeSetInstanceId;
    if (X_PP_MRP.TYPEMRP_Supply.equals(typeMRP)) {
      // Supply goes on Destination Locator
      locator = ddOrderLine.getM_LocatorTo();

      // Qty (supply) on target warehouse
      qty = ddOrderBL.getQtyToReceive(ddOrderLine);

      // Supply plant (taken from DD Order header)
      ppPlantId = ddOrder.getPP_Plant_ID();

      attributeSetInstanceId = ddOrderLine.getM_AttributeSetInstanceTo_ID();
    } else if (X_PP_MRP.TYPEMRP_Demand.equals(typeMRP)) {
      // Demand goes on Source Locator
      locator = ddOrderLine.getM_Locator();

      // Qty (demand) on source warehouse
      qty = ddOrderBL.getQtyToShip(ddOrderLine);

      // Demand plant (taken from line)
      ppPlantId = ddOrderLine.getPP_Plant_From_ID();

      attributeSetInstanceId = ddOrderLine.getM_AttributeSetInstance_ID();
    } else {
      throw new IllegalArgumentException("Unknown TypeMRP: " + typeMRP);
    }

    //
    // In case we moved more then it was required, we shall consider the MRP quantity as ZERO
    // because there are no more movements to plan
    if (qty.signum() < 0) {
      qty = BigDecimal.ZERO;
    }

    //
    // Get/Create MRP Record
    I_PP_MRP mrp = retrieveMRPRecord(ddOrderLine, typeMRP);
    final boolean isNewMRPRecord = mrp == null;
    if (isNewMRPRecord) {
      mrp = mrpBL.createMRP(ddOrderLine);
    }

    //
    // Header
    mrp.setTypeMRP(typeMRP);
    mrp.setDescription(ddOrderLine.getDescription());

    //
    // Dimension (Org, Warehouse, Plant)
    mrp.setAD_Org_ID(locator.getAD_Org_ID());
    mrp.setM_Warehouse_ID(locator.getM_Warehouse_ID());
    mrp.setS_Resource_ID(ppPlantId);

    //
    // Document Reference
    mrp.setOrderType(X_PP_MRP.ORDERTYPE_DistributionOrder);
    mrp.setDD_Order_ID(ddOrderLine.getDD_Order_ID());
    mrp.setDD_OrderLine(ddOrderLine);
    mrp.setDocStatus(docStatus);
    mrp.setC_BPartner_ID(ddOrderLine.getC_BPartner_ID());
    mrp.setC_OrderLineSO_ID(ddOrderLine.getC_OrderLineSO_ID());

    //
    // Dates
    final Timestamp datePromised = ddOrderLine.getDatePromised();
    mrp.setDateOrdered(ddOrderLine.getDateOrdered());
    mrp.setDatePromised(datePromised);
    if (mrp.getDateFinishSchedule() == null) {
      // If DateFinishSchedule was not set, we assume DatePromised
      mrp.setDateFinishSchedule(datePromised);
    }
    if (mrp.getDateStartSchedule() == null) {
      // If DateStartSchedule was not set, we assume DateFinishSchedule
      // => leadtime = 0 because we don't have this information here
      mrp.setDateStartSchedule(mrp.getDateFinishSchedule());
    }

    //
    // Product & Qty
    mrp.setM_Product_ID(ddOrderLine.getM_Product_ID());
    mrp.setM_AttributeSetInstance_ID(attributeSetInstanceId);
    mrpBL.setQty(mrp, qtyTarget, qty, ddOrderLine.getC_UOM());

    InterfaceWrapperHelper.save(mrp);
    return mrp;
  }