Hi,
Part 4: Inbound as XML file
Below Code Reads XML file from below FTP Path and creates Sales Order in D365 AX
using System.Security;
class S_SalesOrderEDI
{
System.Object ftpo,ftpObject;
System.Net.FtpWebRequest request,ftpWebRequest,ftpWebRequestDel;
System.IO.StreamReader reader;
System.Net.NetworkCredential credential,networkCredentials;
System.Net.FtpWebResponse response,ftpWebResponse,ftpWebResponseDel ;
System.String text;
String50 ftpUserName;//="";
String30 ftpPassword;//="";
// FTP files list
List ftpFilesList;
ListEnumerator ftpFilesListEnum;
container conFTPFilesDownload;
ListIterator ftpFilesListIterator;
Object ftpResponse;
System.String strReadLine;
System.IO.Stream ioStream;
System.IO.StreamReader ioStreamReader;
System.IO.StreamWriter ioStreamWriter;
str ftpFileName,ftpHostandFolder;
System.Object credential1;
URL ftpHostName;
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
_SalesOrderEDI salesOrderEDIInound=new _SalesOrderEDI();
salesOrderEDIInound.readingFTPDirFiles();
salesOrderEDIInound.ftpDeleteAllFiles();
}
public void setFTPCredentials()
{
_InterfaceParametersCheck interfaceParametersCheck= new _InterfaceParametersCheck() ;
if(!interfaceParametersCheck.FTPParameters())
{
checkFailed( "FTP details is missing");
}
_InterfaceParameters parameters;
parameters = interfaceParametersCheck.FTPDetails();
ftpHostandFolder = parameters.FTPAddress;
if (subStr(ftpHostandFolder, strLen(ftpHostandFolder) - 1, 1) != '/')
{
ftpHostandFolder += '/';
}
if (parameters.FTPFolder)
{
ftpHostandFolder += parameters.FTPFolder ;
ftpHostandFolder += '/';
}
ftpHostName=parameters.FTPAddress;
FTPUserName=parameters.FTPUserName;
FTPPassword=parameters.FTPPassword;
}
public void readingFTPDirFiles()
{
this.setFTPCredentials();
// Marshaling .NET to X++
ftpObject = System.Net.WebRequest::Create(ftpHostandFolder);
ftpWebRequest = ftpObject;
if (ftpWebRequest)
{
ftpWebRequest.set_KeepAlive(false);
ftpWebRequest.set_UsePassive(true);
ftpWebRequest.set_UseBinary(true);
ftpWebRequest.set_Method('NLST');
ftpWebRequest.EnableSsl=false;
//UploadFile is not supported through an Http proxy, disable the proxy for this request.
// ftpWebRequest.set_Proxy(null);
networkCredentials = new System.Net.NetworkCredential("","","");
// networkCredentials.Domain = "EME";
networkCredentials.UserName = FTPUserName;
networkCredentials.Password = FTPPassword;
// networkCredentials.GetCredential(ftpHostandFolder,24,"Basic");
// networkCredentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword);
ftpWebRequest.set_Credentials(networkCredentials);
// ftpWebRequest.PreAuthenticate=false;
ftpWebResponse = ftpWebRequest.GetResponse();
if (ftpWebResponse)
{
ftpFilesList = new list(Types::String);
ioStreamReader = new System.IO.StreamReader(ftpWebResponse.GetResponseStream());
if (ioStreamReader)
{
strReadLine = ioStreamReader.ReadLine();
while (!System.String::IsNullOrEmpty(strReadLine))
{
ftpFilesListIterator = new Listiterator(strsplit(strReadLine, '/'));
while (ftpFilesListIterator.more())
{
conFTPFilesDownload += ftpFilesListIterator.value();
this.ReadXMLFileAndCreateSalesOrder(ftpFilesListIterator.value());
// info(ftpFilesListIterator.value());
//this.MoveFiletoArchiveFolder(ftpFilesListIterator.value());
ftpFilesListIterator.next();
}
ftpFilesList.addEnd(conPeek(conFTPFilesDownload, conlen(conFTPFilesDownload)));
strReadLine = ioStreamReader.ReadLine();
//info(strReadLine);
}
ioStreamReader.Close();
}
if (ftpFilesList.empty())
{
warning (strfmt("%1 - %2","No files available in - " , ftpHostName));
}
}
}
}
public System.String EscapeXMLValue(System.String xmlString)
{
if (xmlString == null)
return "XML is missing";
return xmlString.Replace("'"," ").Replace(",", " ").Replace("&", " ");
}
public void ReadXMLFileAndCreateSalesOrder(str strFileName)
{
// str ftpFileName;
Commaio file;
container line;
//Reading xml from FTP
ftpFileName=ftpHostandFolder + strFileName;
InteropPermission permI = new InteropPermission(InteropKind::ClrInterop);
FileIOPermission permIO = new FileIOPermission(ftpFileName,'r');
Set permissionSet = new Set(Types::Class);
new InteropPermission(InteropKind::ClrInterop).assert();
ftpo = System.Net.WebRequest::Create(ftpFileName);
request = ftpo;
credential = new System.Net.NetworkCredential(FTPUserName,FTPPassword);
request.set_Credentials(credential);
response = request.GetResponse();
reader = new System.IO.StreamReader(response.GetResponseStream());
text = reader.ReadToEnd();
//Creating Temp Path
String255 TempPath="";
TempPath = System.IO.Path::GetTempPath();
str fileNameTempPath=TempPath + strFileName;
//Added to skip Comma and & in XML (An error occurred while parsing EntityName.)
text= this.EscapeXMLValue(text);
//Saving xml in to Temp Path
XMLDocument xmlDoc = XMLDocument::newxml(text);
xmlDoc.save(fileNameTempPath);
// info(text);
XMLDocument doc;
XMLNode rootNode;
XMLNode custAccountNode;
XMLNodeList salesLines;
XMLParseError xmlError;
int i,j,k,l,m,p;
// Get the XML document
doc = new XMLDocument();
// doc.async(FALSE);
doc.load(fileNameTempPath);
xmlError = doc.parseError();
if (xmlError && xmlError.errorCode() != 0)
{
print "Error: " + xmlError.reason();
// pause;
return;
}
rootNode = doc.documentElement();
XmlNodeList xmlInformationsNodeList,xmlChildNodeList;
XmlNode xmlInformationNode;
XmlNode BodyNode,MessagePartsNode,SalesOrderNode,SalesTableNode,EnvelopeNode,SalesTableChildNode,SalesLineNode;
XmlNodeList SalesOrderNodeList,SalesTableNodeList,SalesTableChildNodeList,SalesLineChildNodeList;
str strCustAccount,strDoDAAC,strDeliveryDate,strPurchOrderFormNum,strReceiptDateRequested,str_ContractNumber,str_ContractOrderDate,str_ContractOrderNumber,str_ExternalEntityKey;
//declaring variables
SalesTable salesTable;
SalesLine salesLine;
NumberSeq numberSeq;
SalesFormLetter salesFormLetter;
SalesId salesId;
xmlInformationsNodeList = doc.documentElement().childNodes();
for ( i = 0; i < xmlInformationsNodeList.length(); i++)
{
xmlChildNodeList = xmlInformationsNodeList.item(i).childNodes();
for (j = 0; j < xmlChildNodeList.length() ; j++)
{
xmlChildNodeList = xmlChildNodeList.item(j).childNodes();
for (k = 0; k < xmlChildNodeList.length() ; k++)
{
xmlInformationNode = xmlChildNodeList.item(k);
if(xmlInformationNode.nodeName()=="SalesOrder")
{
SalesTableNodeList=xmlInformationNode.childNodes();
for (l = 0; l < SalesTableNodeList.length() ; l++)
{
SalesTableNode=SalesTableNodeList.item(l);
if(SalesTableNode.nodeName()=="SalesTable")
{
SalesTableChildNodeList=SalesTableNode.childNodes();
for (m = 0; m < SalesTableChildNodeList.length() ; m++)
{
SalesTableChildNode=SalesTableChildNodeList.item(m);
if(SalesTableChildNode.nodeName()=="CustAccount")
{
strDoDAAC= SalesTableChildNode.innerText();
}
if(SalesTableChildNode.nodeName()=="DeliveryDate")
{
strDeliveryDate=SalesTableChildNode.innerText();
}
if(SalesTableChildNode.nodeName()=="PurchOrderFormNum")
{
strPurchOrderFormNum = SalesTableChildNode.innerText();
}
if(SalesTableChildNode.nodeName()=="ReceiptDateRequested")
{
strReceiptDateRequested =SalesTableChildNode.innerText();
}
// if(m== SalesTableChildNodeList.length()-2) //Creating Sales Order at last Iteration
if(m== 8) //Creating Sales Order at last Iteration
{
ttsBegin;
//creating sales order header
//getting sales order id from number sequence
numberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
numberSeq.used();
salesTable.SalesId = numberSeq.num();
salesId =salesTable.SalesId;
salesTable.initValue();
salesTable.CustAccount = "US-027";
salesTable.initFromCustTable();
salesTable.InventSiteId = "1";
salesTable.InventLocationId ="11";
salesTable.DeliveryDate=str2Date(strDeliveryDate,321); // day: 1 month: 2 year: 3
salesTable.PurchOrderFormNum=strPurchOrderFormNum ;
salesTable.ShippingDateRequested = salesTable.DeliveryDate;
salesTable.ReceiptDateRequested =str2Date(strReceiptDateRequested,321 ); // day: 1 month: 2 year: 3
salesTable.SalesOriginId="EDI";
//validate
if (!salesTable.validateWrite())
{
this.CreateHistory(salesId + "-" +, "Error in Sales order header", _EDIStatus::Error );
throw Exception::Error;
}
salesTable.insert();
ttsCommit;
}
if(m>8) //Creating Sales Order at last Iteration
{
if(SalesTableChildNode.nodeName()=="SalesLine")
{
SalesLineChildNodeList=SalesTableChildNode.childNodes();
str strCustomerLineNum, strItemId, strName, strSalesPrice, strSalesQty, strSalesUnit;
for (p = 0; p < SalesLineChildNodeList.length() ; p++)
{
SalesLineNode=SalesLineChildNodeList.item(p);
if(SalesLineNode.nodeName()=="CustomerLineNum")
{
strCustomerLineNum= SalesLineNode.innerText();
}
if(SalesLineNode.nodeName()=="ItemId")
{
strItemId=SalesLineNode.innerText();
}
if(SalesLineNode.nodeName()=="Name")
{
strName= SalesLineNode.innerText();
}
if(SalesLineNode.nodeName()=="SalesPrice")
{
strSalesPrice = SalesLineNode.innerText();
}
if(SalesLineNode.nodeName()=="SalesQty")
{
strSalesQty= SalesLineNode.innerText();
}
if(SalesLineNode.nodeName()=="SalesUnit")
{
strSalesUnit= SalesLineNode.innerText();
}
}
ttsBegin;
InventDim inventDim;
salesline.clear();
inventDim.clear();
salesLine.initValue();
salesLine.SalesId = salesTable.SalesId;
salesLine.initFromSalesTable(salesTable);
salesLine.CustomerLineNum = str2Int(strCustomerLineNum);
salesLine.ItemId =strItemId;
salesLine.itemIdChanged();
inventDim.InventSiteId="1";
inventDim.InventLocationId="11";
salesLine.InventDimId=InventDim::findDim(inventDim).inventDimId ;
salesLine.SalesQty= str2Num(strSalesQty);
salesLine.SalesUnit =strSalesUnit;
real unitPrice=str2Num(strSalesPrice);
salesLine.SalesPrice = unitPrice;
salesLine.LineAmount = salesLine.calcLineAmount();
//validate
if (!salesLine.validateWrite())
{
this.CreateHistory(salesId + "-" + "Error in Sales order lines", _EDIStatus::Error );
throw Exception::Error;
}
salesLine.createLine(NoYes::Yes, // Validate
NoYes::Yes, // initFromSalesTable
NoYes::Yes, // initFromInventTable
NoYes::Yes, // calcInventQty
NoYes::Yes, // searchMarkup
NoYes::Yes); // searchPrice Enable this for any Trade Aggrement Prices to bring
ttsCommit;
//Update Unit Price from not from TA
ttsbegin ;
SalesLine salesLinePrice;
select forupdate salesLinePrice where salesLinePrice.RecId==salesLine.RecId;
salesLinePrice.SalesPrice =unitPrice;
salesLinePrice.Update();
ttsCommit;
}
}
//salesLine.createLine(true, // Validate true, // initFromSalesTable true, // initFromInventTable true, // calcInventQty true, // searchMarkup true // searchPrice);
////confirm sales order
//salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation);
//salesFormLetter.update(salesTable);
}
// info(strFmt('%1 -> %2', SalesTableNode.selectSingleNode("//CustAccount").innerText() ,""));
}
this.CreateHistory(salesId + "-" + " Sales order created successfully", _EDIStatus::Success );
//info(strFmt('%1 -> %2', SalesTableNode.nodeName(), SalesTableNode.innerText()));
}
// info(strFmt('%1 -> %2', xmlInformationNode.nodeName(), xmlInformationNode.innerText()));
}
}
}
}
}
Public void ftpDeleteAllFiles()
{
Str fileNameType;
ftpFilesListEnum = ftpFilesList.getEnumerator();
ftpFilesListEnum.reset();
while (ftpFilesListEnum.moveNext())
{
fileNameType = ftpFilesListEnum.current();
// Marshaling .NET to X++
ftpObject = System.Net.WebRequest::Create(ftpHostandFolder + fileNameType);
ftpWebRequestDel = ftpObject;
if (ftpWebRequestDel)
{
ftpWebRequestDel.set_Method('DELE');
credential = new System.Net.NetworkCredential(FTPUserName,FTPPassword);
ftpWebRequestDel.set_Credentials(credential);
response = ftpWebRequestDel.GetResponse();
ftpWebResponseDel = ftpWebRequestDel.GetResponse();
if (ftpWebResponseDel)
{
info(strfmt(" Sales order successfully created and removed file %1 from %2", fileNameType, ftpHostandFolder));
}
}
}
}
Public void CreateHistory(_EDIMessageId MessageId,_EDIErrorMessage errorMessage,_EDIStatus ediStatus)
{
//Create Interface History
_EDITransmissionType transmissionType="Sales Order - ";
_EDIDirection direction="Inbound";
//Inserting in to Interface History
_InterfaceHistory::createInterfaceHistory(MessageId,transmissionType,direction,errorMessage,ediStatus);
}
Public void MoveFiletoArchiveFolder(str strFileName)
{
System.IO.Stream requestStream;
System.Byte[] bytes;
System.IO.Stream readStream;
System.Text.Encoding utf8;
System.Exception netExcepn;
str retVal;
// Read file
ftpHostandFolder = strReplace(ftpHostandFolder,"/","\\");
ftpHostandFolder=ftpHostandFolder +strFileName;
reader = new System.IO.StreamReader(ftpHostandFolder);
utf8 = System.Text.Encoding::get_UTF8();
bytes = utf8.GetBytes( reader.ReadToEnd() );
reader.Close();
//Create File in FTP
ftpo = System.Net.WebRequest::Create(ftpFileName);
request = ftpo;
credential = new System.Net.NetworkCredential(FTPUserName,FTPPassword);
request.set_Credentials(credential);
response = request.GetResponse();
////FTP file UploadFile represents STOR
///FTP file UploadFileWithUniqueName represents STOU
request.set_Method('STOR');
requestStream = request.GetRequestStream();
requestStream.Write(bytes,0,bytes.get_Length());
requestStream.Close();
}
}
}
AX D365 FTP Interface with Third Party System
Below Code Reads XML file from below FTP Path and creates Sales Order in D365 AX
using System.Security;
class S_SalesOrderEDI
{
System.Object ftpo,ftpObject;
System.Net.FtpWebRequest request,ftpWebRequest,ftpWebRequestDel;
System.IO.StreamReader reader;
System.Net.NetworkCredential credential,networkCredentials;
System.Net.FtpWebResponse response,ftpWebResponse,ftpWebResponseDel ;
System.String text;
String50 ftpUserName;//="";
String30 ftpPassword;//="";
// FTP files list
List ftpFilesList;
ListEnumerator ftpFilesListEnum;
container conFTPFilesDownload;
ListIterator ftpFilesListIterator;
Object ftpResponse;
System.String strReadLine;
System.IO.Stream ioStream;
System.IO.StreamReader ioStreamReader;
System.IO.StreamWriter ioStreamWriter;
str ftpFileName,ftpHostandFolder;
System.Object credential1;
URL ftpHostName;
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
_SalesOrderEDI salesOrderEDIInound=new _SalesOrderEDI();
salesOrderEDIInound.readingFTPDirFiles();
salesOrderEDIInound.ftpDeleteAllFiles();
}
public void setFTPCredentials()
{
_InterfaceParametersCheck interfaceParametersCheck= new _InterfaceParametersCheck() ;
if(!interfaceParametersCheck.FTPParameters())
{
checkFailed( "FTP details is missing");
}
_InterfaceParameters parameters;
parameters = interfaceParametersCheck.FTPDetails();
ftpHostandFolder = parameters.FTPAddress;
if (subStr(ftpHostandFolder, strLen(ftpHostandFolder) - 1, 1) != '/')
{
ftpHostandFolder += '/';
}
if (parameters.FTPFolder)
{
ftpHostandFolder += parameters.FTPFolder ;
ftpHostandFolder += '/';
}
ftpHostName=parameters.FTPAddress;
FTPUserName=parameters.FTPUserName;
FTPPassword=parameters.FTPPassword;
}
public void readingFTPDirFiles()
{
this.setFTPCredentials();
// Marshaling .NET to X++
ftpObject = System.Net.WebRequest::Create(ftpHostandFolder);
ftpWebRequest = ftpObject;
if (ftpWebRequest)
{
ftpWebRequest.set_KeepAlive(false);
ftpWebRequest.set_UsePassive(true);
ftpWebRequest.set_UseBinary(true);
ftpWebRequest.set_Method('NLST');
ftpWebRequest.EnableSsl=false;
//UploadFile is not supported through an Http proxy, disable the proxy for this request.
// ftpWebRequest.set_Proxy(null);
networkCredentials = new System.Net.NetworkCredential("","","");
// networkCredentials.Domain = "EME";
networkCredentials.UserName = FTPUserName;
networkCredentials.Password = FTPPassword;
// networkCredentials.GetCredential(ftpHostandFolder,24,"Basic");
// networkCredentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword);
ftpWebRequest.set_Credentials(networkCredentials);
// ftpWebRequest.PreAuthenticate=false;
ftpWebResponse = ftpWebRequest.GetResponse();
if (ftpWebResponse)
{
ftpFilesList = new list(Types::String);
ioStreamReader = new System.IO.StreamReader(ftpWebResponse.GetResponseStream());
if (ioStreamReader)
{
strReadLine = ioStreamReader.ReadLine();
while (!System.String::IsNullOrEmpty(strReadLine))
{
ftpFilesListIterator = new Listiterator(strsplit(strReadLine, '/'));
while (ftpFilesListIterator.more())
{
conFTPFilesDownload += ftpFilesListIterator.value();
this.ReadXMLFileAndCreateSalesOrder(ftpFilesListIterator.value());
// info(ftpFilesListIterator.value());
//this.MoveFiletoArchiveFolder(ftpFilesListIterator.value());
ftpFilesListIterator.next();
}
ftpFilesList.addEnd(conPeek(conFTPFilesDownload, conlen(conFTPFilesDownload)));
strReadLine = ioStreamReader.ReadLine();
//info(strReadLine);
}
ioStreamReader.Close();
}
if (ftpFilesList.empty())
{
warning (strfmt("%1 - %2","No files available in - " , ftpHostName));
}
}
}
}
public System.String EscapeXMLValue(System.String xmlString)
{
if (xmlString == null)
return "XML is missing";
return xmlString.Replace("'"," ").Replace(",", " ").Replace("&", " ");
}
public void ReadXMLFileAndCreateSalesOrder(str strFileName)
{
// str ftpFileName;
Commaio file;
container line;
//Reading xml from FTP
ftpFileName=ftpHostandFolder + strFileName;
InteropPermission permI = new InteropPermission(InteropKind::ClrInterop);
FileIOPermission permIO = new FileIOPermission(ftpFileName,'r');
Set permissionSet = new Set(Types::Class);
new InteropPermission(InteropKind::ClrInterop).assert();
ftpo = System.Net.WebRequest::Create(ftpFileName);
request = ftpo;
credential = new System.Net.NetworkCredential(FTPUserName,FTPPassword);
request.set_Credentials(credential);
response = request.GetResponse();
reader = new System.IO.StreamReader(response.GetResponseStream());
text = reader.ReadToEnd();
//Creating Temp Path
String255 TempPath="";
TempPath = System.IO.Path::GetTempPath();
str fileNameTempPath=TempPath + strFileName;
//Added to skip Comma and & in XML (An error occurred while parsing EntityName.)
text= this.EscapeXMLValue(text);
//Saving xml in to Temp Path
XMLDocument xmlDoc = XMLDocument::newxml(text);
xmlDoc.save(fileNameTempPath);
// info(text);
XMLDocument doc;
XMLNode rootNode;
XMLNode custAccountNode;
XMLNodeList salesLines;
XMLParseError xmlError;
int i,j,k,l,m,p;
// Get the XML document
doc = new XMLDocument();
// doc.async(FALSE);
doc.load(fileNameTempPath);
xmlError = doc.parseError();
if (xmlError && xmlError.errorCode() != 0)
{
print "Error: " + xmlError.reason();
// pause;
return;
}
rootNode = doc.documentElement();
XmlNodeList xmlInformationsNodeList,xmlChildNodeList;
XmlNode xmlInformationNode;
XmlNode BodyNode,MessagePartsNode,SalesOrderNode,SalesTableNode,EnvelopeNode,SalesTableChildNode,SalesLineNode;
XmlNodeList SalesOrderNodeList,SalesTableNodeList,SalesTableChildNodeList,SalesLineChildNodeList;
str strCustAccount,strDoDAAC,strDeliveryDate,strPurchOrderFormNum,strReceiptDateRequested,str_ContractNumber,str_ContractOrderDate,str_ContractOrderNumber,str_ExternalEntityKey;
//declaring variables
SalesTable salesTable;
SalesLine salesLine;
NumberSeq numberSeq;
SalesFormLetter salesFormLetter;
SalesId salesId;
xmlInformationsNodeList = doc.documentElement().childNodes();
for ( i = 0; i < xmlInformationsNodeList.length(); i++)
{
xmlChildNodeList = xmlInformationsNodeList.item(i).childNodes();
for (j = 0; j < xmlChildNodeList.length() ; j++)
{
xmlChildNodeList = xmlChildNodeList.item(j).childNodes();
for (k = 0; k < xmlChildNodeList.length() ; k++)
{
xmlInformationNode = xmlChildNodeList.item(k);
if(xmlInformationNode.nodeName()=="SalesOrder")
{
SalesTableNodeList=xmlInformationNode.childNodes();
for (l = 0; l < SalesTableNodeList.length() ; l++)
{
SalesTableNode=SalesTableNodeList.item(l);
if(SalesTableNode.nodeName()=="SalesTable")
{
SalesTableChildNodeList=SalesTableNode.childNodes();
for (m = 0; m < SalesTableChildNodeList.length() ; m++)
{
SalesTableChildNode=SalesTableChildNodeList.item(m);
if(SalesTableChildNode.nodeName()=="CustAccount")
{
strDoDAAC= SalesTableChildNode.innerText();
}
if(SalesTableChildNode.nodeName()=="DeliveryDate")
{
strDeliveryDate=SalesTableChildNode.innerText();
}
if(SalesTableChildNode.nodeName()=="PurchOrderFormNum")
{
strPurchOrderFormNum = SalesTableChildNode.innerText();
}
if(SalesTableChildNode.nodeName()=="ReceiptDateRequested")
{
strReceiptDateRequested =SalesTableChildNode.innerText();
}
// if(m== SalesTableChildNodeList.length()-2) //Creating Sales Order at last Iteration
if(m== 8) //Creating Sales Order at last Iteration
{
ttsBegin;
//creating sales order header
//getting sales order id from number sequence
numberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
numberSeq.used();
salesTable.SalesId = numberSeq.num();
salesId =salesTable.SalesId;
salesTable.initValue();
salesTable.CustAccount = "US-027";
salesTable.initFromCustTable();
salesTable.InventSiteId = "1";
salesTable.InventLocationId ="11";
salesTable.DeliveryDate=str2Date(strDeliveryDate,321); // day: 1 month: 2 year: 3
salesTable.PurchOrderFormNum=strPurchOrderFormNum ;
salesTable.ShippingDateRequested = salesTable.DeliveryDate;
salesTable.ReceiptDateRequested =str2Date(strReceiptDateRequested,321 ); // day: 1 month: 2 year: 3
salesTable.SalesOriginId="EDI";
//validate
if (!salesTable.validateWrite())
{
this.CreateHistory(salesId + "-" +, "Error in Sales order header", _EDIStatus::Error );
throw Exception::Error;
}
salesTable.insert();
ttsCommit;
}
if(m>8) //Creating Sales Order at last Iteration
{
if(SalesTableChildNode.nodeName()=="SalesLine")
{
SalesLineChildNodeList=SalesTableChildNode.childNodes();
str strCustomerLineNum, strItemId, strName, strSalesPrice, strSalesQty, strSalesUnit;
for (p = 0; p < SalesLineChildNodeList.length() ; p++)
{
SalesLineNode=SalesLineChildNodeList.item(p);
if(SalesLineNode.nodeName()=="CustomerLineNum")
{
strCustomerLineNum= SalesLineNode.innerText();
}
if(SalesLineNode.nodeName()=="ItemId")
{
strItemId=SalesLineNode.innerText();
}
if(SalesLineNode.nodeName()=="Name")
{
strName= SalesLineNode.innerText();
}
if(SalesLineNode.nodeName()=="SalesPrice")
{
strSalesPrice = SalesLineNode.innerText();
}
if(SalesLineNode.nodeName()=="SalesQty")
{
strSalesQty= SalesLineNode.innerText();
}
if(SalesLineNode.nodeName()=="SalesUnit")
{
strSalesUnit= SalesLineNode.innerText();
}
}
ttsBegin;
InventDim inventDim;
salesline.clear();
inventDim.clear();
salesLine.initValue();
salesLine.SalesId = salesTable.SalesId;
salesLine.initFromSalesTable(salesTable);
salesLine.CustomerLineNum = str2Int(strCustomerLineNum);
salesLine.ItemId =strItemId;
salesLine.itemIdChanged();
inventDim.InventSiteId="1";
inventDim.InventLocationId="11";
salesLine.InventDimId=InventDim::findDim(inventDim).inventDimId ;
salesLine.SalesQty= str2Num(strSalesQty);
salesLine.SalesUnit =strSalesUnit;
real unitPrice=str2Num(strSalesPrice);
salesLine.SalesPrice = unitPrice;
salesLine.LineAmount = salesLine.calcLineAmount();
//validate
if (!salesLine.validateWrite())
{
this.CreateHistory(salesId + "-" + "Error in Sales order lines", _EDIStatus::Error );
throw Exception::Error;
}
salesLine.createLine(NoYes::Yes, // Validate
NoYes::Yes, // initFromSalesTable
NoYes::Yes, // initFromInventTable
NoYes::Yes, // calcInventQty
NoYes::Yes, // searchMarkup
NoYes::Yes); // searchPrice Enable this for any Trade Aggrement Prices to bring
ttsCommit;
//Update Unit Price from not from TA
ttsbegin ;
SalesLine salesLinePrice;
select forupdate salesLinePrice where salesLinePrice.RecId==salesLine.RecId;
salesLinePrice.SalesPrice =unitPrice;
salesLinePrice.Update();
ttsCommit;
}
}
//salesLine.createLine(true, // Validate true, // initFromSalesTable true, // initFromInventTable true, // calcInventQty true, // searchMarkup true // searchPrice);
////confirm sales order
//salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation);
//salesFormLetter.update(salesTable);
}
// info(strFmt('%1 -> %2', SalesTableNode.selectSingleNode("//CustAccount").innerText() ,""));
}
this.CreateHistory(salesId + "-" + " Sales order created successfully", _EDIStatus::Success );
//info(strFmt('%1 -> %2', SalesTableNode.nodeName(), SalesTableNode.innerText()));
}
// info(strFmt('%1 -> %2', xmlInformationNode.nodeName(), xmlInformationNode.innerText()));
}
}
}
}
}
Public void ftpDeleteAllFiles()
{
Str fileNameType;
ftpFilesListEnum = ftpFilesList.getEnumerator();
ftpFilesListEnum.reset();
while (ftpFilesListEnum.moveNext())
{
fileNameType = ftpFilesListEnum.current();
// Marshaling .NET to X++
ftpObject = System.Net.WebRequest::Create(ftpHostandFolder + fileNameType);
ftpWebRequestDel = ftpObject;
if (ftpWebRequestDel)
{
ftpWebRequestDel.set_Method('DELE');
credential = new System.Net.NetworkCredential(FTPUserName,FTPPassword);
ftpWebRequestDel.set_Credentials(credential);
response = ftpWebRequestDel.GetResponse();
ftpWebResponseDel = ftpWebRequestDel.GetResponse();
if (ftpWebResponseDel)
{
info(strfmt(" Sales order successfully created and removed file %1 from %2", fileNameType, ftpHostandFolder));
}
}
}
}
Public void CreateHistory(_EDIMessageId MessageId,_EDIErrorMessage errorMessage,_EDIStatus ediStatus)
{
//Create Interface History
_EDITransmissionType transmissionType="Sales Order - ";
_EDIDirection direction="Inbound";
//Inserting in to Interface History
_InterfaceHistory::createInterfaceHistory(MessageId,transmissionType,direction,errorMessage,ediStatus);
}
Public void MoveFiletoArchiveFolder(str strFileName)
{
System.IO.Stream requestStream;
System.Byte[] bytes;
System.IO.Stream readStream;
System.Text.Encoding utf8;
System.Exception netExcepn;
str retVal;
// Read file
ftpHostandFolder = strReplace(ftpHostandFolder,"/","\\");
ftpHostandFolder=ftpHostandFolder +strFileName;
reader = new System.IO.StreamReader(ftpHostandFolder);
utf8 = System.Text.Encoding::get_UTF8();
bytes = utf8.GetBytes( reader.ReadToEnd() );
reader.Close();
//Create File in FTP
ftpo = System.Net.WebRequest::Create(ftpFileName);
request = ftpo;
credential = new System.Net.NetworkCredential(FTPUserName,FTPPassword);
request.set_Credentials(credential);
response = request.GetResponse();
////FTP file UploadFile represents STOR
///FTP file UploadFileWithUniqueName represents STOU
request.set_Method('STOR');
requestStream = request.GetRequestStream();
requestStream.Write(bytes,0,bytes.get_Length());
requestStream.Close();
}
}
}
Comments
Post a Comment