public ActionResult Entrada(int id)
        {
            var notaCliente = _notaCliente.Find(id);

            if (notaCliente == null)
            {
                throw new Exception("Dados não encontrados");
            }

            if (notaCliente.num_nota > 0)
            {
                return(RedirectToAction("List"));
            }



            var data = new NotaEntradaModel()
            {
                NotaCliente  = notaCliente,
                ItensNota    = _notaItemCliente.GetAll(a => a.NotaClienteId == notaCliente.Id).ToList(),
                cod_operacao = "",
                obs          = " Nota importada via Garantia " + id.ToString(),
                dta_emissao  = System.DateTime.Now.Date.ToShortDateString(),
                dta_entrada  = System.DateTime.Now.Date.ToShortDateString(),
                cod_serie    = "1",
                id           = id,
                //cod_condicao_pgto = 0,
                //cod_transportador = 1,
                des_chave  = "",
                des_local  = "10",
                des_lote   = id.ToString(),
                des_modelo = "55",
                ind_nfe    = true,
                msg        = "",
                //  num_nota = 0,
                //,
                //num_nota = 1234,
                //
                //obs = "Nota de Teste",
                //msg = "Inicio da Importação de Notas"
            };

            ViewBag.cod_operacao = new SelectList(_operacoes.GetAll().OrderBy(a => a.Des_Operacao), "Cod_Operacao", "Des_Operacao", data.cod_operacao);

            return(View(data));
        }
        public ActionResult Entrada(NotaEntradaModel data)
        {
            var notaCliente       = _notaCliente.Find(data.id);
            var orderItemCustomer = _notaItemCliente.GetAll(a => a.NotaClienteId == notaCliente.Id).ToList();

            ViewBag.cod_operacao = new SelectList(_operacoes.GetAll().OrderBy(a => a.Des_Operacao), "Cod_Operacao", "Des_Operacao", data.cod_operacao);

            data.NotaCliente = notaCliente;
            data.ItensNota   = orderItemCustomer;

            TryUpdateModel(data);
            if (!ModelState.IsValid)
            {
                var messages = ModelState.Values
                               .SelectMany(x => x.Errors)
                               .Select(x => x.ErrorMessage);

                ModelState.AddModelError("Id", "NF Inválida para Entrada");
                return(Json(new { data = data, Msg = messages, hasError = true }));
            }
            var cliente = _clientes.Find(data.NotaCliente.Garantia.cod_cliente);

            // tp_operacao choice;


            // Enum.TryParse(data.tp_operacao.ToString(), out choice);
            // uint value = (uint)choice;



            var chaves = new Ne_Nota_Chave()
            {
                COD_EMP           = 1,
                COD_PESSOA_FORN   = data.NotaCliente.Garantia.cod_cliente.ToString(),
                COD_SERIE         = data.cod_serie,
                COD_UNIDADE       = cliente.CD_REGIONAL,
                DTA_EMISSAO       = Convert.ToDateTime(data.dta_emissao),
                NUM_NOTA          = data.num_nota.Value,
                COD_OPERACAO      = data.cod_operacao,
                COD_TRANSPORTADOR = data.cod_transportador,
                DTA_ENTRADA       = System.DateTime.Now,
                OBS = data.obs,
                COD_CONDICAO_PGTO = data.cod_condicao_pgto,
                DES_CHAVE         = data.des_chave,
                DES_LOCAL         = data.des_local,
                DES_LOTE          = data.des_lote,
                DES_MODELO        = data.des_modelo,
                IND_NFE           = data.ind_nfe ? 1 : 0,
                TP_OPERACAO       = 2//(int)value
            };



            try
            {
                string comand = $" Begin spcDeleteNFEntrada " +
                                $" ({chaves.COD_EMP}, \'{chaves.COD_PESSOA_FORN}\' , \'{chaves.COD_SERIE}\',{chaves.COD_UNIDADE}, \'{chaves.DTA_EMISSAO.ToShortDateString()}\',  \'{Convert.ToDateTime(data.dta_entrada).ToShortDateString()}\', {chaves.NUM_NOTA}); end;";

                db.Database.ExecuteSqlCommand(comand);
            }
            catch (Exception e)
            {
                return(Json(new { data = data, Msg = e.Message, hasError = true }));
            }


            IList <NE_NOTA_MENSAGEM> listamsg = null;

            try
            {
                db.Ne_Nota_Chave.Add(chaves);

                string comand = $" Begin spcInsertNFEntrada " +
                                $" ( {data.id}, {chaves.COD_EMP}, \'{chaves.COD_PESSOA_FORN}\' , \'{chaves.COD_SERIE}\',{chaves.COD_UNIDADE}, \'{chaves.DTA_EMISSAO.ToShortDateString()}\',  \'{Convert.ToDateTime(data.dta_entrada).ToShortDateString()}\', {chaves.NUM_NOTA}); end;";

                chaves.CMD = comand;
                db.SaveChanges();


                db.Database.ExecuteSqlCommand(comand);


                listamsg = db.NE_NOTA_MENSAGEM.Where(a => a.NUM_NOTA == chaves.NUM_NOTA &&
                                                     a.COD_SERIE == chaves.COD_SERIE &&
                                                     a.COD_PESSOA_FORN == chaves.COD_PESSOA_FORN &&
                                                     a.COD_UNIDADE == chaves.COD_UNIDADE).ToList();

                return(Json(new { data = data, Msg = listamsg.OrderBy(a => a.ID_MENSAGEM), hasError = listamsg.Select(a => a.HASERROR).FirstOrDefault() == 1 }));
            }
            catch (Exception e)
            {
                string msg = e.InnerException.Message;
                string details;

                try
                {
                    details = string.IsNullOrEmpty(e.InnerException.InnerException.Message) ? "" : e.InnerException.InnerException.Message;
                }
                catch (Exception)
                {
                    details = "";
                }


                listamsg.Add(new NE_NOTA_MENSAGEM()
                {
                    MSG = details
                });
                listamsg.Add(new NE_NOTA_MENSAGEM()
                {
                    MSG = msg
                });


                return(Json(new { data = data, Msg = listamsg, hasError = true }));
            }
        }