Post Po invoice In D 365 Fo using x++

 Today we i will show how to post the purchase order Invoice using x++ in D365 FO


/// <summary>

    /// This method helps to post the invoice journal journal for the purchase order.

    /// </summary>

    /// <param name = "_vendaccount">The <c>vendaccount</c> Vendor account num.</param>

    /// <param name = "_invoiceNum">he <c>invoiceNum</c> invoice  num.</param>

    /// <param name = "_invoiceDate">he <c>invoiceDate</c> Invoice Date.</param>

    /// <returns> the parm id created after the invoice posted</returns>


    private parmId ngPostInvoice(VendAccount _vendaccount, IGPS_ExcelValueStr _invoiceNum, Transdate _invoiceDate)

    {

        //post invoice for all matched purchase lines

        PurchFormLetter_Invoice purchFormLetter;

        QueryBuildDatasource    invoiceDS;

        UserId                  userId = curUserId();


        vendInvoiceInfoTable    invoiceInfoTableLoc;


        purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);

       

        purchFormLetter.initNewPurchParmUpdate();

        purchFormLetter.transDate(transDate);

       

        purchFormLetter.specQty(PurchUpdate::All);

        purchFormLetter.creditRemaining(NoYes::Yes);

        purchFormLetter.reSelect(purchFormLetter.purchParmUpdate());

        // query to find the record and after that will passed the range to select a record for invoice.

        SysQueryRun chooseLinesQuery = new SysQueryRun(queryStr(PurchUpdate));

        chooseLinesQuery.query().addDataSource(tableNum(VendInvoiceInfoTable)).enabled(false);

        

        

        switch (importType)

        {

            case IGPS_ImportType::POInvoiceAccrual       :

                invoiceDS =  chooseLinesQuery.query().dataSourceTable(tablenum(PurchLine)).addDataSource(tablenum(IGPS_POInvoiceAccrual));

                invoiceDS.addLink(fieldnum(PurchLine, IGPS_LoadNumber),  fieldnum(IGPS_POInvoiceAccrual, LoadNumber));

                invoiceDS.addLink(fieldnum(PurchLine, ItemId),          fieldnum(IGPS_POInvoiceAccrual, ItemId));

                invoiceDS.joinMode(JoinMode::ExistsJoin);

                invoiceDS.addRange(fieldnum(IGPS_POInvoiceAccrual, CreatedBy)).value(userId);

                invoiceDS.addRange(fieldnum(IGPS_POInvoiceAccrual, VendAccount)).value(_vendaccount);

                invoiceDS.addRange(fieldnum(IGPS_POInvoiceAccrual, Invoice)).value(_invoiceNum);

                invoiceDS.addRange(fieldnum(IGPS_POInvoiceAccrual, HasErrors)).value(SysQuery::value(NoYes::No));

                invoiceDS.addRange(fieldnum(IGPS_POInvoiceAccrual, RefInventTransId)).value(SysQuery::valueEmptyString());

                invoiceDS.addSortField(fieldnum(IGPS_POInvoiceAccrual, createdDateTime), SortOrder::Descending);

                break;

            

            case IGPS_ImportType::POInvoiceAccessorial   :

                invoiceDS =  chooseLinesQuery.query().dataSourceTable(tablenum(PurchLine)).addDataSource(tablenum(IGPS_POInvoiceAccessorial));

                invoiceDS.addLink(fieldnum(PurchLine, IGPS_LoadNumber),  fieldnum(IGPS_POInvoiceAccessorial, AccessoriaApprovalNumber));

                invoiceDS.addLink(fieldnum(PurchLine, ItemId),          fieldnum(IGPS_POInvoiceAccessorial, ItemId));

                invoiceDS.joinMode(JoinMode::ExistsJoin);

                invoiceDS.addRange(fieldnum(IGPS_POInvoiceAccessorial, CreatedBy)).value(userId);

                invoiceDS.addRange(fieldnum(IGPS_POInvoiceAccessorial, VendAccount)).value(_vendaccount);

                invoiceDS.addRange(fieldnum(IGPS_POInvoiceAccessorial, Invoice)).value(_invoiceNum);

                invoiceDS.addRange(fieldnum(IGPS_POInvoiceAccessorial, HasErrors)).value(SysQuery::value(NoYes::No));

                invoiceDS.addRange(fieldnum(IGPS_POInvoiceAccessorial, RefInventTransId)).value(SysQuery::valueEmptyString());

                invoiceDS.addSortField(fieldnum(IGPS_POInvoiceAccessorial, createdDateTime), SortOrder::Descending);

                break;

            

            default :

                return purchFormLetter.parmId();

        }

        

      

        purchFormLetter.parmParmTableNum(_invoiceNum);

        purchFormLetter.createParmUpdateFromParmUpdateRecord(purchFormLetter.purchParmUpdate());

        

        purchFormLetter.chooseLinesQuery(chooseLinesQuery);


        SysQueryRun chooseLinesPendingInvoiceQuery = new SysQueryRun(queryStr(PurchUpdatePendingInvoice));

        chooseLinesPendingInvoiceQuery.query().dataSourceTable(tableNum(PurchTable)).addRange(fieldNum(PurchTable, PurchId)).value(queryValue(''));

        purchFormLetter.parmQueryChooseLinesPendingInvoice(chooseLinesPendingInvoiceQuery);

        

        purchFormLetter.sumBy(AccountOrder::Account);


        purchFormLetter.chooseLines();

        purchFormLetter.reArrangeNow(false);

        // To update DocumentDate and then it passed through vendinvoicejour table "DocumentDate" is the field name.

        ttsBegin;

        select forUpdate invoiceInfoTableLoc 

            where invoiceInfoTableLoc.ParmId == purchFormLetter.parmId();


        if (invoiceInfoTableLoc)

        {

            invoiceInfoTableLoc.DocumentDate = _invoiceDate;

            invoiceInfoTableLoc.update();

        }

        ttsCommit;


        purchFormLetter.run();

        

        return purchFormLetter.parmId();

        

    }

Comments

Popular posts from this blog

Customization on Sales invoice Report in D365 F&O

75) COC - Create a coc of the table modified method

46) D365 FO: SHAREPOINT FILE UPLOAD USING X++