Exemple #1
0
  @Override
  public void addQtyReserved(
      final Properties ctx,
      final I_M_Locator locator,
      final int productId,
      final int attributeSetInstanceId,
      final BigDecimal diffQtyReserved,
      final String trxName) {
    final boolean ok =
        MStorage.add(
            ctx,
            locator.getM_Warehouse_ID(),
            locator.getM_Locator_ID(),
            productId, // M_Product_ID,
            attributeSetInstanceId, // M_AttributeSetInstance_ID,
            attributeSetInstanceId, // reservationAttributeSetInstance_ID,
            BigDecimal.ZERO, // diffQtyOnHand,
            diffQtyReserved, // diffQtyReserved,
            BigDecimal.ZERO, // diffQtyOrdered
            trxName);

    if (!ok) {
      throw new AdempiereException();
    }
  }
  @Override
  public void createSupply(final IMRPCreateSupplyRequest request) {
    final IMRPContext mrpContext = request.getMRPContext();
    final IMRPExecutor executor = request.getMRPExecutor();

    final Properties ctx = mrpContext.getCtx();
    final I_PP_Product_Planning productPlanningData = mrpContext.getProductPlanning();
    final I_AD_Org org = mrpContext.getAD_Org();
    final I_S_Resource plant = mrpContext.getPlant();
    final Timestamp supplyDateFinishSchedule = TimeUtil.asTimestamp(request.getDemandDate());

    // QtyToSupply: qty for which we need to produce the supply
    final BigDecimal qtyToSupply = request.getQtyToSupply();

    // TODO vpj-cd I need to create logic for DRP-040 Shipment Due Action Notice
    // Indicates that a shipment for a Order Distribution is due.
    // Action should be taken at the source warehouse to ensure that the order is received on time.

    // TODO vpj-cd I need to create logic for DRP-050 Shipment Pas Due Action Notice
    // Indicates that a shipment for a Order Distribution is past due. You should either delay the
    // orders created the requirement for the product
    // or expedite them when the product does arrive.

    if (productPlanningData.getDD_NetworkDistribution_ID() <= 0) {
      // Indicates that the Product Planning Data for this product does not specify a valid network
      // distribution.
      executor.newMRPNote(mrpContext, ERR_DRP_060_NoSourceOfSupply).collect();
      //
      return;
    }

    final I_DD_NetworkDistribution network = productPlanningData.getDD_NetworkDistribution();
    final List<I_DD_NetworkDistributionLine> networkLines =
        Services.get(IDistributionNetworkDAO.class)
            .retrieveNetworkLinesByTargetWarehouse(
                network, productPlanningData.getM_Warehouse_ID());
    if (networkLines.isEmpty()) {
      // No network lines were found for our target warehouse
      final I_M_Warehouse warehouseTo = productPlanningData.getM_Warehouse();
      executor
          .newMRPNote(mrpContext, ERR_DRP_060_NoSourceOfSupply)
          .setComment("@NotFound@ @DD_NetworkDistributionLine_ID@")
          .addParameter(
              I_DD_NetworkDistribution.COLUMNNAME_DD_NetworkDistribution_ID,
              network == null ? "?" : network.getName())
          .addParameter("M_Warehouse_Dest_ID", warehouseTo == null ? "?" : warehouseTo.getName())
          .collect();
      //
      return;
    }

    int M_Shipper_ID = -1;
    I_DD_Order order = null;

    BigDecimal qtyToSupplyRemaining = qtyToSupply;
    for (final I_DD_NetworkDistributionLine networkLine : networkLines) {
      //
      // Check: if we created DD Orders for all qty that needed to be supplied, stop here
      if (qtyToSupplyRemaining.signum() <= 0) {
        break;
      }

      // get supply source warehouse and locator
      final I_M_Warehouse warehouseFrom = networkLine.getM_WarehouseSource();
      final I_M_Locator locatorFrom =
          Services.get(IWarehouseBL.class).getDefaultLocator(warehouseFrom);

      // get supply target warehouse and locator
      final I_M_Warehouse warehouseTo = networkLine.getM_Warehouse();
      final I_M_Locator locatorTo = Services.get(IWarehouseBL.class).getDefaultLocator(warehouseTo);

      if (locatorFrom == null || locatorTo == null) {
        executor
            .newMRPNote(mrpContext, "DRP-001") // FIXME: DRP-001 error code does not exist
            .addParameter(
                I_DD_NetworkDistribution.COLUMNNAME_DD_NetworkDistribution_ID,
                network == null ? "?" : network.getName())
            .addParameter(
                I_DD_NetworkDistributionLine.COLUMNNAME_M_WarehouseSource_ID,
                warehouseFrom.getName())
            .addParameter(
                I_DD_NetworkDistributionLine.COLUMNNAME_M_Warehouse_ID, warehouseTo.getName())
            .setComment("No locators found for source or target warehouse")
            .collect();
        //
        continue;
      }

      //
      // Get the warehouse in transit
      final I_M_Warehouse warehouseInTrasit =
          retrieveInTransitWarehouse(ctx, warehouseFrom.getAD_Org_ID());
      if (warehouseInTrasit == null) {
        // DRP-010: Do not exist Transit Warehouse to this Organization
        executor
            .newMRPNote(mrpContext, ERR_DRP_010_InTransitWarehouseNotFound)
            .addParameter(I_AD_Org.COLUMNNAME_AD_Org_ID, org.getName())
            .collect();
        //
        continue;
      }

      //
      // DRP-030: Do not exist Shipper for Create Distribution Order
      if (networkLine.getM_Shipper_ID() <= 0) {
        executor
            .newMRPNote(mrpContext, "DRP-030")
            .addParameter(
                I_DD_NetworkDistribution.COLUMNNAME_DD_NetworkDistribution_ID,
                network == null ? "?" : network.getName())
            .addParameter(
                I_DD_NetworkDistributionLine.COLUMNNAME_DD_NetworkDistributionLine_ID, networkLine)
            .collect();
        //
        continue;
      }

      if (M_Shipper_ID != networkLine.getM_Shipper_ID()) {
        // Org Must be linked to BPartner
        final I_AD_Org locatorToOrg = locatorTo.getAD_Org();
        final IBPartnerOrgBL bpartnerOrgBL = Services.get(IBPartnerOrgBL.class);
        final I_C_BPartner orgBPartner = bpartnerOrgBL.retrieveLinkedBPartner(locatorToOrg);
        if (orgBPartner == null) {
          // DRP-020: Target Org has no BP linked to it
          executor
              .newMRPNote(mrpContext, "DRP-020")
              .addParameter(I_AD_Org.COLUMNNAME_AD_Org_ID, locatorToOrg.getName())
              .collect();
          //
          continue;
        }

        final I_C_BPartner_Location orgBPLocation =
            bpartnerOrgBL.retrieveOrgBPLocation(
                mrpContext.getCtx(), locatorToOrg.getAD_Org_ID(), ITrx.TRXNAME_None);

        //
        // Try found some DD_Order with Shipper , Business Partner and Doc Status = Draft
        // Consolidate the demand in a single order for each Shipper , Business Partner ,
        // DemandDateStartSchedule
        order =
            getDDOrderFromCache(
                org,
                plant,
                warehouseInTrasit,
                networkLine.getM_Shipper_ID(),
                orgBPartner.getC_BPartner_ID(),
                supplyDateFinishSchedule);
        if (order == null) {
          order = InterfaceWrapperHelper.newInstance(I_DD_Order.class, mrpContext);
          order.setMRP_Generated(true);
          order.setMRP_AllowCleanup(true);
          order.setAD_Org_ID(warehouseTo.getAD_Org_ID());
          order.setPP_Plant(plant);
          order.setC_BPartner(orgBPartner);
          order.setC_BPartner_Location(orgBPLocation);
          // order.setAD_User_ID(productPlanningData.getPlanner_ID()); // FIXME: improve
          // performances/cache and retrive Primary BP's User
          order.setSalesRep_ID(productPlanningData.getPlanner_ID());

          final int docTypeDO_ID =
              getC_DocType_ID(mrpContext, X_C_DocType.DOCBASETYPE_DistributionOrder);
          order.setC_DocType_ID(docTypeDO_ID);
          order.setM_Warehouse(warehouseInTrasit);
          order.setDocStatus(X_DD_Order.DOCSTATUS_Drafted);
          order.setDocAction(X_DD_Order.DOCACTION_Complete);
          order.setDateOrdered(mrpContext.getDateAsTimestamp());
          order.setDatePromised(supplyDateFinishSchedule);
          order.setM_Shipper_ID(networkLine.getM_Shipper_ID());
          order.setIsInDispute(false);
          order.setIsInTransit(false);

          InterfaceWrapperHelper.save(order);

          executor.addGeneratedSupplyDocument(order);
          addToCache(order);
        }
        M_Shipper_ID = networkLine.getM_Shipper_ID();
      }

      //
      // Crate DD order line
      final BigDecimal qtyToMove =
          calculateQtyToMove(qtyToSupplyRemaining, networkLine.getPercent());
      createDD_OrderLine(
          mrpContext,
          order,
          networkLine,
          locatorFrom,
          locatorTo,
          qtyToMove,
          supplyDateFinishSchedule,
          request);

      qtyToSupplyRemaining = qtyToSupplyRemaining.subtract(qtyToMove);
    }

    //
    // Check: remaining qtyToSupply shall be ZERO
    if (qtyToSupplyRemaining.signum() != 0) {
      // TODO: introduce DRP-XXX notice
      throw new LiberoException(
          "Cannot create DD Order for required Qty To Supply."
              + "\nQtyToSupply: "
              + qtyToSupply
              + "\nQtyToSupply (remaining): "
              + qtyToSupplyRemaining
              + "\n@DD_NetworkDistribution_ID@: "
              + network
              + "\n@DD_NetworkDistributionLine_ID@: "
              + networkLines
              + "\nMRPContext: "
              + mrpContext);
    }
  }
  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;
  }