public void CommandPreparing(PXCache sender, PXCommandPreparingEventArgs e)
        {
            e.Cancel    = true;
            e.FieldName = null;
            e.DataType  = PXDbType.NVarChar;

            if ((e.Operation & PXDBOperation.Command) != PXDBOperation.Select)
            {
                return;
            }

            if (!_BqlTable.IsAssignableFrom(sender.BqlTable))
            {
                return;
            }

            if (((e.Operation & PXDBOperation.Option) == PXDBOperation.External ||
                 (e.Operation & PXDBOperation.Option) == PXDBOperation.Normal && e.Value == null))
            {
                e.DataValue = e.Value;
                e.BqlTable  = e.Table ?? _BqlTable;

                ISqlDialect sql = e.SqlDialect;

                if ((e.Operation & PXDBOperation.Option) == PXDBOperation.External)
                {
                    System.Text.StringBuilder bld = new StringBuilder(BqlCommand.SubSelect);

                    List <IBqlParameter> parameters = new List <IBqlParameter>();

                    bld.Append(typeof(ExtraFieldValue.value).Name);
                    bld.Append(" FROM ");
                    bld.Append(typeof(ExtraFieldValue).Name);
                    bld.Append(" WHERE ");

                    Type sendersNoteID = sender.GetItemType().GetNestedType("noteID");
                    Type where = BqlCommand.Compose(
                        typeof(Where <, ,>), typeof(ExtraFieldValue.extFieldId), typeof(Equal <>), typeof(FieldNameParam),
                        typeof(And <,>), typeof(ExtraFieldValue.noteId), typeof(Equal <>), sendersNoteID);
                    var conditionBql = Activator.CreateInstance(where) as IBqlCreator;
                    conditionBql.Parse(sender.Graph, parameters, null, null, null, bld, null);
                    bld.Replace(typeof(FieldNameParam).FullName, attributeId.ToString());

                    bld.Append(" ORDER BY ");
                    bld.Append(typeof(ExtraFieldValue.value).Name);
                    bld.Append(")");
                    e.FieldName = bld.ToString();
                }
            }
        }
        public void CommandPreparing(PXCache sender, PXCommandPreparingEventArgs e)
        {
            if ((e.Operation & PXDBOperation.Command) != PXDBOperation.Select)
            {
                return;
            }

            e.FieldName = BqlCommand.Null;
            e.DataType  = PXDbType.NVarChar;

            if ((e.Operation & PXDBOperation.Option) == PXDBOperation.Internal)
            {
                return;
            }

            if (!_BqlTable.IsAssignableFrom(sender.BqlTable))
            {
                return;
            }

            e.BqlTable = _BqlTable;


            if ((e.Operation & PXDBOperation.Option) == PXDBOperation.External)
            {
                e.FieldName = BqlCommand.SubSelect + _FieldTypes.Length + ')';
            }
            else if ((e.Operation & PXDBOperation.Option) == PXDBOperation.Normal && e.Value == null)
            {
                Type extTable = e.Table ?? _BqlTable;

                StringBuilder bld = new StringBuilder();
                e.SqlDialect.prepareNoteAttributesJoined(bld, typeof(ExtraFieldValue.value), extTable, extTable);
                e.FieldName = bld.ToString();
                return;
            }
        }
        public override void CommandPreparing(PXCache sender, PXCommandPreparingEventArgs e)
        {
            if (!_IsActive || _Fields == null || !e.IsSelect())
            {
                return;
            }

            PXDBOperation eOp = e.Operation & PXDBOperation.Option;
            bool          operationAllowsSelectForXml =
                eOp == PXDBOperation.External ||
                eOp == PXDBOperation.Internal ||
                eOp == PXDBOperation.Normal && e.Value == null ||
                eOp == PXDBOperation.GroupBy && sender.BqlSelect != null;                    // it's ok because the subselect will be used inside FROM (...) part without any aggregation
            Type tableToUse = eOp == PXDBOperation.External ? sender.GetItemType() : e.Table ?? _BqlTable;


            if (!_BqlTable.IsAssignableFrom(sender.BqlTable))
            {
                if (sender.Graph.Caches[_BqlTable].BqlSelect != null && operationAllowsSelectForXml)
                {
                    e.BqlTable  = _BqlTable;
                    e.FieldName = ((e.Operation & PXDBOperation.Option) == PXDBOperation.External ? sender.GetItemType().Name : _BqlTable.Name) + '.' + _DatabaseFieldName;
                }
                else
                {
                    PXCommandPreparingEventArgs.FieldDescription description;
                    sender.Graph.Caches[_BqlTable].RaiseCommandPreparing(_DatabaseFieldName, e.Row, e.Value, e.Operation, e.Table, out description);
                    if (description != null)
                    {
                        e.DataType  = description.DataType;
                        e.DataValue = description.DataValue;
                        e.BqlTable  = _BqlTable;
                        e.FieldName = description.FieldName;
                    }
                }
            }
            else
            {
                var dialect = e.SqlDialect;
                if (operationAllowsSelectForXml)
                {
                    if (aggregateAttributes && (e.Operation & PXDBOperation.Option) != PXDBOperation.External)
                    {
                        List <Type> init = new List <Type>()
                        {
                            tableToUse
                        };

                        StringBuilder bld = new StringBuilder();
                        dialect.prepareAttributesJoined(sender.Graph, bld, init, ((IBqlSearch)_SingleSelect).GetField(), _PureWhere);
                        e.FieldName = bld.ToString();

                        e.DataType = PXDbType.NVarChar;
                        return;
                    }
                    else
                    {
                        e.BqlTable  = _BqlTable;
                        e.FieldName = BqlCommand.SubSelect + _Fields.Length + ')';
                    }
                }
                else
                {
                    e.FieldName = BqlCommand.Null;
                }
            }
            e.DataType   = PXDbType.Int;
            e.DataLength = 4;
        }
        protected virtual void AttributeCommandPreparing(PXCache sender, PXCommandPreparingEventArgs e, PXFieldState state, string attributeName, int iField)
        {
            if (!_IsActive || (e.Operation & PXDBOperation.Command) != PXDBOperation.Select)
            {
                return;
            }

            if (!_BqlTable.IsAssignableFrom(sender.BqlTable))
            {
                if (sender.Graph.Caches[_BqlTable].BqlSelect != null &&
                    ((e.Operation & PXDBOperation.Option) == PXDBOperation.External ||
                     (e.Operation & PXDBOperation.Option) == PXDBOperation.Normal && e.Value == null))
                {
                    e.Cancel    = true;
                    e.DataType  = PXDbType.NVarChar;
                    e.DataValue = e.Value;
                    e.BqlTable  = _BqlTable;
                    e.FieldName = ((e.Operation & PXDBOperation.Option) == PXDBOperation.External ? sender.GetItemType().Name : _BqlTable.Name) + '.' + state.Name;
                }
                else
                {
                    PXCommandPreparingEventArgs.FieldDescription description;
                    e.Cancel = !sender.Graph.Caches[_BqlTable].RaiseCommandPreparing(state.Name, e.Row, e.Value, e.Operation, e.Table, out description);
                    if (description != null)
                    {
                        e.DataType  = description.DataType;
                        e.DataValue = description.DataValue;
                        e.BqlTable  = _BqlTable;
                        e.FieldName = description.FieldName;
                    }
                }
            }
            else if (((e.Operation & PXDBOperation.Option) == PXDBOperation.External ||
                      (e.Operation & PXDBOperation.Option) == PXDBOperation.Normal && e.Value == null))
            {
                e.Cancel    = true;
                e.DataValue = e.Value;
                string sValue = e.Value as string;
                if (state.DataType == typeof(bool))
                {
                    e.DataType = PXDbType.Bit;

                    bool value;
                    if (sValue != null && bool.TryParse(sValue, out value))
                    {
                        e.DataValue = Convert.ToInt32(value).ToString(CultureInfo.InvariantCulture);
                    }
                }
                else if (state.DataType == typeof(DateTime))
                {
                    e.DataType = PXDbType.DateTime;

                    DateTime dt;
                    if (sValue != null && DateTime.TryParse(sValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                    {
                        e.DataValue = dt.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
                    }
                }
                else
                {
                    e.DataType = PXDbType.NVarChar;
                }

                List <Type> init = new List <Type>();
                if ((e.Operation & PXDBOperation.Option) == PXDBOperation.External)
                {
                    init.Add(sender.GetItemType());
                }
                else if (e.Table != null)
                {
                    init.Add(e.Table);
                }
                else
                {
                    init.Add(_BqlTable);
                }


                if (aggregateAttributes && (e.Operation & PXDBOperation.Option) != PXDBOperation.External)
                {
                    e.FieldName = null;
                }
                else
                {
                    var    bld   = new StringBuilder();
                    String field = BqlCommand.GetSingleField(((IBqlSearch)_SingleSelect).GetField(), sender.Graph, new List <Type>(_SingleSelect.GetTables()), null, BqlCommand.FieldPlace.Select);
                    bld.Append(BqlCommand.SubSelect).Append(field);

                    _SubSelect.Parse(sender.Graph, null, init, null, null, bld, null);
                    bld.Replace((string)(new AttributeIDPlaceholder().Value), attributeName);
                    bld.Append(')');

                    var stringState = state as PXStringState;
                    if (stringState != null && stringState.MultiSelect)
                    {
                        e.FieldName = e.SqlDialect.functions2sql("concat", new[] { "','", bld.ToString(), "','" });
                    }
                    else
                    {
                        e.FieldName = bld.ToString();
                    }
                }
                e.BqlTable = _BqlTable;
            }
            else
            {
                e.FieldName = null /* BqlCommand.Null */;
            }
        }
			public override void CommandPreparing(PXCache sender, PXCommandPreparingEventArgs e)
			{
				if ((e.Operation & PXDBOperation.Command) == PXDBOperation.Select)
				{
					if (((e.Operation & PXDBOperation.Option) == PXDBOperation.Normal ||
					(e.Operation & PXDBOperation.Option) == PXDBOperation.Internal))
					{
						base.CommandPreparing(sender, e);
					}
					else if ((e.Operation & PXDBOperation.Option) == PXDBOperation.GroupBy)
					{
						e.FieldName = BqlCommand.Null;
					}
				}
			}
        protected virtual void AttributeCommandPreparing(PXCache sender, PXCommandPreparingEventArgs e, PXFieldState state, string attributeName, int iField)
        {
            if (!_IsActive || (e.Operation & PXDBOperation.Command) != PXDBOperation.Select)
            {
                return;
            }

            if (!_BqlTable.IsAssignableFrom(sender.BqlTable))
            {
                if (sender.Graph.Caches[_BqlTable].BqlSelect != null &&
                    ((e.Operation & PXDBOperation.Option) == PXDBOperation.External ||
                     (e.Operation & PXDBOperation.Option) == PXDBOperation.Normal && e.Value == null))
                {
                    e.Cancel    = true;
                    e.DataType  = PXDbType.NVarChar;
                    e.DataValue = e.Value;
                    e.BqlTable  = _BqlTable;
                    e.Expr      = new Column(state.Name, (e.Operation & PXDBOperation.Option) == PXDBOperation.External ? sender.GetItemType().Name : _BqlTable.Name);
                }
                else
                {
                    PXCommandPreparingEventArgs.FieldDescription description;
                    e.Cancel = !sender.Graph.Caches[_BqlTable].RaiseCommandPreparing(state.Name, e.Row, e.Value, e.Operation, e.Table, out description);
                    if (description != null)
                    {
                        e.DataType  = description.DataType;
                        e.DataValue = description.DataValue;
                        e.BqlTable  = _BqlTable;
                        e.Expr      = description.Expr;
                    }
                }
            }
            else if (((e.Operation & PXDBOperation.Option) == PXDBOperation.External ||
                      (e.Operation & PXDBOperation.Option) == PXDBOperation.Normal && e.Value == null))
            {
                e.Cancel    = true;
                e.DataValue = e.Value;
                string sValue = e.Value as string;
                if (state.DataType == typeof(bool))
                {
                    e.DataType = PXDbType.Bit;

                    bool value;
                    if (sValue != null && bool.TryParse(sValue, out value))
                    {
                        e.DataValue = Convert.ToInt32(value).ToString(CultureInfo.InvariantCulture);
                    }
                }
                else if (state.DataType == typeof(DateTime))
                {
                    e.DataType = PXDbType.DateTime;
                    string dateFormat = "yyyy-MM-dd HH:mm:ss.fff";
                    if (sValue != null && DateTime.TryParse(sValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dt))
                    {
                        e.DataValue = dt.ToString(dateFormat, CultureInfo.InvariantCulture);
                    }
                    if (e.Value is DateTime date)
                    {
                        e.DataValue = date.ToString(dateFormat, CultureInfo.InvariantCulture);
                    }
                }
                else
                {
                    e.DataType = PXDbType.NVarChar;
                }

                List <Type> init = new List <Type>();
                if ((e.Operation & PXDBOperation.Option) == PXDBOperation.External)
                {
                    init.Add(sender.GetItemType());
                }
                else if (e.Table != null)
                {
                    init.Add(e.Table);
                }
                else
                {
                    init.Add(_BqlTable);
                }


                if (aggregateAttributes && (e.Operation & PXDBOperation.Option) != PXDBOperation.External)
                {
                    e.Expr = null;
                }
                else
                {
                    var    bld   = new StringBuilder();
                    String field = BqlCommand.GetSingleField(((IBqlSearch)_SingleSelect).GetField(), sender.Graph, new List <Type>(_SingleSelect.GetTables()), null, BqlCommand.FieldPlace.Select);
                    bld.Append(BqlCommand.SubSelect).Append(field);

                    _SubSelect.Parse(sender.Graph, null, init, null, null, bld, null);
                    bld.Replace((string)(new AttributeIDPlaceholder().Value), attributeName);
                    bld.Append(')');

                    SQLExpression fexp = BqlCommand.GetSingleExpression(((IBqlSearch)_SingleSelect).GetField(), sender.Graph, new List <Type>(_SingleSelect.GetTables()), null, BqlCommand.FieldPlace.Select);
                    Query         q    = _SubSelect.GetQueryInternal(sender.Graph, new BqlCommandInfo(false)
                    {
                        Tables = init
                    }, null);
                    q.ClearSelection();
                    q.Field(fexp);
                    q.GetWhere().substituteConstant((string)(new AttributeIDPlaceholder().Value), attributeName);

                    var stringState = state as PXStringState;
                    if (stringState != null && stringState.MultiSelect)
                    {
                        e.Expr = new SQLTree.Constant(",").Concat(new SubQuery(q)).Concat(new SQLTree.Constant(","));
                    }
                    else if (state?.DataType == typeof(bool))
                    {
                        e.Expr = new SQLExpression(SQLExpression.Operation.ISNULL_FUNC).SetLeft(new SubQuery(q)).SetRight(SQLTree.Constant.SQLConstant(0));
                    }
                    else
                    {
                        e.Expr = new SubQuery(q);
                    }
                }
                e.BqlTable = _BqlTable;
            }
            else
            {
                e.Expr = null;
            }
        }
		protected virtual void SOLine2_ShippedQty_CommandPreparing(PXCache sender, PXCommandPreparingEventArgs e)
		{
			if ((e.Operation & PXDBOperation.Command) == PXDBOperation.Update)
			{
				e.FieldName = string.Empty;
				e.Cancel = true;
			}
		}
        public override void CommandPreparing(PXCache sender, PXCommandPreparingEventArgs e)
        {
            if (!_IsActive || _Fields == null || !e.IsSelect())
            {
                return;
            }

            PXDBOperation eOp = e.Operation & PXDBOperation.Option;
            bool          operationAllowsSelectForXml =
                eOp == PXDBOperation.External ||
                eOp == PXDBOperation.Internal ||
                eOp == PXDBOperation.Normal && e.Value == null ||
                eOp == PXDBOperation.GroupBy && sender.BqlSelect != null;                    // it's ok because the subselect will be used inside FROM (...) part without any aggregation
            Type tableToUse = eOp == PXDBOperation.External ? sender.GetItemType() : e.Table ?? _BqlTable;

            if (!_BqlTable.IsAssignableFrom(sender.BqlTable))
            {
                if (sender.Graph.Caches[_BqlTable].BqlSelect != null && operationAllowsSelectForXml)
                {
                    e.BqlTable = _BqlTable;
                    e.Expr     = new Column(_DatabaseFieldName, (e.Operation & PXDBOperation.Option) == PXDBOperation.External ? sender.GetItemType() : _BqlTable);
                }
                else
                {
                    PXCommandPreparingEventArgs.FieldDescription description;
                    sender.Graph.Caches[_BqlTable].RaiseCommandPreparing(_DatabaseFieldName, e.Row, e.Value, e.Operation, e.Table, out description);
                    if (description != null)
                    {
                        e.DataType  = description.DataType;
                        e.DataValue = description.DataValue;
                        e.BqlTable  = _BqlTable;
                        e.Expr      = description.Expr;
                    }
                }
            }
            else
            {
                var dialect = e.SqlDialect;
                if (operationAllowsSelectForXml)
                {
                    if (aggregateAttributes && (e.Operation & PXDBOperation.Option) != PXDBOperation.External)
                    {
                        List <Type> init = new List <Type>()
                        {
                            tableToUse
                        };

                        e.BqlTable = tableToUse;
                        e.DataType = PXDbType.NVarChar;

                        e.Expr = new SQLTree.SubQuery(GetAttributesJoinedQuery(sender.Graph, init, ((IBqlSearch)_SingleSelect).GetField(), _PureWhere));
                        return;
                    }
                    else
                    {
                        e.BqlTable = _BqlTable;

                        Query q = new Query().Field(new SQLConst(_Fields.Length));
                        e.Expr = new SubQuery(q);
                    }
                }
                else
                {
                    e.Expr = SQLExpression.Null();
                }
            }
            e.DataType   = PXDbType.Int;
            e.DataLength = 4;
        }