예제 #1
0
        public JsonResult AgregarAjustesObjecion(string sentido, string sociedad, string trafico,
                                                 string servicio, string deudorAcreedor, string operador,
                                                 string grupo, DateTime periodo, decimal importe,
                                                 string moneda, int lineaNegocio)
        {
            object   respuesta      = null;
            DateTime fecha_contable = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

            try {
                ajustesObjecion abj = new ajustesObjecion();

                abj.sentido         = sentido;
                abj.sociedad        = sociedad;
                abj.trafico         = trafico;
                abj.servicio        = servicio;
                abj.deudorAcreedor  = deudorAcreedor;
                abj.operador        = operador;
                abj.grupo           = grupo;
                abj.periodo         = new DateTime(periodo.Year, periodo.Month, 1);
                abj.importe         = importe;
                abj.moneda          = moneda;
                abj.activo          = 1;
                abj.lineaNegocio    = lineaNegocio;
                abj.periodoContable = fecha_contable;
                db.ajustesObjecion.Add(abj);
                Log log = new Log();
                log.insertaNuevoOEliminado(abj, "Nuevo", "ajustesObjecion.html", Request.UserHostAddress);
                db.SaveChanges();
                respuesta = new { success = true, results = "ok" };
                db.spAjustesObjecion(1, lineaNegocio);
            } catch (Exception) {
                respuesta = new { success = false, results = "Hubo un error al momento de realizar la petición." };
            }

            return(Json(respuesta, JsonRequestBehavior.AllowGet));
        }
예제 #2
0
        public JsonResult CargarCSV(HttpPostedFileBase archivoCSV, int lineaNegocio)
        {
            List <string>        listaErrores = new List <string>();
            IEnumerable <string> lineas       = null;
            object   respuesta       = null;
            int      totalProcesados = 0;
            int      lineaActual     = 2;
            bool     status          = false;
            string   exception       = "Error, se presento un error inesperado.";
            DateTime fechaContable   = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

            try {
                /// Se salta la primera linea, el encabezado
                List <string> csvData = new List <string>();
                using (System.IO.StreamReader reader = new System.IO.StreamReader(archivoCSV.InputStream, Encoding.Default)) {
                    while (!reader.EndOfStream)
                    {
                        csvData.Add(reader.ReadLine());
                    }
                }

                lineas = csvData.Skip(1);


                totalProcesados = lineas.Count();
                using (TransactionScope scope = new TransactionScope()) {
                    foreach (string linea in lineas)
                    {
                        var lineaSplit = linea.Split(';');
                        if (lineaSplit.Count() == 10)
                        {
                            try {
                                ajustesObjecion aObj = new ajustesObjecion();

                                aObj.sentido        = lineaSplit[0];
                                aObj.sociedad       = lineaSplit[1];
                                aObj.trafico        = lineaSplit[2];
                                aObj.servicio       = lineaSplit[3];
                                aObj.deudorAcreedor = lineaSplit[4];
                                aObj.operador       = lineaSplit[5];
                                aObj.grupo          = lineaSplit[6];

                                aObj.periodo         = Convert.ToDateTime(lineaSplit[7]);
                                aObj.importe         = decimal.Parse(lineaSplit[8]);
                                aObj.moneda          = lineaSplit[9];
                                aObj.lineaNegocio    = lineaNegocio;
                                aObj.periodoContable = fechaContable;

                                var result = db.spValidaAjustesObj(aObj.sentido, aObj.sociedad, aObj.trafico,
                                                                   aObj.servicio, aObj.deudorAcreedor, aObj.operador, aObj.grupo, lineaNegocio).ToList();

                                aObj.idSociedad       = result[0].idSociedad;
                                aObj.idTrafico        = result[0].idTrafico;
                                aObj.idServicio       = result[0].idServicio;
                                aObj.idDeudorAcreedor = result[0].idDeudorAcreedor;
                                aObj.idOperador       = result[0].idOperador;
                                aObj.idGrupo          = result[0].idGrupo;

                                if (result[0].idStatus == 1)
                                {
                                    aObj.activo = 1;
                                }
                                else
                                {
                                    aObj.activo = 0;
                                }

                                db.ajustesObjecion.Add(aObj);
                                Log log = new Log();
                                log.insertaNuevoOEliminado(aObj, "Nuevo", "ajustesObjecion.html", Request.UserHostAddress);
                            } catch (FormatException) {
                                listaErrores.Add("Línea " + lineaActual + ": Campo con formato erróneo.");
                            }
                        }
                        else
                        {
                            listaErrores.Add("Línea " + lineaActual + ": Número de campos insuficiente.");
                        }
                        ++lineaActual;
                    }
                    // Termina exitosamente la transaccion
                    db.SaveChanges();
                    scope.Complete();
                    exception = "Datos cargados con éxito";
                    status    = true;
                }
            } catch (FileNotFoundException) {
                exception = "El archivo Selecionado aún no existe en el Repositorio.";
                status    = false;
            } catch (UnauthorizedAccessException) {
                exception = "No tiene permiso para acceder al archivo actual.";
                status    = false;
            } catch (IOException e) when((e.HResult & 0x0000FFFF) == 32)
            {
                exception = "Falta el nombre del archivo, o el archivo o directorio está en uso.";
                status    = false;
            } catch (TransactionAbortedException) {
                exception = "Transacción abortada. Se presentó un error.";
                status    = false;
            } finally {
                respuesta = new
                {
                    success = true,
                    results = listaErrores,
                    mensaje = exception,
                    totalProcesados,
                    status
                };
            }

            return(Json(respuesta, JsonRequestBehavior.AllowGet));
        }