Mupltiple purchase orders on a single invoice

Hi All,
I need to consolidate the purchase order invoice i.e. one invoice can contain multiple purchase orders, for that I am using the below code but don’t know what’s wrong, it’s not grouping the PO.
Please see the description.

static void InvoiceConsolidation(Args _args)
{
PurchFormLetter purchFormLetter;
PurchFormletterParmData purchFormLetterParmData;
PurchParmUpdate purchParmUpdate;
PurchParmTable purchParmTable;
PurchParmLine purchParmLine;
PurchTable purchTable;
PurchLine purchLine;
PurchId purchId;
Num packingSlipId;
Num invoiceId;

VendInvoiceInfoTable vendInvoiceInfoTable;
VendInvoiceInfoLine vendInvoiceInfoLine;
VendInvoiceInfoSubTable vendInvoiceInfoSubTable;

purchId = “PO0000223”;
packingSlipId = “PS000006”;
//invoiceId = “PS005” ;

purchTable = PurchTable::find(purchId);

ttsBegin;
// Create PurchParamUpdate table
purchFormLetterParmData = PurchFormletterParmData::newData(
DocumentStatus::PackingSlip,
VersioningUpdateType::Initial);

purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
purchFormLetterParmData.createData(false);
purchParmUpdate = purchFormLetterParmData.parmParmUpdate();

// Set PurchParmTable table
purchParmTable.clear();
purchParmTable.initValue(); //Change
purchParmTable.TransDate = SystemDateGet();
purchParmTable.Ordering = DocumentStatus::Invoice;
purchParmTable.DocumentDate = systemDateGet();
purchParmTable.ParmJobStatus = ParmJobStatus::Waiting;
purchParmTable.Num = packingSlipId;
purchParmTable.PurchId = purchTable.PurchId;
purchParmTable.PurchName = purchTable.PurchName;
purchParmTable.DeliveryName = purchTable.DeliveryName;
purchParmTable.DeliveryPostalAddress = purchTable.DeliveryPostalAddress;
purchParmTable.OrderAccount = purchTable.OrderAccount;
purchParmTable.CurrencyCode = purchTable.CurrencyCode;
purchParmTable.InvoiceAccount = purchTable.InvoiceAccount;
purchParmTable.ParmId = purchParmUpdate.ParmId;
purchParmTable.insert();

vendInvoiceInfoTable.initFromPurchTable(purchTable);
vendInvoiceInfoTable.ParmId = purchParmTable.ParmId;
vendInvoiceInfoTable.DocumentDate = systemDateGet();
vendInvoiceInfoTable.Num = “Inv_” + purchTable.PurchId;
vendInvoiceInfoTable.insert();

// Set PurchParmLine table
while select purchLine
where purchLine.PurchId == ‘PO0000223’
{
purchParmLine.InitFromPurchLine(purchLine);

purchParmLine.ReceiveNow = PurchLine.PurchReceivedNow;
purchParmLine.ParmId = purchParmTable.ParmId;
purchParmLine.TableRefId = purchParmTable.TableRefId;
purchParmLine.setQty(DocumentStatus::Invoice, false, true);
purchParmLine.setLineAmount();
purchParmLine.insert();

vendInvoiceInfoLine.clear();
vendInvoiceInfoLine.initFromPurchLine(purchLine);
vendInvoiceInfoLine.ParmId = vendInvoiceInfoTable.ParmId;
vendInvoiceInfoLine.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoLine.insert();
}

// Set PurchParmLine table
while select purchLine
where purchLine.PurchId == ‘PO0000222’
{
purchParmLine.InitFromPurchLine(purchLine);
purchParmLine.OrigPurchId = ‘PO0000223’;
purchParmLine.ReceiveNow = PurchLine.PurchReceivedNow;
purchParmLine.ParmId = purchParmTable.ParmId;
purchParmLine.TableRefId = purchParmTable.TableRefId;
purchParmLine.setQty(DocumentStatus::Invoice, false, true);
purchParmLine.setLineAmount();
purchParmLine.insert();

vendInvoiceInfoLine.clear();
vendInvoiceInfoLine.initFromPurchLine(purchLine);
//vendInvoiceInfoLine.OrigPurchId = ‘PO0000215’;
vendInvoiceInfoLine.ParmId = vendInvoiceInfoTable.ParmId;
vendInvoiceInfoLine.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoLine.insert();
}

vendInvoiceInfoSubTable.clear();
vendInvoiceInfoSubTable.OrigPurchId = ‘PO0000223’;
vendInvoiceInfoSubTable.ParmId = vendInvoiceInfoTable.ParmId;
vendInvoiceInfoSubTable.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoSubTable.insert();

vendInvoiceInfoSubTable.clear();
vendInvoiceInfoSubTable.OrigPurchId = ‘PO0000222’;
vendInvoiceInfoSubTable.ParmId = vendInvoiceInfoTable.ParmId;
vendInvoiceInfoSubTable.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoSubTable.insert();

purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
purchFormLetter.transDate(systemDateGet());
purchFormLetter.proforma(false);
purchFormLetter.specQty(PurchUpdate::ReceiveNow);
purchFormLetter.purchTable(purchTable);
purchFormLetter.purchParmUpdate(purchParmUpdate);

purchFormLetter.reArrange(true,true,true);

//purchFormLetter.update(purchtable, vendInvoiceInfoTable.Num , systemdateget());

// This is the ID we hard code as the product receipt ID, if we do the posting via UI
// user would have the option to manually enter this value
//purchFormLetter.parmParmTableNum(purchParmTable.ParmId);
//purchFormLetter.parmId(purchParmTable.ParmId);
//purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());
//
//purchFormLetter.update(purchtable, vendInvoiceInfoTable.Num, systemdateget());

//purchFormLetter.run();

//purchFormLetter.update(purchtable, “Inv_” + purchTable.PurchId, systemdateget());
ttsCommit;
}

Best Regards

Pranav Gupta

Don’t try to manipulate the tables directly, you just have to call the posting process with right parameters, because AX already supports summary updates (see Summary update sales and purchase orders [AX 2012]).

Unfortunately you forgot to attach a tag with your version of AX, so we don’t know which version you’re talking about. Please fix it.

Hi Martin,

Thanks for the reply, Currently I am using the Ax 2012 R3 version.
And now I have figure out the issue.

Instead of creating the new “parmId”, I have used the “parmId” created for the consolidated PO Packing slip.
This resolves my problem.

Below is the corrected code:

static void InvoiceConsolidation(Args _args)
{
PurchFormLetter purchFormLetter;
PurchFormletterParmData purchFormLetterParmData;
PurchParmUpdate purchParmUpdate;
PurchParmTable purchParmTable;
PurchParmLine purchParmLine;
PurchTable purchTable;
PurchLine purchLine;
PurchId purchId;
Num packingSlipId;
Num invoiceId;

VendInvoiceInfoTable vendInvoiceInfoTable;
VendInvoiceInfoLine vendInvoiceInfoLine;
VendInvoiceInfoSubTable vendInvoiceInfoSubTable;

purchId = “PO0000230”;
packingSlipId = “PS000012”;

purchTable = PurchTable::find(purchId);

ttsBegin;
// Create PurchParamUpdate table
purchFormLetterParmData = PurchFormletterParmData::newData(
DocumentStatus::PackingSlip,
VersioningUpdateType::Initial);

purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
purchFormLetterParmData.createData(false);

purchParmUpdate = PurchParmUpdate::find(‘PHO-004150’, false);

purchParmTable = PurchParmTable::findPurchId(“PO0000230”, ‘PHO-004150’);

vendInvoiceInfoTable.initFromPurchTable(purchTable);

vendInvoiceInfoTable.Approved = NoYes::Yes;
vendInvoiceInfoTable.DeliveryName = purchTable.DeliveryName;
vendInvoiceInfoTable.PurchName = purchTable.PurchName;
vendInvoiceInfoTable.BatchAdministration = NoYes::Yes;
vendInvoiceInfoTable.ParmJobStatus = ParmJobStatus::Waiting;
vendInvoiceInfoTable.FixedDueDate = purchTable.FixedDueDate;
vendInvoiceInfoTable.LastMatchVariance = LastMatchVarianceOptions::OK;
vendInvoiceInfoTable.TransDate = systemDateGet();
vendInvoiceInfoTable.VendInvoiceSaveStatus = VendInvoiceSaveStatus::Pending;

vendInvoiceInfoTable.Num = “Inv_” + purchTable.PurchId;

vendInvoiceInfoTable.insert();

// Set PurchParmLine table
while select purchLine
where purchLine.PurchId == ‘PO0000230’
{
vendInvoiceInfoLine.clear();
vendInvoiceInfoLine.initFromPurchLine(purchLine);
vendInvoiceInfoLine.InventNow = 1;
vendInvoiceInfoLine.LineAmount = purchLine.LineAmount;
vendInvoiceInfoLine.ReceiveNow = 1;
vendInvoiceInfoLine.RemainBefore = 1;
vendInvoiceInfoLine.RemainBeforeInvent = 1;
vendInvoiceInfoLine.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoLine.insert();
}

// Set PurchParmLine table
while select purchLine
where purchLine.PurchId == ‘PO0000231’
{
vendInvoiceInfoLine.clear();
vendInvoiceInfoLine.initFromPurchLine(purchLine);
vendInvoiceInfoLine.InventNow = 1;
vendInvoiceInfoLine.LineAmount = purchLine.LineAmount;
vendInvoiceInfoLine.ReceiveNow = 1;
vendInvoiceInfoLine.RemainBefore = 1;
vendInvoiceInfoLine.RemainBeforeInvent = 1;
vendInvoiceInfoLine.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoLine.insert();
}

vendInvoiceInfoSubTable.clear();
vendInvoiceInfoSubTable.OrigPurchId = ‘PO0000230’;
vendInvoiceInfoSubTable.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoSubTable.PurchName = purchTable.PurchName;
vendInvoiceInfoSubTable.insert();

vendInvoiceInfoSubTable.clear();
vendInvoiceInfoSubTable.OrigPurchId = ‘PO0000231’;
vendInvoiceInfoSubTable.PurchName = purchTable.PurchName;
vendInvoiceInfoSubTable.TableRefId = vendInvoiceInfoTable.TableRefId;
vendInvoiceInfoSubTable.insert();

purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
purchFormLetter.transDate(systemDateGet());
purchFormLetter.proforma(false);
purchFormLetter.specQty(PurchUpdate::ReceiveNow);
purchFormLetter.purchTable(purchTable);
purchFormLetter.purchParmUpdate(purchParmUpdate);

purchFormLetter.reArrange(true,true,true);
purchFormLetter.update(purchtable, vendInvoiceInfoTable.Num , systemdateget());

info(‘Done’);

Best Regards
Pranav