Posting a Product Receipt with Item Registration through X++ code

In this article we are looking into receiving and registering of purchase order using X++ code in Finance & Operations.

In order to register and receive the purchase order we will be creating a separate class in this demonstration to do both these processes. Please follow below steps in order to do so.

Create a class named as PurchRegistrationReceiving and add the below variables to this class.

    PurchFormLetterParmData          purchFormLetterParmData;
    PurchParmTable                   purchParmTable;
    PurchParmLine                    purchParmLine;
    PurchParmUpdate                  purchParmUpdate;
    PurchTable                       purchTable;
    PurchFormLetter                  purchFormLetter;
    TransDate                        packingSlipDate;
    PackingSlipId                    packingSlipId;

Create a new method named as newFromParameters in this class to construct the object of this new class by providing PurchTable record, Packing slip ID and Date to this method.

public static PurchRegistrationReceiving newFromParameters(PurchTable _purchTable,
                                                 PackingSlipID _packingSlipID,
                                                 TransDate _packingSlipDate)
{
    PurchRegistrationReceiving purchRegReceiving = new PurchRegistrationReceiving();

    purchRegReceiving.PurchTable      = _purchTable;
    purchRegReceiving.packingSlipId   = _packingSlipID;
    purchRegReceiving.packingSlipDate = _packingSlipDate;

    return purchRegReceiving;
}

Create a new method named as insertParmTableData in this class, in this method we will insert data from PurchTable to PurchParmTable.

private void insertParmTableData()
{
    purchFormLetterParmData =
    PurchFormletterParmData::newData(DocumentStatus::PackingSlip,  
                                     VersioningUpdateType::Initial);
    purchFormLetterParmData.parmOnlyCreateParmUpdate(true);
    purchFormLetterParmData.createData(false);
    purchParmUpdate = purchFormLetterParmData.parmParmUpdate();

    purchParmTable.clear();
    purchParmTable.TransDate                = SystemDateGet();
    purchParmTable.Ordering                 = DocumentStatus::PackingSlip;
    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();
}

Create a new method named as registerInventory in this class, in this method we will register the inventory quantity by batch number by passing on the PurchParmLine record and InventDim record.

private void registerInventory(PurchParmLine _purchParmline, InventDim _inventDim)
    {
        InventTransWMS_Register inventTransWMS_register; 
        TmpInventTransWMS       tmpInventTransWMS;        
        InventDim               inventBatchCheck;
        InventTrans             inventTranslocal;
        InventDim               inventDimlocal;

        inventTranslocal = InventTrans::findTransId(_purchParmline.InventTransId, true);

        inventDimlocal   = inventTranslocal.inventDim();
 
        inventDimlocal.inventBatchId    = _inventDim.inventBatchId;
        inventDimlocal.InventLocationId = _inventDim.InventLocationId;
        inventDimlocal.InventSiteId     = _inventDim.inventSiteId;
        inventDimlocal                  = InventDim::findOrCreate(inventDimlocal);
        inventTransWMS_register         = inventTransWMS_register::newStandard(tmpInventTransWMS);

        inventTranslocal.inventDimId    = inventDimlocal.inventDimId;
    
        tmpInventTransWMS.clear();
        tmpInventTransWMS.initFromInventTrans(inventTranslocal);
        tmpInventTransWMS.ItemId    = inventTranslocal.ItemId;
        tmpInventTransWMS.InventQty = _purchParmline.ReceiveNow;
        tmpInventTransWMS.insert();

        inventTransWMS_register.writeTmpInventTransWMS(tmpInventTransWMS,
                                                       inventTranslocal,
                                                       inventDimlocal);
       
        inventTransWMS_register.updateInvent(inventTranslocal);
    }

Create a new method named as insertParmLineData in this class, in this method we will insert data from PurchLine to PurchParmLine and also registers the inventory using registerInventory method.

private void insertParmLineData(container _purchLineRecIds, container _inventBatchIds, container _qtys)
    {
        PurchLine purchLine;
        InventDim inventDim;
        int       i;

        for (i = 1; i <= conLen(_inventBatchIds); i++)
        {
            purchLine.clear();

            purchLine = PurchLine::findRecId(conPeek(_purchLineRecIds, i));

            purchParmLine.InitFromPurchLine(purchLine);

            inventDim = purchLine.inventDim();

            purchParmLine.ReceiveNow  = decRound(conPeek(_qtys, i), 2);
            purchParmLine.InventDimId = inventDim.inventDimId;
            purchParmLine.ParmId      = purchParmTable.ParmId;
            purchParmLine.TableRefId  = purchParmTable.TableRefId;
            purchParmLine.setQty(DocumentStatus::PackingSlip, false, true);
            purchParmLine.setLineAmount();
            purchParmLine.insert();
            inventDim.inventBatchId   = conPeek(_inventBatchIds, i);
 
            inventDim = inventDim::findOrCreate(inventDim);

            this.registerInventory(purchParmLine, inventDim);
        }
    }

Create a new method named as processProductReceipt in this class, in this method we will post the product receipt.

public void processProductReceipt()
    {
        purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);
        purchFormLetter.transDate(DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()));
        purchFormLetter.proforma(false);
        purchFormLetter.specQty(PurchUpdate::All);
        purchFormLetter.purchTable(purchTable);
        purchFormLetter.parmParmTableNum(purchParmTable.ParmId);
        purchFormLetter.parmId(purchParmTable.ParmId);
        purchFormLetter.purchParmUpdate(purchFormLetterParmData.parmParmUpdate());
        purchFormLetter.run();
    }

Create a new method named as run in this class, in this method we will call all the methods to perform the registration and receiving processes.

public void run(container _purchLineRecIds, container _inventBatchIds, container _qtys)
{
    this.insertParmTableData();
    this.insertParmLineData(_purchLineRecIds, _inventBatchIds, _qtys);
    this.processProductReceipt();
}

Now we will create a Runnable class in order to test this class.

Create new runnable class named as TestProductReceipt to test the above class.

In the main method of runnable class add the code given below.

public static void main(Args _args)
{
    PurchTable    purchTable;
    PurchLine     purchLine;
    TransDate     packingSlipDate;
    PackingSlipId packingSlipId;
    container     lineNums;
    container     purchLineRecIds;
    container     inventBatchIds;
    container     qtys;
    int           i;


    purchTable      = PurchTable::find('000039');
    packingSlipDate = DateTimeUtil::date(DateTimeUtil::utcNow());
    packingSlipId   = 'PK0001';

    lineNums       = [1, 1, 1]; // purch line numbers
    inventBatchIds = ['Batch001', 'Batch002', 'Batch003']; //batch numbers to register
    qtys           = [30, 50, 20]; // quantity to register

    for (i = 1; i <= conLen(lineNums); i++)
    {
        purchLine = PurchLine::find(purchTable.PurchId, conPeek(lineNums, i));
        purchLineRecIds = conIns(purchLineRecIds, conLen(purchLineRecIds) + 1, purchLine.RecId);
    }

    PurchRegistrationReceiving purchRec = PurchRegistrationReceiving::newFromParameters(purchTable, packingSlipId, packingSlipDate);
    purchRec.run(purchLineRecIds, inventBatchIds, qtys);
}

Thank you for reading this article hope you find it useful.

Omar Iqbal

Technical Consultant

Total
0
Shares
1 comment

Comments are closed.

Previous Article

Generate Picking List and its registration in X++

Next Article

Set Financial Dimension combination for Purchase line using X++

Related Posts