Exemplo n.º 1
0
        private void btnGrabarVenta_Click(object sender, EventArgs e)
        {
            if (fun.ValidarFecha(txtFechaAltaOrden.Text) == false)
            {
                Mensaje("La fecha ingresada es incorrecta");
                return;
            }
            if (tbVenta.Rows.Count == 0)
            {
                Mensaje("Debe ingresar al menos un producto para vender");
                return;
            }

            if (txtApellido.Text == "")
            {
                Mensaje("Debe ingresar un apellido");
                return;
            }
            if (txtNombre.Text == "")
            {
                Mensaje("Debe ingresar un nombre");
                return;
            }

            if (ValidarImportes() == false)
            {
                Mensaje("La forma de pago no coincide con el total a pagar");
                return;
            }
            SqlConnection con    = new SqlConnection(cConexion.Cadenacon());
            Double        Total  = 0;
            cInsumo       insumo = new cInsumo();
            DateTime      Fecha  = DateTime.Now;

            if (txtTotal.Text != "")
            {
                Total = fun.ToDouble(txtTotal.Text);
            }
            Fecha = Convert.ToDateTime(txtFechaAltaOrden.Text);
            Double Efectivo = 0;

            if (txtEfectivo.Text != "")
            {
                Efectivo = fun.ToDouble(txtEfectivo.Text);
            }
            cVenta venta      = new cVenta();
            Int32  CodVenta   = 0;
            Int32  CodInsumo  = 0;
            Int32  Cantidad   = 0;
            Double Precio     = 0;
            Double Subtotal   = 0;
            Double Costo      = 0;
            Int32? CodCliente = null;

            con.Open();
            SqlTransaction tranOrden;

            tranOrden = con.BeginTransaction("TranOrden");
            try
            {
                if (txtApellido.Text != "" && txtNombre.Text != "")
                {
                    CodCliente = GrabarCliente(con, tranOrden);
                }

                CodVenta = venta.InsertarVenta(con, tranOrden, Fecha, Total, CodCliente, Efectivo);
                for (int i = 0; i < tbVenta.Rows.Count; i++)
                {
                    if (tbVenta.Rows[i]["CodInsumo"].ToString() != "")
                    {
                        CodInsumo = Convert.ToInt32(tbVenta.Rows[i]["CodInsumo"].ToString());
                        Cantidad  = Convert.ToInt32(tbVenta.Rows[i]["Cantidad"].ToString());
                        Precio    = fun.ToDouble(tbVenta.Rows[i]["Precio"].ToString());
                        Subtotal  = fun.ToDouble(tbVenta.Rows[i]["Subtotal"].ToString());
                        Costo     = fun.ToDouble(tbVenta.Rows[i]["Costo"].ToString());
                        venta.InsertarDetalleVenta(con, tranOrden, CodVenta, CodInsumo, Cantidad, Precio, Subtotal, Costo);
                        GrabarFormaPago(con, tranOrden, null, CodVenta);
                        insumo.ActualizarStock(con, tranOrden, CodInsumo, (-1) * Cantidad);
                    }
                }
                tranOrden.Commit();
                con.Close();
                Mensaje("Datos grabados correctamente");
                Limpiar();
            }
            catch (Exception ex)
            {
                tranOrden.Rollback();
                con.Close();
                Mensaje("Hubo un error en el proceso de grabación");
            }
        }
Exemplo n.º 2
0
        private void btnGrabar_Click(object sender, EventArgs e)
        {
            if (fun.ValidarFecha(txtFecha.Text) == false)
            {
                Mensaje("La fecha ingresada es incorrecta");
                return;
            }
            if (Grilla.Rows.Count < 1)
            {
                Mensaje("Debe ingresar insumos para continuar");
                return;
            }
            SqlConnection con = new SqlConnection();

            con.ConnectionString = Clases.cConexion.Cadenacon();
            con.Open();
            SqlTransaction Transaccion;

            Transaccion = con.BeginTransaction();
            Int32  CodCompra    = 0;
            Double PrecioVenta  = 0;
            Int32? CodProveedor = null;

            if (CmbProveedor.SelectedIndex > 0)
            {
                CodProveedor = Convert.ToInt32(CmbProveedor.SelectedValue);
            }
            DateTime       Fecha       = Convert.ToDateTime(txtFecha.Text);
            cCompra        objCompra   = new cCompra();
            cDetalleCompra objDetalle  = new cDetalleCompra();
            cMovimiento    mov         = new cMovimiento();
            string         Descripcion = "";

            try
            {
                cInsumo insumo = new cInsumo();
                CodCompra = objCompra.InsertarCompra(con, Transaccion, Fecha, CodProveedor, txtFactura.Text);
                for (int i = 0; i < Grilla.Rows.Count - 1; i++)
                {
                    Int32  CodInsumo = Convert.ToInt32(Grilla.Rows[i].Cells[0].Value.ToString());
                    string Insumo    = Grilla.Rows[i].Cells[1].Value.ToString();
                    Int32  Cantidad  = Convert.ToInt32(Grilla.Rows[i].Cells[2].Value.ToString());
                    double Precio    = Convert.ToDouble(Grilla.Rows[i].Cells[4].Value.ToString().Replace(",", "."));
                    double Total     = Cantidad * Precio;
                    Descripcion = "COMPRA REPUESTO " + Insumo.ToString();
                    Descripcion = Descripcion + ", CANTIDAD " + Cantidad.ToString();
                    if (Grilla.Rows[i].Cells["PrecioVenta"].ToString() != "")
                    {
                        PrecioVenta = fun.ToDouble(Grilla.Rows[i].Cells["PrecioVenta"].Value.ToString());
                    }
                    mov.GrabarMovimientoTransaccion(con, Transaccion, -1 * Total, Descripcion, Fecha, 1, null);
                    objDetalle.InsertarDetalle(con, Transaccion, CodCompra, CodInsumo, Cantidad, Precio);
                    insumo.ActualizarStock(con, Transaccion, CodInsumo, Cantidad);
                    insumo.ActualizarPrecio(con, Transaccion, CodInsumo, Precio, PrecioVenta);
                }
                Transaccion.Commit();
                con.Close();
                Mensaje("Datos grabados correctamente");
                Limpiar();
            }
            catch (Exception ex)
            {
                Transaccion.Rollback();
                con.Close();
                MessageBox.Show("Hubo un error en el proceso de grabación", Clases.cMensaje.Mensaje());
            }
        }