Ejemplo n.º 1
0
        private void BtnGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                if (entiVenta == null)
                {
                    entiVenta = new EVenta();
                }

                entiVenta.IdCliente       = Convert.ToInt32(lblIdCliente.Text);
                entiVenta.IdTrabajador    = UserCache.IdTrabajador;
                entiVenta.Fecha           = dtpFecha.Value;
                entiVenta.TipoComprobante = cmbComprobante.Text;
                entiVenta.Serie           = txtSerie.Text.Trim();
                entiVenta.Correlativo     = txtCorrelativo.Text.Trim();
                entiVenta.Igv             = Convert.ToDecimal(txtIgv.Text.Trim());

                //Capturamos el IdUltimo
                int idUltimo = venta.RegistrarVenta(entiVenta);
                if (idUltimo > 0)
                {
                    MessageBox.Show("¡Registro de venta con éxito!", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    //Registramos el Detalle ingreso
                    if (entiDetalle == null)
                    {
                        entiDetalle = new EDetalleVenta();
                    }
                    int numFilas = dgvDetalleVentas.Rows.Count;
                    int contador = 0;

                    for (int i = 0; i < numFilas; i++)
                    {
                        entiDetalle.IdVenta      = idUltimo;
                        entiDetalle.IdDetIngreso = Convert.ToInt32(dgvDetalleVentas.Rows[i].Cells[0].Value.ToString());
                        entiDetalle.Cantidad     = Convert.ToInt32(dgvDetalleVentas.Rows[i].Cells[2].Value.ToString());
                        entiDetalle.PrecioVenta  = Convert.ToDecimal(dgvDetalleVentas.Rows[i].Cells[3].Value.ToString());
                        entiDetalle.Descuento    = Convert.ToInt32(dgvDetalleVentas.Rows[i].Cells[4].Value.ToString());

                        int idDetIngreso = Convert.ToInt32(dgvDetalleVentas.Rows[i].Cells[0].Value.ToString());
                        int cantidad     = Convert.ToInt32(dgvDetalleVentas.Rows[i].Cells[2].Value.ToString());

                        if (detalle.RegistrarDetalleVenta(entiDetalle))
                        {
                            venta.DisminuirStockPorVenta(idDetIngreso, cantidad);
                        }
                        else
                        {
                            contador++;
                        }
                    }

                    if (contador == 0)
                    {
                        MessageBox.Show("¡Detalle de venta registrado con éxito!", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                if (venta.builder.Length != 0)
                {
                    MessageBox.Show(venta.builder.ToString(), "VENTA: Para continuar", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    if (detalle.builder.Length != 0)
                    {
                        MessageBox.Show(detalle.builder.ToString(), "DETALLE VENTA: Para continuar", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error inesperado", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                MostrarVenta();
                LimpiarVenta();
                Deshabilitar();
                btnNuevo.Enabled = true;
                EliminarFilaDetalleVenta();
            }
        }