Пример #1
0
        public ActionResult UploadRecords()
        {
            var idUsuario = SessionHelpers.GetIdUsuario();
            var data      = (System.Data.DataTable)TempData["dtEmp"];
            ImportacionMasivaEmpleados ctxImp = new ImportacionMasivaEmpleados();
            var sucursal = Session["Sucursal"] as SucursalDatos;

            ctxImp.UploadRecords(data, sucursal.IdSucursal, sucursal.IdCliente, idUsuario);
            return(RedirectToAction("Index", "Empleados"));
        }
Пример #2
0
        public FileResult DescargarPlantilla()
        {
            //Obtiene el nombre del usuario
            var            id       = SessionHelpers.GetIdUsuario();
            ControlUsuario cu       = new ControlUsuario();
            var            usuario  = cu.GetUsuarioById(id);
            var            userName = usuario == null ? "Alianza" : usuario.Usuario;

            //Se crea la carpeta temporal
            var path = Server.MapPath("~//Files/ImportacionMasiva/" + userName);

            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            else
            {
                System.IO.Directory.Delete(path, true);
                System.IO.Directory.CreateDirectory(path);
            }

            //Path del archivo nuevo en la carpeta temporal
            var fileName = Server.MapPath("~//Files/ImportacionMasiva/" + userName + "/ImportacionMasiva.xlsx");

            //Copia la plantilla a la carpeta temporal
            var pathPlantilla = Server.MapPath("~//Files/ImportacionMasiva/Plantilla.xlsx");

            System.IO.File.Copy(pathPlantilla, fileName);


            //se obtiene los datos de la sucursal
            var sucursal = Session["sucursal"] as SucursalDatos;

            //se genera el archivo
            ImportacionMasivaEmpleados ime = new ImportacionMasivaEmpleados();
            var status = ime.FormarPlantilla(fileName, sucursal.IdCliente, sucursal.IdSucursal, sucursal.Nombre);

            //Se lee el archivo
            byte[] fyleBytes = System.IO.File.ReadAllBytes(fileName);

            //Se crea un nuevo nombre de archivo para el usuario
            string newFileName = "Plantilla-Importacion-Masiva.xlsx";

            //se nombra la plantilla de excel
            var nombreArchivoPlantilla = "Plantilla-" + sucursal.Nombre + "-" + sucursal.Ciudad + ".xlsx";

            //Regresa el archivo
            return(File(fyleBytes, System.Net.Mime.MediaTypeNames.Application.Octet, nombreArchivoPlantilla));
        }
Пример #3
0
        public PartialViewResult UploadFile()
        {
            var file = Request.Files["UploadedImage"];
            var data = new System.Data.DataTable();
            List <ValidationEmpleado> empleadosValidacion = new List <ValidationEmpleado>();

            String[] warnings = new String[] {
                "",
                "EL ARCHIVO TIENE QUE SER EXCEL",
                "LA PLANTILLA NO CORRESPONDE A LA SUCURSAL EN LA CUAL SE QUIERE IMPORTAR",
                "EL ARCHIVO ESTA VACIO",
                "DOCUMENTO NO VALIDO"
            };
            int validation = 0;

            TempData["dtEmp"] = null;// limpia la variable para que no exista informacion previa guardada

            if (file != null)
            {
                ImportacionMasivaEmpleados ctxImp = new ImportacionMasivaEmpleados();
                var sucursal = Session["Sucursal"] as SucursalDatos;
                //valida que el documento sea un documento de .xls/.xlsx si no devuelve 1 y que el documento le pertenesca a la empresa si no devuelve 2.
                validation = ctxImp.ValidateExcel(file, sucursal.Nombre);

                if (validation == 0)
                {
                    //se verifica el datatable y los datos se validan/ devuelve una tupla con 3 items 1.-el DartaTable, 2.-la lista de los errore y advertencias, 3.-la lista de los enbezados de las coluimnas que presentan un error
                    var infoDatatable = ctxImp.ExcelToDataTable(file);
                    data = infoDatatable.Item2;

                    if (data.Rows.Count > 0)
                    {
                        empleadosValidacion = infoDatatable.Item1;
                        TempData["dtEmp"]   = empleadosValidacion.Count == 0 ? data : null;
                    }
                    else
                    {
                        validation = 3;
                    }
                }
            }
            else
            {
                validation = 3;
            }
            var resultado = Tuple.Create(empleadosValidacion, warnings[validation]);

            return(PartialView(resultado));
        }
Пример #4
0
        public async Task <JsonResult> UploadRecords()
        {
            var data = (System.Data.DataTable)TempData["dtEmp"];

            //si la variable global que contiene los datos de datatable es diferente a null
            if (data != null)
            {
                var idUsuario = SessionHelpers.GetIdUsuario();
                ImportacionMasivaEmpleados ctxImp = new ImportacionMasivaEmpleados();
                var sucursal = Session["Sucursal"] as SucursalDatos;
                //se verifica si hubo algun error al registrar a los empleados
                var resutadoUploadAsync = await ctxImp.UploadRecordsAsync(data, sucursal.IdSucursal, sucursal.IdCliente, idUsuario);

                if (resutadoUploadAsync)
                {
                    return(Json(new
                    {
                        type = "success",
                        msg = "DATOS REGISTRADOS"
                    }));
                    //return Json( "success", "DATOS REGISTRADOS" );
                }
                else
                {
                    return(Json(new
                    {
                        type = "danger",
                        msg = "OCURRIÓ UN ERROR AL INSERTAR LOS DATOS"
                    }));
                    //return Json("danger","OCURRIO UN ERROR AL INSERTAR LOS DATOS");
                }
            }
            else
            {
                return(Json(new
                {
                    type = "danger",
                    msg = "ACCION NO VALIDA"
                }));
                //return Json("danger","ACCION NO VALIDA");
            }
        }
Пример #5
0
        public PartialViewResult UploadFile()
        {
            var file       = System.Web.HttpContext.Current.Request.Files["UploadedImage"];
            var data       = new System.Data.DataTable();
            var validation = 1;

            if (file != null)
            {
                var sucursal = Session["Sucursal"] as SucursalDatos;
                if (sucursal == null)
                {
                    return(PartialView());
                }

                ImportacionMasivaEmpleados ctxImp = new ImportacionMasivaEmpleados();
                if (!file.FileName.EndsWith(".xls"))
                {
                    validation = ctxImp.ValidateExcel(file, sucursal.Nombre);
                }

                if (validation == 1)
                {
                    data = ctxImp.ExcelToDataTable(file);
                    if (data != null)
                    {
                        data = ctxImp.LimpiarTabla(data);

                        DataView dv = data.DefaultView;
                        dv.Sort = "Paterno asc";//desc

                        DataTable sortedDT = dv.ToTable();
                        data = sortedDT;
                        TempData["dtEmp"] = data;
                    }
                    else
                    {
                        validation = 2;
                    }
                }
            }
            else
            {
                validation = 2;
            }
            switch (validation)
            {
            case 1:
                ViewBag.Error = "El archivo está vacío";
                break;

            case 2:
                ViewBag.Error = "El archivo está vacío!";
                break;

            case 3:
                ViewBag.Error = "No es un archivo de excel!";
                break;

            case 4:
                ViewBag.Error = "La plantilla no corresponde al Cliente en el que desea importar los datos!";
                break;

            default:
                break;
            }
            return(PartialView(data));
        }