Create Pending Invoice based on Product Receipt and Post it

To create a pending invoice from the Product receipt and post it, you can follow up on the below-attached code:



static void createPendingInvAndPost(Args _args)
{

    PurchTable                        purchTable;

    PurchLine                          purchLine;

    PurchFormLetter               purchFormLetter;

    VendPackingSlipJour        vendPackingSlipJour;

    VendPackingSlipTrans      vendPackingSlipTrans;


    PurchId                  purchId;

    PackingSlipId        packingSlipId;


    VendInvoiceInfoSubTable    vendInvoiceInfoSubTable;

    VendInvoiceInfoSubLine      vendInvoiceInfoSubLine;

    VendInvoiceInfoTable           vendInvoiceInfoTable;

    VendInvoiceInfoLine             vendInvoiceInfoLine;

     ;


     purchId               = "YourPuchId";

     packingSlipId     = "YourPackingSlipId";


     ttsBegin;

     purchTable = PurchTable::find(purchId);

    if(purchTable )

    {

         while select vendPackingSlipJour

             where vendPackingSlipJour.PurchId         == purchTable.PurchId

             && vendPackingSlipJour.PackingSlipId  == packingSlipId

         {

             //Created Pending Invoice Header

             vendInvoiceInfoTable.clear();

             vendInvoiceInfoTable.initValue();

             vendInvoiceInfoTable.initFromPurchTable(purchTable);


             vendInvoiceInfoTable.DocumentOrigin = DocumentOrigin::Manual;

             vendInvoiceInfoTable.CurrencyCode = purchTable.CurrencyCode;

             vendInvoiceInfoTable.DeliveryName = purchTable.DeliveryName;

             vendInvoiceInfoTable.Num = "YourInvPrefix" + vendPackingSlipJour.PackingSlipId;             

            //add invoice number in here

             vendInvoiceInfoTable.PurchName = purchTable.PurchName;

             vendInvoiceInfoTable.VendInvoiceSaveStatus = VendInvoiceSaveStatus::Pending;

             vendInvoiceInfoTable.DocumentDate = systemDateGet();

             vendInvoiceInfoTable.LastMatchVariance = LastMatchVarianceOptions::OK;

             vendInvoiceInfoTable.ParmJobStatus = ParmJobStatus::Waiting;

             vendInvoiceInfoTable.Approved = NoYes::Yes;

             vendInvoiceInfoTable.Approver = DirPersonUser::currentWorker();


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

            vendInvoiceInfoTable.defaultField(fieldNum

(VendInvoiceInfoTable, RemittanceLocation),null,purchTable);

            vendInvoiceInfoTable.defaultField(fieldNum

(VendInvoiceInfoTable,FixedDueDate),null,purchTable);

            vendInvoiceInfoTable.defaultField(fieldNum

(VendInvoiceInfoTable,ExchRate),null,purchTable);

            vendInvoiceInfoTable.defaultField(fieldNum

(VendInvoiceInfoTable,TransDate),null,purchTable);

            vendInvoiceInfoTable.defaultField(fieldNum

(VendInvoiceInfoTable,PaymMode),null,purchTable);

        

            // Insert pending invoice

            vendInvoiceInfoTable.insert();


            //Create Vend Invoice Info reference

            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();

            }


            //select all packing slip line

            while select vendPackingSlipTrans

                where vendPackingSlipTrans.PackingSlipId == vendPackingSlipJour.PackingSlipId

                && vendPackingSlipTrans.VendPackingSlipJour == vendPackingSlipJour.RecId

            {

                //Create Pending Invoice Line

                 purchLine = vendPackingSlipTrans.purchLine();

                 vendInvoiceInfoLine.clear();

                 vendInvoiceInfoLine.initValue();

                 vendInvoiceInfoLine.defaultRow(null,purchLine);

                 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 = vendPackingSlipTrans.Qty;

                 vendInvoiceInfoLine.RemainBefore = vendPackingSlipTrans.Qty;

                 vendInvoiceInfoLine.RemainBeforeInvent = vendPackingSlipTrans.Qty;


                 vendInvoiceInfoLine.PurchPrice = purchLine.PurchPrice;

                 vendInvoiceInfoLine.InventNow = vendPackingSlipTrans.Qty;

                 vendInvoiceInfoLine.LineAmount = purchLine.QtyOrdered !=0 ? (purchLine.LineAmount / purchLine.QtyOrdered) * vendPackingSlipTrans.Qty : 0;


                 vendInvoiceInfoLine.DefaultDimension = purchLine.DefaultDimension;


                 vendInvoiceInfoLine.insert();


                //Create Vend Invoice Info reference from packing slip

                if(vendInvoiceInfoLine.RecId)

                {

                    vendInvoiceInfoSubLine.clear();

                    vendInvoiceInfoSubLine.initValue();

                    vendInvoiceInfoSubLine.defaultRow();

                    vendInvoiceInfoSubLine.ParmId = vendInvoiceInfoTable.ParmId;

                    vendInvoiceInfoSubLine.LineRefRecId = vendInvoiceInfoLine.RecId;

                    vendInvoiceInfoSubLine.ReceiveNow = vendPackingSlipTrans.Qty;


                    vendInvoiceInfoSubLine.InventNow = vendPackingSlipTrans.Qty;

                    vendInvoiceInfoSubLine.JournalRefRecId = vendPackingSlipTrans.RecId;

                    vendInvoiceInfoSubLine.JournalRefTableId = vendPackingSlipTrans.TableId;

                    vendInvoiceInfoSubLine.DocumentId = vendPackingSlipTrans.PackingSlipId;

                    vendInvoiceInfoSubLine.insert();

                }

            }

        }

    }


    //Posting pending invoice invoice

    purchFormLetter = PurchFormLetter_Invoice::newFromSavedInvoice(vendInvoiceInfoTable);

    purchFormLetter.update(vendInvoiceInfoTable.purchTable(),

vendInvoiceInfoTable.Num);

    ttsCommit;

    info(strFmt('Purchase order %1 has been posted', purchTable.PurchId));

 }



Note: Please keep in mind that this is an example code. To optimize your code, use the insert record list or insert record set instead of while-select to insert.

Comments

Popular posts from this blog

Build HTML and send email in D365 FO with X++

How to customize electronic reporting in D365

Batch parallelism or multithreading in Dynamics 365 for Finance and Operations

How to Enable/Disable a Form Button with X++

How to create and run the Runbase batch class in D365

How to create and run the Runbase class in D365

Customize the standard excel template in D365FO

Difference between InMemory and TempDB tables in D365 F&O

How to create and run a batch class that extends RunBaseBatch in D365

How to apply a package in LCS Dynamics 365 F&O