Example #1
0
        private void btnGuardar_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cmbProveedor.Text) || string.IsNullOrEmpty(cmbFormaPago.Text) || string.IsNullOrEmpty(txtTotalFinal.Text))
            {
                MessageBox.Show("Debe de llenar los campos de la compra", "Completar", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                try
                {
                    using (SICOVE1Entities2 db = new SICOVE1Entities2())
                    {
                        // se hacer el insert de la compra en la tabla de compras
                        String comboProveedor = cmbProveedor.SelectedValue.ToString();
                        compras.IdProveedor = Convert.ToInt32(comboProveedor);

                        String comboFPago = cmbFormaPago.SelectedValue.ToString();
                        compras.IdFormaPago = Convert.ToInt32(comboFPago);

                        compras.IdEmpleado    = 1;
                        compras.NumFac        = Convert.ToInt32(txtNunFac.Text);
                        compras.DetalleCompra = txtDetalleCompra.Text;
                        compras.TotalCompra   = Convert.ToDecimal(txtTotalFinal.Text);
                        compras.FechaRegistro = Convert.ToDateTime(dtpFechaReg.Text);


                        db.tb_Compras.Add(compras);
                        db.SaveChanges();


                        ///////////////////////////////////////////////////////////////////// Lilian Bonilla.
                        //se hace el insert para la tabla detalle de la compra

                        tb_DetalleCompras detalleCompra = new tb_DetalleCompras();
                        for (int i = 0; i < dgvCompras.RowCount; i++)
                        {
                            String idProducto            = dgvCompras.Rows[i].Cells[0].Value.ToString();
                            int    IdProductoConvertidos = Convert.ToInt32(idProducto);

                            String idCategoria           = dgvCompras.Rows[i].Cells[1].Value.ToString();
                            int    IdCategoriaConvertida = Convert.ToInt32(idCategoria);

                            String  precio            = dgvCompras.Rows[i].Cells[4].Value.ToString();
                            decimal precioConvertidos = Convert.ToDecimal(precio);

                            String cantidad            = dgvCompras.Rows[i].Cells[5].Value.ToString();
                            int    cantidadConvertidos = Convert.ToInt32(cantidad);

                            String  SubTotal            = dgvCompras.Rows[i].Cells[6].Value.ToString();
                            decimal SubTotalConvertidos = Convert.ToDecimal(SubTotal);

                            String  IVA            = dgvCompras.Rows[i].Cells[7].Value.ToString();
                            decimal IVAConvertidos = Convert.ToDecimal(IVA);

                            String  total            = dgvCompras.Rows[i].Cells[8].Value.ToString();
                            decimal totalConvertidos = Convert.ToDecimal(total);


                            detalleCompra.IdCompra = Convert.ToInt32(txtNunFac.Text);

                            detalleCompra.IdProducto   = IdProductoConvertidos;
                            detalleCompra.IdCategoria  = IdCategoriaConvertida;
                            detalleCompra.PrecioCompra = precioConvertidos;
                            detalleCompra.Cantidad     = cantidadConvertidos;
                            detalleCompra.SubTotal     = SubTotalConvertidos;
                            detalleCompra.IVA          = IVAConvertidos;
                            detalleCompra.Total        = totalConvertidos;


                            var listaInventario = db.tb_Inventarios;
                            try
                            {
                                inventarios = db.tb_Inventarios.Where(VerificarId => VerificarId.IdProducto == IdProductoConvertidos).First();
                                foreach (var iterardatostbventa in listaInventario)
                                {
                                    int idCompra = inventarios.Existencia;

                                    inventarios.Existencia      = inventarios.Existencia + cantidadConvertidos;
                                    db.Entry(inventarios).State = System.Data.Entity.EntityState.Modified;
                                }
                            }
                            catch (Exception ex)
                            {
                                inventarios.Existencia = inventarios.Existencia + cantidadConvertidos;
                                inventarios.IdProducto = IdProductoConvertidos;
                                db.tb_Inventarios.Add(inventarios);
                            }


                            db.tb_DetalleCompras.Add(detalleCompra);
                            db.SaveChanges();
                        }
                        MessageBox.Show("La Compra se registro con exito ");
                        dgvCompras.Rows.Clear();
                        limpiarVenta();
                        Limpiar();
                        CargarCombos();
                        retornoId();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Algo salio mal... " + ex.ToString());
                }
            }
        }
Example #2
0
        private void btnGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                using (SICOVE1Entities db = new SICOVE1Entities())
                {
                    // se hacer el insert de la compra en la tabla de compras
                    String comboProveedor = cmbProveedor.SelectedValue.ToString();
                    compras.IdProveedor = Convert.ToInt32(comboProveedor);

                    String comboFPago = cmbFormaPago.SelectedValue.ToString();
                    compras.IdFormaPago = Convert.ToInt32(comboFPago);

                    compras.IdEmpleado    = 1;
                    compras.NumFac        = Convert.ToInt32(txtNunFac.Text);
                    compras.DetalleCompra = txtDetalleCompra.Text;
                    compras.TotalCompra   = Convert.ToDecimal(txtTotalFinal.Text);
                    compras.FechaRegistro = Convert.ToDateTime(dtpFechaReg.Text);


                    db.tb_Compras.Add(compras);
                    db.SaveChanges();


                    ///////////////////////////////////////////////////////////////////////////////// Lilian Bonilla.
                    //se hace el insert para la tabla detalle de la compra

                    tb_DetalleCompras detalleCompra = new tb_DetalleCompras();
                    for (int i = 0; i < dgvCompras.RowCount; i++)
                    {
                        String idProducto            = dgvCompras.Rows[i].Cells[0].Value.ToString();
                        int    IdProductoConvertidos = Convert.ToInt32(idProducto);

                        String idCategoria           = dgvCompras.Rows[i].Cells[1].Value.ToString();
                        int    IdCategoriaConvertida = Convert.ToInt32(idCategoria);

                        String  precio            = dgvCompras.Rows[i].Cells[4].Value.ToString();
                        decimal precioConvertidos = Convert.ToDecimal(precio);

                        String cantidad            = dgvCompras.Rows[i].Cells[5].Value.ToString();
                        int    cantidadConvertidos = Convert.ToInt32(cantidad);

                        String  SubTotal            = dgvCompras.Rows[i].Cells[6].Value.ToString();
                        decimal SubTotalConvertidos = Convert.ToDecimal(SubTotal);

                        String  IVA            = dgvCompras.Rows[i].Cells[7].Value.ToString();
                        decimal IVAConvertidos = Convert.ToDecimal(IVA);

                        String  total            = dgvCompras.Rows[i].Cells[8].Value.ToString();
                        decimal totalConvertidos = Convert.ToDecimal(total);


                        detalleCompra.IdCompra = Convert.ToInt32(txtNunFac.Text);

                        detalleCompra.IdProducto   = IdProductoConvertidos;
                        detalleCompra.IdCategoria  = IdCategoriaConvertida;
                        detalleCompra.PrecioCompra = precioConvertidos;
                        detalleCompra.Cantidad     = cantidadConvertidos;
                        detalleCompra.SubTotal     = SubTotalConvertidos;
                        detalleCompra.IVA          = IVAConvertidos;
                        detalleCompra.Total        = totalConvertidos;

                        db.tb_DetalleCompras.Add(detalleCompra);
                        db.SaveChanges();
                    }
                    MessageBox.Show("La venta se registro con exito");
                    dgvCompras.Rows.Clear();
                    limpiarVenta();
                    Limpiar();
                    CargarCombos();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Algo salio mal... " + ex.ToString());
            }
        }