242 lines
9.5 KiB
C#
242 lines
9.5 KiB
C#
//namespace NfProcessorApp
|
|
//{
|
|
// class Program
|
|
// {
|
|
// static void Main()
|
|
// {
|
|
// // Caminho da pasta configurado em appsettings.json
|
|
// var app = Application.Build();
|
|
// app.Run();
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
using System.Data;
|
|
using System.Text;
|
|
using Unimake.Business.DFe.Xml.NFe;
|
|
using Emit = Unimake.Business.DFe.Xml.NFe.Emit;
|
|
using System.Reflection;
|
|
|
|
class Program
|
|
{
|
|
static async Task Main()
|
|
{
|
|
string pasta = @"X:\Back\Controle NFs\NFs";
|
|
var arquivosXML = Directory.GetFiles(pasta, "*.Xml");
|
|
var arquivosPDF = Directory.GetFiles(pasta, "*.pdf");
|
|
var arquivos = arquivosXML.Concat(arquivosPDF);
|
|
var tarefas = new List<Task<string>>();
|
|
var linhas = new List<string>();
|
|
//Processar arquivosXML em paralelo
|
|
foreach (string arquivo in arquivos)
|
|
{
|
|
tarefas.Add(ProcessarXMLAsync(arquivo));
|
|
//linhas.Add(await ProcessarXMLAsync(arquivo));
|
|
}
|
|
|
|
linhas.AddRange(await Task.WhenAll(tarefas));
|
|
|
|
// Gerar CSV
|
|
StringBuilder csv = new();
|
|
|
|
foreach (string linha in linhas)
|
|
{
|
|
csv.AppendLine(linha);
|
|
}
|
|
|
|
// Escrever CSV no StdOut (VBA vai capturar)
|
|
Console.OutputEncoding = Encoding.Latin1;
|
|
Console.Write(csv.ToString());
|
|
//dados.Dispose();
|
|
}
|
|
static string ProcessarArquivo(string caminhoArquivo)
|
|
{
|
|
string nomeArquivo = Path.GetFileName(caminhoArquivo);
|
|
string tipoArquivo = Path.GetExtension(caminhoArquivo).TrimStart('.');
|
|
string referencia = nomeArquivo.Split(" - ").FirstOrDefault() ?? "";
|
|
|
|
// Formatar como CSV (com proteção contra vírgulas)
|
|
return $"\"{caminhoArquivo}\";\"{nomeArquivo}\";\"{tipoArquivo}\";\"{referencia}\"";
|
|
}
|
|
static async Task<string> ProcessarXMLAsync(string caminhoArquivo)
|
|
{
|
|
NFresponse response = new();
|
|
string status = "";
|
|
try
|
|
{
|
|
if (Path.GetExtension(caminhoArquivo).Equals(".Xml", StringComparison.CurrentCultureIgnoreCase))
|
|
{
|
|
//if (caminhoArquivo == @"X:\Back\Controle NFs\NFs\345425911,145611 - NFe35250603984862000194550010001466521382788552-procNFe.xml")
|
|
//{
|
|
// Thread.Sleep(1000);
|
|
//}
|
|
NF Current_NF = new(caminhoArquivo);
|
|
|
|
InfNFe infNFe = new NfeProc().LoadFromFile(caminhoArquivo).NFe.InfNFe.First();
|
|
Dest Comprador = infNFe.Dest;
|
|
Emit Vendedor = infNFe.Emit;
|
|
List<Det> Produtos = infNFe.Det;
|
|
|
|
float soma1 = Produtos.Sum(prod => (float?)prod.Prod.QCom ?? 0);
|
|
decimal soma2 = Produtos.Sum(prod =>
|
|
(
|
|
(
|
|
prod.Prod.UCom == "KWH" ?
|
|
prod.Prod.QCom / 1000M :
|
|
prod.Prod.QCom
|
|
)
|
|
) *
|
|
(
|
|
(
|
|
prod.Prod.UCom == "KWH" ?
|
|
prod.Prod.VUnCom * 1000M :
|
|
prod.Prod.VUnCom
|
|
)
|
|
));
|
|
|
|
if (Current_NF.CNPJ_Vendedor == "06981176000158" || Current_NF.CNPJ_Vendedor == "06981176002100" || Current_NF.CNPJ_Vendedor == "17155730000164")
|
|
{
|
|
soma1 /= 1000;
|
|
}
|
|
|
|
if (soma1 != 0)
|
|
{
|
|
decimal Valor_unitário = soma2 / (decimal)soma1;
|
|
}
|
|
|
|
response.CNPJ_Comprador = Current_NF.CNPJ_Comprador;
|
|
response.CNPJ_Vendedor = Current_NF.CNPJ_Vendedor;
|
|
response.Montante_operação = Current_NF.Montante_operação;
|
|
response.Valor_unitário = Current_NF.Valor_unitário;
|
|
response.Valor_final_c_impostos = Current_NF.Valor_final_c_impostos;
|
|
response.RS_Comprador = Current_NF.RS_Comprador ?? "";
|
|
response.RS_Vendedor = Current_NF.RS_Vendedor ?? "";
|
|
response.Numero_NF = Current_NF.Numero_NF ?? 0;
|
|
response.ICMS_NF = Current_NF.ICMS_NF;
|
|
status = Current_NF.UF_NF ?? "";
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
status = "XML vazio";
|
|
}
|
|
return await Task.Run(() =>
|
|
{
|
|
return $"{ProcessarArquivo(caminhoArquivo)};\"{response.CNPJ_Comprador}\";\"{response.CNPJ_Vendedor}\";\"{response.Montante_operação.ToString()?.Replace(',', '.')}\";\"{response.Valor_unitário.ToString()?.Replace(',', '.')}\";\"{response.Valor_final_c_impostos.ToString()?.Replace(',', '.')}\";\"{response.RS_Comprador}\";\"{response.RS_Vendedor}\";\"{response.Numero_NF}\";\"{response.ICMS_NF.ToString()?.Replace(',', '.')}\";\"{status}\"";
|
|
});
|
|
}
|
|
public static double GetICMS(ICMS icms)
|
|
{
|
|
var propICMS = icms.GetType()
|
|
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
|
// Opcional: filtrar apenas pelas que começam com "ICMS"
|
|
.Where(p => p.Name.StartsWith("ICMS"))
|
|
.ToList();
|
|
var primeiraProp = propICMS
|
|
.Select(p => new { Prop = p, Valor = p.GetValue(icms) })
|
|
.FirstOrDefault(x => x.Valor != null);
|
|
if (primeiraProp == null) { return 0; }
|
|
if (primeiraProp.Valor == null) { return 0; }
|
|
|
|
var tipo = primeiraProp.Valor.GetType();
|
|
|
|
var valor = Convert.ChangeType(primeiraProp.Valor, tipo);
|
|
|
|
var ListICMSReal = valor.GetType()
|
|
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
|
.Where(p => p.Name.StartsWith("PICMS"))
|
|
.Select(p => p.GetValue(valor))
|
|
.FirstOrDefault();
|
|
|
|
double ICMSReal = 0;
|
|
if (ListICMSReal != null && double.TryParse(ListICMSReal.ToString(), out ICMSReal))
|
|
{
|
|
ICMSReal /= 100d; // Convert percentage to decimal
|
|
}
|
|
return ICMSReal;
|
|
}
|
|
public class NFresponse
|
|
{
|
|
public string CNPJ_Comprador { get; set; } = "";
|
|
public string CNPJ_Vendedor { get; set; } = "";
|
|
public decimal Montante_operação { get; set; } = 0;
|
|
public decimal Valor_unitário { get; set; } = 0;
|
|
public double Valor_final_c_impostos { get; set; } = 0;
|
|
public string RS_Comprador { get; set; } = "";
|
|
public string RS_Vendedor { get; set; } = "";
|
|
public int Numero_NF { get; set; } = 0;
|
|
public double ICMS_NF { get; set; } = 0;
|
|
}
|
|
public class NF
|
|
(
|
|
string caminhoArquivo
|
|
)
|
|
{
|
|
readonly InfNFe infNFe = new NfeProc().LoadFromFile(caminhoArquivo).NFe.InfNFe.First();
|
|
Dest Comprador => infNFe.Dest;
|
|
Emit Vendedor => infNFe.Emit;
|
|
Det Detalhes => infNFe.Det.First();
|
|
List<Det> Produtos => infNFe.Det;
|
|
Imposto Impostos => Detalhes.Imposto;
|
|
public string NomeArquivo => Path.GetFileName(caminhoArquivo);
|
|
public string? TipoArquivo => Path.GetExtension(caminhoArquivo).TrimStart('.');
|
|
public string? Referencia => this.NomeArquivo.Split(" - ").FirstOrDefault() ?? "";
|
|
public string CNPJ_Comprador => Convert.ToInt64(Comprador.CNPJ ?? Comprador.CPF).ToString(@"00000000000000");
|
|
public string CNPJ_Vendedor => Convert.ToInt64(Vendedor.CNPJ).ToString(@"00000000000000");
|
|
|
|
public decimal Soma2 =>
|
|
Produtos.Sum(prod =>
|
|
(
|
|
(
|
|
prod.Prod.UCom == "KWH" ?
|
|
prod.Prod.QCom / 1000M :
|
|
prod.Prod.QCom
|
|
)
|
|
) *
|
|
(
|
|
(
|
|
prod.Prod.UCom == "KWH" ?
|
|
prod.Prod.VUnCom * 1000M :
|
|
prod.Prod.VUnCom
|
|
)
|
|
));
|
|
|
|
public decimal Montante_operação
|
|
=>
|
|
Produtos.Sum(prod =>
|
|
(
|
|
prod.Prod.UCom == "KWH" ?
|
|
prod.Prod.QCom / 1000M :
|
|
prod.Prod.QCom
|
|
)
|
|
);
|
|
public decimal Valor_unitário =>
|
|
(
|
|
Comprador.EnderDest.UF.ToString() == "SP" &&
|
|
Vendedor.EnderEmit.UF.ToString() == "SP"
|
|
)
|
|
?
|
|
(
|
|
Montante_operação == 0
|
|
?
|
|
0
|
|
:
|
|
(Soma2 / Montante_operação) * (1 - (decimal)ICMS_NF))
|
|
:
|
|
(
|
|
Montante_operação == 0
|
|
?
|
|
0
|
|
:
|
|
(Soma2 / Montante_operação)
|
|
);
|
|
public double Valor_final_c_impostos => (infNFe.Pag.DetPag.Sum(pag => (pag.VPag)) != 0 ? infNFe.Pag.DetPag.Sum(pag => (pag.VPag)) : ((infNFe.Cobr == null ? infNFe.Total.ICMSTot.VNF : infNFe.Cobr.Fat.VLiq)));
|
|
public string? RS_Comprador => Comprador.XNome.Replace("&amp;", "&").Replace("&", "&");
|
|
public string? RS_Vendedor => Vendedor.XNome.Replace("&amp;", "&").Replace("&", "&");
|
|
public int? Numero_NF => infNFe.Ide.NNF;
|
|
public double ICMS_NF => (GetICMS(Impostos.ICMS) == 0 && Valor_final_c_impostos != 0) ? 1 - ((double)Soma2 / Valor_final_c_impostos) : GetICMS(Impostos.ICMS);
|
|
public string? UF_NF => Comprador.EnderDest.UF.ToString();
|
|
public string? UF_Vend => Vendedor.EnderEmit.UF.ToString();
|
|
}
|
|
}
|