Confirming a Purchase Order in X++ has already been done, but I thought I'd include the use case of when a client is marked as 'InterCompany', thereby the purchase orders are mimicked in another company but as a Sales Order for a supplier. The issue is that once the sales order copy is created, it does not get confirmed as well (nor may it not accept the delivery note automatically once it is marked as dispatched).
Below is the code in it's simplest form. Note: Ensure InterCompanyAutoCreateOrders and InterCompanyDirectDeliver are selected for the Sales Order.
static void tutorial_POConfirmJob(Args _args)
{
// Post Purch Confirmation, cross company
// --------------------------------------
PurchFormLetter purchFormLetter;
PurchTable purchTable;
;
changeCompany('qca')
{
purchTable = purchTable::find('QCA-000041');
purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);
purchFormLetter.update(purchTable, strFmt('%1-%2', purchTable.PurchId, VendPurchOrderJour::numberOfPurchOrder(purchTable)+1));
}
}
To plug the above into the existing framework try creating the following method in the SalesConfirmJournalPost class.
/// <summary>
/// Runs after posting a journal.
/// </summary>
public void postJournalPost()
{
SetEnumerator se = ordersPosted.getEnumerator();
PurchFormLetter purchFormLetter;
PurchTable purchTable;
ttsbegin;
while (se.moveNext())
{
salesTable = SalesTable::find(se.current(),true);
if (salesTable && strLen(salesTable.InterCompanyCompanyId) > 0
&& strLen(salesTable.InterCompanyPurchId) > 0)
{
// Post Purch Confirmation, cross company.
changeCompany(salesTable.InterCompanyCompanyId)
{
purchTable = purchTable::find(salesTable.InterCompanyPurchId);
if (purchTable
&& purchTable.DocumentState < VersioningDocumentState::Confirmed)
{
purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);
purchFormLetter.update(purchTable,
strFmt('%1-%2', purchTable.PurchId,
VendPurchOrderJour::numberOfPurchOrder(purchTable)+1));
if (purchTable::find(salesTable.InterCompanyPurchId).DocumentState
== VersioningDocumentState::Confirmed)
{
info(strfmt('Intercompany PO '%1' has also been Confirmed.', purchTable.PurchId));
}
}
}
}
}
ttscommit;
}
Obviously, please test the code. Yours truly will not be held responsible for creating phantom stock in one of your companies!