Tuesday 19 July 2016

Consolidate multiple PO's to a single Invoice in Ax.

Consolidate multiple PO's to a single Invoice in Ax.
Below is the 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

No comments:

Post a Comment

Insert/Update or remove the default dimension value in D365 FSCM via x++

Use below method to insert/update the dimension values, just pass the parameter values to the method and it will return the updated value: p...