//public static int RetornoCodigoCFOP(string CFOP) //{ // int i = 0; // foreach (var item in Enum.GetValues(typeof(nfe.TCfop))) // { // if (item.ToString().Substring(item.ToString().Count() - 4, 4) == CFOP.ToString()) // { // break; // } // i += 1; // } // return i; //} public string GerarChaveAcessoNfe(int Loja, int CodFornecedor, string Uf, DateTime DtEmissao, string CnpjEmissor, string ModNfe, string Serie, string NumeroNf, string TpEmis, int Lote, out string cNF, out string cDV) { mNF = new Model_NotaFiscal(); mLote = new Model_Lote(); Random rd = new Random(); string CodigoRandom = string.Format("{0:00000000}", rd.Next(99999999)); List <int> arrayMult = new List <int>() { 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 }; string ChaveNfe = Uf + string.Format("{0:yyMM}", DtEmissao) + TiraCampos(CnpjEmissor) + ModNfe + Serie.PadLeft(3, '0') + NumeroNf.PadRight(9, '0') + TpEmis + CodigoRandom; int[] arryaCh = ChaveNfe.Select(x => Int32.Parse(x.ToString())).ToArray(); int soma = 0; int resto = 0; int DV = 0; for (int i = 0; i < arryaCh.Length; i++) { soma += arrayMult[i] * Convert.ToInt32(arryaCh[i]); } resto = soma % 11; cNF = CodigoRandom; if (resto == 0 || resto == 1) { cDV = "0"; if (CodFornecedor == 0) { mNF.UpdateChavedeAcessocNfe(Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Loja, 0, ChaveNfe + cDV.ToString(), CodigoRandom); mLote.UpdateChAcessoItemLote(Loja, Convert.ToInt32(NumeroNf), Serie, Lote, ChaveNfe + cDV.ToString()); } else { mNF.UpdateChavedeAcessocNfe(Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Loja, CodFornecedor, ChaveNfe + cDV.ToString(), CodigoRandom); mLote.UpdateChAcessoItemLote(Loja, Convert.ToInt32(NumeroNf), Serie, Lote, ChaveNfe + cDV.ToString()); } return(ChaveNfe += "0"); } else { DV = 11 - resto; cDV = DV.ToString(); if (CodFornecedor == 0) { mNF.UpdateChavedeAcessocNfe(Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Loja, 0, ChaveNfe + DV.ToString(), CodigoRandom); mLote.UpdateChAcessoItemLote(Loja, Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Lote, ChaveNfe + DV.ToString()); } else { mNF.UpdateChavedeAcessocNfe(Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Loja, CodFornecedor, ChaveNfe + DV.ToString(), CodigoRandom); mLote.UpdateChAcessoItemLote(Loja, Convert.ToInt32(NumeroNf), Serie == "9" ? "D1" : Serie, Lote, ChaveNfe + DV.ToString()); } return(ChaveNfe += DV.ToString()); } }