Пример #1
0
        private void Btn_addGrid_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Txt_descProducto.Text) && !String.IsNullOrEmpty(Txt_precioProducto.Text))
            {
                if (cantidadProducto > 0 && Int32.Parse(Nup_cantidad.Value.ToString()) <= cantidadProducto)
                {
                    double subtotal = 0;
                    double total    = 0;
                    double impuesto = 0;
                    int    fila     = -1;
                    int    cantidad;
                    int    contador      = 0;
                    int    codigoFactura = 0;
                    int    codigoActual  = Int32.Parse(listaProductos.ElementAt(Cbo_prod.SelectedIndex).ToString());

                    if (Dgv_factura.Rows.Count - 1 > 0)
                    {
                        for (int i = 0; i < Dgv_factura.Rows.Count - 1; i++)
                        {
                            codigoFactura = Int32.Parse(Dgv_factura.Rows[i].Cells[0].Value.ToString());
                            if (codigoActual == codigoFactura)
                            {
                                fila     = i;
                                contador = 1;
                            }
                        }
                    }

                    if (contador == 0)
                    {
                        subtotal = Double.Parse(Txt_precioProducto.Text) * Double.Parse(Nup_cantidad.Value.ToString());
                        Dgv_factura.Rows.Add(
                            listaProductos.ElementAt(Cbo_prod.SelectedIndex).ToString(),
                            Nup_cantidad.Value.ToString(),
                            Txt_descProducto.Text,
                            Txt_precioProducto.Text,
                            String.Format("{0:0.00}", subtotal),
                            "-"
                            );
                    }
                    else
                    {
                        cantidad = Int32.Parse(Dgv_factura.Rows[fila].Cells[1].Value.ToString()) + Int32.Parse(Nup_cantidad.Value.ToString());
                        double subtotal2 = Double.Parse(Txt_precioProducto.Text) * cantidad;
                        Dgv_factura.Rows[fila].Cells[1].Value = cantidad.ToString();
                        Dgv_factura.Rows[fila].Cells[4].Value = String.Format("{0:0.00}", subtotal2);
                    }


                    if (Dgv_factura.Rows.Count - 1 > 0)
                    {
                        for (int i = 0; i < Dgv_factura.Rows.Count - 1; i++)
                        {
                            subtotal += Double.Parse(Dgv_factura.Rows[i].Cells[4].Value.ToString());
                        }
                    }

                    cantidadProducto -= Int32.Parse(Nup_cantidad.Value.ToString());

                    try
                    {
                        logicaConsulta.disminuirInventario(listaProductos.ElementAt(Cbo_prod.SelectedIndex).ToString(), Nup_cantidad.Value.ToString());
                    }
                    catch
                    {
                        MessageBox.Show("Fallo al Actualizar Inventario", "Cotizacion", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    Txt_subtotalGeneral.Text = "Q. " + String.Format("{0:0.00}", subtotal);

                    if (listaPorcentajes.ElementAt(Cbo_impuestos.SelectedIndex) > 0)
                    {
                        impuesto     = subtotal * (listaPorcentajes.ElementAt(Cbo_impuestos.SelectedIndex) / 100);
                        Txt_iva.Text = "Q. " + String.Format("{0:0.00}", impuesto);
                    }
                    else
                    {
                        impuesto     = 0;
                        Txt_iva.Text = "Q. 0.00";
                    }

                    impuestoGeneral = impuesto;
                    total           = subtotal + impuesto;

                    Txt_total.Text = "Q. " + String.Format("{0:0.00}", total);
                    int registros = Dgv_factura.Rows.Count - 1;
                    Txt_registros.Text = registros.ToString();
                }
                else
                {
                    MessageBox.Show("No hay producto disponible!", "Facturacion", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Hay Campos Vacios!", "Facturacion", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }