Criado nova biblioteca de classes para os demais projetos referênciarem a classe "Fatura.cs" Atualização de bibliotecas.
752 lines
32 KiB
C#
752 lines
32 KiB
C#
// <copyright file="Fatura.cs" company="Smart Energia">
|
|
// Copyright (c) Smart Energia. All rights reserved.
|
|
// </copyright>
|
|
|
|
namespace Faturas
|
|
{
|
|
using System.Data.OleDb;
|
|
using System.Net.Http.Headers;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.RegularExpressions;
|
|
|
|
/// <summary>
|
|
/// Representa uma fatura eletrônica e fornece métodos para processá-la e movê-la.
|
|
/// </summary>
|
|
public class Fatura
|
|
{
|
|
private const string Token = "UFY4VWzqcHYcGNd0gkBOMFL9G5ZThV6gXBQIJ79F5HSqITzavz4Fe7iXvAbJLvZJ";
|
|
|
|
/// <summary>
|
|
/// Gets or sets the data to be recorded for TUSD in the database.
|
|
/// </summary>
|
|
private readonly RecordSet dadosTusd = new ();
|
|
private readonly JsonElement faturaParsed;
|
|
private readonly string? id;
|
|
private readonly int? pagina;
|
|
private string? uc;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Fatura"/> class.
|
|
/// Inicializa uma nova instância da classe <see cref="Fatura"/>.
|
|
/// </summary>
|
|
/// <param name="id">Identificador da fatura no serviço 4docs.</param>
|
|
/// <param name="fatura_path">Caminho completo para o arquivo PDF da fatura local.</param>
|
|
/// <param name="httpClient">Instância de HttpClient usada para realizar chamadas HTTP à API.</param>
|
|
public Fatura(string id, string fatura_path, HttpClient httpClient)
|
|
{
|
|
// Utilizado para gerar novo token
|
|
// this.token = Req_token(HttpClient).ToString();
|
|
HttpResponseMessage fatura_response = GetStatus(httpClient, Token, id);
|
|
if (fatura_response.IsSuccessStatusCode)
|
|
{
|
|
faturaParsed = JsonDocument.Parse(fatura_response.Content.ReadAsStringAsync().Result).RootElement;
|
|
Agrupada = faturaParsed.TryGetProperty("multiple", out var _);
|
|
Status = faturaParsed.GetProperty("status").GetString();
|
|
Arquivo = new FileInfo(fatura_path);
|
|
this.id = id;
|
|
|
|
if (faturaParsed.GetProperty("external").GetString() is not null)
|
|
{
|
|
pagina = JsonDocument.Parse(faturaParsed.GetProperty("external").GetString() !).RootElement.GetProperty("origin")[0].GetProperty("page").GetInt32();
|
|
}
|
|
|
|
if (Agrupada)
|
|
{
|
|
Agrupada_children = faturaParsed.GetProperty("children").EnumerateArray();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Fatura"/> class.
|
|
/// Inicializa uma nova instância da classe <see cref="Fatura"/> para faturas já processadas.
|
|
/// </summary>
|
|
/// <param name="id">Identificador da fatura no serviço 4docs.</param>
|
|
/// <param name="fatura_Parsed">JsonElement contendo os dados da fatura já parseados.</param>
|
|
public Fatura(string id, JsonElement fatura_Parsed)
|
|
{
|
|
// Utilizado para gerar novo token
|
|
// this.token = Req_token(HttpClient).ToString();
|
|
faturaParsed = fatura_Parsed;
|
|
Agrupada = false;
|
|
this.id = id;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the TUSD code for the invoice.
|
|
/// </summary>
|
|
public double CodTusd { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets the consumer unit code associated with the invoice.
|
|
/// </summary>
|
|
public string? Gestao { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the company name associated with the invoice.
|
|
/// </summary>
|
|
public string? Empresa { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the unit name associated with the invoice.
|
|
/// </summary>
|
|
public string? Unidade { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the month associated with the invoice.
|
|
/// </summary>
|
|
public int? Mes { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the status of the invoice.
|
|
/// </summary>
|
|
public string? Status { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the file information of the invoice PDF.
|
|
/// </summary>
|
|
public FileInfo? Arquivo { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the directory information for the TUSD folder.
|
|
/// </summary>
|
|
public DirectoryInfo? PastaTUSD { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether the invoice is grouped.
|
|
/// </summary>
|
|
public bool Agrupada { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the children elements if the invoice is grouped.
|
|
/// </summary>
|
|
public JsonElement.ArrayEnumerator Agrupada_children { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Processes the invoice and records the TUSD data in the database.
|
|
/// </summary>
|
|
/// <param name="conn">An open OleDbConnection to the database used to insert or update TUSD records.</param>
|
|
public void Processar(OleDbConnection conn)
|
|
{
|
|
// Resultado da fatura processada
|
|
if (!faturaParsed.TryGetProperty("result", out JsonElement a))
|
|
{
|
|
faturaParsed.TryGetProperty("json", out a);
|
|
}
|
|
|
|
Rootobject parsedResult = a.Deserialize<Rootobject>() !;
|
|
if (parsedResult == null || parsedResult.dates == null || parsedResult.dates.reading == null || parsedResult.locationNumber == null || parsedResult.customer == null || parsedResult.items == null || parsedResult.dates.reading.periodUntil == DateTime.MinValue)
|
|
{
|
|
return;
|
|
}
|
|
|
|
dadosTusd.Mes = int.Parse(parsedResult.dates.reading.periodUntil.AddDays(-15).ToString("yMM"));
|
|
|
|
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
|
|
int? unidades;
|
|
string sqlQuery = $"SELECT COUNT (Cod_Smart_unidade) FROM Dados_cadastrais WHERE Codigo_Instalacao = @uc AND unidade_gerenciada";
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@uc", uc);
|
|
|
|
unidades = (int?)cmd.ExecuteScalar();
|
|
|
|
if (unidades == 1)
|
|
{
|
|
sqlQuery = $"SELECT Cod_Smart_unidade, Gestao, Cliente, Unidade, PerfilCCEE, Submercado, Status_unidade, Grupo, Perfil, Distribuidora, ICMS_TUSD, Demanda_P, Demanda_FP, Caminho_NFs, Data_de_Migracao FROM Dados_cadastrais WHERE Codigo_Instalacao = @uc AND unidade_gerenciada";
|
|
}
|
|
else
|
|
{
|
|
sqlQuery = $"SELECT Cod_Smart_unidade, Gestao, Cliente, Unidade, PerfilCCEE, Submercado, Status_unidade, Grupo, Perfil, Distribuidora, ICMS_TUSD, Demanda_P, Demanda_FP, Caminho_NFs, Data_de_Migracao FROM Dados_cadastrais WHERE (CNPJ_CPF LIKE @cnpj AND Codigo_Instalacao = @uc AND unidade_gerenciada) OR (Razao_Social LIKE @razao_social AND Codigo_Instalacao = @uc AND unidade_gerenciada)";
|
|
}
|
|
}
|
|
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
if (unidades == 1)
|
|
{
|
|
cmd.Parameters.AddWithValue("@uc", uc);
|
|
}
|
|
else
|
|
{
|
|
cmd.Parameters.AddWithValue("@cnpj", parsedResult.customer.cnpj?[..8] != null ? parsedResult.customer.cnpj?[..8] + "%" : "NAN");
|
|
cmd.Parameters.AddWithValue("@uc", uc);
|
|
cmd.Parameters.AddWithValue("@razao_social", parsedResult.customer.name);
|
|
}
|
|
|
|
using (OleDbDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
// Dados cadastrais
|
|
dadosTusd.Cod_Smart_unidade = long.Parse(reader["Cod_Smart_unidade"].ToString() !);
|
|
dadosTusd.Perfil_CliqCCEE = reader["PerfilCCEE"].ToString();
|
|
dadosTusd.Submercado = reader["Submercado"].ToString();
|
|
DateTime dataMigração = DateTime.Parse(reader["Data_de_Migracao"].ToString() !);
|
|
if (int.Parse(dataMigração.ToString("yMM")) <= dadosTusd.Mes)
|
|
{
|
|
dadosTusd.Ambiente = reader["Status_unidade"].ToString();
|
|
}
|
|
else
|
|
{
|
|
dadosTusd.Ambiente = "Cativo";
|
|
}
|
|
|
|
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() !);
|
|
dadosTusd.Perfil = reader["Perfil"].ToString();
|
|
PastaTUSD = new DirectoryInfo(reader["Caminho_NFs"].ToString() !.Replace("\\NFe", string.Empty, StringComparison.OrdinalIgnoreCase) + "\\TUSD");
|
|
Gestao = reader["Gestao"].ToString();
|
|
Empresa = reader["Cliente"].ToString();
|
|
Unidade = reader["Unidade"].ToString();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Verifica se a fatura ja foi lançada
|
|
bool tusdLanc;
|
|
|
|
sqlQuery = $"SELECT Cod_TUSD FROM Dados_TUSD WHERE Cod_TUSD = ?";
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
CodTusd = double.Parse(dadosTusd.Cod_Smart_unidade.ToString() + dadosTusd.Mes.ToString());
|
|
cmd.Parameters.AddWithValue("?", CodTusd);
|
|
|
|
using (OleDbDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
tusdLanc = reader.HasRows;
|
|
}
|
|
}
|
|
|
|
if (dadosTusd.Cod_Smart_unidade == 0)
|
|
{
|
|
Status = "UNIDADE CONSUMIDORA NÃO LOCALIZADA NO BD";
|
|
this.uc = uc;
|
|
Mes = dadosTusd.Mes;
|
|
return;
|
|
}
|
|
else if (tusdLanc)
|
|
{
|
|
Status = "FATURA DUPLICADA NO BD";
|
|
this.uc = uc;
|
|
Mes = dadosTusd.Mes;
|
|
return;
|
|
}
|
|
|
|
// PIS e Cofins
|
|
sqlQuery = $"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 = ? AND Distribuidoras_geral.Distribuidora = ?";
|
|
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("?", dadosTusd.Mes.ToString());
|
|
cmd.Parameters.AddWithValue("?", dadosTusd.Distribuidora);
|
|
|
|
using (OleDbDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
// PIS e Cofins
|
|
dadosTusd.PIS = float.Parse(reader["PIS"].ToString() !);
|
|
dadosTusd.COFINS = float.Parse(reader["COFINS"].ToString() !);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Dados da fatura processada
|
|
dadosTusd.Cod_TUSD = long.Parse(dadosTusd.Cod_Smart_unidade.ToString() + dadosTusd.Mes.ToString());
|
|
switch (parsedResult.tariffModality, parsedResult.subgroup)
|
|
{
|
|
case ("blue", _):
|
|
dadosTusd.Perfil = "AZUL";
|
|
break;
|
|
|
|
case ("green", _):
|
|
dadosTusd.Perfil = "VERDE";
|
|
break;
|
|
|
|
case ("standart", _):
|
|
dadosTusd.Perfil = "CONVENCIONAL";
|
|
break;
|
|
|
|
case (_, "B3"):
|
|
dadosTusd.Perfil = "CONVENCIONAL";
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
dadosTusd.Valor = parsedResult.totalCharges;
|
|
dadosTusd.Inicio_Leitura = parsedResult.dates.reading.periodFrom;
|
|
dadosTusd.Fim_leitura = parsedResult.dates.reading.periodUntil;
|
|
DateTime d = DateTime.Now;
|
|
dadosTusd.Hora_TUSD = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second);
|
|
dadosTusd.Dem_Reativa_kvar = 0;
|
|
dadosTusd.Multa = 0;
|
|
dadosTusd.Credito = 0;
|
|
dadosTusd.Bandeira_RS_MWh = 0;
|
|
dadosTusd.FIC_DIC = 0;
|
|
dadosTusd.Enc_conexao = 0;
|
|
dadosTusd.Liminar_ICMS = 0;
|
|
dadosTusd.Outros = 0;
|
|
dadosTusd.Cred_livre = 0;
|
|
dadosTusd.Tempo_TUSD = 0;
|
|
dadosTusd.Lanc_aut = true;
|
|
dadosTusd.Rev_atual = true;
|
|
dadosTusd.Revisao = 0;
|
|
|
|
// Loop entre os dados faturados na fatura
|
|
int j = 0;
|
|
float? dem_Reg_P = null;
|
|
float? dem_Reg_FP = null;
|
|
float? consumo_Reg_FP = 0;
|
|
string? kind_P = string.Empty;
|
|
string? kind_FP = string.Empty;
|
|
|
|
List<(string, float)> insertOthers = [];
|
|
foreach (Item item in parsedResult.items)
|
|
{
|
|
switch (item.type, item.period)
|
|
{
|
|
// Energia Ponta
|
|
case ("energy", "peak"):
|
|
if (kind_P == string.Empty)
|
|
{
|
|
kind_P = item.kind;
|
|
}
|
|
|
|
if (item.kind == kind_P || item.kind == "TUSD")
|
|
{
|
|
if (item.billed / 1000 != dadosTusd.Consumo_P)
|
|
{
|
|
dadosTusd.Consumo_P += item.billed / 1000;
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
// Energia Fora de Ponta
|
|
case ("energy", _):
|
|
if (item.period == "off-peak" || item.period == "off-peak inductive" || item.period == "off-peak capacitive" || item.period == "reserved")
|
|
{
|
|
if (kind_FP == string.Empty)
|
|
{
|
|
kind_FP = item.kind;
|
|
}
|
|
|
|
if (item.kind == kind_FP)
|
|
{
|
|
if (item.billed / 1000 != dadosTusd.Consumo_FP)
|
|
{
|
|
dadosTusd.Consumo_FP += item.billed / 1000;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
insertOthers.Add((item.texts[0], item.charge));
|
|
}
|
|
|
|
break;
|
|
|
|
// Demanda
|
|
case ("demand", _):
|
|
|
|
if (item.contract != 0)
|
|
{
|
|
if (item.period == "peak")
|
|
{
|
|
dadosTusd.Dem_Reg_P = item.billed;
|
|
dadosTusd.Dem_Cont_P = item.contract;
|
|
}
|
|
else if (item.period == "off-peak")
|
|
{
|
|
dadosTusd.Dem_Reg_FP = item.billed;
|
|
dadosTusd.Dem_Cont_FP = item.contract;
|
|
}
|
|
|
|
if (dadosTusd.Perfil == "AZUL")
|
|
{
|
|
if (item.period == "peak")
|
|
{
|
|
sqlQuery = $"UPDATE Dados_cadastrais SET Demanda_P = @demanda WHERE Cod_Smart_unidade = @cod_smart_unidade";
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@demanda", item.contract);
|
|
cmd.Parameters.AddWithValue("@cod_smart_unidade", dadosTusd.Cod_Smart_unidade);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
else if (item.period == "off-peak")
|
|
{
|
|
sqlQuery = $"UPDATE Dados_cadastrais SET Demanda_FP = @demanda WHERE Cod_Smart_unidade = @cod_smart_unidade";
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@demanda", item.contract);
|
|
cmd.Parameters.AddWithValue("@cod_smart_unidade", dadosTusd.Cod_Smart_unidade);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
else if (dadosTusd.Perfil == "VERDE")
|
|
{
|
|
sqlQuery = $"UPDATE Dados_cadastrais SET Demanda_P = @demanda, Demanda_FP = @demanda WHERE Cod_Smart_unidade = @cod_smart_unidade";
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@demanda", item.contract);
|
|
cmd.Parameters.AddWithValue("@cod_smart_unidade", dadosTusd.Cod_Smart_unidade);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
// Ilum. publica
|
|
case ("publicLighting", _):
|
|
dadosTusd.Ilum_Publica = item.charge;
|
|
break;
|
|
|
|
// Energia Reativa
|
|
case ("excessReactiveEnergy", _):
|
|
dadosTusd.En_Reativa_Mvarh += item.billed / 1000;
|
|
break;
|
|
|
|
// Demanda Reativa
|
|
case ("excessReactiveDemand", _):
|
|
dadosTusd.Dem_Reativa_kvar += item.billed;
|
|
break;
|
|
|
|
// Bandeira Tarifaria
|
|
case ("flagSurcharge", _):
|
|
dadosTusd.Bandeira_RS_MWh = item.charge;
|
|
break;
|
|
|
|
// Items não classificados
|
|
case ("other", _):
|
|
j++;
|
|
|
|
// Exclui os items lançados anteriormente para a fatura e cria a instrução SQL para inserir os novos items
|
|
if (j == 1)
|
|
{
|
|
sqlQuery = $"DELETE FROM Dados_TUSD_aux WHERE Cod_TUSD = @cod_tusd";
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
cmd.Parameters.AddWithValue("@cod_tusd", dadosTusd.Cod_TUSD);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
|
|
insertOthers.Add((item.name, item.charge));
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Busca a demanda registrada nos itens medidos
|
|
foreach (Measureditem measuredItem in parsedResult.measuredItems)
|
|
{
|
|
switch (measuredItem.type, measuredItem.period)
|
|
{
|
|
case ("demand", "peak"):
|
|
dem_Reg_P = measuredItem.measured.Max();
|
|
break;
|
|
case ("demand", "off-peak"):
|
|
dem_Reg_FP = measuredItem.measured.Max();
|
|
break;
|
|
case ("energy", "off-peak"):
|
|
consumo_Reg_FP = consumo_Reg_FP.GetValueOrDefault() + measuredItem.measured.Sum();
|
|
break;
|
|
case ("energy", "off-peak inductive"):
|
|
consumo_Reg_FP = consumo_Reg_FP.GetValueOrDefault() + measuredItem.measured.Sum();
|
|
break;
|
|
case ("energy", "off-peak capacitive"):
|
|
consumo_Reg_FP = consumo_Reg_FP.GetValueOrDefault() + measuredItem.measured.Sum();
|
|
break;
|
|
case ("energy", "off-peak capacitive green flag"):
|
|
consumo_Reg_FP = consumo_Reg_FP.GetValueOrDefault() + measuredItem.measured.Sum();
|
|
break;
|
|
case ("energy", "off-peak inductive green flag"):
|
|
consumo_Reg_FP = consumo_Reg_FP.GetValueOrDefault() + measuredItem.measured.Sum();
|
|
break;
|
|
case ("energy", "reserved"):
|
|
consumo_Reg_FP = consumo_Reg_FP.GetValueOrDefault() + measuredItem.measured.Sum();
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (dem_Reg_P != null && Math.Round((decimal)dadosTusd.Consumo_FP, 2) == Math.Round((decimal)(consumo_Reg_FP * 1.025 / 1000) !, 2))
|
|
{
|
|
dadosTusd.Dem_Reg_P = (float)(dem_Reg_P * 1.025);
|
|
}
|
|
else if (dem_Reg_P != null)
|
|
{
|
|
dadosTusd.Dem_Reg_P = (float)dem_Reg_P;
|
|
}
|
|
|
|
if (dem_Reg_FP != null && Math.Round((decimal)dadosTusd.Consumo_FP, 2) == Math.Round((decimal)(consumo_Reg_FP * 1.025 / 1000) !, 2))
|
|
{
|
|
dadosTusd.Dem_Reg_FP = (float)(dem_Reg_FP * 1.025);
|
|
}
|
|
else if (dem_Reg_FP != null)
|
|
{
|
|
dadosTusd.Dem_Reg_FP = dem_Reg_FP ?? 0;
|
|
}
|
|
|
|
// var dados = this.dadosTusd.GetType().GetProperties();
|
|
// StringBuilder fields = new StringBuilder();
|
|
// StringBuilder values = new StringBuilder();
|
|
|
|
// Verifica se já existe a fatura lançada | atualizar fatura ou nova fatura
|
|
if (tusdLanc)
|
|
{
|
|
// Cria o comando para atualização da fatura
|
|
sqlQuery = "UPDATE Dados_TUSD SET ";
|
|
|
|
// Loop por todos os dados ja obtidos
|
|
PropertyInfo[] propriedades = typeof(RecordSet).GetProperties();
|
|
foreach (PropertyInfo propriedade in propriedades)
|
|
{
|
|
string nomeColuna = propriedade.Name;
|
|
|
|
// Adicione a coluna à instrução SQL
|
|
sqlQuery += $"{nomeColuna} = @{nomeColuna}, ";
|
|
}
|
|
|
|
// Remova a última vírgula e adicione a cláusula WHERE
|
|
sqlQuery = sqlQuery.TrimEnd(',', ' ') + $" WHERE Cod_TUSD = @cod_tusd";
|
|
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
foreach (PropertyInfo propriedade in propriedades)
|
|
{
|
|
string nomeColuna = propriedade.Name;
|
|
object valorColuna = propriedade.GetValue(dadosTusd) ?? string.Empty;
|
|
cmd.Parameters.AddWithValue($"@{nomeColuna}", valorColuna);
|
|
}
|
|
|
|
// Valor do código TUSD na cláusula WHERE
|
|
cmd.Parameters.AddWithValue("@cod_tusd", dadosTusd.Cod_TUSD);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
|
foreach ((string name, float valor) in insertOthers)
|
|
{
|
|
j++;
|
|
sqlQuery = "INSERT INTO Dados_TUSD_aux (Cod_TUSD,Id,Nome,Valor,Cod_lanc) VALUES (@cod_tusd, @j, @name, @valor,0)";
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Cria o comando para lançar da fatura
|
|
sqlQuery = "INSERT INTO Dados_TUSD (";
|
|
|
|
PropertyInfo[] propriedades = typeof(RecordSet).GetProperties();
|
|
/*int i = 0;*/
|
|
foreach (PropertyInfo propriedade in propriedades)
|
|
{
|
|
string nomeColuna = propriedade.Name;
|
|
|
|
// Adicione a coluna à instrução SQL
|
|
sqlQuery += $"{nomeColuna}, ";
|
|
}
|
|
|
|
// Remova a última vírgula e adicione a cláusula VALUES
|
|
sqlQuery = sqlQuery.TrimEnd(',', ' ') + ") VALUES (";
|
|
|
|
foreach (PropertyInfo propriedade in propriedades)
|
|
{
|
|
// Adicione os parâmetros à instrução SQL
|
|
string nomeColuna = propriedade.Name;
|
|
sqlQuery += $"@{nomeColuna}, ";
|
|
}
|
|
|
|
// Remova a última vírgula e feche a instrução SQL
|
|
sqlQuery = sqlQuery.TrimEnd(',', ' ') + ")";
|
|
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
foreach (PropertyInfo propriedade in propriedades)
|
|
{
|
|
string nomeColuna = propriedade.Name;
|
|
object valorColuna = propriedade.GetValue(dadosTusd) ?? string.Empty;
|
|
cmd.Parameters.AddWithValue($"@{nomeColuna}", valorColuna);
|
|
}
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
// Inseri os itens classificados como "othes" após criar o registro da TUSD devido as relação entre as tabelas
|
|
j = 0;
|
|
foreach ((string name, float valor) in insertOthers)
|
|
{
|
|
j++;
|
|
sqlQuery = "INSERT INTO Dados_TUSD_aux (Cod_TUSD,Id,Nome,Valor,Cod_lanc) VALUES (@cod_tusd, @j, @name, @valor, 0)";
|
|
using (OleDbCommand cmd = new (sqlQuery, conn))
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
Status = "FATURA INCLUIDA NO BD";
|
|
this.uc = uc;
|
|
Mes = dadosTusd.Mes;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Moves the invoice PDF to the appropriate directory based on its status.
|
|
/// </summary>
|
|
/// <param name="separar">If true, split the PDF into the target page range before moving; if false, move the whole file.</param>
|
|
public void Mover(bool separar)
|
|
{
|
|
string destino;
|
|
|
|
switch (Status, separar)
|
|
{
|
|
case ("UNIDADE CONSUMIDORA NÃO LOCALIZADA NO BD", _):
|
|
|
|
destino = Arquivo?.Directory?.Parent?.FullName + $@"\5 - {Status}\ID {id!} - Mês {Mes} - UC {uc}.pdf";
|
|
|
|
if (separar)
|
|
{
|
|
_ = new PDFSplitter(pagina, Arquivo!.ToString(), destino);
|
|
Arquivo = new FileInfo(destino);
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
Arquivo!.MoveTo(destino);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
case ("FATURA DUPLICADA NO BD", _):
|
|
|
|
destino = Arquivo?.Directory?.Parent?.FullName + $@"\4 - {Status}\ID {id!} - Mês {Mes} - Empresa {Empresa} - Unidade {Unidade}.pdf";
|
|
|
|
if (separar)
|
|
{
|
|
_ = new PDFSplitter(pagina, Arquivo!.ToString(), destino);
|
|
Arquivo = new FileInfo(destino);
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
Arquivo!.MoveTo(destino);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
case ("FATURA INCLUIDA NO BD", _):
|
|
|
|
if (PastaTUSD!.Exists)
|
|
{
|
|
destino = PastaTUSD!.FullName + $@"\ID {id!} - Mês {Mes} - Empresa {Empresa} - Unidade {Unidade}.pdf";
|
|
}
|
|
else
|
|
{
|
|
destino = Arquivo?.Directory?.Parent?.FullName + $@"\6 - {Status}\ID {id!} - Mês {Mes} - Empresa {Empresa} - Unidade {Unidade}.pdf";
|
|
}
|
|
|
|
if (separar)
|
|
{
|
|
_ = new PDFSplitter(pagina, Arquivo!.ToString(), destino);
|
|
Arquivo = new FileInfo(destino);
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
Arquivo!.MoveTo(destino);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
case ("INVALID", _):
|
|
|
|
destino = Arquivo?.Directory?.Parent?.FullName + $@"\7 - ERRO\ID {id!} - {Arquivo?.Name}";
|
|
|
|
if (separar)
|
|
{
|
|
_ = new PDFSplitter(pagina, Arquivo!.ToString(), destino);
|
|
Arquivo = new FileInfo(destino);
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
Arquivo!.MoveTo(destino);
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
case (_, true):
|
|
|
|
destino = Arquivo?.Directory?.Parent?.FullName + $@"\3 - PROCESSANDO\ID {id!} - {Arquivo?.Name}";
|
|
|
|
_ = new PDFSplitter(pagina, Arquivo!.ToString(), destino);
|
|
Arquivo = new FileInfo(destino);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private static HttpResponseMessage GetStatus(HttpClient httpClient, string token, string id)
|
|
{
|
|
var request = new HttpRequestMessage(new HttpMethod("GET"), $"https://api.4docs.cloud/v2/request/status?id={id}");
|
|
request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {token}");
|
|
var response = httpClient.Send(request);
|
|
return response;
|
|
}
|
|
|
|
private static string Req_token(HttpClient httpClient)
|
|
{
|
|
var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.4docs.cloud/v2/oauth2/token");
|
|
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("smart:vnqtvmesikjzyipc"));
|
|
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
|
|
request.Content = new StringContent("grant_type=client_credentials");
|
|
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
|
|
var response = httpClient.Send(request);
|
|
return JsonDocument.Parse(response.Content.ReadAsStringAsync().Result).RootElement.GetProperty("access_token").GetString() !;
|
|
}
|
|
}
|
|
} |