BQL查询如何使用当前公司

How to use current company in BQL query

我需要知道如何获取用户的当前公司,以便我可以在 BQL 查询的 Where 子句中使用它。我不是在谈论租户“公司”。我说的是在屏幕 CS101500 上定义的公司。我在 AccessInfo 上没有看到任何似乎表明当前公司的信息。当前的分行 ID 在那里,但是,我需要分行所属的公司。

使用版本 21.203

TIA!

您可以尝试使用此代码

 public int? GetOrganizationID(PXGraph graph, int? branchID)
 {
    int? accountID = ((Organization)OrganizationMaint.FindOrganizationByID(graph, PXAccess.GetParentOrganizationID(branchID))).BAccountID;
    return accountID;
 }

这将需要创建自定义 BQL 元素 class。

IBqlOperand - BQL 标量操作数

IBqlCreator - 需要实施 AppendExpression 和 Verify 方法的 Bql 元素创建器。

public class CurrentOrganization : IBqlCreator, IBqlOperand
{

    #region Methods

    #region AppendExpression

    /// <summary>
    /// Appends the SQL tree expression that corresponds to the BQL command to an SQL tree query.
    /// </summary>
    /// <param name="exp">The SQL tree expression to be appended.</param>
    /// <param name="graph">A graph instance.</param>
    /// <param name="info">The information about the BQL command.</param>
    /// <param name="selection">The fragment of the BQL command that is translated to an SQL tree expression.</param>
    /// <returns></returns>
    public bool AppendExpression(ref SQLExpression exp, PXGraph graph, BqlCommandInfo info, BqlCommand.Selection selection)
    {
        if (graph == null || !info.BuildExpression)
        {
            return true;
        }

        PXMutableCollection.AddMutableItem(this);

        exp = new SQLConst(graph.BranchOrganizationID());

        return true;
    }

    #endregion

    #region Verify

    /// <summary>
    /// No Clue.
    /// </summary>
    /// <param name="cache"></param>
    /// <param name="item"></param>
    /// <param name="pars"></param>
    /// <param name="result"></param>
    /// <param name="value"></param>
    public void Verify(PXCache cache, object item, List<object> pars, ref bool? result, ref object value)
    {
        value = cache.Graph.BranchOrganizationID();
    }

    #endregion

    #endregion

}

方法调用 .BranchOrganizationID() 是一个 PXGraph 扩展方法,returns 来自 Branch IPrefetchable

的 OrganizationID

可以用 BQL PXSelect 替换对此预取的调用:

PXSelect<Branch, Where<Branch.branchID, Equal<Current<AccessInfo.branchID>>>>.Select(...)

示例用法如下:

    [PXDefault(typeof(Search<Branch.branchID,Where<Branch.organizationID,Equal<CurrentOrganization>>>))]

根据 Josh 的解决方案,这是满足我需要的最终代码。范霍森获胜!!

public class CurrentOrganization : IBqlCreator, IBqlOperand
{
    public virtual bool AppendExpression(ref SQLExpression exp, PXGraph graph, BqlCommandInfo info, BqlCommand.Selection selection)
    {
        if ((graph == null) || (!info.BuildExpression))
            return true;

        PXMutableCollection.AddMutableItem(this);

        //exp = new SQLConst(graph.BranchOrganizationID());
        Branch branch = PXSelect<Branch, Where<Branch.branchID, Equal<Current<AccessInfo.branchID>>>>.Select(graph);
        exp = new SQLConst( branch.OrganizationID);

        return true;

    }
    public void Verify(PXCache cache, object item, List<object> pars, ref bool? result, ref object value)
    {
        Branch branch = PXSelect<Branch, Where<Branch.branchID, Equal<Current<AccessInfo.branchID>>>>.Select(cache.Graph);
        value = branch.OrganizationID;
        //value = cache.Graph.BranchOrganizationID();
    }
}