web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

create pending vendor invoice for multiple PO for same vendor

(0) ShareShare
ReportReport
Posted on by 439

hi  I am trying to single pending vendor invoice by x++ for multiple PO for same vendor  but while invocing it says error 

public void CreateVendinvoiceForPosting(SON_SummaryInvoice   _poHeaderSummaryInvIB)
{
    Name                        son_EANCode;
    PurchTable                  purchTable;
    PurchLine                   purchLine;
    VendPackingSlipJour         vendPackingSlipJour,vendPackingSlipJourloc;
    VendPackingSlipTrans        vendPackingSlipTrans,vendPackingSlipTransloc;
    VendInvoiceInfoTable        vendInvoiceInfoTable;
    VendInvoiceInfoLine         vendInvoiceInfoLine;
    VendInvoiceInfoSubTable     vendInvoiceInfoSubTable;
    VendInvoiceInfoSubLine      vendInvoiceInfoSubLine;
    PurchId                   purchId;
    PackingSlipId               packingSlipId;
    rAXItemEANCode              rAXItemEANCode;
    PurchParmUpdate             activePurchParmUpdate;
    VendInvoiceInfoTable        activeVendInvoiceInfoTable;
    VendInvoiceInfoTable        savedVendInvoiceInfoTable; 
    SON_AttachmentInvoice     attachmentDDInvoice;
    SON_AttachmentInvoice     SON_AttachmentDDInvoiceloc;
    int64                       LineNumber;
    VendBankAccount             VendBankAccount;
    str invoicenumber;
    MarkupTrans                 markupTrans,markupTransloc;

    ;

    select DeliveryNote,AXPONumber from  attachmentDDInvoice
        where attachmentDDInvoice.IsInvoicedCreated == NoYes::No
        && attachmentDDInvoice.SummaryInvoiceNumber == _poHeaderSummaryInvIB.InvoiceNumber;

    purchId = attachmentDDInvoice.AXPONumber;
    packingSlipId = attachmentDDInvoice.DeliveryNote;
    invoicenumber = poHeaderSummaryInvIB.InvoiceNumber;


    ttsBegin;


    purchTable = PurchTable::find(purchId);
    packingSlipId = packingSlipId;

    while select vendPackingSlipJour
       where vendPackingSlipJour.PurchId == purchTable.PurchId
        && vendPackingSlipJour.PackingSlipId == packingSlipId
    {

        //Generate Pending Invoice Header
        vendInvoiceInfoTable.clear();
        vendInvoiceInfoTable.initValue();
        vendInvoiceInfoTable.initFromPurchTable(purchTable);

        vendInvoiceInfoTable.DocumentOrigin  = DocumentOrigin::Manual;
        vendInvoiceInfoTable.CurrencyCode = purchTable.CurrencyCode;
        vendInvoiceInfoTable.DeliveryName = purchTable.DeliveryName;
        vendInvoiceInfoTable.Num = _poHeaderSummaryInvIB.InvoiceNumber; //add invoice number in here
        vendInvoiceInfoTable.PurchName = purchTable.PurchName;
        vendInvoiceInfoTable.VendInvoiceSaveStatus = VendInvoiceSaveStatus::Pending;
        vendInvoiceInfoTable.DocumentDate =_poHeaderSummaryInvIB.InvoiceDate;
        vendInvoiceInfoTable.TransDate = _poHeaderSummaryInvIB.InvoiceDate;
        vendInvoiceInfoTable.LastMatchVariance = LastMatchVarianceOptions::OK;
        vendInvoiceInfoTable.ParmJobStatus = ParmJobStatus::Waiting;

        vendInvoiceInfoTable.PaymMode=VendTable::find(purchTable.OrderAccount).PaymMode;
        vendInvoiceInfoTable.Payment=VendTable::find(purchTable.OrderAccount).PaymTermId;

        vendInvoiceInfoTable.TaxGroup = VendTable::find(purchTable.OrderAccount).TaxGroup;
        vendInvoiceInfoTable.PostingProfile =purchTable.PostingProfile;
        vendInvoiceInfoTable.VATNum = purchTable.VATNum;

        select AccountID from  VendBankAccount  where VendBankAccount.VendAccount == purchTable.OrderAccount;
        vendInvoiceInfoTable.VendBankAccountID = VendBankAccount.AccountID;

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

        vendInvoiceInfoTable.insert();
        attachmentDDInvoice.clear();

        while select  attachmentDDInvoice group by  AXPONumber
            where attachmentDDInvoice.SummaryInvoiceNumber == poHeaderSummaryInvIB.InvoiceNumber

        //Generate Vend Invoice Info reference
        if(vendInvoiceInfoTable)
        {
            vendInvoiceInfoSubTable.clear();
            vendInvoiceInfoSubTable.initValue();
            vendInvoiceInfoSubTable.defaultRow();

            vendInvoiceInfoSubTable.ParmId = vendInvoiceInfoTable.ParmId;
            vendInvoiceInfoSubTable.OrigPurchId = attachmentDDInvoice.AXPONumber;//vendInvoiceInfoTable.PurchId;
            vendInvoiceInfoSubTable.PurchName = vendInvoiceInfoTable.PurchName;
            vendInvoiceInfoSubTable.TableRefId = vendInvoiceInfoTable.TableRefId;

            vendInvoiceInfoSubTable.insert();
        }

        attachmentDDInvoice.clear();

        while select  attachmentDDInvoice  where attachmentDDInvoice.IsInvoicedCreated ==NoYes::No
            && attachmentDDInvoice.SummaryInvoiceNumber == _poHeaderSummaryInvIB.InvoiceNumber
        {
            if(attachmentDDInvoice.AXPOLineNumber != 0)
            {
                select vendPackingSlipTrans where vendPackingSlipTrans.PackingSlipId == attachmentDDInvoice.DeliveryNote;
                LineNumber = (attachmentDDInvoice.AxPoLineNumber);
                select purchLine  where purchLine.PurchId == attachmentDDInvoice.AXPONumber
                && purchLine.LineNumber == LineNumber;
            }
            else
            {
                select vendPackingSlipTrans where vendPackingSlipTrans.PackingSlipId == attachmentDDInvoice.DeliveryNote;
               // LineNumber = (attachmentDDInvoice.AxPoLineNumber);
                select purchLine  where purchLine.PurchId == attachmentDDInvoice.AXPONumber
                && purchLine.LineNumber == this.getpurchaseline(attachmentDDInvoice).LineNumber;
                LineNumber = purchLine.LineNumber;

            }




            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 = purchLine.InventDimId;
            vendInvoiceInfoLine.OrderAccount  = vendInvoiceInfoTable.OrderAccount;
            vendInvoiceInfoLine.ItemId = purchLine.ItemId;
            vendInvoiceInfoLine.InventTransId = purchLine.InventTransId;
            vendInvoiceInfoLine.OrigPurchId = attachmentDDInvoice.AXPONumber;

            vendInvoiceInfoLine.DocumentOrigin = DocumentOrigin::Manual;

           
            select firstonly ItemId, EanCode, SON_PurchaseEAN from rAXItemEANCode
                where rAXItemEANCode.ItemId == vendPackingSlipTrans.ItemId
                &&    rAXItemEANCode.SON_PurchaseEAN == noyes::Yes;

            if(rAXItemEANCode)
            {
                SON_EANCode = rAXItemEANCode.EANCode;
            }
           


            {
                vendInvoiceInfoLine.ReceiveNow =  attachmentDDInvoice.InvoicedQty;
                vendInvoiceInfoLine.RemainBefore = attachmentDDInvoice.InvoicedQty;
                vendInvoiceInfoLine.RemainBeforeInvent =attachmentDDInvoice.InvoicedQty;
                vendInvoiceInfoLine.PurchPrice = purchLine.PurchPrice;
                vendInvoiceInfoLine.InventNow = attachmentDDInvoice.InvoicedQty;
                vendInvoiceInfoLine.LineAmount =  attachmentDDInvoice.VendorInvoiceAmt;
            }

            vendInvoiceInfoLine.DefaultDimension = purchLine.DefaultDimension;

            vendInvoiceInfoLine.insert();

            //
            //insert maisc charges

            while select markupTrans where markupTrans.TransRecId == attachmentDDInvoice.RecId
                && markupTrans.TransTableId == attachmentDDInvoice.TableId
            {
                markupTransloc.clear();
                markupTransloc.initFromVendInvoiceInfoLine(vendInvoiceInfoLine);
                markupTransloc.Value      = markupTrans.Value;
                markupTransloc.MarkupCode = markupTrans.MarkupCode;
                markupTransloc.Txt        = markupTrans.Txt;
                markupTransloc.insert();

            }
            markupTrans = null;

            while select markupTrans where markupTrans.TransRecId == purchLine.RecId
                && markupTrans.TransTableId == purchLine.TableId
            {

                markupTransloc.clear();
                markupTransloc.initFromVendInvoiceInfoLine(vendInvoiceInfoLine);
                markupTransloc.Value      = markupTrans.Value;
                markupTransloc.MarkupCode = markupTrans.MarkupCode;
                markupTransloc.Txt        = markupTrans.Txt;
                markupTransloc.insert();

            }

            //Generate 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 = purchLine.PurchQty;
                vendInvoiceInfoSubLine.InventNow = purchLine.PurchQty;
                vendInvoiceInfoSubLine.JournalRefRecId = vendPackingSlipTrans.RecId;
                vendInvoiceInfoSubLine.JournalRefTableId = vendPackingSlipTrans.TableId;
                vendInvoiceInfoSubLine.DocumentId = vendPackingSlipTrans.PackingSlipId;
                vendInvoiceInfoSubLine.insert();
            }

        }


    }

         ttsCommit;
   info(strFmt("Vendor invoice is created for %1 ",vendInvoiceInfoTable.Num));
}


above is my code  while invoving some times it throws error like "

 insufficient inventory transaction with status purchased""

how ever manually when i create a pending vendor invoice with multiple PO for same vendor it has no issue it gets invoiced also, how ever when i do it from my code it is throwing error while invoicing.

"

The purchase order or product receipt was corrected after the vendor invoice was created. All accounting distributions must be re-created to reflect any changes to the purchase order's or product receipt's accounting distr"

*This post is locked for comments

  • Community Member Profile Picture
    on at

    Hi,

    Did you resolve the issue? I am also getting the same error while posting the invoice for multiple PO in a single invoice with the same vendor account.

    Let me know if you found any solution?

    Regards,

    Manoj

  • Ludwig Reinhard Profile Picture
    Microsoft Employee on at

    Hello Manoj,

    Did you use the same X++ code that is shown above?

    If this is not the case then please open a new thread and post your question there ideally supported by some illustrative screenprints.

    Many thanks and best regards,

    Ludwig

  • Suggested answer
    Sangeeth R Profile Picture
    86 on at

    Hi,

    Might be AccountingEvent was not set proper data on VendInvoiceInfoSubTable.

    AccountingEvent accountingEvent;

           select firstonly accountingEvent

               order by CreatedDateTime desc

               where accountingEvent.SourceDocumentHeader == PurchTable::find(_purchId).SourceDocumentHeader

                   && accountingEvent.Type != AccountingEventType::PeriodClose;

           vendInvoiceInfoSubTable.SourceAccountingEvent   = accountingEvent.RecId;

    Try this.

    Thanks,

    Sangeeth

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans