I would like to fill 2 fields in the purchase order from sales order marked for PO (Purchase To Order) (one from SOOrderExt, the second from SOLineExt). How to do this with the Processing Form "Create Purchase Order" (PO.50.50.00)
You need to override FindOrCreatePOOrder
and FindOrCreatePOLine
on POCreate
graph.
In FindOrCreatePOOrder
the new poOrder is not in the cache yet, it is just a simple object, so you can't get it's extension. You have to set Agence on your new PO in FindOrCreatePOLine
.
using PX.Data;
using PX.Objects.CS;
using PX.Objects.PO;
using PX.Objects.SO;
namespace PX.Objects.PO
{
// Acuminator disable once PX1016 ExtensionDoesNotDeclareIsActiveMethod extension should be constantly active
public class POCreateExt : PXGraphExtension<POCreate>
{
private int? AgenceFromSO;
public delegate POOrder FindOrCreatePOOrderDelegate(DocumentList<POOrder> created, POOrder previousOrder,
POFixedDemand demand, SOOrder soorder, POOrderEntry.SOLineSplit3 soline, bool requireSingleProject);
[PXOverride]
public virtual POOrder FindOrCreatePOOrder(DocumentList<POOrder> created, POOrder previousOrder,
POFixedDemand demand, SOOrder soorder, POOrderEntry.SOLineSplit3 soline, bool requireSingleProject,
FindOrCreatePOOrderDelegate baseMethod)
{
var poOrder = baseMethod.Invoke(created, previousOrder, demand, soorder, soline, requireSingleProject);
// Save UsrAgence for FindOrCreatePOLine
var soOrderExt = soorder?.GetExtension<SOOrderExt>();
AgenceFromSO = soOrderExt?.UsrAgence;
return poOrder;
}
public delegate POLine FindOrCreatePOLineDelegate(POOrderEntry docgraph, DocumentList<POLine> ordered,
string orderType,
POFixedDemand demand, POOrderEntry.SOLineSplit3 soline);
[PXOverride]
public virtual POLine FindOrCreatePOLine(POOrderEntry docgraph, DocumentList<POLine> ordered, string orderType,
POFixedDemand demand, POOrderEntry.SOLineSplit3 soline, FindOrCreatePOLineDelegate baseMethod)
{
// Set Agence on header here
var poOrderExt = docgraph.Document.Current.GetExtension<POOrderExt>();
poOrderExt.UsrAgence = AgenceFromSO;
POLine poLine = baseMethod.Invoke(docgraph, ordered, orderType, demand, soline);
SOLine soLineActual = SOLine.PK.Find(docgraph, soline.OrderType, soline.OrderNbr, soline.LineNbr);
SOLineExt soLineExt = soLineActual.GetExtension<SOLineExt>();
if (poLine != null)
{
decimal? price = soLineExt.UsrVendorPrice;
docgraph.Transactions.SetValueExt<POLine.curyUnitCost>(poLine, price);
}
return poLine;
}
}
}