Monday, June 5, 2023

How to create and post pending vendor invoice through X++ code in D365FO

 Job:

Note : Purchase order must be complete product reciept.

   public static void main(Args _args)

    {

        VendInvoiceInfoTable    vendInvoiceInfoTable,vendInvoiceInfoTableloc;

        VendInvoiceInfoSubTable     vendInvoiceInfoSubTable;

        VendInvoiceInfoLine         vendInvoiceInfoLine;

        VendPackingSlipTrans        vendPackingSlipTrans;

        VendInvoiceInfoSubLine      vendInvoiceInfoSubLine;

        PurchLine                   purchLine;

        VendTable                   vendTable;

        PurchTable                  purchTable;

        PurchFormLetter         purchFormLetter;

        purchtable = purchtable::find("000036");

        vendInvoiceInfoTable.clear();

        vendInvoiceInfoTable.initValue();

        vendInvoiceInfoTable.initFromPurchTable(purchTable);


        vendInvoiceInfoTable.DocumentOrigin    = DocumentOrigin::Manual;

        vendInvoiceInfoTable.CurrencyCode      = purchTable.CurrencyCode;

        vendInvoiceInfoTable.DeliveryName      = purchTable.DeliveryName;

        vendInvoiceInfoTable.Num               = "LM-036";

        vendInvoiceInfoTable.PurchName         = purchTable.PurchName;

        vendInvoiceInfoTable.VendInvoiceSaveStatus = VendInvoiceSaveStatus::Pending;

        vendInvoiceInfoTable.TransDate         = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());

        vendInvoiceInfoTable.DocumentDate      = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());

        vendInvoiceInfoTable.LastMatchVariance = LastMatchVarianceOptions::OK;

        vendInvoiceInfoTable.ParmJobStatus     = ParmJobStatus::Waiting;

        vendInvoiceInfoTable.DefaultDimension  = vendInvoiceInfoTable.copyDimension(purchTable.DefaultDimension);

        vendInvoiceInfoTable.defaultRow(purchTable);

        vendInvoiceInfoTable.insert();

        if(vendInvoiceInfoTable)

        {

            vendInvoiceInfoSubTable.clear();

            vendInvoiceInfoSubTable.initValue();

            vendInvoiceInfoSubTable.defaultRow();

            vendInvoiceInfoSubTable.ParmId      = vendInvoiceInfoTable.ParmId;

            vendInvoiceInfoSubTable.OrigPurchId = vendInvoiceInfoTable.PurchId;

            vendInvoiceInfoSubTable.PurchName   = vendInvoiceInfoTable.PurchName;

            vendInvoiceInfoSubTable.TableRefId  = vendInvoiceInfoTable.TableRefId;

            vendInvoiceInfoSubTable.insert();

        }

          while  select purchline

           where purchline.PurchId == purchtable.PurchId

        {

            select * from VendPackingSlipTrans

               where VendPackingSlipTrans.OrigPurchid ==  purchtable.PurchId

               &&    VendPackingSlipTrans.PackingSlipId == vendPackingSlipTrans.PackingSlipId

               && VendPackingSlipTrans.ItemId == purchline.ItemId ;

            vendInvoiceInfoLine.clear();

            vendInvoiceInfoLine.initValue();

            vendInvoiceInfoLine.initFromPurchLine(purchLine);

            vendInvoiceInfoLine.DeliveryName   = vendInvoiceInfoTable.DeliveryName;

            vendInvoiceInfoLine.ParmId         = vendInvoiceInfoTable.ParmId;

            vendInvoiceInfoLine.TableRefId     = vendInvoiceInfoTable.TableRefId;

            vendInvoiceInfoLine.currencyCode   = vendInvoiceInfoTable.CurrencyCode;

            vendInvoiceInfoLine.LineNum        = any2int(purchLine.LineNumber);

            vendInvoiceInfoLine.InvoiceAccount = vendInvoiceInfoTable.InvoiceAccount;

            vendInvoiceInfoLine.InventDimId    = vendPackingSlipTrans.InventDimId;

            vendInvoiceInfoLine.OrderAccount   = vendInvoiceInfoTable.OrderAccount;

            vendInvoiceInfoLine.ItemId         = vendPackingSlipTrans.ItemId;

            vendInvoiceInfoLine.InventTransId  = vendPackingSlipTrans.InventTransId;

            vendInvoiceInfoLine.DocumentOrigin = DocumentOrigin::Manual; 

           vendInvoiceInfoLine.ReceiveNow     = 1;

            vendInvoiceInfoLine.modifiedReceiveNow();

            vendInvoiceInfoLine.PurchPrice     = purchLine.PurchPrice;

            vendInvoiceInfoLine.InventNow      = vendInvoiceInfoLine.ReceiveNow;

            vendInvoiceInfoLine.LineAmount   = (purchLine.LineAmount / purchLine.PurchQty) * vendInvoiceInfoLine.ReceiveNow;

            vendInvoiceInfoLine.DefaultDimension = purchLine.DefaultDimension; 

           vendInvoiceInfoLine.insert();

              if(vendInvoiceInfoLine)

            {

                vendInvoiceInfoSubLine.clear();

                vendInvoiceInfoSubLine.initValue();

                vendInvoiceInfoSubLine.defaultRow();

                vendInvoiceInfoSubLine.ParmId            = vendInvoiceInfoTable.ParmId;

                vendInvoiceInfoSubLine.LineRefRecId      = vendInvoiceInfoLine.RecId;

                vendInvoiceInfoSubLine.ReceiveNow        = vendInvoiceInfoLine.ReceiveNow;

                vendInvoiceInfoSubLine.InventNow         = vendInvoiceInfoLine.InventNow ;

                vendInvoiceInfoSubLine.JournalRefRecId   = vendPackingSlipTrans.RecId;

                vendInvoiceInfoSubLine.JournalRefTableId = vendPackingSlipTrans.TableId;

                vendInvoiceInfoSubLine.DocumentId        = vendPackingSlipTrans.PackingSlipId;

                vendInvoiceInfoSubLine.insert();

            }

          }

        select vendInvoiceInfoTableloc

             where vendInvoiceInfoTableloc.Num == vendInvoiceInfoTable.Num;

        purchFormLetter = PurchFormLetter_Invoice::newFromSavedInvoice(vendInvoiceInfoTableloc);

        purchFormLetter.update(vendInvoiceInfoTableloc.purchTable(), vendInvoiceInfoTableloc.Num);

    }




No comments:

Post a Comment