Atualizando para utilizar parametros nos comandos SQL
This commit is contained in:
parent
dab453279b
commit
43ed54c5fe
@ -9,6 +9,7 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using iText.Kernel.Pdf;
|
using iText.Kernel.Pdf;
|
||||||
using iText.Kernel.Utils;
|
using iText.Kernel.Utils;
|
||||||
|
using Org.BouncyCastle.Asn1.Cmp;
|
||||||
|
|
||||||
public class Fatura
|
public class Fatura
|
||||||
{
|
{
|
||||||
@ -55,7 +56,7 @@
|
|||||||
|
|
||||||
public JsonElement.ArrayEnumerator Agrupada_children { get; private set; }
|
public JsonElement.ArrayEnumerator Agrupada_children { get; private set; }
|
||||||
|
|
||||||
public void Processar(OleDbCommand cmd)
|
public void Processar(OleDbConnection conn)
|
||||||
{
|
{
|
||||||
// Variavel para armazenar os dados a serem lancados para a TUSD no BD
|
// Variavel para armazenar os dados a serem lancados para a TUSD no BD
|
||||||
RecordSet dadosTusd = new ();
|
RecordSet dadosTusd = new ();
|
||||||
@ -68,9 +69,10 @@
|
|||||||
string uc = new Regex("^0+").Replace(parsedResult.locationNumber, string.Empty).Replace("/", string.Empty).Replace("-", string.Empty).Replace(".", string.Empty);
|
string uc = new Regex("^0+").Replace(parsedResult.locationNumber, string.Empty).Replace("/", string.Empty).Replace("-", string.Empty).Replace(".", string.Empty);
|
||||||
|
|
||||||
// Vinculo da fatura com os dados cadastrais
|
// Vinculo da fatura com os dados cadastrais
|
||||||
string selectSQL = $"SELECT COUNT (Cod_Smart_unidade) FROM Dados_cadastrais WHERE Codigo_Instalacao = '{uc}'";
|
string selectSQL = $"SELECT COUNT (Cod_Smart_unidade) FROM Dados_cadastrais WHERE Codigo_Instalacao = '@uc'";
|
||||||
|
using (OleDbCommand cmd = new OleDbCommand(selectSQL, conn))
|
||||||
cmd.CommandText = selectSQL;
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@uc", uc);
|
||||||
|
|
||||||
int? unidades = (int?)cmd.ExecuteScalar();
|
int? unidades = (int?)cmd.ExecuteScalar();
|
||||||
|
|
||||||
@ -80,13 +82,18 @@
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
selectSQL = $"SELECT Cod_Smart_unidade, Cliente, Unidade, PerfilCCEE, Submercado, Status_unidade, Grupo, Distribuidora, ICMS_TUSD, Demanda_P, Demanda_FP, Caminho_NFs FROM Dados_cadastrais WHERE (CNPJ_CPF LIKE '{parsedResult.customer.cnpj?.Substring(0, 8) ?? "NAN"}%' AND Codigo_Instalacao = '{uc}') OR (Razao_Social LIKE '{parsedResult.customer.name.Replace("'", "''")}' AND Codigo_Instalacao = '{uc}')";
|
selectSQL = $"SELECT Cod_Smart_unidade, Cliente, Unidade, PerfilCCEE, Submercado, Status_unidade, Grupo, Distribuidora, ICMS_TUSD, Demanda_P, Demanda_FP, Caminho_NFs FROM Dados_cadastrais WHERE (CNPJ_CPF LIKE '@CNPJ%' AND Codigo_Instalacao = '@uc') OR (Razao_Social LIKE '@Razao_Social' AND Codigo_Instalacao = '@uc')";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.CommandText = selectSQL;
|
using (OleDbCommand cmd = new OleDbCommand(selectSQL, conn))
|
||||||
|
{
|
||||||
OleDbDataReader reader = cmd.ExecuteReader();
|
cmd.Parameters.AddWithValue("@CNPJ", parsedResult.customer.cnpj?[..8] ?? "NAN");
|
||||||
|
cmd.Parameters.AddWithValue("@uc", uc);
|
||||||
|
cmd.Parameters.AddWithValue("@Razao_Social", parsedResult.customer.name.Replace("'", "''"));
|
||||||
|
|
||||||
|
using (OleDbDataReader reader = cmd.ExecuteReader())
|
||||||
|
{
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
// Dados cadastrais
|
// Dados cadastrais
|
||||||
@ -103,15 +110,23 @@
|
|||||||
this.empresa = reader["Cliente"].ToString();
|
this.empresa = reader["Cliente"].ToString();
|
||||||
this.unidade = reader["Unidade"].ToString();
|
this.unidade = reader["Unidade"].ToString();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
reader.Close();
|
}
|
||||||
|
|
||||||
// Verifica se a fatura ja foi lançada
|
// Verifica se a fatura ja foi lançada
|
||||||
selectSQL = $"SELECT Cod_TUSD FROM Dados_TUSD WHERE Cod_TUSD = {long.Parse(dadosTusd.Cod_Smart_unidade.ToString() + dadosTusd.Mes.ToString())}";
|
bool tusdLanc;
|
||||||
cmd.CommandText = selectSQL;
|
|
||||||
reader = cmd.ExecuteReader();
|
selectSQL = $"SELECT Cod_TUSD FROM Dados_TUSD WHERE Cod_TUSD = @Cod_TUSD";
|
||||||
bool tusdLanc = reader.HasRows;
|
using (OleDbCommand cmd = new OleDbCommand(selectSQL, conn))
|
||||||
reader.Close();
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@Cod_TUSD", long.Parse(dadosTusd.Cod_Smart_unidade.ToString() + dadosTusd.Mes.ToString()));
|
||||||
|
|
||||||
|
using (OleDbDataReader reader = cmd.ExecuteReader())
|
||||||
|
{
|
||||||
|
tusdLanc = reader.HasRows;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (dadosTusd.Cod_Smart_unidade == 0)
|
if (dadosTusd.Cod_Smart_unidade == 0)
|
||||||
{
|
{
|
||||||
this.Status = "UNIDADE CONSUMIDORA NÃO LOCALIZADA NO BD";
|
this.Status = "UNIDADE CONSUMIDORA NÃO LOCALIZADA NO BD";
|
||||||
@ -127,20 +142,23 @@
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// PIS e Cofins
|
// PIS e Cofins
|
||||||
string pisSQL = $"SELECT Distribuidoras_PIS.PIS, Distribuidoras_PIS.COFINS FROM Distribuidoras_geral INNER JOIN Distribuidoras_PIS ON Distribuidoras_geral.ID_dist = Distribuidoras_PIS.ID_dist WHERE Distribuidoras_PIS.Mes='{dadosTusd.Mes}' AND Distribuidoras_geral.Distribuidora='{dadosTusd.Distribuidora}'";
|
string pisSQL = $"SELECT Distribuidoras_PIS.PIS, Distribuidoras_PIS.COFINS FROM Distribuidoras_geral INNER JOIN Distribuidoras_PIS ON Distribuidoras_geral.ID_dist = Distribuidoras_PIS.ID_dist WHERE Distribuidoras_PIS.Mes='@mes' AND Distribuidoras_geral.Distribuidora='@distribuidora'";
|
||||||
|
|
||||||
cmd.CommandText = pisSQL;
|
using (OleDbCommand cmd = new OleDbCommand(pisSQL, conn))
|
||||||
|
{
|
||||||
reader = cmd.ExecuteReader();
|
cmd.Parameters.AddWithValue("@mes", dadosTusd.Mes);
|
||||||
|
cmd.Parameters.AddWithValue("@distribuidora", dadosTusd.Distribuidora);
|
||||||
|
|
||||||
|
using (OleDbDataReader reader = cmd.ExecuteReader())
|
||||||
|
{
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
// PIS e Cofins
|
// PIS e Cofins
|
||||||
dadosTusd.PIS = float.Parse(reader["PIS"].ToString() !);
|
dadosTusd.PIS = float.Parse(reader["PIS"].ToString() !);
|
||||||
dadosTusd.COFINS = float.Parse(reader["COFINS"].ToString() !);
|
dadosTusd.COFINS = float.Parse(reader["COFINS"].ToString() !);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
reader.Close();
|
}
|
||||||
|
|
||||||
// Dados da fatura processada
|
// Dados da fatura processada
|
||||||
dadosTusd.Cod_TUSD = long.Parse(dadosTusd.Cod_Smart_unidade.ToString() + dadosTusd.Mes.ToString());
|
dadosTusd.Cod_TUSD = long.Parse(dadosTusd.Cod_Smart_unidade.ToString() + dadosTusd.Mes.ToString());
|
||||||
@ -191,7 +209,7 @@
|
|||||||
float? dem_Reg_FP = null;
|
float? dem_Reg_FP = null;
|
||||||
float? consumo_Reg_FP = null;
|
float? consumo_Reg_FP = null;
|
||||||
|
|
||||||
List<string> insertOthers = new List<string>();
|
List<(string, float)> insertOthers = new List<(string, float)>();
|
||||||
foreach (Item item in parsedResult.items)
|
foreach (Item item in parsedResult.items)
|
||||||
{
|
{
|
||||||
switch (item.type, item.period)
|
switch (item.type, item.period)
|
||||||
@ -224,43 +242,47 @@
|
|||||||
dadosTusd.Consumo_FP = dadosTusd.Consumo_FP + (item.billed / 1000);
|
dadosTusd.Consumo_FP = dadosTusd.Consumo_FP + (item.billed / 1000);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Demanda Ponta
|
// Demanda
|
||||||
case ("demand", "peak"):
|
case ("demand", _):
|
||||||
|
|
||||||
if (item.contract != 0)
|
if (item.contract != 0)
|
||||||
|
{
|
||||||
|
if (item.period == "peak")
|
||||||
{
|
{
|
||||||
dadosTusd.Dem_Cont_P = item.contract;
|
dadosTusd.Dem_Cont_P = item.contract;
|
||||||
if (dadosTusd.Perfil == "AZUL")
|
|
||||||
{
|
|
||||||
cmd.CommandText = $"UPDATE Dados_cadastrais SET Demanda_P = {item.contract} WHERE Cod_Smart_unidade = {dadosTusd.Cod_Smart_unidade}";
|
|
||||||
}
|
}
|
||||||
else if (dadosTusd.Perfil == "VERDE")
|
else if (item.period == "off-peak")
|
||||||
{
|
|
||||||
cmd.CommandText = $"UPDATE Dados_cadastrais SET Demanda_P = {item.contract}, Demanda_FP = {item.contract} WHERE Cod_Smart_unidade = {dadosTusd.Cod_Smart_unidade}";
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Demanda Fora de Ponta
|
|
||||||
case ("demand", "off-peak"):
|
|
||||||
|
|
||||||
if (item.contract != 0)
|
|
||||||
{
|
{
|
||||||
dadosTusd.Dem_Cont_FP = item.contract;
|
dadosTusd.Dem_Cont_FP = item.contract;
|
||||||
|
}
|
||||||
|
|
||||||
|
using (OleDbCommand cmd = new OleDbCommand(selectSQL, conn))
|
||||||
|
{
|
||||||
if (dadosTusd.Perfil == "AZUL")
|
if (dadosTusd.Perfil == "AZUL")
|
||||||
{
|
{
|
||||||
cmd.CommandText = $"UPDATE Dados_cadastrais SET Demanda_FP = {item.contract} WHERE Cod_Smart_unidade = {dadosTusd.Cod_Smart_unidade}";
|
if (item.period == "peak")
|
||||||
|
{
|
||||||
|
selectSQL = $"UPDATE Dados_cadastrais SET Demanda_P = @demanda WHERE Cod_Smart_unidade = @cod_smart_unidade";
|
||||||
|
cmd.Parameters.AddWithValue("@demanda", item.contract);
|
||||||
|
cmd.Parameters.AddWithValue("@cod_smart_unidade", dadosTusd.Cod_Smart_unidade);
|
||||||
|
}
|
||||||
|
else if (item.period == "off-peak")
|
||||||
|
{
|
||||||
|
selectSQL = $"UPDATE Dados_cadastrais SET Demanda_FP = @demanda WHERE Cod_Smart_unidade = @cod_smart_unidade";
|
||||||
|
cmd.Parameters.AddWithValue("@demanda", item.contract);
|
||||||
|
cmd.Parameters.AddWithValue("@cod_smart_unidade", dadosTusd.Cod_Smart_unidade);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (dadosTusd.Perfil == "VERDE")
|
else if (dadosTusd.Perfil == "VERDE")
|
||||||
{
|
{
|
||||||
cmd.CommandText = $"UPDATE Dados_cadastrais SET Demanda_P = {item.contract}, Demanda_FP = {item.contract} WHERE Cod_Smart_unidade = {dadosTusd.Cod_Smart_unidade}";
|
selectSQL = $"UPDATE Dados_cadastrais SET Demanda_P = @demanda, Demanda_FP = @demanda WHERE Cod_Smart_unidade = @cod_smart_unidade";
|
||||||
|
cmd.Parameters.AddWithValue("@demanda", item.contract);
|
||||||
|
cmd.Parameters.AddWithValue("@cod_smart_unidade", dadosTusd.Cod_Smart_unidade);
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -291,13 +313,15 @@
|
|||||||
// Exclui os items lançados anteriormente para a fatura e cria a instrução SQL para inserir os novos items
|
// Exclui os items lançados anteriormente para a fatura e cria a instrução SQL para inserir os novos items
|
||||||
if (j == 1)
|
if (j == 1)
|
||||||
{
|
{
|
||||||
string deleteOthers = $"DELETE FROM Dados_TUSD_aux WHERE Cod_TUSD = {dadosTusd.Cod_TUSD}";
|
string deleteOthers = $"DELETE FROM Dados_TUSD_aux WHERE Cod_TUSD = @cod_tusd";
|
||||||
cmd.CommandText = deleteOthers;
|
using (OleDbCommand cmd = new OleDbCommand(deleteOthers, conn))
|
||||||
|
{
|
||||||
|
cmd.Parameters.AddWithValue("@cod_tusd", dadosTusd.Cod_TUSD);
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
insertOthers.Add("INSERT INTO Dados_TUSD_aux (Cod_TUSD,Id,Nome,Valor,Cod_lanc) VALUES (" + dadosTusd.Cod_TUSD + "," + j + ",'" + item.name + "'," + item.charge.ToString(new CultureInfo("en-US")) + "," + 0 + ")");
|
insertOthers.Add((item.name, item.charge));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -401,11 +425,16 @@
|
|||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
|
|
||||||
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
||||||
foreach (string insert in insertOthers)
|
foreach ((string name, float valor) in insertOthers)
|
||||||
{
|
{
|
||||||
if (insert.Length != 0)
|
j++;
|
||||||
|
selectSQL = "INSERT INTO Dados_TUSD_aux (Cod_TUSD,Id,Nome,Valor,Cod_lanc) VALUES ( @cod_tusd, @j,'@name',@valor,0)";
|
||||||
|
using (OleDbCommand cmd = new OleDbCommand(selectSQL, conn))
|
||||||
{
|
{
|
||||||
cmd.CommandText = insert;
|
cmd.Parameters.AddWithValue("@cod_tusd", dadosTusd.Cod_TUSD);
|
||||||
|
cmd.Parameters.AddWithValue("@j", j);
|
||||||
|
cmd.Parameters.AddWithValue("@name", name);
|
||||||
|
cmd.Parameters.AddWithValue("@valor", valor);
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -457,11 +486,16 @@
|
|||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
|
|
||||||
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
||||||
foreach (string insert in insertOthers)
|
foreach ((string name, float valor) in insertOthers)
|
||||||
{
|
{
|
||||||
if (insert.Length != 0)
|
j++;
|
||||||
|
selectSQL = "INSERT INTO Dados_TUSD_aux (Cod_TUSD,Id,Nome,Valor,Cod_lanc) VALUES ( @cod_tusd, @j,'@name',@valor,0)";
|
||||||
|
using (OleDbCommand cmd = new OleDbCommand(selectSQL, conn))
|
||||||
{
|
{
|
||||||
cmd.CommandText = insert;
|
cmd.Parameters.AddWithValue("@cod_tusd", dadosTusd.Cod_TUSD);
|
||||||
|
cmd.Parameters.AddWithValue("@j", j);
|
||||||
|
cmd.Parameters.AddWithValue("@name", name);
|
||||||
|
cmd.Parameters.AddWithValue("@valor", valor);
|
||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,8 +18,6 @@
|
|||||||
#endif
|
#endif
|
||||||
public const string CaminhoDB = "X:/Middle/Informativo Setorial/Modelo Word/BD1_dados cadastrais e faturas.accdb";
|
public const string CaminhoDB = "X:/Middle/Informativo Setorial/Modelo Word/BD1_dados cadastrais e faturas.accdb";
|
||||||
private static HttpClient httpClient = new ();
|
private static HttpClient httpClient = new ();
|
||||||
private static OleDbConnection conn = new (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + CaminhoDB + ";Jet OLEDB:Database Password=gds21");
|
|
||||||
private static OleDbCommand cmd = new ();
|
|
||||||
private static StreamReader sr = new (LogFaturas);
|
private static StreamReader sr = new (LogFaturas);
|
||||||
private static StreamWriter sw = new (LogFaturas2);
|
private static StreamWriter sw = new (LogFaturas2);
|
||||||
private static string? fatura;
|
private static string? fatura;
|
||||||
@ -27,8 +25,9 @@
|
|||||||
public static void Main()
|
public static void Main()
|
||||||
{
|
{
|
||||||
// Abre a conexao com o banco de dados
|
// Abre a conexao com o banco de dados
|
||||||
|
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + CaminhoDB + ";Jet OLEDB:Database Password=gds21"))
|
||||||
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
cmd.Connection = conn;
|
|
||||||
|
|
||||||
// Loop entre as faturas pendentes
|
// Loop entre as faturas pendentes
|
||||||
while ((fatura = sr.ReadLine()) != null)
|
while ((fatura = sr.ReadLine()) != null)
|
||||||
@ -51,7 +50,7 @@
|
|||||||
|
|
||||||
if (fatura.Status == "SUCCESS" & !fatura.Agrupada)
|
if (fatura.Status == "SUCCESS" & !fatura.Agrupada)
|
||||||
{
|
{
|
||||||
fatura.Processar(cmd);
|
fatura.Processar(conn);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
fatura.Mover(separar: false);
|
fatura.Mover(separar: false);
|
||||||
@ -70,7 +69,7 @@
|
|||||||
|
|
||||||
if (faturaIndividual.Status == "SUCCESS")
|
if (faturaIndividual.Status == "SUCCESS")
|
||||||
{
|
{
|
||||||
faturaIndividual.Processar(cmd);
|
faturaIndividual.Processar(conn);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
faturaIndividual.Mover(separar: true);
|
faturaIndividual.Mover(separar: true);
|
||||||
@ -120,10 +119,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.Close();
|
|
||||||
sr.Close();
|
sr.Close();
|
||||||
sw.Close();
|
sw.Close();
|
||||||
File.Move(LogFaturas2, LogFaturas, true);
|
File.Move(LogFaturas2, LogFaturas, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user