是否可以自定义要在Acumatica日期选择器中选择的日期

时间:2019-08-08 08:47:21

标签: acumatica

是否可以在Acumatica日期字段选择器上禁用某些日期?

如果是,怎么办?

我的目标是要禁用今天之前的日期后的数据。

谢谢

1 个答案:

答案 0 :(得分:0)

我们使用的是控制最小值的属性。因此,请勿像您的问题一样更改选择器UI,但应避免使用最小值和最大值进行任何输入。 (请记住,用户无需使用选择器就可以键入任何值)

在日期字段上使用此属性,您可以控制今天之前和/或之后的几天:

public sealed class DaysOffsetDateAttribute : PXDBDateAttribute
{
    /// <summary>
    /// Min date offset in days
    /// </summary>
    private int? _MinOffsetValue;
    /// <summary>
    /// Max date offset in days
    /// </summary>
    private int? _MaxOffsetValue;


    /// <summary>
    /// Minimum date offset in days. The offset adds days to the current business date to define the minimum date value.
    /// If no offset is defined then the default min date value is used without an offset
    /// </summary>
    /// 
    /// <example>
    /// The example below shows a StartDate field that will not allow a date less that yesterday based on the current business date of today.
    /// 
    /// <code>
    /// [DaysOffsetDate(MinOffsetDays = "-1")]
    ///             public virtual DateTime? StartDate { get; set; }
    /// 
    /// </code>
    /// 
    /// </example>
    public string MinOffsetDays
    {
        get { return Convert.ToString(_MinOffsetValue); }
        set { _MinOffsetValue = ConvertToInt(value); }
    }

    /// <summary>
    /// Maximum date offset in days. The offset adds days to the current business date to define the maximum date value.
    /// If no offset is defined then the default max date value is used without an offset
    /// </summary>
    /// 
    /// <example>
    /// The example below shows a EndDate field that will not allow a date less that yesterday and greater than 30 days from now based on the current business date of today.
    /// 
    /// <code>
    /// [DaysOffsetDate(MinOffsetDays = "-1", MaxOffsetDays = "30")]
    ///             public virtual DateTime? EndDate { get; set; }
    /// 
    /// </code>
    /// 
    /// </example>
    public string MaxOffsetDays
    {
        get { return Convert.ToString(_MaxOffsetValue); }
        set { _MaxOffsetValue = ConvertToInt(value); }
    }

    private int? ConvertToInt(string stringValue)
    {
        int v;
        if (Int32.TryParse(stringValue, out v))
        {
            return v;
        }
        return null;
    }

    public override void CacheAttached(PXCache sender)
    {
        if (IsKey)
        {
            sender.Keys.Add(_FieldName);
        }

        DateTime businessDate = Common.Current.BusinessDate(sender.Graph);

        if (_MinValue == null)
        {
            if (_MinOffsetValue != null)
            {
                _MinValue = businessDate.AddDays(_MinOffsetValue.GetValueOrDefault());
            }
            else
            {
                _MinValue = Common.Dates.BeginOfTimeDate;
            }
        }
        if (_MaxValue == null)
        {
            if (_MaxOffsetValue != null)
            {
                _MaxValue = businessDate.AddDays(_MaxOffsetValue.GetValueOrDefault());
            }
            else
            {
                _MaxValue = Common.Dates.EndOfTimeDate;
            }
        }
    }
}

然后您可以在如下所示的字段上使用它。当最小偏移量为零时,表示不小于今天。我们需要退回很多天,因此无论哪种情况,我们都使用此属性。例如,如果您希望日期早于7天,请使用7作为值与0。

#region EffDate
public abstract class effDate : PX.Data.IBqlField
{
}
protected DateTime? _EffDate;
[DaysOffsetDate(MinOffsetDays = "0")]
[PXDefault(typeof(AccessInfo.businessDate))]
[PXUIField(DisplayName = "Start Date")]
public virtual DateTime? EffDate
{
    get
    {
        return this._EffDate;
    }
    set
    {
        this._EffDate = value;
    }
}
#endregion