public ActionResult crearMatricula(int id, int tramitador_id, int cartera, string placa, int valor, string fechamatricula, string observacion, encab_documento encabezado)/*cartera,tramitador*/ { placaVh(id, placa, fechamatricula); string mensaje = ""; int iduser = Convert.ToInt32(Session["user_usuarioid"]); icb_tpeventos tpevento = context.icb_tpeventos.Where(a => a.tpevento_nombre == "Matriculado").FirstOrDefault(); vw_pendientesAlistamiento x = context.vw_pendientesAlistamiento.FirstOrDefault(t => t.id == id); icb_terceros tercero = context.icb_terceros.Where(b => b.doc_tercero == x.doc_tercero).FirstOrDefault(); icb_vehiculo vh = context.icb_vehiculo.Where(c => c.plan_mayor == x.plan_mayor).FirstOrDefault(); icb_vehiculo_eventos ve = new icb_vehiculo_eventos { planmayor = x.planmayor, eventofec_creacion = DateTime.Now, eventouserid_creacion = iduser, evento_nombre = "Matriculado", evento_estado = true, bodega_id = x.bodega, id_tpevento = tpevento.tpevento_id, fechaevento = DateTime.Now, terceroid = tercero.tercero_id, placa = placa, vin = vh.vin, evento_observacion = observacion, cartera_id = cartera,/*cartera*/ }; if (encabezado.idencabezado != 0) { ve.idencabezado = encabezado.idencabezado; encabezado.id_pedido_vehiculo = id; vh.tramitador_id = tramitador_id;/*tramitador*/ } context.icb_vehiculo_eventos.Add(ve); try { context.SaveChanges(); } catch (DbEntityValidationException e) { mensaje = e.Message; } return(Json(valor, JsonRequestBehavior.AllowGet)); }
public ActionResult Index(HttpPostedFileBase excelfile, int?menu) { int items = 0; string log = ""; int itemsCorrectos = 0; int itemsFallidos = 0; string nombreArchivo = excelfile.FileName; bool agregarArchivo = false; int idUltimoArchTomBat = 0; int tomaBateriaId = 0; try { tomaBateriaId = Convert.ToInt32(Request["tomaBateriaExcel"]); } catch (FormatException) { //Validacion para cuando no se asigna una toma de bateria al importar el archivo excel } int codigoBodega = 0; try { codigoBodega = Convert.ToInt32(Request["bodegaExcel"]); } catch (FormatException) { //Validacion para cuando no se asigna una bodega al importar el archivo excel } if (excelfile == null || excelfile.ContentLength == 0) { TempData["mensajeCargue"] = "El archivo esta vacio o no es un archivo valido!"; ViewBag.tomaBateria = context.icb_toma_baterias; ViewBag.bodegas = context.bodega_concesionario; BuscarFavoritos(menu); return(View()); } icb_arch_tombat buscaNombre = context.icb_arch_tombat.FirstOrDefault(x => x.arch_tombat_nombre == excelfile.FileName); if (buscaNombre != null) { TempData["mensajeCargue"] = "El nombre del archivo ya se encuentra registrado, verifique que el archivo no ha sido cargado antes o cambie de nombre!"; ViewBag.tomaBateria = context.icb_toma_baterias; ViewBag.bodegas = context.bodega_concesionario; BuscarFavoritos(menu); return(View()); } if (excelfile.FileName.EndsWith("xls") || excelfile.FileName.EndsWith("xlsx")) { string path = Server.MapPath("~/Content/" + excelfile.FileName); // Validacion para cuando el archivo esta en uso y no puede ser usado desde visual try { if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } catch (IOException) { TempData["mensajeCargue"] = "El archivo esta siendo usado por otro proceso, asegurece de cerrarlo o cree una copia del archivo e intente de nuevo!"; ViewBag.tomaBateria = context.icb_toma_baterias; ViewBag.bodegas = context.bodega_concesionario; BuscarFavoritos(menu); return(View()); } excelfile.SaveAs(path); // Read data from excel file Application application = new Application(); Workbook workbook = application.Workbooks.Open(path); Worksheet worksheet = workbook.ActiveSheet; Range range = worksheet.UsedRange; items = range.Rows.Count; // Se hace la validacion para agregar solo una vez el archivo de facturacion con el total de registros que existen if (!agregarArchivo) { icb_arch_tombat archivoTomBat = new icb_arch_tombat { arch_tombat_nombre = nombreArchivo, arch_tombat_fecha = DateTime.Now, items = items - 1, toma_bateria_id = tomaBateriaId }; context.icb_arch_tombat.Add(archivoTomBat); bool resultArchTombat = context.SaveChanges() > 0; if (resultArchTombat) { idUltimoArchTomBat = context.icb_arch_tombat.OrderByDescending(x => x.arch_tombat_id).First() .arch_tombat_id; } agregarArchivo = true; } byte[] fileBytes = new byte[excelfile.ContentLength]; int data = excelfile.InputStream.Read(fileBytes, 0, Convert.ToInt32(excelfile.ContentLength)); for (int row = 2; row <= range.Rows.Count; row++) { icb_vehiculo_eventos nuevoRegistroTomaBateria = new icb_vehiculo_eventos(); dynamic capturaFecha = ((Range)range.Cells[row, 12]).Text; //DateTime resultFecha = DateTime.Parse(capturaFecha); DateTime parseo = new DateTime(); try { parseo = DateTime.Parse(capturaFecha); nuevoRegistroTomaBateria.eventofec_creacion = parseo; nuevoRegistroTomaBateria.eventouserid_creacion = Convert.ToInt32(Session["user_usuarioid"]); string VoltajeBateria = ((Range)range.Cells[row, 2]).Text; ; string CCATomado = ((Range)range.Cells[row, 3]).Text; ; string CCABateria = ((Range)range.Cells[row, 4]).Text; string observacion = "CCA Bateria = " + CCABateria + " , Voltaje Bateria = " + VoltajeBateria + " ,CCA Tomado = " + CCATomado; string decision = ((Range)range.Cells[row, 7]).Text; string vin = ((Range)range.Cells[row, 14]).Text; icb_vehiculo buscarVin = context.icb_vehiculo.FirstOrDefault(x => x.vin == vin); if (buscarVin != null) { icb_vehiculo_bateria buscarTomaExiste = context.icb_vehiculo_bateria.FirstOrDefault(x => x.veh_bat_vhid == buscarVin.icbvh_id && x.veh_bat_toma_id == tomaBateriaId); if (buscarTomaExiste != null) { itemsFallidos++; if (log.Length < 1) { log += vin + "*El VIN ya registro inspeccion de bateria"; } else { log += "|" + vin + "*El VIN ya registro inspeccion de bateria"; } } else { if (buscarVin.icbvh_estatus == "0") { itemsFallidos++; if (log.Length < 1) { log += vin + "*El VIN se encuentra en estado 0"; } else { log += "|" + vin + "*El VIN se encuentra en estado 0"; } } else { itemsCorrectos++; nuevoRegistroTomaBateria.vin = vin; nuevoRegistroTomaBateria.evento_nombre = "Toma Bateria"; nuevoRegistroTomaBateria.evento_estado = true; //nuevoRegistroTomaBateria.id_vehiculo = buscarVin.icbvh_id; nuevoRegistroTomaBateria.bodega_id = codigoBodega; icb_sysparameter buscarParametroTpEvento = context.icb_sysparameter.FirstOrDefault(x => x.syspar_cod == "P12"); string tpEventoParametro = buscarParametroTpEvento != null ? buscarParametroTpEvento.syspar_value : "6"; nuevoRegistroTomaBateria.id_tpevento = Convert.ToInt32(tpEventoParametro); nuevoRegistroTomaBateria.evento_observacion = observacion; context.icb_vehiculo_eventos.Add(nuevoRegistroTomaBateria); bool guardarEventoVehiculo = context.SaveChanges() > 0; context.icb_vehiculo_bateria.Add(new icb_vehiculo_bateria { veh_bat_fecha = parseo, veh_bat_toma_id = tomaBateriaId, veh_bat_vhid = buscarVin.icbvh_id, veh_bat_vin = buscarVin.vin, veh_bat_archid = idUltimoArchTomBat, veh_bat_decision = decision }); bool guardarTipoTomaBateria = context.SaveChanges() > 0; if (guardarEventoVehiculo && guardarTipoTomaBateria) { buscarVin.icbvh_fecing_bateria = DateTime.Now; context.Entry(buscarVin).State = EntityState.Modified; context.SaveChanges(); } } } } else { itemsFallidos++; if (log.Length < 1) { log += vin + "*El VIN no esta registrado en base de datos"; } else { log += "|" + vin + "*El VIN no esta registrado en base de datos"; } } } catch (FormatException) { //excelfile.InputStream.Close(); //excelfile.InputStream.Dispose(); //System.IO.File.Delete(path); //TempData["mensajeError"] = "Verifique que los campos de excel no tengan valores vacios o mal escritos!"; //return View(); } } // Fin del ciclo FOR workbook.Close(0); application.Quit(); } icb_arch_tombat_log archLog = new icb_arch_tombat_log { tombat_log_fecha = DateTime.Now, tombat_log_itemscorrecto = itemsCorrectos, tombat_log_itemserror = itemsFallidos, tombat_log_nombrearchivo = nombreArchivo, tombat_log_items = items - 1, tombat_log_log = log, id_arch_tombat = idUltimoArchTomBat }; context.icb_arch_tombat_log.Add(archLog); context.SaveChanges(); ViewBag.tomaBateria = context.icb_toma_baterias; ViewBag.bodegas = context.bodega_concesionario; BuscarFavoritos(menu); return(View()); }