protected void btnGuardar_Click(object sender, EventArgs e) { FacturaEntidad factura = new FacturaEntidad(); List<DetalleFacturaEntidad> listaDetalle = new List<DetalleFacturaEntidad>(); List<MatrizEntidad> listaMatrices = (List<MatrizEntidad>) Session["carrito"]; decimal total = (decimal) Session["total"]; if(cbxDescuento.Checked == true) { total = (decimal) (((double)total) - (((double)total) * 0.15)); } factura.cliente = int.Parse(ddlClientes.SelectedValue); factura.vendedor = int.Parse(ddlVendedor.SelectedValue); factura.tipo = int.Parse(ddlTipoFactura.SelectedValue); factura.numero = int.Parse(txtnumeroFactura.Text); factura.fecha = DateTime.Parse(txtFechaFactura.Text); factura.total = total; factura.descuentoBit = 0; if(cbxDescuento.Checked == true) { factura.descuentoBit = 1; } foreach (var matriz in listaMatrices) { DetalleFacturaEntidad detalle = new DetalleFacturaEntidad(); string nombreMatriz = matriz.nombreArchivo; detalle.idMatriz = MatrizGestor.buscarIdMatriz(nombreMatriz); detalle.cantidad = matriz.cantidad; detalle.precio = matriz.precio; detalle.subtotal = matriz.subTotal; listaDetalle.Add(detalle); } FacturaGestor.guardarFactura(factura,listaDetalle); resetearTodo(); }
public static void guardarFactura(FacturaEntidad factura, List<DetalleFacturaEntidad> lista) { SqlConnection connection = new SqlConnection(); SqlCommand command = new SqlCommand(); SqlTransaction transaction = null; int idFactura = 0; try { connection.ConnectionString = cadenaConeccion; connection.Open(); transaction = connection.BeginTransaction(); command.Transaction = transaction; string sql = "insert into Factura (numeroFactura, idTipoFactura, idCliente, idVendedor, fecha, total, descuento) values (@numeroFactura,@idTipoFactura,@idCliente,@idVendedor,@fecha,@total,@descuento)"; command.Connection = connection; command.CommandText = sql; command.Parameters.AddWithValue("@numeroFactura",factura.numero); command.Parameters.AddWithValue("@idTipoFactura",factura.tipo); command.Parameters.AddWithValue("@idCliente",factura.cliente); command.Parameters.AddWithValue("@idVendedor",factura.vendedor); command.Parameters.AddWithValue("@fecha",factura.fecha); command.Parameters.AddWithValue("@total",factura.total); command.Parameters.AddWithValue("@descuento",factura.descuentoBit); command.ExecuteNonQuery(); command.Parameters.Clear(); sql = "SELECT @@IDENTITY as 'id'"; command.CommandText = sql; SqlDataReader dr = command.ExecuteReader(); while(dr.Read()) { idFactura = int.Parse(dr["id"].ToString()); } dr.Close(); sql = "insert into DetalleFactura (idFactura, idMatriz, cantidad, precio, subTotal) values (@idFactura, @idMatriz, @cantidad, @precio, @subTotal)"; command.CommandText = sql; foreach (var detalle in lista) { command.Parameters.AddWithValue("@idFactura",idFactura); command.Parameters.AddWithValue("@idMatriz",detalle.idMatriz); command.Parameters.AddWithValue("@cantidad",detalle.cantidad); command.Parameters.AddWithValue("@precio",detalle.precio); command.Parameters.AddWithValue("@subTotal",detalle.subtotal); command.ExecuteNonQuery(); command.Parameters.Clear(); } transaction.Commit(); } catch(SqlException ex) { transaction.Rollback(); } finally { if(connection.State == ConnectionState.Open) { connection.Close(); } } }