diff --git a/Download Faturas/Fatura.cs b/Download Faturas/Fatura.cs index c4fd706..e30e495 100644 --- a/Download Faturas/Fatura.cs +++ b/Download Faturas/Fatura.cs @@ -9,16 +9,14 @@ public class Fatura { - /* A fatura deve ter alguma propriedades públicas: Local de origem, local de destino, Status do processamento - * Outras propriedas privadas: ID, Mes, agrupada, token, UC - * - * - * - */ - private const string Token = "UFY4VWzqcHYcGNd0gkBOMFL9G5ZThV6gXBQIJ79F5HSqITzavz4Fe7iXvAbJLvZJ"; - private JsonElement faturaParsed; + private string? id; + private string? empresa; + private string? unidade; + private string? pastaMiddle; + private string? uc; + private int mes; public Fatura(string id, string fatura_path, HttpClient httpClient) { @@ -29,8 +27,10 @@ { this.faturaParsed = JsonDocument.Parse(fatura_response.Content.ReadAsStringAsync().Result).RootElement; this.Agrupada = this.faturaParsed.TryGetProperty("multiple", out var _); - this.Status = this.faturaParsed.GetProperty("status").GetString() !; - this.Arquivo = fatura_path; + this.Status = this.faturaParsed.GetProperty("status").GetString(); + this.Arquivo = new FileInfo(fatura_path); + this.id = id; + if (this.Agrupada) { this.Agrupada_children = this.faturaParsed.GetProperty("children").EnumerateArray(); @@ -40,13 +40,7 @@ public string? Status { get; private set; } - public string? Pasta_middle { get; private set; } - - public string? Arquivo { get; private set; } - - public string? UC { get; private set; } - - public int Mes { get; private set; } + public FileInfo? Arquivo { get; private set; } public bool Agrupada { get; private set; } @@ -73,11 +67,11 @@ if (unidades == 1) { - selectSQL = $"SELECT Cod_Smart_unidade, PerfilCCEE, Submercado, Status_unidade, Grupo, Distribuidora, ICMS_TUSD, Demanda_P, Demanda_FP, Caminho_NFs FROM Dados_cadastrais WHERE 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 Codigo_Instalacao = '{uc}'"; } else { - selectSQL = $"SELECT Cod_Smart_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}' 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 '{parsedResult.customer.cnpj?.Substring(0, 8) ?? "NAN"}%' AND Codigo_Instalacao = '{uc}') OR (Razao_Social LIKE '{parsedResult.customer.name}' AND Codigo_Instalacao = '{uc}')"; } cmd.CommandText = selectSQL; @@ -87,16 +81,18 @@ while (reader.Read()) { // Dados cadastrais - dadosTusd.Cod_Smart_unidade = long.Parse(reader["Cod_Smart_unidade"].ToString()); + dadosTusd.Cod_Smart_unidade = long.Parse(reader["Cod_Smart_unidade"].ToString() !); dadosTusd.Perfil_CliqCCEE = reader["PerfilCCEE"].ToString(); dadosTusd.Submercado = reader["Submercado"].ToString(); dadosTusd.Ambiente = reader["Status_unidade"].ToString(); dadosTusd.Grupo = reader["Grupo"].ToString(); dadosTusd.Distribuidora = reader["Distribuidora"].ToString(); - dadosTusd.ICMS = float.Parse(reader["ICMS_TUSD"].ToString()); - dadosTusd.Dem_Cont_P = float.Parse(reader["Demanda_P"].ToString()); - dadosTusd.Dem_Cont_FP = float.Parse(reader["Demanda_FP"].ToString()); - this.Pasta_middle = reader["Caminho_NFs"].ToString(); + dadosTusd.ICMS = float.Parse(reader["ICMS_TUSD"].ToString() !); + dadosTusd.Dem_Cont_P = float.Parse(reader["Demanda_P"].ToString() !); + dadosTusd.Dem_Cont_FP = float.Parse(reader["Demanda_FP"].ToString() !); + this.pastaMiddle = reader["Caminho_NFs"].ToString(); + this.empresa = reader["Cliente"].ToString(); + this.unidade = reader["Unidade"].ToString(); } reader.Close(); @@ -110,14 +106,14 @@ if (dadosTusd.Cod_Smart_unidade == 0) { this.Status = "UNIDADE CONSUMIDORA NÃO LOCALIZADA NO BD"; - this.UC = uc; - this.Mes = dadosTusd.Mes; + this.uc = uc; + this.mes = dadosTusd.Mes; } else if (tusdLanc) { this.Status = "FATURA DUPLICADA NO BD"; - this.UC = uc; - this.Mes = dadosTusd.Mes; + this.uc = uc; + this.mes = dadosTusd.Mes; } else { @@ -131,8 +127,8 @@ while (reader.Read()) { // PIS e Cofins - dadosTusd.PIS = float.Parse(reader["PIS"].ToString()); - dadosTusd.COFINS = float.Parse(reader["COFINS"].ToString()); + dadosTusd.PIS = float.Parse(reader["PIS"].ToString() !); + dadosTusd.COFINS = float.Parse(reader["COFINS"].ToString() !); } reader.Close(); @@ -160,6 +156,7 @@ // Loop entre os dados faturados na fatura int j = 0; + StringBuilder insertOthers = new StringBuilder(); foreach (Item item in parsedResult.items) { switch (item.type, item.period) @@ -208,18 +205,20 @@ case ("other", _): j++; - // Exclui os items lançados anteriormente para a fatura + // Exclui os items lançados anteriormente para a fatura e cria a instrução SQL para inserir os novos items if (j == 1) { string deleteOthers = $"DELETE FROM Dados_TUSD_aux WHERE Cod_TUSD = {dadosTusd.Cod_TUSD}"; cmd.CommandText = deleteOthers; cmd.ExecuteNonQuery(); + + insertOthers.Append("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 + ")"); + } + else + { + insertOthers.Append(",(" + dadosTusd.Cod_TUSD + "," + j + ",'" + item.name + "'," + item.charge.ToString(new CultureInfo("en-US")) + "," + 0 + ")"); } - // Insere os novos items - string insertOthers = "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 + ")"; - cmd.CommandText = insertOthers; - cmd.ExecuteNonQuery(); break; } } @@ -288,6 +287,10 @@ updateSQL.Append($" WHERE Cod_TUSD = {dadosTusd.Cod_TUSD}"); cmd.CommandText = updateSQL.ToString(); cmd.ExecuteNonQuery(); + + // Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas + cmd.CommandText = insertOthers.ToString(); + cmd.ExecuteNonQuery(); } else { @@ -334,11 +337,15 @@ cmd.CommandText = insertSQL.ToString(); cmd.ExecuteNonQuery(); + + // Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas + cmd.CommandText = insertOthers.ToString(); + cmd.ExecuteNonQuery(); } this.Status = "FATURA INCLUIDA NO BD"; - this.UC = uc; - this.Mes = dadosTusd.Mes; + this.uc = uc; + this.mes = dadosTusd.Mes; } } @@ -347,27 +354,21 @@ switch (this.Status) { case "UNIDADE CONSUMIDORA NÃO LOCALIZADA NO BD": - + this.Arquivo!.MoveTo(this.Arquivo.Directory?.Parent?.FullName + $@"\5 - {this.Status}\ID: {this.id!} - Mês: {this.mes} - UC: {this.uc} - {this.Arquivo.Name}"); break; case "FATURA DUPLICADA NO BD": - + this.Arquivo!.MoveTo(this.Arquivo.Directory?.Parent?.FullName + $@"\4 - {this.Status}\ID: {this.id!} - Mês: {this.mes} - Empresa: {this.empresa} - Unidade: {this.unidade}"); break; case "FATURA INCLUIDA NO BD": - + this.Arquivo!.MoveTo(this.Arquivo.Directory?.Parent?.FullName + $@"\6 - {this.Status}\ID: {this.id!} - Mês: {this.uc} - Empresa: {this.empresa} - Unidade: {this.unidade}"); break; case "INVALID": - - break; - - case "DELAYED": - + this.Arquivo!.MoveTo(this.Arquivo.Directory?.Parent?.FullName + $@"\7 - ERRO\ID: {this.id!} - {this.Arquivo.Name}"); break; } - - } private HttpResponseMessage GetStatus(HttpClient httpClient, string token, string id) diff --git a/Download Faturas/Program.cs b/Download Faturas/Program.cs index 094d08f..3ab88cf 100644 --- a/Download Faturas/Program.cs +++ b/Download Faturas/Program.cs @@ -46,9 +46,8 @@ if (fatura.Status == "SUCCESS" & !fatura.Agrupada) { fatura.Processar(cmd); + fatura.Mover(); sw.WriteLine(fatura_ID + "," + fatura.Status + "," + fatura_arquivo); - - // mover_fatura(fatura_processamento, fatura_arquivo); } else if (fatura.Status == "SUCCESS" & fatura.Agrupada) { @@ -59,24 +58,24 @@ if (faturaIndividual.Status == "SUCCESS") { faturaIndividual.Processar(cmd); - + fatura.Mover(); sw.WriteLine(individual_ID.ToString() + "," + faturaIndividual.Status + "," + fatura_arquivo); - faturaIndividual.Mover(); - // mover fatura } else { sw.WriteLine(individual_ID.ToString() + "," + faturaIndividual.Status + "," + fatura_arquivo); - - // mover fatura + fatura.Mover(); } } } + else if (fatura.Status == "INVALID") + { + fatura.Mover(); + sw.WriteLine(fatura_ID + "," + fatura.Status + "," + fatura_arquivo); + } else { sw.WriteLine(fatura_ID + "," + fatura.Status + "," + fatura_arquivo); - - // mover fatura } } else