Skip to Content
avatar image
Former Member

Error writing object to xml DeliveryNotes

Good day gentlemen

I am having trouble writing a document using the method (SAPbobsCOM.Documents) oCompany.GetBusinessObjectFromXML. You are returning the error -10 - Nota fiscal default sequence not defined or locked; On "Tax" tab, in "NF No.", select different sequence. If I write it by the traditional way it is filling the object I do not have this error, but if I use the XML write it returns that error. The client is multi-branch and the SAP configuration is correct because it is already in use for some time. Follow the code below for what I'm using.

/*----------------------------------------------------------------------------------------------------*/
            /*                                Tratamento do arquivo XML                                           */
            /*----------------------------------------------------------------------------------------------------*/
            string strPathXml = System.Web.HttpContext.Current.Server.MapPath("~/XML/");
            if (!Directory.Exists(strPathXml))
            {
                Directory.CreateDirectory(strPathXml);
            }
            string strData = DateTime.Now.ToString("yyyyMMddHHMMss").ToString();
            string strFileXml = System.Web.HttpContext.Current.Server.MapPath("~/XML/DeliveryNotes" + strData + ".xml");
            if (File.Exists(strFileXml))
                File.Delete(strFileXml);
            /*----------------------------------------------------------------------------------------------------*/


            BusinessPartners oBP = null;
            SAPbobsCOM.Company oCompany = null;
            Validacao_Retorno_SAP validacao_Retorno_SAP = new Validacao_Retorno_SAP();


            oCompany = DiCompany.Conectar();
            DataSet dsResult = new DataSet();
            int Filial = 0;


            try
            {
                Filial = validacao_Retorno_SAP.RetornaBPLID(odln.CodFilial);


                oBP = (BusinessPartners)oCompany.GetBusinessObject(BoObjectTypes.oBusinessPartners);


                oCompany.XmlExportType = BoXmlExportTypes.xet_ExportImportMode;
                oCompany.XMLAsString = false;


                var vCardCode = validacao_Retorno_SAP.RetornaCarCodeCPF_CNPJ(odln.cliente_cpf_cnpj);


                if (!vCardCode.Contains("F"))
                {


                    oBP.GetByKey(vCardCode);


                    var Frozen = oBP.Frozen;
                    var Valid = oBP.Valid;
                    var FrozenComm = oBP.FrozenRemarks;


                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-16\"?>");
                    stringBuilder.Append("<BOM>");
                    stringBuilder.Append("  <BO>");
                    stringBuilder.Append("    <AdmInfo>");
                    stringBuilder.Append("      <Object>15</Object>");
                    stringBuilder.Append("      <Version>2</Version>");
                    stringBuilder.Append("    </AdmInfo>");
                    stringBuilder.Append("    <ODLN>");
                    stringBuilder.Append("      <row>");
                    stringBuilder.Append("        <BPL_IDAssignedToInvoice>" + Filial + "</BPL_IDAssignedToInvoice>");
                    stringBuilder.Append("        <SeqCode>-1</SeqCode>");
                    stringBuilder.Append("        <Serial>" + odln.NumCTE + "</Serial>");
                    stringBuilder.Append("        <Model>44</Model>");
                    stringBuilder.Append("        <SeriesStr>" + odln.Serie + "</SeriesStr>");
                    stringBuilder.Append("        <CardCode>" + vCardCode + "</CardCode>");
                    stringBuilder.Append("        <NumAtCard>" + odln.Referencia + "</NumAtCard>");
                    stringBuilder.Append("        <DocDate>" + odln.DataLancamento + "</DocDate>");
                    stringBuilder.Append("        <DocDueDate>" + odln.DataVencimento + "</DocDueDate>");
                    stringBuilder.Append("        <U_TP_CTe>" + odln.TipoCTE + "</U_TP_CTe>");
                    stringBuilder.Append("        <U_SKILL_STACTE>" + odln.StatusCTE + "</U_SKILL_STACTE>");
                    stringBuilder.Append("        <U_SKILL_DTPRE>" + odln.DataCTE + "</U_SKILL_DTPRE>");
                    stringBuilder.Append("        <U_chaveacesso>" + odln.ChaveCTE + "</U_chaveacesso>");
                    stringBuilder.Append("        <U_TDS_Usuario>" + odln.Usuario + "</U_TDS_Usuario>");
                    stringBuilder.Append("        <U_TDS_CodTMS>" + odln.codTMS + "</U_TDS_CodTMS>");
                    stringBuilder.Append("      </row>");
                    stringBuilder.Append("    </ODLN>");
                    stringBuilder.Append("    <DLN12>");
                    stringBuilder.Append("      <row>");
                    stringBuilder.Append("        <Incoterms>" + odln.TipoFrete + "</Incoterms>");
                    stringBuilder.Append("      </row>");
                    stringBuilder.Append("    </DLN12>");
                    stringBuilder.Append("    <DLN1>");


                    #region Linhas
                    for (int i = 0; i < Itens.Count; i++)
                    {
                        stringBuilder.Append("      <row>");


                        dsResult = null;
                        dsResult = validacao_Retorno_SAP.RetornaEstruturaCentroCusto(Itens[i].Placa);


                        if (validacao_Retorno_SAP.VerificaItemCodeExiste(Itens[i].CodigoItem))
                        {
                            if (validacao_Retorno_SAP.VerificaAliquotaICMSExiste(Itens[i].AliquotaICMS))
                            {
                                stringBuilder.Append("        <ItemCode>" + validacao_Retorno_SAP.RetornaItemCode(Itens[i].CodigoItem) + "</ItemCode>");
                                stringBuilder.Append("        <Quantity>" + Itens[i].Qtde + "</Quantity>");
                                stringBuilder.Append("        <UnitPrice>" + Itens[i].Valor + "</UnitPrice>");
                                stringBuilder.Append("        <DiscountPercent>" + Itens[i].Desconto + "</DiscountPercent>");
                                stringBuilder.Append("        <TaxCode>" + validacao_Retorno_SAP.RetornaAliquotaICMS(Itens[i].AliquotaICMS) + "</TaxCode>");
                                stringBuilder.Append("        <CFOPCode>" + Itens[i].CFOP + "</CFOPCode>");
                                stringBuilder.Append("        <FreeText>" + odln.Referencia + "</FreeText>");
                                stringBuilder.Append("        <WarehouseCode>" + validacao_Retorno_SAP.RetornaDepositoxFilial(Filial.ToString()) + "</WarehouseCode>");


                                if (validacao_Retorno_SAP.VerificaPlacaExiste(Itens[i].Placa))
                                {
                                    stringBuilder.Append("        <CostingCode>1.7</CostingCode>");
                                    stringBuilder.Append("        <ProjectCode>1</ProjectCode>");


                                    if (string.IsNullOrEmpty(validacao_Retorno_SAP.RetornaBU(Convert.ToInt32(Itens[i].BU))))
                                        throw new Exception("Não existe configuração de BU (Unidade Negócio) para a frota informada. Verifique o BU Tabela de Mapeamento: " + Itens[i].BU);
                                    else
                                        stringBuilder.Append("        <CostingCode2>" + validacao_Retorno_SAP.RetornaBU(Convert.ToInt32(Itens[i].BU)) + "</CostingCode2>");


                                    stringBuilder.Append("        <CostingCode3>" + validacao_Retorno_SAP.RetornaEmpresaxFilial(Convert.ToString(Filial)) + "</CostingCode3>");
                                    stringBuilder.Append("        <CostingCode4>4.D</CostingCode4>");


                                    if (string.IsNullOrEmpty(dsResult.Tables[0].Rows[0]["U_TDS_CENTROCUSTO"].ToString()))
                                        throw new Exception("Não existe configuração de centro de custo para a frota informada. Verifique: Placa: " + Itens[i].Placa);
                                    else
                                        stringBuilder.Append("        <CostingCode5>" + dsResult.Tables[0].Rows[0]["U_TDS_CENTROCUSTO"].ToString() + "</CostingCode5>");


                                    OJDT_Public.TDS_OBZ = "1.7";
                                    OJDT_Public.TDS_PAIS = "1";
                                    OJDT_Public.TDS_BU = validacao_Retorno_SAP.RetornaBU(Convert.ToInt32(Itens[i].BU));
                                    OJDT_Public.TDS_EMPRESA = validacao_Retorno_SAP.RetornaEmpresaxFilial(Convert.ToString(Filial));
                                    OJDT_Public.TDS_DEPARTAMENTO = "4.D";
                                    OJDT_Public.TDS_CENTROCUSTO = dsResult.Tables[0].Rows[0]["U_TDS_CENTROCUSTO"].ToString();
                                }
                                else
                                {
                                    stringBuilder.Append("        <CostingCode>1.7</CostingCode>");
                                    stringBuilder.Append("        <ProjectCode>1</ProjectCode>");


                                    if (string.IsNullOrEmpty(validacao_Retorno_SAP.RetornaBU(Convert.ToInt32(Itens[i].BU))))
                                        throw new Exception("Não existe configuração de BU (Unidade Negócio) para a frota informada. Verifique o BU: " + Itens[i].BU);
                                    else
                                        stringBuilder.Append("        <CostingCode2>" + validacao_Retorno_SAP.RetornaBU(Convert.ToInt32(Itens[i].BU)) + "</CostingCode2>");


                                    stringBuilder.Append("        <CostingCode3>3.0</CostingCode3>");
                                    stringBuilder.Append("        <CostingCode4>4.D</CostingCode4>");
                                    stringBuilder.Append("        <CostingCode5>5.BB</CostingCode5>");


                                    OJDT_Public.TDS_OBZ = "1.7";
                                    OJDT_Public.TDS_PAIS = "1";
                                    OJDT_Public.TDS_BU = validacao_Retorno_SAP.RetornaBU(Convert.ToInt32(Itens[i].BU));
                                    OJDT_Public.TDS_EMPRESA = "3.0";
                                    OJDT_Public.TDS_DEPARTAMENTO = "4.D";
                                    OJDT_Public.TDS_CENTROCUSTO = "5.BB";
                                }
                                stringBuilder.Append("      </row>");
                            }
                            else
                            {
                                throw new Exception("Aliquota de ICMS não existe na tabela de De-para de impostos TMS x SAP. Código: " + Itens[i].AliquotaICMS);
                            }
                        }
                        else
                        {
                            throw new Exception("Código do TMS informado não exite vinculado a nenhum item do SAP. Código: " + Itens[i].CodigoItem);
                        }
                    }
                    #endregion
                    stringBuilder.Append("    </DLN1>");
                    stringBuilder.Append("  </BO>");
                    stringBuilder.Append("</BOM>");


                    TextReader textReader = new StringReader(stringBuilder.ToString());
                    XDocument xmlDocument = XDocument.Load(textReader);


                    XDocument doc = XDocument.Parse(stringBuilder.ToString());


                    doc.Save(strFileXml);


                    SAPbobsCOM.Documents oDocuments = (SAPbobsCOM.Documents)oCompany.GetBusinessObjectFromXML(strFileXml, 0);


                    int iErrorSAP = 0;


                    iErrorSAP = oDocuments.Add();


                    if (iErrorSAP != 0)
                    {
                        throw new Exception(oCompany.GetLastErrorCode() + " - " + oCompany.GetLastErrorDescription());
                    }




                    Documents oDeliveryNotes = null;
                    string sODLN = string.Empty;
                    oCompany.GetNewObjectCode(out sODLN);
                    oDeliveryNotes.GetByKey(Convert.ToInt32(sODLN));


                    OJDT oJDT = new OJDT();


                    oJDT.GeraLCM(oCompany, oDeliveryNotes.DocDueDate, oDeliveryNotes.DocDate, oDeliveryNotes.TaxDate, oDeliveryNotes.DocNum, oDeliveryNotes.BPL_IDAssignedToInvoice, oDeliveryNotes.DocTotal, oDeliveryNotes.NumAtCard);


                    return iErrorSAP.ToString();
                }
                else
                {
                    throw new Exception("11-CNPJ com formato inválido: " + vCardCode);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                validacao_Retorno_SAP = null;
                oBP = null;


                if (oCompany != null)
                {
                    if (oCompany.Connected)
                    {
                        oCompany.Disconnect();
                    }


                    while ((System.Runtime.InteropServices.Marshal.ReleaseComObject(oCompany)) > 0) ;
                }


                //GC.Collect();
            }
        }
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Jan 26, 2017 at 01:22 AM

    Hi,

    Do you need to fill this line ?

    stringBuilder.Append("        <SeqCode>-1</SeqCode>");

    Regards

    Edy

    Add comment
    10|10000 characters needed characters exceeded