private void dgvVentas_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (ready) { string sql = ""; if (e.ColumnIndex == dgvCols["CerrarVenta"]) { if (dgvVentas.Rows[e.RowIndex] .Cells[dgvCols["AsientosSeleccionados"]].Value != null) { //MessageBox.Show("se ha presionado el boton"); int cantBoletos = int.Parse(dgvVentas.Rows[e.RowIndex] .Cells[dgvCols["CantidadAsientos"]].Value.ToString()); float precioBoletos = precioUnitario * cantBoletos; float importeTotal = precioBoletos - descuento; DialogResult d = MessageBox.Show("Confirmar Pago\n" + "Cantidad de Boletos: \t" + cantBoletos + "\n" + "Precio Unitario: \t\t$" + precioUnitario + ".00\n" + "Descuento: \t\t$" + descuento + ".00\n" + "Importe Total: \t\t$" + importeTotal + ".00\n" , "Aceptar", MessageBoxButtons.OKCancel); int i = 0; if (d.Equals(DialogResult.OK)) { int p = 0; int sumaPuntos = 0;//solo puede ser 0 o 1 ProcesosBD pbd = new ProcesosBD(); pbd.Conectar(); List <long> liClaveBoletos = new List <long>(); sql = "select NEXT VALUE FOR SCH_Boletos.AI_Boletos"; for (int n = 0; n < liButacas.Count; n++) { liClaveBoletos.Add(long.Parse(pbd.SqlSelect(sql).Tables[0] .Rows[0][0].ToString())); } sql = "select next value for SCH_Ventas.AI_Ventas"; int claveVenta = int.Parse(pbd.SqlSelect(sql).Tables[0].Rows[0][0] .ToString()); pbd.Conectar(); pbd.abrirConexion(); pbd.comenzarTransaccion(); try { if (!claveCli.Equals("NULL")) { sumaPuntos = 1; //el cliente es válido por lo que la compra suma puntos //este valor se usa en otro punto del código sql = "update Clientes " + "set puntosUsados = (select puntosUsados " + "from Clientes where claveCliente =" + "" + claveCli + ") + " + puntos + ", " + "puntos = (select puntos from Clientes where claveCliente = " + "" + claveCli + ") + " + p + " " + "where claveCliente = " + "" + claveCli + ""; pbd.sqlUpdateTransaction(sql); } sql = "insert into ventas(claveVenta,claveCliente,claveUsuario" + ",horaFechaVenta,importeTotal) values " + "(" + claveVenta + "," + "" + claveCli + "" + "," + Program.ClaveUsario + "" + ",CONVERT(datetime, GETDATE(),103)" + "," + importeTotal + ")"; pbd.sqlUpdateTransaction(sql); sql = "insert into Boletos(boleto,claveFuncion, asiento,fila" + ",fechaHoraImpresion,claveVenta)" + " values"; foreach (KeyValuePair <int, Butaca> butaca in liButacas) { if (i == 0) { sql += "(" + "" + liClaveBoletos[i++] + "," + "" + claveFuncion + "," + "" + butaca.Value.Asiento + "," + "" + butaca.Value.Fila + "," + "CONVERT(datetime,getdate(),103)" + "," + "" + claveVenta + ")"; } else { sql += ",(" + "" + liClaveBoletos[i++] + "," + "" + claveFuncion + "," + "" + butaca.Value.Asiento + "," + "" + butaca.Value.Fila + "," + "CONVERT(datetime,getdate(),103)" + "," + "" + claveVenta + ")"; } } i = 0; pbd.sqlUpdateTransaction(sql); sql = "insert into DetallesVentas(claveVenta,boleto,claveTipoVenta" + ",descuento,iva,precioUnitario,importeParcial" + ",fechaHoraRegistro,sumaPuntos) values"; for (int n = 0; n < liButacas.Count; n++) { if (puntos <= 0) { descuento = 0; } if (n == 0) { sql += "(" + "" + claveVenta + "," + liClaveBoletos[i++] + ",1" +//tipoventa 1 solo para venta de boletos "," + descuento + "" + ",0" + "," + precioUnitario + "," + (precioUnitario + precioUnitario * iva - descuento) + "" + ",CONVERT(datetime, GETDATE(),103)" + "," + sumaPuntos + ")"; } else { sql += ",(" + "" + claveVenta + "" + "," + liClaveBoletos[i++] + "" + ",1" +//tipoventa 1 solo para venta de boletos "," + descuento + "" + ",0" + "," + precioUnitario + "," + (precioUnitario + precioUnitario * iva - descuento) + "" + ",CONVERT(datetime, GETDATE(),103)" + "," + sumaPuntos + ")"; } puntos--; } pbd.sqlUpdateTransaction(sql); // Attempt to commit the transaction. pbd.Transaction.Commit(); Console.WriteLine("Both records are written to database."); MessageBox.Show("Venta registrada"); } catch (Exception ex) { Console.WriteLine("Commit Exception Type: {0}", ex.GetType()); Console.WriteLine(" Message: {0}", ex.Message); MessageBox.Show("Venta no registrada"); // Attempt to roll back the transaction. try { pbd.Transaction.Rollback(); } catch (Exception ex2) { // This catch block will handle any errors that may have occurred // on the server that would cause the rollback to fail, such as // a closed connection. Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType()); Console.WriteLine(" Message: {0}", ex2.Message); } } finally { veces = 0; filaActual = e.RowIndex; limpiarControles(); pbd.Conexion.Close(); if (!claveCli.Equals("NULL")) { sql = "select count(boleto)puntos from DetallesVentas d " + "where fechaHoraRegistro >= GETDATE()-90 and fechaPuntosUsados is null " + "and boleto not in (select boleto from Cancelados) " + "and d.claveVenta not in (select d.claveVenta from Cancelados) " + "and sumaPuntos = 1 and claveVenta in (select claveVenta " + "from Ventas where claveCliente = " + claveCli + ")"; Console.WriteLine(sql); dt = pbd.SqlSelect(sql).Tables[0]; if (dt.Rows.Count > 0) { if (int.Parse(dt.Rows[0][0].ToString()) >= 5) { p = int.Parse(dt.Rows[0][0].ToString()) / 5; //MessageBox.Show("puntos" + p); sql = "update DetallesVentas set fechaPuntosUsados=GETDATE() " + "where claveVenta in(select claveVenta from Ventas where " + "claveCliente = " + claveCli + ") " + "and sumaPuntos = 1 " +//sumaPuntos=1 habilita la suma de los puntos "and fechaPuntosUsados is null"; pbd.SqlUpdate(sql); sql = "update Clientes " + "set puntos = (select puntos from Clientes where claveCliente = " + "" + claveCli + ") + " + p + " " + "where claveCliente = " + "" + claveCli + ""; pbd.SqlUpdate(sql); } } } } } } else { MessageBox.Show("Debe seleccionar los asientos"); } } else if (e.ColumnIndex == dgvCols["SeleccionarAsientos"]) { if (dgvVentas.Rows[e.RowIndex].Cells[dgvCols["CantidadAsientos"]].Value != null) { sql = "select capacidad from TipoSala t,Salas s,Funciones f " + " where f.claveSala = s.claveSala and s.claveTipoSala = t.claveTipoSala" + " and claveFuncion =" + claveFuncion; Console.WriteLine("consulta para capacidad:" + sql); int capacidad = int.Parse(pdb.SqlSelect(sql).Tables[0].Rows[0][0] .ToString()); Console.WriteLine("capacidad:" + capacidad); sql = "select fila,asiento from Boletos where claveFuncion = " + claveFuncion + "" + " and claveVenta not in(select c.claveVenta from Cancelados c," + "boletos b where c.claveVenta = b.claveVenta)" + " order by 1,2 asc"; Console.WriteLine("consulta para boletos: " + sql); ds = pdb.SqlSelect(sql); dt = ds.Tables[0]; Sala sala = null; if (capacidad == 20) { sala = new SalaTipo2(); } else if (capacidad == 50) { sala = new SalaTipo1(); } sala.ObtenerAcomodamiento(dt, capacidad); Console.Write(sala); sala.imprimirAcomodamiento(); int cantAsientosPorVender = int.Parse(dgvVentas.Rows[e.RowIndex].Cells[dgvCols["CantidadAsientos"]].Value.ToString()); int row = e.RowIndex; FrmVentasLugares frmVentasLugares = new FrmVentasLugares(this, cantAsientosPorVender, sala, row); frmVentasLugares.ShowDialog(); } else { MessageBox.Show("Debe elegir la cantidad de boletos"); } } } }