Exemplo n.º 1
0
        public ActionResult AsistenteCuentas()
        {
            AsistenteCuentasModel model = new AsistenteCuentasModel();

            model.Iso = new List <SelectListItem> {
                new SelectListItem {
                    Text = "Iso alfanumérico 2", Value = "CodigoIsoAlfa2"
                },
                new SelectListItem {
                    Text = "Iso alfanumérico 3", Value = "CodigoIsoAlfa3"
                },
                new SelectListItem {
                    Text = "Iso numérico", Value = "CodigoIsoNumerico"
                }
            };

            return(View("AsistenteCuentas", model));
        }
Exemplo n.º 2
0
        public ActionResult AsistenteCuentas(AsistenteCuentasModel model)
        {
            var idPeticion = 0;

            // Para que no de error al devolver la vista, en un futuro cambiar esto
            model.Iso = new List <SelectListItem> {
                new SelectListItem {
                    Text = "Iso alfanumérico 2", Value = "CodigoIsoAlfa2"
                },
                new SelectListItem {
                    Text = "Iso alfanumérico 3", Value = "CodigoIsoAlfa3"
                },
                new SelectListItem {
                    Text = "Iso numérico", Value = "CodigoIsoNumerico"
                }
            };

            var    file        = model.Fichero;
            char   delimitador = model.Delimitador.ToCharArray()[0];
            string iso         = model.SelectedId;

            if (ModelState.IsValid)
            {
                if (file != null && file.ContentLength > 0)
                {
                    if (file.FileName.ToLower().EndsWith(".csv") || file.FileName.ToLower().EndsWith(".CSV"))
                    {
                        var           service = FService.Instance.GetService(typeof(CuentasModel), ContextService) as CuentasService;
                        StreamReader  sr      = new StreamReader(file.InputStream);
                        StringBuilder sb      = new StringBuilder();
                        DataTable     dt      = new DataTable();
                        DataRow       dr;
                        string        s;
                        int           j = 0;

                        dt.Columns.Add("Cuenta");
                        dt.Columns.Add("Descripcion");
                        dt.Columns.Add("Razonsocial");
                        dt.Columns.Add("Nif");
                        dt.Columns.Add("TipoNif");
                        dt.Columns.Add(iso);

                        while (!sr.EndOfStream)
                        {
                            while ((s = sr.ReadLine()) != null)
                            {
                                //Ignorar cabecera
                                if (j > 0 || !model.Cabecera)
                                {
                                    string[] str = s.Split(delimitador);
                                    dr = dt.NewRow();

                                    for (int i = 0; i < dt.Columns.Count; i++)
                                    {
                                        try {
                                            dr[dt.Columns[i]] = str[i].Replace("\"", string.Empty).ToString();
                                        }
                                        catch (Exception ex)
                                        {
                                            ModelState.AddModelError("File", General.ErrorDelimitadorFormato);
                                            return(View("AsistenteCuentas", model));
                                        }
                                    }
                                    dt.Rows.Add(dr);
                                }

                                j++;
                            }
                        }
                        try
                        {
                            idPeticion = service.CrearPeticionImportacion(ContextService);
                            HostingEnvironment.QueueBackgroundWorkItem(async token => await GetAsync(dt, idPeticion, token));
                            //service.Importar(dt, ContextService);
                        }
                        catch (ValidationException ex)
                        {
                            if (string.IsNullOrEmpty(ex.Message))
                            {
                                TempData["Errors"] = null;
                            }
                            else
                            {
                                TempData["Errors"] = ex.Message;
                            }
                        }
                        sr.Close();

                        //TempData["Success"] = "Importado correctamente!";
                        TempData["Success"] = "Ejecutando, proceso con id = " + idPeticion.ToString() + ", para comprobar su ejecución ir al menú de peticiones asíncronas";
                        return(RedirectToAction("AsistenteCuentas", "Cuentas"));
                    }
                    else
                    {
                        ModelState.AddModelError("File", General.ErrorFormatoFichero);
                    }
                }
                else
                {
                    ModelState.AddModelError("File", General.ErrorFichero);
                }
            }

            return(View("AsistenteCuentas", model));
        }