代码之家  ›  专栏  ›  技术社区  ›  Maxime

未绑定字段中行的总和

  •  0
  • Maxime  · 技术社区  · 6 年前

    我想使用PXUnboundFormula,但字段保持为0。

    dac定义:

    public abstract class usrTotalLignes : IBqlField { }
    [PXDecimal]
    [PXDefault(TypeCode.Decimal, "0.0")]
    [PXUIField(DisplayName = "Total des lignes")]
    public virtual Decimal? UsrTotalLignes { get; set; }
    

    这似乎管用。

    protected virtual void POLine_RowInserted(PXCache sender, PXRowInsertedEventArgs e)
            {
                POLine orderLine = (POLine)e.Row;
                POOrder order = Base.Document.Current;
                POOrderExt orderExt = order.GetExtension<POOrderExt>();
                bool isLineUpdated = false;
    
                if (orderLine != null)
                {
                    orderExt.UsrTotalLignes += orderLine.OrderQty;
                    isLineUpdated = true;
                }
    
                if (isLineUpdated)
                {
                    Base.Document.Update(order);
                }
            }
    
            protected virtual void POLine_RowUpdated(PXCache sender, PXRowUpdatedEventArgs e)
            {
                POLine newOrderLine = (POLine)e.Row;
                POLine oldOrderLine = (POLine)e.OldRow;
                POOrder order = Base.Document.Current;
                POOrderExt orderExt = order.GetExtension<POOrderExt>();
                bool isLineUpdated = false;
    
                if (!sender.ObjectsEqual<POLine.orderQty>(newOrderLine, oldOrderLine))
                {
                    if (oldOrderLine.OrderQty != null)
                    {
                        orderExt.UsrTotalLignes -= oldOrderLine.OrderQty;
                    }
                    if (newOrderLine.OrderQty != null)
                    {
                        orderExt.UsrTotalLignes += newOrderLine.OrderQty;
                    }
                    isLineUpdated = true;
                }
    
                if (isLineUpdated)
                {
                    Base.Document.Update(order);
                }
            }
    
            protected virtual void POLine_RowDeleted(PXCache sender, PXRowDeletedEventArgs e)
            {
                POLine orderLine = (POLine)e.Row;
                POOrder order = Base.Document.Current;
                POOrderExt orderExt = order.GetExtension<POOrderExt>();
                bool isLineUpdated = false;
    
                if (orderLine != null)
                {
                    orderExt.UsrTotalLignes -= orderLine.OrderQty;
                    isLineUpdated = true;
                }
    
                if (isLineUpdated)
                {
                    Base.Document.Update(order);
                }
            }
    

    当我添加、编辑或删除一行时,它起作用。

    现在我的问题是,当我想在加载页面时设置此未绑定字段的值时,我尝试了以下方法:

    [PXDecimal]
    [PXDefault(TypeCode.Decimal, "0.0")]
    [PXUIField(DisplayName = "Total des lignes")]
    [PXUnboundFormula(typeof(POLine.orderQty), typeof(SumCalc<POOrderExt.usrTotalLignes>))]
    public virtual void POOrder_UsrTotalLignes_CacheAttached(PXCache sender)
    {
    
    }
    

    我试着:

    [PXDecimal]
    [PXDefault(TypeCode.Decimal, "0.0")]
    [PXUIField(DisplayName = "Total des lignes")]
    [PXUnboundFormula(typeof(Sum<POLine.orderQty>), typeof(SumCalc<POOrderExt.usrTotalLignes>))]
    public virtual void POOrder_UsrTotalLignes_CacheAttached(PXCache sender)
    {
    
    }
    

    我还试图补充:

    [PXParent(typeof(Select<POOrder, Where<POOrder.orderNbr, Equal<Current<POLine.orderNbr>>, 
                And<POOrder.orderType, Equal<Current<POLine.orderType>>>>>))]
    

    但它只是内环和崩溃。

    protected void POOrder_RowSelecting(PXCache sender, PXRowSelectingEventArgs e)
    {
        POOrder order = (POOrder)e.Row;
        if (order == null) return;
    
        var extension = PXCache<POOrder>.GetExtension<POOrderExt>(order);
        using (PXConnectionScope cs = new PXConnectionScope())
        {
            extension.UsrTotalLignes = 0;
            foreach(POLine line in PXSelectReadonly<POLine, 
                Where<POLine.orderNbr, Equal<Required<POOrder.orderNbr>>, 
                    And<POLine.orderType, Equal<Required<POOrder.orderType>>>>>.Select(Base, order.OrderNbr, order.OrderType))
            {
                extension.UsrTotalLignes += line.OrderQty;
            }
        }
    }
    

    如果我修改了一个字段(如果我加载了一个旧的记录,在我修改它之前它不会工作,然后它会写入值),那么我如何要求UI刷新缓存的值呢?

    我的错误在哪里?

    编辑:

    我设法让它与这个一起工作:

    protected void POOrder_RowSelecting(PXCache sender, PXRowSelectingEventArgs e)
            {
                POOrder order = (POOrder)sender.Current;
                if (order == null) return;
    
                var extension = PXCache<POOrder>.GetExtension<POOrderExt>(order);
                using (PXConnectionScope cs = new PXConnectionScope())
                {
                    extension.UsrTotalLignes = 0;
                    foreach(POLine line in PXSelectReadonly<POLine, 
                        Where<POLine.orderNbr, Equal<Required<POOrder.orderNbr>>, 
                            And<POLine.orderType, Equal<Required<POOrder.orderType>>>>>.Select(Base, order.OrderNbr, order.OrderType))
                    {
                        extension.UsrTotalLignes += line.OrderQty;
                    }
                }
                Base.Document.Current.GetExtension<POOrderExt>().UsrTotalLignes = extension.UsrTotalLignes;
            }
    

    我不得不使用:

    POOrder order = (POOrder)sender.Current;
    

    POOrder order = (POOrder)e.Row 
    

    另外,由于某些原因,我不明白,当第一次加载的记录,它工作得很好。然后,当我更改了任何包含“commit”的字段时,它再次调用了rowSelectingEvent,这次e.row是列表中的下一个差的字段。

    知道为什么会这样吗?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Hugues Beauséjour    6 年前

    正如John answer提到的,在POLine.OrderQty DAC字段中有一个PXFormula属性,它带有一个SumCalc BQL元素,目标是POOrder.OrderQty的total字段:

    #region OrderQty
    public abstract class orderQty : PX.Data.IBqlField
    {
    }
    protected Decimal? _OrderQty;
    [PXDBQuantity(typeof(POLineS.uOM), typeof(POLineS.baseOrderQty), HandleEmptyKey = true, BqlField = typeof(POLine.orderQty))]
    [PXDefault(TypeCode.Decimal, "0.0")]
    [PXFormula(null, typeof(SumCalc<POOrder.orderQty>))]
    [PXUIField(DisplayName = "Order Qty.", Visibility = PXUIVisibility.Visible)]
    public virtual Decimal? OrderQty
    {
        get
        {
            return this._OrderQty;
        }
        set
        {
            this._OrderQty = value;
        }
    }
    #endregion
    

    有时您可能需要实现更复杂的逻辑,这不适合简单的公式求和计算。

    您可以在基本事务数据视图上扩展POOrderEntry并调用Select,以迭代“文档详细信息”选项卡中显示的所有记录,并手动计算每行的OrderQty之和:

    public class POOrderEntry_Extension : PXGraphExtension<POOrderEntry>
    {      
      public virtual void POOrder_RowSelected(PXCache sender, PXRowSelectedEventArgs e)
      {
          POOrder order = e.Row as POOrder;
    
          if (order != null)
          {
            sender.SetValue<POOrderExt.usrTotalQuantiteCommande>(order, ComputeQuantiteCommandeTotal());
          }
      }
    
      public virtual void POOrder_UsrTotalQuantiteCommande_FieldSelecting(PXCache sender, PXFieldSelectingEventArgs e)
      {
          e.ReturnValue = ComputeQuantiteCommandeTotal();
      }
    
      public virtual decimal ComputeQuantiteCommandeTotal()
      {
          decimal total = 0M;
    
          foreach (POLine line in Base.Transactions.Select())
          {
              total += (line.OrderQty != null ? line.OrderQty.Value : 0M);
          }
    
          return total;
      }
    }
    
        2
  •  2
  •   John    6 年前