Good morning,
We are developing a webservice / method that creates sales documents.
In the header I can pass the values all to the header parameters. But on the lines how can I do the same? For example, if you want to change the item description, or the rate of VAT, or if you want to pass values to user fields ... how do I?
In the following example I can not ....
Any help or suggestion? Thank you.
public string SalesDocument(string sTransactionID, string sInstance, string sCompanyCode, string sUsername, string sPassword, string xml)
{
string sNrDoc = "";
string sResp = string.Empty;
var Lista = new StdBELista();
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xml);
var trans = new StdBETransaccao();
var vTipoPlataforma = new EnumTipoPlataforma();
var DocVenda = new GcpBEDocumentoVenda();
var CDUPrimavera = new StdBECampos();
string sExtDocNumber = string.Empty;
string sSONumber = string.Empty;
string sTipoDoc = string.Empty;
string sEntidade = string.Empty;
string sTipoEntidade = string.Empty;
string sSerie = string.Empty;
string sMoeda = string.Empty;
string sCN = string.Empty;
string sCNOriginalDocNumber = string.Empty;
string sCondPag = string.Empty;
string sModoPag = string.Empty;
string sNumberLines = string.Empty;
string sComments = string.Empty;
string sShippingAddress1 = string.Empty;
string sShippingAddress2 = string.Empty;
string sShippingCity = string.Empty;
string sShippingPostalCode = string.Empty;
string sShippingPostalCodeCity = string.Empty;
string sShippingCountry = string.Empty;
int iNrLines = 0;
string sArtigo = string.Empty;
string sDescricao = string.Empty;
int sCodIva;
double sPrecoUnit;
double sQuantidade;
string sUnidade;
double sDescontoLinha;
string sObs = string.Empty;
string dataXML;
DateTime dDataDoc;
int codigoInteracao;
string strSql = "";
string strQuantos = "";
StdBELista qyrConsultaCodigo = new StdBELista();
vTipoPlataforma = EnumTipoPlataforma.tpProfissional;
ErpBS motor = new ErpBS();
motor = DaMotorInicializado(vTipoPlataforma, sCompanyCode, sUsername, sPassword, trans, sInstance);
var plataforma = DaPlataformaInicializada(sCompanyCode, sUsername, sPassword, sInstance);
strQuantos = "select count(CDU_Codigo) as Total from TDU_web_moneris";
qyrConsultaCodigo = motor.Consulta(strQuantos);
codigoInteracao = qyrConsultaCodigo.Valor("Total") + 1;
XmlNodeList xnList = xDoc.GetElementsByTagName("DocHeader");
sExtDocNumber = xnList[0]["ExternalDocNumber"].InnerXml;
sSONumber = xnList[0]["SONumber"].InnerXml;
sTipoDoc = xnList[0]["DocCode"].InnerXml;
sEntidade = xnList[0]["ClientCode"].InnerXml;
sTipoEntidade = "C";
sSerie = xnList[0]["Serie"].InnerXml;
dataXML = xnList[0]["Date"].InnerXml;
dDataDoc = DateTime.Parse(dataXML);
sMoeda = xnList[0]["Currency"].InnerXml;
sCN = xnList[0]["CN"].InnerXml;
sCNOriginalDocNumber = xnList[0]["CNOriginalDocNumber"].InnerXml;
sModoPag = xnList[0]["PaymentMethod"].InnerXml;
sCondPag = xnList[0]["PaymentTerm"].InnerXml;
sNumberLines = xnList[0]["NumberOfLines"].InnerXml;
sComments = xnList[0]["Comments"].InnerXml;
sShippingAddress1 = xnList[0]["ShippingAddress1"].InnerXml;
sShippingAddress2 = xnList[0]["ShippingAddress2"].InnerXml;
sShippingCity = xnList[0]["ShippingCity"].InnerXml;
sShippingPostalCode = xnList[0]["ShippingPostalCode"].InnerXml;
sShippingPostalCodeCity = xnList[0]["ShippingPostalCodeCity"].InnerXml;
sShippingCountry = xnList[0]["ShippingCountry"].InnerXml;
//bool bErro = false;
//string sIsAlive = IsAlive(sInstance, sCompanyCode, sUsername, sPassword);
//if (sIsAlive == "000")
//{
// sResp = sTransactionID;
//}
//else
//{
// sResp = sTransactionID + "#" + sIsAlive;
// bErro = true;
//}
//if (motor.Comercial.Vendas.ExisteReferencia("000",sTipoDoc,sSerie,sEntidade,sExtDocNumber))
//{
// bErro = true;
// sResp += "#200";
//}
CDUPrimavera.set_Item("CDU_ExternalDocNumber", sExtDocNumber);
DocVenda.set_Referencia(sSONumber);
DocVenda.set_Tipodoc(ref sTipoDoc);
DocVenda.set_Entidade(ref sEntidade);
DocVenda.set_TipoEntidade(ref sTipoEntidade);
DocVenda.set_Serie(ref sSerie);
DocVenda.set_DataDoc(ref dDataDoc);
DocVenda.set_Moeda(sMoeda);
CDUPrimavera.set_Item("CDU_CN", sCN);
CDUPrimavera.set_Item("CDU_CNOrigDocNumber", sCNOriginalDocNumber);
DocVenda.set_ModoPag(ref sModoPag);
DocVenda.set_CondPag(ref sCondPag);
motor.Comercial.Vendas.PreencheDadosRelacionados(ref DocVenda);
CDUPrimavera.set_Item("CDU_TotalLines", sNumberLines);
DocVenda.set_Observacoes(sComments);
DocVenda.set_MoradaFac(sShippingAddress1);
DocVenda.set_Morada2Fac (sShippingAddress2);
DocVenda.set_LocalidadeFac(sShippingCity);
DocVenda.set_CodigoPostalFac(sShippingPostalCode);
DocVenda.set_CodigoPostalFac(sShippingPostalCodeCity);
DocVenda.set_PaisFac(sShippingCountry);
DocVenda.set_CamposUtil(CDUPrimavera);
XmlNodeList xnListLinhas = xDoc.GetElementsByTagName("Line");
foreach (XmlNode oNo in xnListLinhas)
{
sArtigo = oNo["ItemCode"].InnerXml;
sCodIva = Int32.Parse(oNo["VatCode"].InnerXml);
sPrecoUnit = double.Parse(oNo["UnitPrice"].InnerXml);
sQuantidade = double.Parse(oNo["Quantity"].InnerXml);
sUnidade = oNo["Unit"].InnerXml;
sDescontoLinha = double.Parse(oNo["Discount"].InnerXml);
motor.Comercial.Vendas.AdicionaLinha(DocVenda, sArtigo, sQuantidade, "", "", sPrecoUnit, sDescontoLinha, "", sQuantidade, 0, 0, 0, 0, 0, 0, false, false, 0);
//Aqui os parametros a passar são fixos, não me permite alterar a descrição ou a taxa de iva, etc....
}
motor.Comercial.Vendas.Actualiza(DocVenda, "", "");
string qNumDoc = "select max(numdoc) as Ndoc from CabecDoc where TipoDoc = '" + sTipoDoc + "' and Serie = '" + sSerie + "'";
Lista = motor.Consulta(qNumDoc);
sNrDoc = Lista.Valor("Ndoc").ToString();
string sDocPDF = "";
sDocPDF = @Server.MapPath("~/Files/Documentos/") + sCompanyCode + "_" + sNrDoc + "_" + sSerie + ".pdf";
motor.Comercial.Vendas.ImprimeDocumento(sTipoDoc, sSerie, int.Parse(sNrDoc), "000", 1, "", false, sDocPDF, 1);
System.IO.FileStream dd = System.IO.File.OpenRead(sDocPDF);
byte[] Bytes = new byte[dd.Length];
dd.Read(Bytes, 0, Bytes.Length);
strSql = "Insert into TDU_web_moneris values('" + codigoInteracao + "','" + sInstance + "','" + sCompanyCode + "','" + sUsername + "','" + sPassword + "','" + xml + "','" + DateTime.Now + "','" + Bytes + "')";
plataforma.ExecSql.ExecutaXMLSqlServer(strSql);
motor.FechaEmpresaTrabalho();
plataforma.FechaPlataforma();
return sResp;
}
catch (Exception ex)
{
using (StreamWriter writer = new StreamWriter(Server.MapPath("~/Files/Logs/") + DateTime.Now.ToString("yyyy-MM-dd hh_mm_ss") + ".txt", true))
{
writer.WriteLine("SalesDoc Method - Error: " + ex.Message);
writer.WriteLine(ex.Message.ToString());
}
return sResp;
}
finally
{
}
}