//Guarda Toda La compra
        /// <summary>
        /// tb_Compra_detalle es cada linea de la fact. relacionada con tb_Compra y con insumo.
        /// Actualizar CantDisponibleInsumo. actualizar datagrids de productos.
        /// 
        /// 
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GuardarCompraBtn_Click(object sender, EventArgs e)
        {
            if (!Validaciones.Validation.hasValidationErrors(this.Controls))
            {
                tb_Compras compra = new tb_Compras();
                using (var ctx = new LabDBEntities())
                {
                    foreach (DataGridViewRow fila in dvgDetalle.Rows)
                    {
                        if (fila.Cells[0].Value != null)
                        {

                            tb_Compra_Detalle compraDetalle = new tb_Compra_Detalle();

                            compraDetalle.compra_id = compra.id_compra;
                            compraDetalle.cantidad = Convert.ToInt32(fila.Cells[2].Value);
                            compraDetalle.insumo_id = Convert.ToInt32(fila.Cells[0].Value);

                            ctx.tb_Insumos.Find(compraDetalle.insumo_id).cant_disponible = ctx.tb_Insumos.Find(compraDetalle.insumo_id).cant_disponible + Convert.ToInt32(fila.Cells[2].Value);
                            //Medida
                            compraDetalle.medida = fila.Cells[4].Value.ToString();
                            //Descuento
                            compraDetalle.descuento = Convert.ToDouble(fila.Cells[5].Value);
                            //precio
                            compraDetalle.precio = Convert.ToDecimal(fila.Cells[3].Value);
                            //subtotal
                            compraDetalle.subtotal_prod_cant = Convert.ToDecimal(fila.Cells[6].Value);
                            //fecha
                            compra.fecha = dtpFechaCompra.Value;
                            //tipopago
                            compra.pago = Convert.ToInt32(comboTipoPago.SelectedValue.ToString());
                            //total
                            compraDetalle.total = Convert.ToDecimal(PrecioTotalTxt.Text);
                            compra.total = Convert.ToDecimal(PrecioTotalTxt.Text);
                            //Descripcion
                            compra.descripcion = textBoxDescripcion.Text;
                            if (checkBoxVincularNota.Checked == true)
                            {
                                if (comboNotasPedido.SelectedValue != null)
                                {
                                    compra.notapedido_id = Convert.ToInt32(comboNotasPedido.SelectedValue);
                                }

                            }

                            compra.tb_Compra_Detalle.Add(compraDetalle);
                        }
                    }

                    ctx.tb_Compras.Add(compra);

                    if (ctx.SaveChanges() == 0)
                    {
                    }
                    else
                    {
                        MessageBox.Show("Compra guardada con exito. Mostrando Nota de Pedido relacionada y Compra realizada");
                        if (checkBoxVincularNota.Checked == true)
                        {
                            ReportesLaboratorio.ReporteNotaPedidoFrm notarel = new ReportesLaboratorio.ReporteNotaPedidoFrm(compra.notapedido_id.Value);
                            notarel.ShowDialog();
                        }
                        ReportesLaboratorio.ReporteCompraFrm rptComp = new ReportesLaboratorio.ReporteCompraFrm(compra.id_compra);
                        rptComp.Show();
                        LimpiarVentana(groupBox2);
                        LimpiarVentana(groupBox3);
                        dvgDetalle.Rows.Clear();
                        cargarDatagridProductos();
                    }
                }
            }
        }
        private void GenerarNotaPedBtn_Click(object sender, EventArgs e)
        {
            tb_NotaPedido compra = new tb_NotaPedido();
            if (dvgDetalle.Rows.Count > 0)
            {
                using (var ctx = new LabDBEntities())
                {
                    foreach (DataGridViewRow fila in dvgDetalle.Rows)
                    {
                        if (fila.Cells[0].Value != null)
                        {

                            Pedido_Detalle compraDetalle = new Pedido_Detalle();

                            compraDetalle.pedido_id = compra.id_nota_pedido;
                            compraDetalle.insumo_id = Convert.ToInt32(fila.Cells[0].Value);
                            //Cantidad
                            compraDetalle.cantidad = Convert.ToInt32(fila.Cells[2].Value);
                            //Precio
                            compraDetalle.precio = Convert.ToDecimal(fila.Cells[3].Value);
                            //Subtotal
                            compraDetalle.subtotal = Convert.ToDecimal(fila.Cells[6].Value);
                            //

                            compraDetalle.total = Convert.ToDecimal(PrecioTotalTxt.Text);
                            //Medida
                            compraDetalle.unidad_medida = fila.Cells[4].Value.ToString();
                            //Descuento
                            compraDetalle.descuento = Convert.ToDouble(fila.Cells[5].Value);
                            //Descripcion de la nota de pedido
                            compra.descripcion = textBoxDescripcion.Text;

                            ctx.Pedido_Detalle.Add(compraDetalle);
                            compra.fecha_emision = DateTime.Now;

                            compra.Pedido_Detalle.Add(compraDetalle);

                        }
                    }

                    ctx.tb_NotaPedido.Add(compra);

                    if (ctx.SaveChanges() == 0)
                    {
                    }
                    else
                    {

                        MessageBox.Show("Nota Generada con exito");
                        ReportesLaboratorio.ReporteNotaPedidoFrm notapedfrm = new ReportesLaboratorio.ReporteNotaPedidoFrm(compra.id_nota_pedido);

                        notapedfrm.Show();
                        LimpiarVentana(groupBox2);
                        LimpiarVentana(groupBox3);
                        dvgDetalle.Rows.Clear();
                        cargarDatagridProductos();
                    }
                }
            }
        }