Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Thursday, May 23, 2024

XML

 My requirement is to create  XML files for the vend payment journal through code and send them to SharePoint.


using System.IO;

using System.IO.Path;

using Microsoft.Azure;

using Blobstorage = Microsoft.WindowsAzure.Storage;

using Microsoft.WindowsAzure.Storage.Blob;

using Microsoft.Dynamics.Platform.Integration.SharePoint;

using Microsoft.Dynamics.ApplicationPlatform.Services.Instrumentation;

using Microsoft.DynamicsOnline.Infrastructure.Components.SharedServiceUnitStorage;

using Microsoft.Dynamics.AX.Framework.FileManagement;

internal final class YNV_PoBankIntegration

{

    /// <summary>

    /// Class entry point. The system will call this method when a designated menu 

    /// is selected or when execution starts and this class is set as the startup class.

    /// </summary>

    /// <param name = "_args">The specified arguments.</param>

    public static void createXML(LedgerJournalTrans ledgerJournalTrans)

    {

            XmlDocument doc;

            XmlElement Document;

            XmlElement CstmrCdtTrfInitn;

            XmlElement GrpHdr;

            XmlElement oneChild;

            XmlElement secondChild;

            XmlElement thirdChild;

            XmlElement fourthChild;

            XmlElement InitgPtyNode;

            XmlElement InitgPtyChild;

            XmlElement PmtInfList;

            XmlElement PmtInfListChild1;

            XmlElement PmtInfListChild2;

            XmlElement PmtInfListChild3;

            XmlElement PmtInfListChild4;

            XmlElement PmtInfListChild5;

            XmlElement PmtTpInf;

            XmlElement  SvcLvlList;

            XmlElement SvcLvlListChild1;

            XmlElement ReqdExctnDt;

            XmlElement DbtrList;

            XmlElement DbtrListChild1;

            XmlElement PstlAdr;

            XmlElement Ctry;

            XmlElement AdrLine;

            XmlElement DbtrAcct;

            XmlElement IBAN;

            XmlElement Id;

            XmlElement DbtrAgt;

            XmlElement FinInstnId;

            XmlElement BIC;

            XmlElement CdtTrfTxInf;

            XmlElement PmtId;

            XmlElement EndToEndId;

            XmlElement Amt;

            XmlElement InstdAmt;

            XmlElement CdtrAgt;

            XmlElement FinInstnIdList;

            XmlElement Cdtr;

            XmlElement Nm;

            XmlElement CdtrAcct;

            boolean    transactionStatus = NoYes::No;

            DimensionAttributeValueCombination  combination,combinationOffset;

            LedgerJournalTrans      ledgerJournalTransLoc,ledgerJournalTransLocSum;

            LedgerJournalTable      ledgerJournalTable = LedgerJournalTable::find(ledgerJournalTrans.JournalNum);

            doc = XmlDocument::newBlank();

            Document = doc.createElement('Document');

            Document.setAttribute('xsi:schemaLocation','urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd');

            Document.setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');

            Document.setAttribute('xmlns','urn:iso:std:iso:20022:tech:xsd:pain.001.001.03');

            doc.appendChild(Document);

        //CstmrCdtTrfInitn

            CstmrCdtTrfInitn = doc.createElement('CstmrCdtTrfInitn');

            Document.appendChild(CstmrCdtTrfInitn);

        str formatted = System.String::Format(

                                '{0:yyyyMMddTHHmmss}',

                                DateTimeUtil::newDateTime(systemDateGet(), timeNow()));

        System.DateTime dateTime = System.DateTime::get_UtcNow();

        str utcTimeAsStr = dateTime.ToString('yyyyMdHHmmss');

        select sum(AmountCurDebit),sum(AmountCurCredit) from ledgerJournalTransLocSum where ledgerJournalTransLocSum.JournalNum == ledgerJournalTable.JournalNum;

        // GrpHdr

            GrpHdr = doc.createElement('GrpHdr');

            CstmrCdtTrfInitn.appendChild(GrpHdr);

            utcDateTime    todayDate = DateTimeUtil::getSystemDateTime();

       


            oneChild = doc.createElement('MsgId');

            oneChild.appendChild(doc.createTextNode(strFmt("%1-%2" ,ledgerJournalTrans.JournalNum , utcTimeAsStr)));

            GrpHdr.appendChild(oneChild);



            secondChild = doc.createElement('CreDtTm');

            secondChild.appendChild(doc.createTextNode(formatted));

            GrpHdr.appendChild(secondChild);


            thirdChild = doc.createElement('NbOfTxs');

            thirdChild.appendChild(doc.createTextNode(any2Str(ledgerJournalTable.numOfLines())));

            GrpHdr.appendChild(thirdChild);


            

            fourthChild = doc.createElement('CtrlSum');

            fourthChild.appendChild(doc.createTextNode(any2Str(trunc(ledgerJournalTransLocSum.AmountCurDebit - ledgerJournalTransLocSum.AmountCurCredit))));

            GrpHdr.appendChild(fourthChild);

        // InitgPtyNode

            InitgPtyNode = doc.createElement('InitgPtyc');

            GrpHdr.appendChild(InitgPtyNode);


            InitgPtyChild = doc.createElement('Nm');

            InitgPtyChild.appendChild(doc.createTextNode(CompanyInfo::findRecId(CompanyInfo::current()).Name));

            InitgPtyNode.appendChild(InitgPtyChild);



            PaymentFormatCodeSets_W     paymentFormatCodeSets_W = PaymentFormatCodeSets_W::find(VendPaymModeTable::find(ledgerJournalTrans.PaymMode).RecId);


            // PmtInfList Node

            PmtInfList = doc.createElement('PmtInf');

            CstmrCdtTrfInitn.appendChild(PmtInfList);


            PmtInfListChild1 = doc.createElement('PmtInfId');

            PmtInfListChild1.appendChild(doc.createTextNode(strFmt('0%1%2 020240515-01',ledgerJournalTrans.JournalNum,paymentFormatCodeSets_W.Code)));

            PmtInfList.appendChild(PmtInfListChild1);



            PmtInfListChild2 = doc.createElement('PmtMtd');

            PmtInfListChild2.appendChild(doc.createTextNode('TRF'));

            PmtInfList.appendChild(PmtInfListChild2);



            PmtInfListChild3 = doc.createElement('BtchBookg');

            PmtInfListChild3.appendChild(doc.createTextNode('false'));

            PmtInfList.appendChild(PmtInfListChild3);


            PmtInfListChild4 = doc.createElement('NbOfTxs');

            PmtInfListChild4.appendChild(doc.createTextNode(any2Str(ledgerJournalTable.numOfLines())));

            PmtInfList.appendChild(PmtInfListChild4);

            

            PmtInfListChild5 = doc.createElement('CtrlSum');

            PmtInfListChild5.appendChild(doc.createTextNode(any2Str(trunc(ledgerJournalTransLocSum.AmountCurDebit - ledgerJournalTransLocSum.AmountCurCredit))));

            PmtInfList.appendChild(PmtInfListChild5);

        // PmtTpInf

            PmtTpInf = doc.createElement('PmtTpInf');

            PmtInfList.appendChild(PmtTpInf);

        // SvcLvlList

            SvcLvlList = doc.createElement('SvcLvl');

            PmtTpInf.appendChild(SvcLvlList);

            

            SvcLvlListChild1 = doc.createElement('Cd');

            SvcLvlListChild1.appendChild(doc.createTextNode(paymentFormatCodeSets_W.Code));

            SvcLvlList.appendChild(SvcLvlListChild1);

        // ReqdExctnDt

            ReqdExctnDt = doc.createElement('ReqdExctnDt');

            ReqdExctnDt.appendChild(doc.createTextNode(any2Str((systemDateGet()))));

            PmtInfList.appendChild(ReqdExctnDt);

        // DbtrList

            DbtrList = doc.createElement('Dbtr');

            PmtInfList.appendChild(DbtrList);

            

            DbtrListChild1 = doc.createElement('Nm');

            DbtrListChild1.appendChild(doc.createTextNode(CompanyInfo::findByCompany_IN(ledgerJournalTrans.Company).Name));

            DbtrList.appendChild(DbtrListChild1);

        //PstlAdr

            PstlAdr = doc.createElement('PstlAdr');

            DbtrList.appendChild(PstlAdr);


            Ctry = doc.createElement('Ctry');

            Ctry.appendChild(doc.createTextNode(SysCountryRegionCode::countryInfo(ledgerJournalTrans.Company)));

            PstlAdr.appendChild(Ctry);


            AdrLine = doc.createElement('AdrLine');

            AdrLine.appendChild(doc.createTextNode(CompanyInfo::findByCompany_IN(ledgerJournalTrans.Company).postalAddress().Address));

            PstlAdr.appendChild(AdrLine);

            

             //   

            DbtrAcct = doc.createElement('DbtrAcct');

            PmtInfList.appendChild(DbtrAcct);

           

            Id = doc.createElement('Id');

            DbtrAcct.appendChild(Id)

;

            

                

            IBAN = doc.createElement('IBAN');

            combinationOffset = DimensionAttributeValueCombination::find(ledgerJournalTrans.OffsetLedgerDimension);

            IBAN.appendChild(doc.createTextNode(BankAccountTable::find(combinationOffset.DisplayValue).AccountNum));

            Id.appendChild(IBAN);


            

            DbtrAgt = doc.createElement('DbtrAgt');

            PmtInfList.appendChild(DbtrAgt);


            FinInstnId = doc.createElement('FinInstnId');

            DbtrAgt.appendChild(FinInstnId);


            BIC = doc.createElement('BIC');

            BIC.appendChild(doc.createTextNode(BankAccountTable::find(combinationOffset.DisplayValue).SWIFTNo));

            FinInstnId.appendChild(BIC);

 

        while select ledgerJournalTransLoc where ledgerJournalTransLoc.JournalNum == ledgerJournalTrans.JournalNum 

        {

            transactionStatus = NoYes::Yes;

            combination = DimensionAttributeValueCombination::find(ledgerJournalTransLoc.LedgerDimension);

            CdtTrfTxInf = doc.createElement('CdtTrfTxInf');

            PmtInfList.appendChild(CdtTrfTxInf);


            PmtId = doc.createElement('PmtId');

            CdtTrfTxInf.appendChild(PmtId);


            EndToEndId = doc.createElement('EndToEndId');

            EndToEndId.appendChild(doc.createTextNode(strFmt('%1-%2-05',ledgerJournalTransLoc.Voucher,ledgerJournalTransLoc.RecId)));

            PmtId.appendChild(EndToEndId);


            Amt = doc.createElement('Amt');

            CdtTrfTxInf.appendChild(Amt);

           

                InstdAmt = doc.createElement('InstdAmt');

                InstdAmt.setAttribute("Ccy",ledgerJournalTransLoc.CurrencyCode);

            if (ledgerJournalTransLoc.AmountCurDebit != 0)

            {

                InstdAmt.appendChild(doc.createTextNode(any2Str(trunc(ledgerJournalTransLoc.AmountCurDebit))));

            }

            else

            {

                InstdAmt.appendChild(doc.createTextNode(strFmt("-%1",any2Str(trunc(ledgerJournalTransLoc.AmountCurCredit)))));

            }

            Amt.appendChild(InstdAmt);


            CdtrAgt = doc.createElement('CdtrAgt');

            CdtTrfTxInf.appendChild(CdtrAgt);


            FinInstnIdList = doc.createElement('FinInstnId');

            CdtrAgt.appendChild(FinInstnIdList);


            BIC  = doc.createElement('BIC');

            BIC.appendChild(doc.createTextNode(VendBankAccount::find(combination.displayValue,ledgerJournalTransLoc.CustVendBankAccountId).SWIFTNo));

            FinInstnIdList.appendChild(BIC);


            

            Nm  = doc.createElement('Nm');

            Nm.appendChild(doc.createTextNode(VendBankAccount::find(combination.displayValue,ledgerJournalTransLoc.CustVendBankAccountId).Name));

            FinInstnIdList.appendChild(Nm);


            PstlAdr    = doc.createElement('PstlAdr');

            FinInstnIdList.appendChild(PstlAdr);


            Ctry   = doc.createElement('Ctry');

            Ctry.appendChild(doc.createTextNode(LogisticsAddressCountryRegion::find(LogisticsPostalAddress::findByLocation(VendBankAccount::find(combination.displayValue,ledgerJournalTransLoc.CustVendBankAccountId).Location).CountryRegionId).ISOcode));

            PstlAdr.appendChild(Ctry);


            AdrLine   = doc.createElement('AdrLine');

            AdrLine.appendChild(doc.createTextNode(LogisticsPostalAddress::findByLocation(VendBankAccount::find(combination.displayValue,ledgerJournalTransLoc.CustVendBankAccountId).Location).Address));

            PstlAdr.appendChild(AdrLine);


            

            Cdtr    = doc.createElement('Cdtr');

            CdtTrfTxInf.appendChild(Cdtr);


            Nm  = doc.createElement('Nm');

            Nm.appendChild(doc.createTextNode('...... ..'));

            Cdtr.appendChild(Nm);


            PstlAdr    = doc.createElement('PstlAdr');

            Cdtr.appendChild(PstlAdr);


            Ctry   = doc.createElement('Ctry');

            Ctry.appendChild(doc.createTextNode(LogisticsAddressCountryRegion::find(DirParty::primaryPostalAddress(VendTable::find(ledgerJournalTransLoc.accountDisplay()).Party).CountryRegionId).ISOcode));

            PstlAdr.appendChild(Ctry);


            AdrLine   = doc.createElement('AdrLine');

            AdrLine.appendChild(doc.createTextNode(DirParty::primaryPostalAddress(VendTable::find(ledgerJournalTransLoc.accountDisplay()).Party).Address));

            PstlAdr.appendChild(AdrLine);


            CdtrAcct     = doc.createElement('CdtrAcct');

            CdtTrfTxInf.appendChild(CdtrAcct);

            

            Id = doc.createElement('Id');

            CdtrAcct.appendChild(Id)

;

            

            IBAN   = doc.createElement('IBAN');

            IBAN.appendChild(doc.createTextNode(VendBankAccount::find(combination.displayValue,ledgerJournalTransLoc.CustVendBankAccountId).BankIBAN));

            Id.appendChild(IBAN);

        }

            Info("File sent to sharepoint");


            System.Byte[]   reportBytes = new System.Byte[0]();

            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();


            reportBytes = enc.GetBytes(doc.outerXml());


            System.IO.Stream streamLoc = new System.IO.MemoryStream(reportBytes);

        if (transactionStatus == NoYes::Yes)

        {

            YNV_PoBankIntegration::sendFileToSharePoint(streamLoc,ledgerJournalTrans.JournalNum);

        }

    }


    public static void sendFileToSharePoint(System.IO.Stream            stream, LedgerJournalId ledgerJournalId)

    {

       

        Filename                    fileName;

        FileUploadTemporaryStorageResult fileUploadResult;

        VendParameters  vendParameters =VendParameters::find();


        if (fileUploadResult == null)

        {

            //fileName = System.Text.RegularExpressions.Regex::Replace(strFmt("%1-%2",ledgerJournalId,utcTimeAsStr), @"[\W_]", "");

            System.DateTime dateTime = System.DateTime::get_UtcNow();

            str utcTimeAsStr = dateTime.ToString('yyyyMdHHmmss');

            str flileNameLoc = strFmt("%1%2.Xml",ledgerJournalId,utcTimeAsStr);


            try

            {

                

                str defaultSPServer = vendParameters.YNV_SharePointURL;   //'tekexperts.sharepoint.com';

                str spSite = vendParameters.YNV_SharePointSite; //'sites/YNVArchitectureAndDesign';


                str spFolderPath = vendParameters.YNV_FileLocation; //"Shared Documents/Finance/Integration/POC_BankIntegration";


                str fileContetType = System.Web.MimeMapping::GetMimeMapping(flileNameLoc);


            

                str externalId = xUserInfo::getExternalId(vendParameters.YNV_UserForSharepointUser);


                    Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider = new Microsoft.Dynamics.AX.Framework.FileManagement.SharePointDocumentStorageProvider(

                    defaultSPServer,

                    spSite,

                    spFolderPath,

                    externalId);


                storageProvider.ProviderId = DocuStorageProviderType::SharePoint;


                if (storageProvider != null)

                {

                    str uniqueFilename = storageProvider.GenerateUniqueName(filename);

                    Microsoft.Dynamics.AX.Framework.FileManagement.DocumentLocation location = storageProvider.SaveFile(

                        newGuid(),

                        flileNameLoc,

                        fileContetType,

                        stream);

                }


            }

            catch(Exception::Error)

            {

                error("Upload failed.");

            }

        }

    }


}


output:-

<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd">
<CstmrCdtTrfInitn>
<GrpHdr>
<MsgId>JBN001963-2024521072602</MsgId>
<CreDtTm>05/21/2024 07:26:02</CreDtTm>
<NbOfTxs>2</NbOfTxs>
<CtrlSum>1120</CtrlSum>
<InitgPtyc>
<Nm>Tek Experts EOOD</Nm>
</InitgPtyc>
</GrpHdr>
<PmtInf>
<PmtInfId>0JBN001963 %2020240515-01</PmtInfId>
<PmtMtd>TRF</PmtMtd>
<BtchBookg>false</BtchBookg>
<NbOfTxs>2</NbOfTxs>
<CtrlSum>1120</CtrlSum>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
</PmtTpInf>
<ReqdExctnDt/>
<Dbtr>
<Nm>Tek Experts EOOD</Nm>
<PstlAdr>
<Ctry>BG</Ctry>
<AdrLine>xxxxx 1000 BGR</AdrLine>
</PstlAdr>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN/>
</Id>
</DbtrAcct>
<DbtrAgt>
<FinInstnId>
<BIC/>
</FinInstnId>
</DbtrAgt>
<CdtTrfTxInf>
<PmtId>
<EndToEndId>VPAY005613-5637299076-05</EndToEndId>
</PmtId>
<Amt>
<InstdAmt>Ccy=BGN>780.00</InstdAmt>
</Amt>
<CdtrAgt>
<FinInstnId>
<BIC>UNCRBGSFXXX</BIC>
<Nm>UNCR Bank</Nm>
<PstlAdr>
<Ctry>BG</Ctry>
<AdrLine>xxxxx Sofia 1000 BGR</AdrLine>
</PstlAdr>
</FinInstnId>
</CdtrAgt>
<Cdtr>
<Nm>...... ..</Nm>
<PstlAdr>
<Ctry>BG</Ctry>
<AdrLine>xxxxx Sofia BGR</AdrLine>
</PstlAdr>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>BG16UNCR70001521952092</IBAN>
</Id>
</CdtrAcct>
</CdtTrfTxInf>
<CdtTrfTxInf>
<PmtId>
<EndToEndId>VPAY005614-5637299077-05</EndToEndId>
</PmtId>
<Amt>
<InstdAmt>Ccy=BGN>340.00</InstdAmt>
</Amt>
<CdtrAgt>
<FinInstnId>
<BIC>UNCRBGSFXXX</BIC>
<Nm>UNCR Bank</Nm>
<PstlAdr>
<Ctry>BG</Ctry>
<AdrLine>xxxxx Sofia 1000 BGR</AdrLine>
</PstlAdr>
</FinInstnId>
</CdtrAgt>
<Cdtr>
<Nm>...... ..</Nm>
<PstlAdr>
<Ctry>BG</Ctry>
<AdrLine>xxxxx Sofia BGR</AdrLine>
</PstlAdr>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>BG16UNCR70001521952092</IBAN>
</Id>
</CdtrAcct>
</CdtTrfTxInf>
</PmtInf>
</CstmrCdtTrfInitn>
</Document>

How to Retrieve the Purchase Order Number in the LedgerTransAccount Form using X++ in D365 Finance & Operations

 How to Retrieve the Purchase Order Number in the LedgerTransAccount Form using X++ in D365 Finance & Operations [ExtensionOf(tableStr(G...