/**
         *  Get (first) valid Counter document for document type
         *	@param ctx context
         *	@param C_DocType_ID base document
         *	@return counter document (may be invalid) or null
         */
        public static MDocTypeCounter GetCounterDocType(Ctx ctx, int C_DocType_ID)
        {
            int             key      = (int)C_DocType_ID;
            MDocTypeCounter retValue = (MDocTypeCounter)s_counter[key];

            if (retValue != null)
            {
                return(retValue);
            }

            //	Direct Relationship
            MDocTypeCounter temp = null;
            String          sql  = "SELECT * FROM C_DocTypeCounter WHERE IsActive = 'Y' AND C_DocType_ID=" + C_DocType_ID;
            //  DataSet pstmt = new DataSet();
            DataTable   dt  = null;
            IDataReader idr = null;

            try
            {
                idr = DataBase.DB.ExecuteReader(sql, null, null);
                dt  = new DataTable();
                dt.Load(idr);
                idr.Close();
                foreach (DataRow dr in dt.Rows)
                //while (idr.Read() && retValue == null)
                {
                    if (retValue == null)
                    {
                        retValue = new MDocTypeCounter(ctx, dr, null);
                        if (!retValue.IsCreateCounter() || !retValue.IsValid())
                        {
                            temp     = retValue;
                            retValue = null;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                _log.Log(Level.SEVERE, "getCounterDocType", e);
            }
            finally
            {
                dt = null;
            }

            if (retValue != null)       //	valid
            {
                return(retValue);
            }
            if (temp != null)           //	invalid
            {
                return(temp);
            }
            return(null);                        //	nothing found
        }
        /**
         *  Get Counter document for document type
         *	@param ctx context
         *	@param C_DocType_ID base document
         *	@return counter document C_DocType_ID or 0 or -1 if no counter doc
         */
        public static int GetCounterDocType_ID(Ctx ctx, int C_DocType_ID)
        {
            //	Direct Relationship
            MDocTypeCounter dtCounter = GetCounterDocType(ctx, C_DocType_ID);

            if (dtCounter != null)
            {
                if (!dtCounter.IsCreateCounter() || !dtCounter.IsValid())
                {
                    return(-1);
                }
                return(dtCounter.GetCounter_C_DocType_ID());
            }

            //	Indirect Relationship
            int      Counter_C_DocType_ID = 0;
            MDocType dt = MDocType.Get(ctx, C_DocType_ID);

            if (!dt.IsCreateCounter())
            {
                return(-1);
            }
            String cDocBaseType = "";// = getCounterDocBaseType(dt.getDocBaseType());

            if (cDocBaseType == null)
            {
                return(0);
            }
            MDocType[] counters = MDocType.GetOfDocBaseType(ctx, cDocBaseType);
            for (int i = 0; i < counters.Length; i++)
            {
                MDocType counter = counters[i];
                if (counter.IsDefaultCounterDoc())
                {
                    Counter_C_DocType_ID = counter.GetC_DocType_ID();
                    break;
                }
                if (counter.IsDefault())
                {
                    Counter_C_DocType_ID = counter.GetC_DocType_ID();
                }
                else if (i == 0)
                {
                    Counter_C_DocType_ID = counter.GetC_DocType_ID();
                }
            }
            return(Counter_C_DocType_ID);
        }
        /*	Get MDocTypeCounter from Cache
         *	@param ctx context
         *	@param C_DocTypeCounter_ID id
         *	@return MDocTypeCounter
         *	@param trxName transaction
         */
        public static MDocTypeCounter Get(Ctx ctx, int C_DocTypeCounter_ID, Trx trxName)
        {
            int             key      = C_DocTypeCounter_ID;
            MDocTypeCounter retValue = (MDocTypeCounter)s_cache[key];

            if (retValue != null)
            {
                return(retValue);
            }
            retValue = new MDocTypeCounter(ctx, C_DocTypeCounter_ID, trxName);
            if (retValue.Get_ID() != 0)
            {
                s_cache.Add(key, retValue);
            }
            return(retValue);
        }
        /**
         *  Get Counter document for document type
         *	@param ctx context
         *	@param C_DocType_ID base document
         *	@return counter document C_DocType_ID or 0 or -1 if no counter doc
         */
        public static int GetCounterDocType_ID(Ctx ctx, int C_DocType_ID)
        {
            //	Direct Relationship
            MDocTypeCounter dtCounter = GetCounterDocType(ctx, C_DocType_ID);

            if (dtCounter != null)
            {
                if (!dtCounter.IsCreateCounter() || !dtCounter.IsValid())
                {
                    return(-1);
                }
                return(dtCounter.GetCounter_C_DocType_ID());
            }
            return(0);
            //	Indirect Relationship
            //int Counter_C_DocType_ID = 0;
            //MDocType dt = MDocType.Get(ctx, C_DocType_ID);
            //if (!dt.IsCreateCounter())
            //    return -1;
            //String cDocBaseType = "";// = getCounterDocBaseType(dt.getDocBaseType());
            //if (cDocBaseType == null)
            //    return 0;
            //MDocType[] counters = MDocType.GetOfDocBaseType(ctx, cDocBaseType);
            //for (int i = 0; i < counters.Length; i++)
            //{
            //    MDocType counter = counters[i];
            //    if (counter.IsDefaultCounterDoc())
            //    {
            //        Counter_C_DocType_ID = counter.GetC_DocType_ID();
            //        break;
            //    }
            //    if (counter.IsDefault())
            //        Counter_C_DocType_ID = counter.GetC_DocType_ID();
            //    else if (i == 0)
            //        Counter_C_DocType_ID = counter.GetC_DocType_ID();
            //}
            //return Counter_C_DocType_ID;
        }