private int CadastrarEmpresa(string arqXML, int emp)
{
string cnpj = "";
string nomeEmp = "";
string servico = "";
bool temEmpresa = false;
if (Path.GetExtension(arqXML).ToLower() == ".xml")
{
XmlDocument doc = new XmlDocument();
doc.Load(arqXML);
XmlElement dadosEmpresa = (XmlElement)doc.GetElementsByTagName("DadosEmpresa")[0];
if (dadosEmpresa != null)
{
#region Nome da empresa
if (dadosEmpresa.GetElementsByTagName("Nome")[0] != null)
{
nomeEmp = dadosEmpresa.GetElementsByTagName("Nome")[0].InnerText;
temEmpresa = true;
}
else if (dadosEmpresa.GetElementsByTagName("nome")[0] != null)
{
nomeEmp = dadosEmpresa.GetElementsByTagName("nome")[0].InnerText;
temEmpresa = true;
}
#endregion
#region CNPJ
if (!String.IsNullOrEmpty(dadosEmpresa.GetAttribute(NFe.Components.TpcnResources.CNPJ.ToString())))
{
cnpj = dadosEmpresa.GetAttribute(NFe.Components.TpcnResources.CNPJ.ToString());
temEmpresa = true;
}
else if (!String.IsNullOrEmpty(dadosEmpresa.GetAttribute("cnpj")))
{
cnpj = dadosEmpresa.GetAttribute("cnpj");
temEmpresa = true;
}
else if (!String.IsNullOrEmpty(dadosEmpresa.GetAttribute("Cnpj")))
{
cnpj = dadosEmpresa.GetAttribute("Cnpj");
temEmpresa = true;
}
#endregion
#region Servico
if (!String.IsNullOrEmpty(dadosEmpresa.GetAttribute("Servico")))
{
servico = dadosEmpresa.GetAttribute("Servico");
temEmpresa = true;
}
else if (!String.IsNullOrEmpty(dadosEmpresa.GetAttribute("servico")))
{
servico = dadosEmpresa.GetAttribute("servico");
temEmpresa = true;
}
#endregion
}
}
else
{
List<string> cLinhas = Functions.LerArquivo(arqXML);
foreach (string texto in cLinhas)
{
string[] dados = texto.Split('|');
int nElementos = dados.GetLength(0);
if (nElementos <= 1)
continue;
switch (dados[0].ToLower())
{
case "nome":
nomeEmp = dados[1].Trim();
temEmpresa = true;
break;
case "cnpj":
cnpj = dados[1].Trim();
temEmpresa = true;
break;
case "servico":
servico = dados[1].Trim();
temEmpresa = true;
break;
}
}
}
if (temEmpresa)
{
if (string.IsNullOrEmpty(cnpj) || string.IsNullOrEmpty(nomeEmp) || string.IsNullOrEmpty(servico))
{
throw new Exception("Não foi possível localizar os dados da empresa no arquivo de configuração. (CNPJ/Nome ou Tipo de Serviço)");
}
if (Char.IsLetter(servico, 0))
{
var lista = NFe.Components.EnumHelper.ToStrings(typeof(TipoAplicativo));
if (!lista.Contains(servico))
throw new Exception(string.Format("Serviço deve ser ({0}, {1}, {2}, {3}, {4} ou {5})",
NFe.Components.EnumHelper.GetDescription(TipoAplicativo.Nfe),
NFe.Components.EnumHelper.GetDescription(TipoAplicativo.Cte),
NFe.Components.EnumHelper.GetDescription(TipoAplicativo.Nfse),
NFe.Components.EnumHelper.GetDescription(TipoAplicativo.MDFe),
NFe.Components.EnumHelper.GetDescription(TipoAplicativo.NFCe),
NFe.Components.EnumHelper.GetDescription(TipoAplicativo.Todos)));
///
/// veio como 'NFe, NFCe, CTe, MDFe ou NFSe
/// converte para numero correspondente
servico = ((int)NFe.Components.EnumHelper.StringToEnum<TipoAplicativo>(servico)).ToString();
}
else
{
if (!("0,1,2,3,4,10").Contains(servico))
{
throw new Exception(string.Format("Serviço deve ser ({0} p/{1}, {2} p/{3}, {4} p/{5}, {6} p/{7}, {8} p/{9} ou {10} p/{11})",
(int)TipoAplicativo.Nfe, NFe.Components.EnumHelper.GetDescription(TipoAplicativo.Nfe),
(int)TipoAplicativo.Cte, NFe.Components.EnumHelper.GetDescription(TipoAplicativo.Cte),
(int)TipoAplicativo.Nfse, NFe.Components.EnumHelper.GetDescription(TipoAplicativo.Nfse),
(int)TipoAplicativo.MDFe, NFe.Components.EnumHelper.GetDescription(TipoAplicativo.MDFe),
(int)TipoAplicativo.NFCe, NFe.Components.EnumHelper.GetDescription(TipoAplicativo.NFCe),
(int)TipoAplicativo.Todos, NFe.Components.EnumHelper.GetDescription(TipoAplicativo.Todos)));
}
}
if (Empresas.FindConfEmpresa(cnpj.Trim(), (TipoAplicativo)Convert.ToInt16(servico)) == null)
{
Empresa empresa = new Empresa();
empresa.CNPJ = cnpj;
empresa.Nome = nomeEmp;
empresa.Servico = (TipoAplicativo)Convert.ToInt16(servico);
Empresas.Configuracoes.Add(empresa);
//GravarArqEmpresas(); //tirado daqui pq ele somente grava quando a empresa for gravada com sucesso
}
return Empresas.FindConfEmpresaIndex(cnpj, (TipoAplicativo)Convert.ToInt16(servico));
}
else
{
return emp;
}
}
#endregion