/**
         *  Get Material Allocations for Line
         *	@param ctx context
         *	@param M_InventoryLine_ID line
         *	@param trxName trx
         *	@return allocations
         */
        public static MInventoryLineMA[] Get(Ctx ctx, int M_InventoryLine_ID, Trx trxName)
        {
            List <MInventoryLineMA> list = new List <MInventoryLineMA>();
            String      sql = "SELECT * FROM M_InventoryLineMA WHERE M_InventoryLine_ID=" + M_InventoryLine_ID;
            DataTable   dt  = null;
            IDataReader idr = null;

            try
            {
                idr = DataBase.DB.ExecuteReader(sql, null, trxName);
                dt  = new DataTable();
                dt.Load(idr);
                idr.Close();
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(new MInventoryLineMA(ctx, dr, trxName));
                }
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                _log.Log(Level.SEVERE, sql, e);
            }
            finally { dt = null; }
            MInventoryLineMA[] retValue = new MInventoryLineMA[list.Count];
            retValue = list.ToArray();
            return(retValue);
        }
        /// <summary>
        /// Create Material Allocations for new Instances
        /// </summary>
        /// <param name="updateQtyBooked"></param>
        public void CreateMA(bool updateQtyBooked)
        {
            int delMA = MInventoryLineMA.DeleteInventoryLineMA(GetM_InventoryLine_ID(), Get_TrxName());

            log.Info("DeletedMA=" + delMA);

            MStorage[] storages = MStorage.GetAll(GetCtx(), GetM_Product_ID(),
                                                  GetM_Locator_ID(), Get_TrxName());
            bool allZeroASI = true;

            for (int i = 0; i < storages.Length; i++)
            {
                if (storages[i].GetM_AttributeSetInstance_ID() != 0)
                {
                    allZeroASI = false;
                    break;
                }
            }
            if (allZeroASI)
            {
                return;
            }

            MInventoryLineMA ma  = null;
            Decimal          sum = Env.ZERO;

            for (int i = 0; i < storages.Length; i++)
            {
                MStorage storage = storages[i];
                // nnayak - ignore negative layers
                if (Env.Signum(storage.GetQtyOnHand()) <= 0)
                {
                    continue;
                }
                if (ma != null &&
                    ma.GetM_AttributeSetInstance_ID() == storage.GetM_AttributeSetInstance_ID())
                {
                    ma.SetMovementQty(Decimal.Add(ma.GetMovementQty(), storage.GetQtyOnHand()));
                }
                else
                {
                    ma = new MInventoryLineMA(this,
                                              storage.GetM_AttributeSetInstance_ID(), storage.GetQtyOnHand());
                }
                if (!ma.Save())
                {
                    ;
                }
                sum = Decimal.Add(sum, storage.GetQtyOnHand());
            }
            if (updateQtyBooked && sum.CompareTo(GetQtyBook()) != 0)
            {
                log.Warning("QtyBook=" + GetQtyBook() + " corrected to Sum of MA=" + sum);
                SetQtyBook(sum);
            }
        }
        /// <summary>
        /// Is Used to Get or Create  Instance of MInventoryLineMA (Attribute)
        /// </summary>
        /// <param name="line"></param>
        /// <param name="M_AttributeSetInstance_ID"></param>
        /// <param name="MovementQty"></param>
        /// <param name="DateMaterialPolicy"></param>
        /// <returns></returns>
        public static MInventoryLineMA GetOrCreate(MInventoryLine line, int M_AttributeSetInstance_ID, Decimal MovementQty, DateTime?DateMaterialPolicy)
        {
            MInventoryLineMA retValue = null;
            String           sql      = "SELECT * FROM M_InventoryLineMA " +
                                        @" WHERE  M_InventoryLine_ID = " + line.GetM_InventoryLine_ID() +
                                        @" AND MMPolicyDate = " + GlobalVariable.TO_DATE(DateMaterialPolicy, true) + @" AND ";

            if (M_AttributeSetInstance_ID == 0)
            {
                sql += "(M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID + " OR M_AttributeSetInstance_ID IS NULL)";
            }
            else
            {
                sql += "M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID;
            }
            DataTable   dt  = null;
            IDataReader idr = null;

            try
            {
                idr = DB.ExecuteReader(sql, null, line.Get_Trx());
                dt  = new DataTable();
                dt.Load(idr);
                idr.Close();
                foreach (DataRow dr in dt.Rows)
                {
                    retValue = new MInventoryLineMA(line.GetCtx(), dr, line.Get_Trx());
                }
            }
            catch (Exception ex)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                _log.Log(Level.SEVERE, sql, ex);
            }
            finally
            {
                if (idr != null)
                {
                    idr.Close();
                }
                dt = null;
            }
            if (retValue == null)
            {
                retValue = new MInventoryLineMA(line, M_AttributeSetInstance_ID, MovementQty, DateMaterialPolicy);
            }
            else
            {
                retValue.SetMovementQty(Decimal.Add(retValue.GetMovementQty(), MovementQty));
            }
            return(retValue);
        }