public static void GrabarVencimientos(List <FactVencimiento> vencimientos, GrupoVencimiento grupo, string USDTag) { List <FactVencimiento> listAllFactVencimientosInDB = new List <FactVencimiento>(); string connectionString = ConfigurationManager.ConnectionStrings[Config.Common.JASPER].ConnectionString; using (NpgsqlConnection pgConnection = new NpgsqlConnection(connectionString)) { pgConnection.Open(); NpgsqlDataReader dr; NpgsqlCommand cmd; string sqlString = "SELECT id_cliente, id_grupo_vencimiento, saldo_vencido " + "FROM fact_vencido" + USDTag + ";"; cmd = new NpgsqlCommand(sqlString, pgConnection); dr = cmd.ExecuteReader(); while (dr.Read()) { FactVencimiento oFactVencimiento = new FactVencimiento(); DimCliente dCliente = new DimCliente(); dCliente.IdCliente = int.Parse(dr["id_cliente"].ToString()); oFactVencimiento.Cliente = dCliente; oFactVencimiento.IdVencimiento = int.Parse(dr["id_grupo_vencimiento"].ToString()); oFactVencimiento.Importe = double.Parse(dr["saldo_vencido"].ToString()); listAllFactVencimientosInDB.Add(oFactVencimiento); } dr.Close(); cmd.Dispose(); pgConnection.Close(); } lGrabarVencimientos(vencimientos, grupo, listAllFactVencimientosInDB, USDTag); }
public static void GrabarPorVencer(List <FactVencimiento> porvencer, GrupoVencimiento grupo, string USDTag) { List <FactVencimiento> listAllFactPorVencerInDB = new List <FactVencimiento>(); string connectionString = ConfigurationManager.ConnectionStrings[Config.Common.JASPER].ConnectionString; using (NpgsqlConnection pgConnection = new NpgsqlConnection(connectionString)) { pgConnection.Open(); NpgsqlDataReader dr; NpgsqlCommand cmd; string sqlString = "SELECT id_cliente, id_grupo_vencimiento, saldo_por_vencer " + "FROM fact_por_vencer" + USDTag + ";"; cmd = new NpgsqlCommand(sqlString, pgConnection); dr = cmd.ExecuteReader(); while (dr.Read()) { FactVencimiento oFactPorVencer = new FactVencimiento(); DimCliente dCliente = new DimCliente(); dCliente.IdCliente = int.Parse(dr["id_cliente"].ToString()); oFactPorVencer.Cliente = dCliente; oFactPorVencer.IdVencimiento = int.Parse(dr["id_grupo_vencimiento"].ToString()); oFactPorVencer.Importe = double.Parse(dr["saldo_por_vencer"].ToString()); listAllFactPorVencerInDB.Add(oFactPorVencer); } dr.Close(); cmd.Dispose(); pgConnection.Close(); } // Para cada ID Doco en la BD de postgres using (NpgsqlConnection pgConnection = new NpgsqlConnection(connectionString)) { try { pgConnection.Open(); using (NpgsqlTransaction transaction = pgConnection.BeginTransaction()) { try { foreach (FactVencimiento currentPorVencer in porvencer) { NpgsqlCommand theCommand; FactVencimiento oFactPorVencerToPersist = currentPorVencer; // Check if record exists. bool exists = false; foreach (FactVencimiento record in listAllFactPorVencerInDB) { if (record.Cliente.IdCliente == currentPorVencer.Cliente.IdCliente) { exists = true; break; } } //FactPorVencer oFactPorVencerExistente = listAllFactPorVencerInDB.First(record => record.IdCliente == currentPorVencer.IdCliente && record.IdEmpresa == currentPorVencer.IdEmpresa && record.IdVencimiento == currentPorVencer.IdVencimiento); // if record exists, update the record. if (exists) { string sqlUpdateVencimiento = "UPDATE fact_por_vencer" + USDTag + " " + "SET saldo_por_vencer=@saldo_por_vencer " + "WHERE id_cliente=@id_cliente and id_grupo_vencimiento=@id_grupo_vencimiento;"; theCommand = new NpgsqlCommand(sqlUpdateVencimiento, pgConnection); } // if record does not exists, insert the record else { string sqlUpdateVencimiento = "INSERT INTO fact_por_vencer" + USDTag + "(" + "id_cliente, id_grupo_vencimiento, saldo_por_vencer) " + "VALUES (@id_cliente, @id_grupo_vencimiento, @saldo_por_vencer);"; theCommand = new NpgsqlCommand(sqlUpdateVencimiento, pgConnection); } theCommand.Parameters.Add("@saldo_por_vencer", NpgsqlTypes.NpgsqlDbType.Double); theCommand.Parameters.Add("@id_cliente", NpgsqlTypes.NpgsqlDbType.Integer); theCommand.Parameters.Add("@id_grupo_vencimiento", NpgsqlTypes.NpgsqlDbType.Integer); theCommand.Parameters["@saldo_por_vencer"].Value = oFactPorVencerToPersist.Importe; theCommand.Parameters["@id_cliente"].Value = oFactPorVencerToPersist.Cliente.IdCliente; theCommand.Parameters["@id_grupo_vencimiento"].Value = grupo.Id; theCommand.ExecuteNonQuery(); } transaction.Commit(); } catch (Exception ex) { if (transaction != null) { transaction.Rollback(); } throw ex; } } } catch (Exception ex) { throw ex; } pgConnection.Close(); } }