首页 文章

计算和保存自定义字段

提问于
浏览
0

我有一个绑定的自定义字段毛利润添加到SOLine网格中,它是根据Item UnitCost,AverageCost和Qty计算的 .

我将代码写入SOLine_CuryUnitPrice_FieldSelecting事件 . 但是,它不会保存到数据库中 .

此外,我需要总计自定义字段的所有lineitems作为OrderGrossProfit on Order摘要 . 它正在计算但由于某种原因它没有反映在屏幕上 .

谁有人建议?

这是代码

public class SOOrderExtension : PXCacheExtension<SOOrder>
{
	#region UsrTotalGrossProfit

	public abstract class usrTotalGrossProfit : PX.Data.IBqlField
	{
	}
	protected Decimal? _UsrTotalGrossProfit;

	[PXCurrency(typeof(SOOrder.curyInfoID), typeof(SOOrder.orderWeight))]
	[PXDefault(TypeCode.Decimal, "0.0")]
	[PXUIField(DisplayName = "Total Gross Profit")]
	public virtual Decimal? UsrTotalGrossProfit
	{
		get
		{
			return this._UsrTotalGrossProfit;
		}
		set
		{
			this._UsrTotalGrossProfit = value;
		}
	}
	#endregion
}

public class SOLineExtension : PXCacheExtension<SOLine>
{
	#region UsrGrossProfit

	public abstract class usrGrossProfit : PX.Data.IBqlField
	{
	}
	protected Decimal? _UsrGrossProfit;

	[PXDBCurrency(typeof(SOOrder.curyInfoID), typeof(SOOrder.discTot))]
	[PXDefault(TypeCode.Decimal, "0.0")]
	[PXUIField(DisplayName = "Gross Profit")]
	public virtual Decimal? UsrGrossProfit
	{
		get
		{
			return this._UsrGrossProfit;
		}
		set
		{
			this._UsrGrossProfit = value;
		}
	}
	#endregion
}

public class SOOrderEntryExtension : PXGraphExtension<SOOrderEntry>
{	
	protected virtual void SOOrder_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
	{
		SOOrder row = e.Row as SOOrder;
		if (row == null) return;

		SOOrderExtension orderExtension = PXCache<SOOrder>.GetExtension<SOOrderExtension>(row);

		foreach (SOLine soLine in Base.Transactions.Select())
		{
			SOLineExtension lineItem = PXCache<SOLine>.GetExtension<SOLineExtension>(soLine);

			orderExtension.UsrTotalGrossProfit = orderExtension.UsrTotalGrossProfit + lineItem.UsrGrossProfit;
		}
	}
			
	protected void SOLine_CuryUnitPrice_FieldSelecting(PXCache sender, PXFieldSelectingEventArgs e)
	{
		var row = (SOLine)e.Row;

		if (row != null && Base.Document.Current != null && Base.Document.Current.Status != "C")
		{
			SOLineExtension opportunity = PXCache<SOLine>.GetExtension<SOLineExtension>(row);

			InventoryItem inv = PXSelect<InventoryItem,
				Where<InventoryItem.inventoryID, Equal<Required<InventoryItem.inventoryID>>>>.Select(new PXGraph(), row.InventoryID);

			if (inv.ItemType == "F")      // Stock Item
			{
				opportunity.UsrGrossProfit = (row.UnitPrice - invc.AvgCost) * row.Qty;
			}

			Base.Save.Press();
			Base.Persist();
		}
	}
}

1 回答

  • 0

    这些是我将要使用的两个事件..你可能需要将它们更改为你的C#版本我正在使用7 ...

    protected void SOLine_RowUpdated(PXCache sender, PXRowUpdatedEventArgs e)
        {
            if (!e.row is SOLine row) return;
            if (!e.oldRow is SOLine oldRow) return;
            //check for unwanted status, if true return
            if (Base.Document.Current.Status == "C") return;
            //check for the fields to have value, if no return
            if (!row.UnitPrice.HasValue || !row.Qty.HasValue) return;
            //get row extension
            var rowExt = row..GetExtension<SOLineExtension>();
            //select inventory record
            var invItem = (InventoryItem)PXSelect<InventoryItem,
                Where<InventoryItem.inventoryID, Equal<Required<InventoryItem.inventoryID>>>>.Select(Base, row.InventoryID);    
            if (invItem == null) return;
            //add value to field, no persist or save
            if (inv.ItemType == "F")      // Stock Item
            {
                rowExt.UsrGrossProfit = (row.UnitPrice - invc.AvgCost) * row.Qty;
            }           
        }
    
        protected virtual void SOOrder_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
        {
            //this may not work in older versions of C#
            if (!(e.row is SOOrder row) return;
    
            var rowExt = row.GetExtension<SOOrderExtension>();
    
            foreach (SOLine soLine in Base.Transactions.Select())
            {
                var lineItem = soLine.GetExtension<SOLineExtension>();
                rowExt.UsrTotalGrossProfit += lineItem.UsrGrossProfit;
            }
            //this is probably why not showing up...
            Base.Document.Cache.Update(Base.Document.Current);
        }
    

相关问题