Exemplo n.º 1
0
  @Test
  public void testOnlySelection() throws Exception {
    // Get one AD_PInstance_ID
    int AD_PInstance_ID = DB.getSQLValueEx(null, "SELECT MAX(AD_PInstance_ID) FROM AD_PInstance");
    assertTrue(AD_PInstance_ID > 0);

    // Create selection list
    List<Integer> elements = new ArrayList<Integer>();
    elements.add(102); // AD_Element_ID=102 => AD_Client_ID
    elements.add(104); // AD_Element_ID=104 => AD_Column_ID
    DB.executeUpdateEx(
        "DELETE FROM T_Selection WHERE AD_PInstance_ID=" + AD_PInstance_ID, getTrxName());
    DB.createT_Selection(AD_PInstance_ID, elements, getTrxName());

    String whereClause = "1=1"; // some dummy where clause
    int[] ids =
        new Query(getCtx(), X_AD_Element.Table_Name, whereClause, getTrxName())
            .setOnlySelection(AD_PInstance_ID)
            .setOrderBy(X_AD_Element.COLUMNNAME_AD_Element_ID)
            .getIDs();
    assertEquals("Resulting number of elements differ", elements.size(), ids.length);

    for (int i = 0; i < elements.size(); i++) {
      int expected = elements.get(i);
      assertEquals("Element " + i + " not equals", expected, ids[i]);
    }
  }
Exemplo n.º 2
0
  /**
   * ************************************************************************ Invoice Line -
   * Product. - reset C_Charge_ID / M_AttributeSetInstance_ID - PriceList, PriceStd, PriceLimit,
   * C_Currency_ID, EnforcePriceLimit - UOM Calls Tax
   *
   * @param ctx context
   * @param WindowNo window no
   * @param mTab tab
   * @param mField field
   * @param value value
   * @return null or error message
   */
  public String product(
      Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) {
    Integer M_Product_ID = (Integer) value;
    Integer M_AttributeSetInstance_ID = 0;

    if (M_Product_ID == null || M_Product_ID.intValue() == 0) return "";
    mTab.setValue("C_Charge_ID", null);

    //	Get Model and check the Attribute Set Instance from the context
    MProduct m_product = MProduct.get(Env.getCtx(), M_Product_ID);
    mTab.setValue("M_AttributeSetInstance_ID", m_product.getEnvAttributeSetInstance(ctx, WindowNo));

    /** *** Price Calculation see also qty *** */
    boolean IsSOTrx = Env.getContext(ctx, WindowNo, "IsSOTrx").equals("Y");
    int C_BPartner_ID = Env.getContextAsInt(ctx, WindowNo, WindowNo, "C_BPartner_ID");
    BigDecimal Qty = (BigDecimal) mTab.getValue("QtyInvoiced");
    MProductPricing pp = new MProductPricing(M_Product_ID.intValue(), C_BPartner_ID, Qty, IsSOTrx);
    //
    int M_PriceList_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_ID");
    pp.setM_PriceList_ID(M_PriceList_ID);

    Timestamp invoiceDate = Env.getContextAsDate(ctx, WindowNo, "DateInvoiced");
    /** PLV is only accurate if PL selected in header */
    int M_PriceList_Version_ID = Env.getContextAsInt(ctx, WindowNo, "M_PriceList_Version_ID");
    if (M_PriceList_Version_ID == 0 && M_PriceList_ID > 0) {
      String sql =
          "SELECT plv.M_PriceList_Version_ID "
              + "FROM M_PriceList_Version plv "
              + "WHERE plv.M_PriceList_ID=? " //	1
              + " AND plv.ValidFrom <= ? "
              + "ORDER BY plv.ValidFrom DESC";
      //	Use newest price list - may not be future

      M_PriceList_Version_ID = DB.getSQLValueEx(null, sql, M_PriceList_ID, invoiceDate);
      if (M_PriceList_Version_ID > 0)
        Env.setContext(ctx, WindowNo, "M_PriceList_Version_ID", M_PriceList_Version_ID);
    }

    pp.setM_PriceList_Version_ID(M_PriceList_Version_ID);
    Timestamp date = Env.getContextAsDate(ctx, WindowNo, "DateInvoiced");
    pp.setPriceDate(date);
    //
    mTab.setValue("PriceList", pp.getPriceList());
    mTab.setValue("PriceLimit", pp.getPriceLimit());
    mTab.setValue("PriceActual", pp.getPriceStd());
    mTab.setValue("PriceEntered", pp.getPriceStd());
    mTab.setValue("C_Currency_ID", new Integer(pp.getC_Currency_ID()));
    //	mTab.setValue("Discount", pp.getDiscount());
    mTab.setValue("C_UOM_ID", new Integer(pp.getC_UOM_ID()));
    Env.setContext(ctx, WindowNo, "EnforcePriceLimit", pp.isEnforcePriceLimit() ? "Y" : "N");
    Env.setContext(ctx, WindowNo, "DiscountSchema", pp.isDiscountSchema() ? "Y" : "N");
    //
    return tax(ctx, WindowNo, mTab, mField, value);
  } //	product
 @Override
 protected boolean beforeSave(boolean newRecord) {
   if (getSeqNo() == 0) {
     final String sql =
         "SELECT COALESCE(MAX(SeqNo),0) + 10 FROM "
             + Table_Name
             + " WHERE AD_Table_ID=? AND EventModelValidator=?";
     int seqNo = DB.getSQLValueEx(get_TrxName(), sql, getAD_Table_ID(), getEventModelValidator());
     setSeqNo(seqNo);
   }
   //
   return true;
 }
Exemplo n.º 4
0
  /**
   * Receive notification of the end of an element.
   *
   * @param uri namespace
   * @param localName simple name
   * @param qName qualified name
   * @throws SAXException
   */
  public void endElement(String uri, String localName, String qName) throws SAXException {
    //	Log.trace(Log.l6_Database+1, "TranslationHandler.endElement", qName);
    if (qName.equals(Translation.XML_TAG)
        || qName.equals(Translation.XML_TAG2)
        || qName.equals(Translation.XML_TAG3)) {

    } else if (qName.equals(Translation.XML_ROW_TAG)) {
      //	Set section
      if (m_sql.length() > 0) m_sql.append(",");
      m_sql.append("Updated=").append(DB.TO_DATE(m_time, false));
      if (!m_isBaseLanguage) {
        if (m_trl != null && ("Y".equals(m_trl) || "N".equals(m_trl)))
          m_sql.append(",IsTranslated='").append(m_trl).append("'");
        else m_sql.append(",IsTranslated='Y'");
      }
      //	Where section
      m_sql.append(" WHERE ");
      if (m_curUUID != null) {
        StringBuilder sql =
            new StringBuilder("SELECT ")
                .append(m_TableName)
                .append("_ID")
                .append(" FROM ")
                .append(m_TableName)
                .append(" WHERE ")
                .append(m_TableName)
                .append("_UU =?");
        int ID = DB.getSQLValueEx(null, sql.toString(), m_curUUID);
        m_sql.append(m_TableName).append("_ID=").append(ID);
      } else {
        m_sql.append(m_TableName).append("_ID=").append(m_curID);
      }
      if (!m_isBaseLanguage) m_sql.append(" AND AD_Language='").append(m_AD_Language).append("'");
      if (m_AD_Client_ID >= 0) m_sql.append(" AND AD_Client_ID=").append(m_AD_Client_ID);
      //	Update section
      m_sql.insert(0, m_updateSQL);

      //	Execute
      int no = DB.executeUpdate(m_sql.toString(), null);
      if (no == 1) {
        if (log.isLoggable(Level.FINE)) log.fine(m_sql.toString());
        m_updateCount++;
      } else if (no == 0) log.warning("Not Found - " + m_sql.toString());
      else log.severe("Update Rows=" + no + " (Should be 1) - " + m_sql.toString());
    } else if (qName.equals(Translation.XML_VALUE_TAG)) {
      if (m_sql.length() > 0) m_sql.append(",");
      m_sql.append(m_curColumnName).append("=").append(DB.TO_STRING(m_curValue.toString()));
    }
  } //	endElement
 /**
  * get account balancing
  *
  * @param AcctSchema_ID
  * @param HR_Concept_ID
  * @param AccountSign D or C only
  * @return
  */
 private int getAccountBalancing(int AcctSchema_ID, int HR_Concept_ID, String AccountSign) {
   String field;
   if (MElementValue.ACCOUNTSIGN_Debit.equals(AccountSign)) {
     field = X_HR_Concept_Acct.COLUMNNAME_HR_Expense_Acct;
   } else if (MElementValue.ACCOUNTSIGN_Credit.equals(AccountSign)) {
     field = X_HR_Concept_Acct.COLUMNNAME_HR_Revenue_Acct;
   } else {
     throw new IllegalArgumentException("Invalid value for AccountSign=" + AccountSign);
   }
   final String sqlAccount =
       "SELECT "
           + field
           + " FROM HR_Concept_Acct"
           + " WHERE HR_Concept_ID=? AND C_AcctSchema_ID=?";
   int Account_ID = DB.getSQLValueEx(getTrxName(), sqlAccount, HR_Concept_ID, AcctSchema_ID);
   return Account_ID;
 }
Exemplo n.º 6
0
  /**
   * ************************************************************************ Before Save
   *
   * @param newRecord
   * @return true if it can be saved
   */
  @Override
  protected boolean beforeSave(boolean newRecord) {

    final boolean complete = getParent().isComplete();
    if (newRecord && complete) {
      throw new AdempiereException("@ParentComplete@ @C_OrderLine_ID@");
    }

    // In case our order is complete do nothing, don't update any field
    if (complete) {
      // TODO: make sure that only QtyDelivered, QtyInvoiced fields are updated.
      // The rest shall be forbidden.
      // NOTE: also please check if those are the only fields which are updated after an order is
      // completed
      return true;
    }

    // Get Defaults from Parent
    final I_M_Warehouse warehouse = Services.get(IWarehouseAdvisor.class).evaluateWarehouse(this);
    if (getC_BPartner_ID() <= 0
        || getC_BPartner_Location_ID() <= 0
        || warehouse == null
        || warehouse.getM_Warehouse_ID() <= 0
        || getC_Currency_ID() <= 0) {
      setOrder(getParent());
    }

    // metas: try to get the pl-id from our plv
    if (m_M_PriceList_ID <= 0) {
      final int plvId =
          get_ValueAsInt(de.metas.interfaces.I_C_OrderLine.COLUMNNAME_M_PriceList_Version_ID);
      if (plvId > 0) {
        m_M_PriceList_ID =
            DB.getSQLValueEx(
                get_TrxName(),
                "SELECT M_PriceList_ID FROM M_PriceList_Version WHERE M_PriceList_Version_ID="
                    + plvId);
      }
    }
    // metas: end
    if (m_M_PriceList_ID <= 0) setHeaderInfo(getParent());

    // R/O Check - Product/Warehouse Change
    if (!newRecord && (is_ValueChanged("M_Product_ID") || is_ValueChanged("M_Warehouse_ID"))) {
      if (!canChangeWarehouse(true)) return false;
    } // Product Changed

    // Charge
    if (getC_Charge_ID() != 0 && getM_Product_ID() != 0) setM_Product_ID(0);
    // No Product
    if (getM_Product_ID() == 0) setM_AttributeSetInstance_ID(0);
    // Product
    else
    // Set/check Product Price
    {
      // Set Price if Actual = 0
      if (m_productPrice == null
          && getPriceActual().signum() == 0
          && getPriceList().signum() == 0) {
        setPrice();
      }

      // Check if on Price list
      if (m_productPrice == null)
        getProductPricing(
            m_M_PriceList_ID,
            get_ValueAsInt(de.metas.interfaces.I_C_OrderLine.COLUMNNAME_M_PriceList_Version_ID));
      if (!m_productPrice.isCalculated()) {
        throw new ProductNotOnPriceListException(m_productPrice, getLine());
      }
    }

    // metas: Not allowed to save without (Product or Charge) and qty > 0
    if (getM_Product_ID() == 0 && getC_Charge_ID() == 0 && getQtyEntered().intValue() > 0)
      throw new AdempiereException("@NotFound@ @M_Product_ID@/@C_Charge_ID@ (@QtyEntered@>0)");

    // UOM
    if (getC_UOM_ID() == 0
        && (getM_Product_ID() != 0
            || getPriceEntered().compareTo(Env.ZERO) != 0
            || getC_Charge_ID() != 0)) {
      int C_UOM_ID = MUOM.getDefault_UOM_ID(getCtx());
      if (C_UOM_ID > 0) setC_UOM_ID(C_UOM_ID);
    }

    // Price_UOM task 06942
    // note: we do not set the price-UOM, because that would only make sense if we also set the
    // prices themselves.

    // Qty Precision
    if (newRecord || is_ValueChanged("QtyEntered")) setQtyEntered(getQtyEntered());
    if (newRecord || is_ValueChanged("QtyOrdered")) setQtyOrdered(getQtyOrdered());

    // task 05295: commenting this out because also for ASI-Order-Lines it shall be allowed to order
    // qty that is not yet fully avalable on stock
    // // Qty on instance ASI for SO
    // if (m_IsSOTrx
    // && getM_AttributeSetInstance_ID() != 0
    // && (newRecord || is_ValueChanged("M_Product_ID")
    // || is_ValueChanged("M_AttributeSetInstance_ID")
    // || is_ValueChanged("M_Warehouse_ID")))
    // {
    // MProduct product = getProduct();
    // if (product.isStocked())
    // {
    // int M_AttributeSet_ID = product.getM_AttributeSet_ID();
    // boolean isInstance = M_AttributeSet_ID != 0;
    // if (isInstance)
    // {
    // MAttributeSet mas = MAttributeSet.get(getCtx(), M_AttributeSet_ID);
    // isInstance = mas.isInstanceAttribute();
    // }
    // // Max
    // if (isInstance)
    // {
    // MStorage[] storages = MStorage.getWarehouse(getCtx(),
    // Services.get(IWarehouseAdvisor.class).evaluateWarehouse(this).getM_Warehouse_ID(),
    // getM_Product_ID(), getM_AttributeSetInstance_ID(),
    // M_AttributeSet_ID, false, null, true, get_TrxName());
    // BigDecimal qty = Env.ZERO;
    // for (int i = 0; i < storages.length; i++)
    // {
    // if (storages[i].getM_AttributeSetInstance_ID() == getM_AttributeSetInstance_ID())
    // qty = qty.add(storages[i].getQtyOnHand());
    // }
    //
    // if (getQtyOrdered().compareTo(qty) > 0)
    // {
    // log.warn("Qty - Stock=" + qty + ", Ordered=" + getQtyOrdered());
    // log.error("QtyInsufficient", "=" + qty);
    // return false;
    // }
    // }
    // } // stocked
    // } // SO instance

    // FreightAmt Not used
    if (Env.ZERO.compareTo(getFreightAmt()) != 0) setFreightAmt(Env.ZERO);

    // Set Tax
    // metas: Steuer muss immer ermittelt werden, da durch eine Anschriftenaenderung im Kopf auch
    // Steueraenderungen in Positionen auftreten.
    // if (getC_Tax_ID() == 0)
    if (!setTax()) {
      return false;
    }
    // metas ende

    // Get Line No
    if (getLine() == 0) {
      String sql = "SELECT COALESCE(MAX(Line),0)+10 FROM C_OrderLine WHERE C_Order_ID=?";
      int ii = DB.getSQLValue(get_TrxName(), sql, getC_Order_ID());
      setLine(ii);
    }

    // Calculations & Rounding

    // FIXME: commented out because actually was doing nothing (see, it was updating another
    // instance of this order line, which is not saved), and more, setLineNetAmt is no longer called
    // from here
    // final I_C_OrderLine orderLine = InterfaceWrapperHelper.create(getCtx(), getC_OrderLine_ID(),
    // I_C_OrderLine.class, get_TrxName());
    // Services.get(IOrderLineBL.class).setPrices(orderLine);

    // 07264
    // commented out because we are not using this anymore
    // setLineNetAmt(); // extended Amount with or without tax

    // metas
    // setDiscount();
    // metas ende

    return true;
  } // beforeSave
Exemplo n.º 7
0
  /**
   * ************************************************************************ Before Save
   *
   * @param newRecord new
   * @return save
   */
  protected boolean beforeSave(boolean newRecord) {
    log.fine("");
    if (newRecord && getParent().isComplete()) {
      log.saveError("ParentComplete", Msg.translate(getCtx(), "M_InOutLine"));
      return false;
    }
    // Locator is mandatory if no charge is defined - teo_sarca BF [ 2757978 ]
    if (getProduct() != null && MProduct.PRODUCTTYPE_Item.equals(getProduct().getProductType())) {
      if (getM_Locator_ID() <= 0 && getC_Charge_ID() <= 0) {
        throw new FillMandatoryException(COLUMNNAME_M_Locator_ID);
      }
    }

    //	Get Line No
    if (getLine() == 0) {
      String sql = "SELECT COALESCE(MAX(Line),0)+10 FROM M_InOutLine WHERE M_InOut_ID=?";
      int ii = DB.getSQLValueEx(get_TrxName(), sql, getM_InOut_ID());
      setLine(ii);
    }
    //	UOM
    if (getC_UOM_ID() == 0) setC_UOM_ID(Env.getContextAsInt(getCtx(), "#C_UOM_ID"));
    if (getC_UOM_ID() == 0) {
      int C_UOM_ID = MUOM.getDefault_UOM_ID(getCtx());
      if (C_UOM_ID > 0) setC_UOM_ID(C_UOM_ID);
    }
    //	Qty Precision
    if (newRecord || is_ValueChanged("QtyEntered")) setQtyEntered(getQtyEntered());
    if (newRecord || is_ValueChanged("MovementQty")) setMovementQty(getMovementQty());

    //	Order/RMA Line
    if (getC_OrderLine_ID() == 0 && getM_RMALine_ID() == 0) {
      if (getParent().isSOTrx()) {
        log.saveError("FillMandatory", Msg.translate(getCtx(), "C_Order_ID"));
        return false;
      }
    }

    // Validate Locator/Warehouse - teo_sarca, BF [ 2784194 ]
    if (getM_Locator_ID() > 0) {
      MLocator locator = MLocator.get(getCtx(), getM_Locator_ID());
      if (getM_Warehouse_ID() != locator.getM_Warehouse_ID()) {
        throw new WarehouseLocatorConflictException(
            MWarehouse.get(getCtx(), getM_Warehouse_ID()), locator, getLine());
      }
    }

    //	if (getC_Charge_ID() == 0 && getM_Product_ID() == 0)
    //		;

    /**
     * Qty on instance ASI if (getM_AttributeSetInstance_ID() != 0) { MProduct product =
     * getProduct(); int M_AttributeSet_ID = product.getM_AttributeSet_ID(); boolean isInstance =
     * M_AttributeSet_ID != 0; if (isInstance) { MAttributeSet mas = MAttributeSet.get(getCtx(),
     * M_AttributeSet_ID); isInstance = mas.isInstanceAttribute(); } // Max if (isInstance) {
     * MStorage storage = MStorage.get(getCtx(), getM_Locator_ID(), getM_Product_ID(),
     * getM_AttributeSetInstance_ID(), get_TrxName()); if (storage != null) { BigDecimal qty =
     * storage.getQtyOnHand(); if (getMovementQty().compareTo(qty) > 0) { log.warning("Qty - Stock="
     * + qty + ", Movement=" + getMovementQty()); log.saveError("QtyInsufficient", "=" + qty);
     * return false; } } } } /*
     */
    return true;
  } //	beforeSave