private void dgvItems_CellLeave(object sender, DataGridViewCellEventArgs e) { if (dgvItems.EditingControl == null) { return; } if (dgvItems[e.ColumnIndex, e.RowIndex].OwningColumn.Name == "clmArt") { ArticuloPlanta artPla = Global.Servicio.BuscarUnArticuloPlanta(dgvItems.EditingControl.Text); if (artPla != null) { dgvItems.Rows[e.RowIndex].Tag = artPla; dgvItems.EditingControl.Text = artPla.Planta.codigo.Trim() + artPla.contador.ToString().Trim(); dgvItems[dgvItems.Columns["clmDesc"].Index, e.RowIndex].Value = artPla.TipoArticulo.nombre; dgvItems[dgvItems.Columns["clmUnidad"].Index, e.RowIndex].Value = artPla.TipoArticulo.Unidad.abreviatura; dgvItems[dgvItems.Columns["clmMon"].Index, e.RowIndex].Value = ((Moneda)((ComboBoxItem)cboMoneda.SelectedItem).Value).simbologia; dgvItems[dgvItems.Columns["clmPrecio"].Index, e.RowIndex].Value = Global.Servicio.ConviertePrecio(artPla.precio, artPla.Moneda, (Moneda)((ComboBoxItem)cboMoneda.SelectedItem).Value); dgvItems.Rows[e.RowIndex].Cells["clmPrecio"].Tag = null; if (dgvItems[dgvItems.Columns["clmDesc"].Index, e.RowIndex].FormattedValue != "") { calcularImportes(decimal.Parse(dgvItems[dgvItems.Columns["clmCant"].Index, e.RowIndex].FormattedValue.ToString()), decimal.Parse(dgvItems[dgvItems.Columns["clmPrecio"].Index, e.RowIndex].Value.ToString()), e.RowIndex); } calcularPeso(); } else { dgvItems.Rows[e.RowIndex].Tag = null; dgvItems[dgvItems.Columns["clmDesc"].Index, e.RowIndex].Value = ""; dgvItems[dgvItems.Columns["clmCant"].Index, e.RowIndex].Value = "0"; dgvItems[dgvItems.Columns["clmUnidad"].Index, e.RowIndex].Value = ""; dgvItems[dgvItems.Columns["clmMon"].Index, e.RowIndex].Value = ""; dgvItems[dgvItems.Columns["clmPrecio"].Index, e.RowIndex].Value = ""; dgvItems[dgvItems.Columns["clmImp"].Index, e.RowIndex].Value = "0"; calcularImportes(0, 0, e.RowIndex); calcularPeso(); } } if (dgvItems[e.ColumnIndex, e.RowIndex].OwningColumn.Name == "clmDesc") { ArticuloPlanta artPla = Global.Servicio.BuscarUnArticuloPlantaXDesc(planta, dgvItems.EditingControl.Text); if (artPla != null) { dgvItems.Rows[e.RowIndex].Tag = artPla; dgvItems[dgvItems.Columns["clmArt"].Index, e.RowIndex].Value = artPla.Planta.codigo.Trim() + artPla.contador.ToString().Trim(); dgvItems.EditingControl.Text = artPla.TipoArticulo.nombre; dgvItems[dgvItems.Columns["clmUnidad"].Index, e.RowIndex].Value = artPla.TipoArticulo.Unidad.abreviatura; dgvItems[dgvItems.Columns["clmMon"].Index, e.RowIndex].Value = ((Moneda)((ComboBoxItem)cboMoneda.SelectedItem).Value).simbologia; dgvItems[dgvItems.Columns["clmPrecio"].Index, e.RowIndex].Value = Global.Servicio.ConviertePrecio(artPla.precio, artPla.Moneda, (Moneda)((ComboBoxItem)cboMoneda.SelectedItem).Value); dgvItems.Rows[e.RowIndex].Cells["clmPrecio"].Tag = null; calcularPeso(); if (dgvItems[dgvItems.Columns["clmArt"].Index, e.RowIndex].FormattedValue != "") { calcularImportes(decimal.Parse(dgvItems[dgvItems.Columns["clmCant"].Index, e.RowIndex].FormattedValue.ToString()), decimal.Parse(dgvItems[dgvItems.Columns["clmPrecio"].Index, e.RowIndex].Value.ToString()), e.RowIndex); } } else { dgvItems.Rows[e.RowIndex].Tag = null; dgvItems[dgvItems.Columns["clmArt"].Index, e.RowIndex].Value = ""; dgvItems[dgvItems.Columns["clmCant"].Index, e.RowIndex].Value = "0"; dgvItems[dgvItems.Columns["clmUnidad"].Index, e.RowIndex].Value = ""; dgvItems[dgvItems.Columns["clmMon"].Index, e.RowIndex].Value = ""; dgvItems[dgvItems.Columns["clmPrecio"].Index, e.RowIndex].Value = ""; dgvItems[dgvItems.Columns["clmImp"].Index, e.RowIndex].Value = "0"; calcularImportes(0, 0, e.RowIndex); calcularPeso(); } } if (dgvItems[e.ColumnIndex, e.RowIndex].OwningColumn.Name == "clmCant") { decimal val; if (decimal.TryParse(dgvItems.EditingControl.Text, out val)) { if (dgvItems[dgvItems.Columns["clmPrecio"].Index, e.RowIndex].FormattedValue != "") { calcularImportes(val, decimal.Parse(dgvItems[dgvItems.Columns["clmPrecio"].Index, e.RowIndex].Value.ToString()), e.RowIndex); calcularPeso(); } } else { dgvItems.EditingControl.Text = "0"; calcularImportes(0, 0, e.RowIndex); } } if (dgvItems[e.ColumnIndex, e.RowIndex].OwningColumn.Name == "clmLote") { Lote lote = Global.Servicio.obtenerLote(dgvItems.EditingControl.Text); dgvItems[e.ColumnIndex, e.RowIndex].Tag = lote; if (lote == null) { dgvItems.EditingControl.Text = ""; } } if (dgvItems[e.ColumnIndex, e.RowIndex].OwningColumn.Name == "clmPresent") { string[] textoCelda = dgvItems.EditingControl.Text.Split(new string[] { "x " }, StringSplitOptions.None); int litros = 0; if (textoCelda.Count() == 2) { int.TryParse(textoCelda[1], out litros); } if (litros != 0) { Presentacion present = Global.Servicio.obtenerPresentacionPorLitros(litros); if (present != null) { dgvItems[e.ColumnIndex, e.RowIndex].Tag = present; ArticuloPlanta artPla = (ArticuloPlanta)dgvItems.Rows[e.RowIndex].Tag; if (artPla != null) { PreciosAdicionales precio = artPla.PreciosAdicionales.Where(p => p.idPresentacion == present.id).SingleOrDefault(); if (precio != null) { dgvItems.Rows[e.RowIndex].Cells["clmPrecio"].Tag = precio.precio; dgvItems.Rows[e.RowIndex].Cells["clmPrecio"].Value = precio.precio.ToString("0.00"); calcularImportes(decimal.Parse(dgvItems[dgvItems.Columns["clmCant"].Index, e.RowIndex].FormattedValue.ToString()), decimal.Parse(dgvItems[dgvItems.Columns["clmPrecio"].Index, e.RowIndex].Value.ToString()), e.RowIndex); } } } else { dgvItems[e.ColumnIndex, e.RowIndex].Tag = null; dgvItems.EditingControl.Text = ""; } } else { dgvItems[e.ColumnIndex, e.RowIndex].Tag = null; dgvItems.EditingControl.Text = ""; } } if (dgvItems[e.ColumnIndex, e.RowIndex].OwningColumn.Name == "clmPrecio") { ArticuloPlanta artPla = (ArticuloPlanta)dgvItems.Rows[e.RowIndex].Tag; if (artPla != null) { decimal val; if (decimal.TryParse(dgvItems.EditingControl.Text, out val)) { var precios = artPla.PreciosAdicionales.Select(p => p.precio).ToList(); precios.Add(artPla.precio); if (precios.Contains(val)) { dgvItems.Rows[e.RowIndex].Cells["clmPrecio"].Tag = val; calcularImportes(decimal.Parse(dgvItems[dgvItems.Columns["clmCant"].Index, e.RowIndex].FormattedValue.ToString()), decimal.Parse(dgvItems.EditingControl.Text), e.RowIndex); var present = artPla.PreciosAdicionales.Where(p => p.precio.ToString("0.00") == val.ToString("0.00")).SingleOrDefault(); if (present != null) { dgvItems.Rows[e.RowIndex].Cells["clmPresent"].Tag = present.Presentacion; dgvItems.Rows[e.RowIndex].Cells["clmPresent"].Value = "x " + present.Presentacion.litrosEnvase.ToString(); } } else { dgvItems.Rows[e.RowIndex].Cells["clmPrecio"].Tag = 0; dgvItems.EditingControl.Text = "0.00"; calcularImportes(0, 0, e.RowIndex); } } else { dgvItems.Rows[e.RowIndex].Cells["clmPrecio"].Tag = 0; dgvItems.EditingControl.Text = "0.00"; calcularImportes(0, 0, e.RowIndex); } } else { dgvItems.Rows[e.RowIndex].Cells["clmPrecio"].Tag = null; dgvItems.EditingControl.Text = ""; } } }
protected override bool guardar() { if (cboPlanta.Text.ToLower() == "sin especificar" || cboPlanta.SelectedItem == null) { Mensaje mensaje = new Mensaje("Seleccione una planta", Mensaje.TipoMensaje.Error, Mensaje.Botones.OK); mensaje.ShowDialog(); return(false); } if (cboArticulo.Text.ToLower() == "sin especificar" || cboArticulo.SelectedItem == null) { Mensaje mensaje = new Mensaje("Seleccione un artículo", Mensaje.TipoMensaje.Error, Mensaje.Botones.OK); mensaje.ShowDialog(); return(false); } ArticuloPlantaHistorico aph = new ArticuloPlantaHistorico(); if (Estado == Estados.Modificar) { aph.ArticuloPlanta = ArticuloPlanta; aph.idMoneda = ArticuloPlanta.idMoneda; aph.precio = ArticuloPlanta.precio; aph.fechaCambio = ArticuloPlanta.fechaCambio; } ArticuloPlanta.idArticulo = cboArticulo.SelectedItem != null ? ((TipoArticulo)((ComboBoxItem)cboArticulo.SelectedItem).Value).id : -1; ArticuloPlanta.idPlanta = cboPlanta.SelectedItem != null ? ((Planta)((ComboBoxItem)cboPlanta.SelectedItem).Value).id : -1; ArticuloPlanta.idMoneda = cboMoneda.SelectedItem != null ? ((Moneda)((ComboBoxItem)cboMoneda.SelectedItem).Value).id : -1; ArticuloPlanta.precio = decimal.Parse(txtPrecioInicial.Text); ArticuloPlanta.fechaCambio = DateTime.Now; try { string cadenaMensaje = ""; //Guarda los precios adicionales ArticuloPlanta.PreciosAdicionales.Clear(); foreach (DataGridViewRow fila in dgvPrecios.Rows) { if (!fila.IsNewRow) { Presentacion present = (Presentacion)fila.Cells["clmPresent"].Tag; PreciosAdicionales precioAdicional = new PreciosAdicionales(); if (present != null) { precioAdicional.Presentacion = present; precioAdicional.precio = decimal.Parse(fila.Cells["clmPrecio"].FormattedValue.ToString()); ArticuloPlanta.PreciosAdicionales.Add(precioAdicional); } } } // Guardamos los datos del ArticuloPlanta if (Estado == Estados.Agregar) { ArticuloPlanta = Global.Servicio.AgregarArticuloPlanta(ArticuloPlanta, Global.DatosSesion); cadenaMensaje = "Precio dado de Alta exitosamente."; } else { Global.Servicio.ActualizarArticuloPlanta(ArticuloPlanta, aph, Global.DatosSesion); cadenaMensaje = "Precio Modificado con éxito."; } lblUltimaModificacion.Text = "Última modificación: " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(); CargarPreciosHist(); // Mostramos mensaje de éxito Mensaje mensaje = new Mensaje(cadenaMensaje, Mensaje.TipoMensaje.Exito, Mensaje.Botones.OK); mensaje.ShowDialog(); // Indica que el ArticuloPlanta se guardó correctamente return(true); } catch (Exception ex) { Mensaje unMensaje; if (ex.Message.Contains("key")) //Ver si la excepcion es del tipo System.InvalidOperationException { unMensaje = new Mensaje("No es posible cambiar la Planta o el Artículo a una relación ya cargada.", Mensaje.TipoMensaje.Error, Mensaje.Botones.OK); unMensaje.ShowDialog(); } else { unMensaje = new Mensaje(ex.Message, Mensaje.TipoMensaje.Error, Mensaje.Botones.OK); unMensaje.ShowDialog(); } } return(false); }