Download Faturas: Substituição de pasta NFe para TUSD utilizando Case Insensitive.

WebHook: Salvar somente arquivos do tipo "nota_fiscal", ignorar arquivos devec e decisões judiciais.
Salvar no histórico a mensagem de erro.
This commit is contained in:
Adriano Serighelli 2025-06-03 10:23:22 -03:00
parent b29ae2e3d3
commit 48c729e5b6
3 changed files with 31 additions and 15 deletions

View File

@ -147,7 +147,7 @@
dadosTusd.Dem_Cont_P = float.Parse(reader["Demanda_P"].ToString() !); dadosTusd.Dem_Cont_P = float.Parse(reader["Demanda_P"].ToString() !);
dadosTusd.Dem_Cont_FP = float.Parse(reader["Demanda_FP"].ToString() !); dadosTusd.Dem_Cont_FP = float.Parse(reader["Demanda_FP"].ToString() !);
dadosTusd.Perfil = reader["Perfil"].ToString(); dadosTusd.Perfil = reader["Perfil"].ToString();
this.PastaTUSD = new DirectoryInfo(reader["Caminho_NFs"].ToString() !.Replace("\\NFe", string.Empty) + "\\TUSD"); this.PastaTUSD = new DirectoryInfo(reader["Caminho_NFs"].ToString() !.Replace("\\NFe", string.Empty, StringComparison.OrdinalIgnoreCase) + "\\TUSD");
this.Gestao = reader["Gestao"].ToString(); this.Gestao = reader["Gestao"].ToString();
this.Empresa = reader["Cliente"].ToString(); this.Empresa = reader["Cliente"].ToString();
this.Unidade = reader["Unidade"].ToString(); this.Unidade = reader["Unidade"].ToString();
@ -254,6 +254,12 @@
string? kind_P = string.Empty; string? kind_P = string.Empty;
string? kind_FP = string.Empty; string? kind_FP = string.Empty;
if (this.id == "2356193")
{
int i = 0;
i++;
}
List<(string, float)> insertOthers = new List<(string, float)>(); List<(string, float)> insertOthers = new List<(string, float)>();
foreach (Item item in parsedResult.items) foreach (Item item in parsedResult.items)
{ {

View File

@ -1,4 +1,5 @@
using System.Net.Http.Headers; using System.Net;
using System.Net.Http.Headers;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
@ -82,19 +83,29 @@ public class Program
public static HttpResponseMessage SendFatura(string token, string fatura, bool agrupada) public static HttpResponseMessage SendFatura(string token, string fatura, bool agrupada)
{ {
var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.4docs.cloud/v2/parse"); var handler = new HttpClientHandler
{
//Proxy = new WebProxy("http://127.0.0.1:8888"),
//UseProxy = true,
//ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
};
request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {token}"); using (var httpClient = new HttpClient(handler))
{
var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.4docs.cloud/v2/parse");
var multipartContent = new MultipartFormDataContent(); request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {token}");
multipartContent.Add(new ByteArrayContent(File.ReadAllBytes(fatura)), @"""file""", @$"""{Path.GetFileName(fatura)}""");
multipartContent.ElementAt(0).Headers.Add("Content-Type", "application/pdf");
multipartContent.Add(new StringContent($"{{\"callbackUrl\":\"https://api.4docs.cloud/v2/null\",\"pipelineName\":\"energy\",\"multiple\":{agrupada.ToString().ToLower()},\"clientData\":{{\"fatura_PATH\":\"{fatura.Replace(",", string.Empty)}\"}}}}"), "json"); var multipartContent = new MultipartFormDataContent();
multipartContent.Add(new ByteArrayContent(File.ReadAllBytes(fatura)), @"""file""", @$"""{Path.GetFileName(fatura)}""");
multipartContent.ElementAt(0).Headers.Add("Content-Type", "application/pdf");
request.Content = multipartContent; multipartContent.Add(new StringContent($"{{\"callbackUrl\":\"https://api.4docs.cloud/v2/null\",\"pipelineName\":\"energy\",\"multiple\":{agrupada.ToString().ToLower()},\"clientData\":{{\"fatura_PATH\":\"{fatura.Replace(",", string.Empty)}\"}}}}"), "json");
return httpClient.Send(request); request.Content = multipartContent;
return httpClient.Send(request);
}
} }
public static bool IsFileLocked(FileInfo file) public static bool IsFileLocked(FileInfo file)

View File

@ -118,8 +118,7 @@ namespace Webhook_4docs
JsonElement DadosJson = JsonDocument.Parse(root.ToString()).RootElement; JsonElement DadosJson = JsonDocument.Parse(root.ToString()).RootElement;
if (root.Get("documentType")?.ToString().ToLower() == "devec") { return; } if (root.Get("documentType")?.ToString().ToLower() != "nota_fiscal") { return; }
Fatura fatura = new(fatura_ID, JsonBody); Fatura fatura = new(fatura_ID, JsonBody);
bool completed = false; bool completed = false;
@ -235,7 +234,7 @@ namespace Webhook_4docs
break; break;
} }
int test = await UpdateErrorIdStatusAsync(CaminhoDB, JsonBody.GetProperty("locationID").GetInt64(), errorID, JsonBody.GetProperty("systemText").ToString(), logger); int test = await UpdateErrorIdStatusAsync(CaminhoDB, JsonBody.GetProperty("locationID").GetInt64(), errorID, (JsonBody.GetProperty("systemText").ToString() == "" ? JsonBody.GetProperty("websiteText").ToString() : JsonBody.GetProperty("systemText").ToString()), logger);
if (test == 0) if (test == 0)
{ {
@ -361,8 +360,8 @@ namespace Webhook_4docs
using (OleDbCommand cmd = new OleDbCommand( using (OleDbCommand cmd = new OleDbCommand(
@"UPDATE AgVirtual4Docs @"UPDATE AgVirtual4Docs
SET errorID = ?, status = ? SET errorID = @errorID, status = @status
WHERE location_id = ?", conn)) WHERE location_id = @location_id", conn))
{ {
// Adiciona parâmetros de forma segura // Adiciona parâmetros de forma segura
cmd.Parameters.AddWithValue("@errorID", errorID); cmd.Parameters.AddWithValue("@errorID", errorID);