You can put method to InventTable or any table which has ItemId field:
public CostPrice SNBfindLastPrice(DatePhysical _date = systemDateGet(),InventDim _inventdim = null)
{
InventTrans inventTrans;
InventTransOrigin origin;
InventDim inventdim;
LineAmount purchprice;
RecId OK = false;
InventDimParm dimParm;
InventDim dimJoin;
dimparm.initFromInventDim(_inventdim);
select firstOnly CostAmountAdjustment,CostAmountPosted,Qty from inventTrans
order by inventTrans.DateClosed desc,inventTrans.DateFinancial desc
where inventTrans.ItemId == this.ItemId &&
!inventTrans.ReturnInventTransOrigin &&
inventTrans.DateClosed != dateNull() &&
inventTrans.DateClosed <= _date
exists join origin
where origin.RecId == inventTrans.InventTransOrigin &&
origin.ReferenceCategory == InventTransType::SummedUp
#InventDimExistsJoin(inventTrans.InventDimId,dimJoin,_inventdim,dimParm);
OK = inventTrans.RecId;
if (!OK)
{
select firstOnly CostAmountAdjustment,CostAmountPosted,Qty from inventTrans
order by inventTrans.DateFinancial desc
where inventTrans.ItemId == this.ItemId &&
!inventTrans.ReturnInventTransOrigin &&
inventTrans.Qty > 0 &&
inventTrans.DateFinancial <= _date
exists join origin
where origin.RecId == inventTrans.InventTransOrigin &&
origin.ReferenceCategory == InventTransType::Purch
#InventDimExistsJoin(inventTrans.InventDimId,dimJoin,_inventdim,dimParm);
OK = inventTrans.RecId;
}
if (!OK)
{
select firstOnly CostAmountAdjustment,CostAmountPosted,Qty from inventTrans
order by inventTrans.DateFinancial desc
where inventTrans.ItemId == this.ItemId &&
inventTrans.Qty > 0 &&
inventTrans.DateFinancial <= _date
exists join origin
where origin.RecId == inventTrans.InventTransOrigin &&
origin.ReferenceCategory == InventTransType::SummedUp
#InventDimExistsJoin(inventTrans.InventDimId,dimJoin,_inventdim,dimParm);
OK = inventTrans.RecId;
}
if (!OK)
{
select firstOnly CostAmountAdjustment,CostAmountPosted,Qty from inventTrans
order by inventTrans.DateFinancial desc
where inventTrans.ItemId == this.ItemId &&
inventTrans.Qty > 0 &&
inventTrans.DateFinancial <= _date
exists join origin
where origin.RecId == inventTrans.InventTransOrigin &&
( origin.ReferenceCategory == InventTransType::InventTransaction ||
origin.ReferenceCategory == InventTransType::BOMMain)
#InventDimExistsJoin(inventTrans.InventDimId,dimJoin,_inventdim,dimParm);
}
if (inventTrans.Qty != 0)
return abs( (inventTrans.CostAmountAdjustment+inventTrans.CostAmountPosted ) / inventTrans.Qty );
else
return 0;
}