Example #1
0
  /**
   * Create Order from Basket
   *
   * @param wu web User
   * @param wb web basket
   * @return true if created & processed
   */
  private boolean createOrder(WebUser wu, WebBasket wb) {
    m_order = new MOrder(m_ctx, 0, null);
    log.fine(
        "AD_Client_ID="
            + m_order.getAD_Client_ID()
            + ",AD_Org_ID="
            + m_order.getAD_Org_ID()
            + " - "
            + m_order);
    //
    m_order.setC_DocTypeTarget_ID(MOrder.DocSubTypeSO_Prepay);
    m_order.setPaymentRule(MOrder.PAYMENTRULE_CreditCard);
    m_order.setDeliveryRule(MOrder.DELIVERYRULE_AfterReceipt);
    m_order.setInvoiceRule(MOrder.INVOICERULE_Immediate);
    m_order.setIsSelfService(true);
    if (wb.getM_PriceList_ID() > 0) m_order.setM_PriceList_ID(wb.getM_PriceList_ID());
    if (wb.getSalesRep_ID() != 0) m_order.setSalesRep_ID(wb.getSalesRep_ID());

    //	BPartner
    m_order.setC_BPartner_ID(wu.getC_BPartner_ID());
    m_order.setC_BPartner_Location_ID(wu.getC_BPartner_Location_ID());
    m_order.setAD_User_ID(wu.getAD_User_ID());
    //
    m_order.setSendEMail(true);
    m_order.setDocAction(MOrder.DOCACTION_Prepare);
    m_order.saveEx();
    log.fine("ID=" + m_order.getC_Order_ID() + ", DocNo=" + m_order.getDocumentNo());

    ArrayList<WebBasketLine> lines = wb.getLines();
    for (int i = 0; i < lines.size(); i++) {
      WebBasketLine wbl = lines.get(i);
      MOrderLine ol = new MOrderLine(m_order);
      ol.setM_Product_ID(wbl.getM_Product_ID(), true);
      ol.setQty(wbl.getQuantity());
      ol.setPrice();
      ol.setPrice(wbl.getPrice());
      ol.setTax();
      ol.saveEx();
    } //	for all lines
    boolean ok = m_order.processIt(MOrder.DOCACTION_Prepare);
    m_order.saveEx();

    //	Web User = Customer
    if (!wu.isCustomer()) {
      //	log.info("-------------------------------------- " + wu.isCustomer());
      wu.setIsCustomer(true);
      wu.save();
      //	log.info("-------------------------------------- " + wu.isCustomer());
    }
    BigDecimal amt = m_order.getGrandTotal();
    log.info("Amt=" + amt);
    return ok;
  } //	createOrder
  /**
   * Process. Create purchase order(s) for the resonse(s) and lines marked as Selected Winner using
   * the selected Purchase Quantity (in RfQ Line Quantity) . If a Response is marked as Selected
   * Winner, all lines are created (and Selected Winner of other responses ignored). If there is no
   * response marked as Selected Winner, the lines are used.
   *
   * @return message
   */
  protected String doIt() throws Exception {
    MRfQ rfq = new MRfQ(getCtx(), p_C_RfQ_ID, get_TrxName());
    if (rfq.get_ID() == 0) throw new IllegalArgumentException("No RfQ found");
    log.info(rfq.toString());

    //	Complete
    MRfQResponse[] responses = rfq.getResponses(true, true);
    log.config("#Responses=" + responses.length);
    if (responses.length == 0)
      throw new IllegalArgumentException("No completed RfQ Responses found");

    //	Winner for entire RfQ
    for (int i = 0; i < responses.length; i++) {
      MRfQResponse response = responses[i];
      if (!response.isSelectedWinner()) continue;
      //
      MBPartner bp = new MBPartner(getCtx(), response.getC_BPartner_ID(), get_TrxName());
      log.config("Winner=" + bp);
      MOrder order = new MOrder(getCtx(), 0, get_TrxName());
      order.setIsSOTrx(false);
      if (p_C_DocType_ID != 0) order.setC_DocTypeTarget_ID(p_C_DocType_ID);
      else order.setC_DocTypeTarget_ID();
      order.setBPartner(bp);
      order.setC_BPartner_Location_ID(response.getC_BPartner_Location_ID());
      order.setSalesRep_ID(rfq.getSalesRep_ID());
      if (response.getDateWorkComplete() != null)
        order.setDatePromised(response.getDateWorkComplete());
      else if (rfq.getDateWorkComplete() != null) order.setDatePromised(rfq.getDateWorkComplete());
      order.saveEx();
      //
      MRfQResponseLine[] lines = response.getLines(false);
      for (int j = 0; j < lines.length; j++) {
        //	Respones Line
        MRfQResponseLine line = lines[j];
        if (!line.isActive()) continue;
        MRfQResponseLineQty[] qtys = line.getQtys(false);
        //	Response Line Qty
        for (int k = 0; k < qtys.length; k++) {
          MRfQResponseLineQty qty = qtys[k];
          //	Create PO Lline for all Purchase Line Qtys
          if (qty.getRfQLineQty().isActive() && qty.getRfQLineQty().isPurchaseQty()) {
            MOrderLine ol = new MOrderLine(order);
            ol.setM_Product_ID(
                line.getRfQLine().getM_Product_ID(), qty.getRfQLineQty().getC_UOM_ID());
            ol.setDescription(line.getDescription());
            ol.setQty(qty.getRfQLineQty().getQty());
            BigDecimal price = qty.getNetAmt();
            ol.setPrice();
            ol.setPrice(price);
            ol.saveEx();
          }
        }
      }
      response.setC_Order_ID(order.getC_Order_ID());
      response.saveEx();
      return order.getDocumentNo();
    }

    //	Selected Winner on Line Level
    int noOrders = 0;
    for (int i = 0; i < responses.length; i++) {
      MRfQResponse response = responses[i];
      MBPartner bp = null;
      MOrder order = null;
      //	For all Response Lines
      MRfQResponseLine[] lines = response.getLines(false);
      for (int j = 0; j < lines.length; j++) {
        MRfQResponseLine line = lines[j];
        if (!line.isActive() || !line.isSelectedWinner()) continue;
        //	New/different BP
        if (bp == null || bp.getC_BPartner_ID() != response.getC_BPartner_ID()) {
          bp = new MBPartner(getCtx(), response.getC_BPartner_ID(), get_TrxName());
          order = null;
        }
        log.config("Line=" + line + ", Winner=" + bp);
        //	New Order
        if (order == null) {
          order = new MOrder(getCtx(), 0, get_TrxName());
          order.setIsSOTrx(false);
          order.setC_DocTypeTarget_ID();
          order.setBPartner(bp);
          order.setC_BPartner_Location_ID(response.getC_BPartner_Location_ID());
          order.setSalesRep_ID(rfq.getSalesRep_ID());
          order.saveEx();
          noOrders++;
          addLog(0, null, null, order.getDocumentNo());
        }
        //	For all Qtys
        MRfQResponseLineQty[] qtys = line.getQtys(false);
        for (int k = 0; k < qtys.length; k++) {
          MRfQResponseLineQty qty = qtys[k];
          if (qty.getRfQLineQty().isActive() && qty.getRfQLineQty().isPurchaseQty()) {
            MOrderLine ol = new MOrderLine(order);
            ol.setM_Product_ID(
                line.getRfQLine().getM_Product_ID(), qty.getRfQLineQty().getC_UOM_ID());
            ol.setDescription(line.getDescription());
            ol.setQty(qty.getRfQLineQty().getQty());
            BigDecimal price = qty.getNetAmt();
            ol.setPrice();
            ol.setPrice(price);
            ol.saveEx();
          }
        } //	for all Qtys
      } //	for all Response Lines
      if (order != null) {
        response.setC_Order_ID(order.getC_Order_ID());
        response.saveEx();
      }
    }

    return "#" + noOrders;
  } //	doIt