Adicionar arquivos de projeto.

This commit is contained in:
giuliano 2025-03-17 15:04:00 -03:00
parent 43b880ac30
commit 9ed56aa987
5 changed files with 417 additions and 0 deletions

29
OldComplianceNFs.csproj Normal file
View File

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.OleDb" Version="9.0.3" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>

22
OldComplianceNFs.sln Normal file
View File

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OldComplianceNFs", "OldComplianceNFs.csproj", "{6497B886-8A32-4965-8CA0-3A241B98B81A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6497B886-8A32-4965-8CA0-3A241B98B81A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6497B886-8A32-4965-8CA0-3A241B98B81A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6497B886-8A32-4965-8CA0-3A241B98B81A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6497B886-8A32-4965-8CA0-3A241B98B81A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

202
Program.cs Normal file
View File

@ -0,0 +1,202 @@
using System.Data;
using System.Data.OleDb;
using System.Text;
using System.Xml.Linq;
using static System.Net.Mime.MediaTypeNames;
class Program
{
static readonly DataTable dados = new();
static void ObterDadosAccess(string connectionString, string query)
{
using OleDbConnection connection = new(connectionString);
OleDbCommand command = new(query, connection);
connection.Open();
var reader = command.ExecuteReader();
dados.Load(reader);
//reader.Close();
//command.Dispose();
//connection.Close();
}
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>>();
// Obter dados do banco de dados Access
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=X:/Middle/Informativo Setorial/Modelo Word/BD1_dados cadastrais e faturas.accdb;Jet OLEDB:Database Password=gds21";
string query = "SELECT * FROM Dados_TE LEFT JOIN Dados_Cadastrais ON Dados_TE.Cod_smart_unidade = Dados_Cadastrais.Cod_smart_unidade";
ObterDadosAccess(connectionString, query);
// Processar arquivosXML em paralelo
foreach (string arquivo in arquivos)
{
tarefas.Add(ProcessarXMLAsync(arquivo));
}
var linhas = await Task.WhenAll(tarefas);
// Gerar CSV
StringBuilder csv = new();
foreach (string linha in linhas)
{
csv.AppendLine(linha);
}
//StringBuilder csv = new();
//foreach (string arquivo in arquivos)
//{
// csv.AppendLine(await ProcessarXMLAsync(arquivo));
//}
// Escrever CSV no StdOut (VBA vai capturar)
//Console.OutputEncoding = Encoding.UTF8;
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)
{
string? CNPJ_Comprador = "";
string? CNPJ_Vendedor = "";
float? Montante_operação = null;
float? Valor_unitário = null;
float? Valor_final_c_impostos = null;
string? RS_Comprador = "";
string? RS_Vendedor = "";
int? Numero_NF = null;
float? ICMS_NF = null;
string? UF_NF = "";
string? UF_Vend = "";
string? Cod_TE = "";
if (Path.GetExtension(caminhoArquivo).Equals(".xml", StringComparison.CurrentCultureIgnoreCase))
{
if (caminhoArquivo == "X:\\Back\\Controle NFs\\NFs\\581556552,910625 - NFE31250317155730000164550010000669361412505682.XML") { ICMS_NF = 0; }
XDocument xml = XDocument.Load(caminhoArquivo);
XNamespace ns = "http://www.portalfiscal.inf.br/nfe"; // Namespace do XML
CNPJ_Comprador = xml.Descendants(ns + "dest").Elements(ns + "CNPJ").FirstOrDefault()?.Value ?? xml.Descendants(ns + "dest").Elements(ns + "CPF").FirstOrDefault()?.Value;
CNPJ_Comprador = Convert.ToInt64(CNPJ_Comprador).ToString(@"00000000000000");
CNPJ_Vendedor = xml.Descendants(ns + "emit").Elements(ns + "CNPJ").FirstOrDefault()?.Value;
CNPJ_Vendedor = Convert.ToInt64(CNPJ_Vendedor).ToString(@"00000000000000");
RS_Comprador = xml.Descendants(ns + "dest").Elements(ns + "xNome").FirstOrDefault()?.Value;
RS_Vendedor = xml.Descendants(ns + "emit").Elements(ns + "xNome").FirstOrDefault()?.Value;
float soma1 = xml.Descendants(ns + "prod").Sum(prod => (float?)prod.Element(ns + "qCom") ?? 0);
float soma2 = xml.Descendants(ns + "prod").Sum(prod => ((float?)prod.Element(ns + "qCom") ?? 0) * ((float?)prod.Element(ns + "vUnCom") ?? 0));
if (CNPJ_Vendedor == "06981176000158" || CNPJ_Vendedor == "06981176002100" || CNPJ_Vendedor == "17155730000164")
{
soma1 /= 1000;
}
Montante_operação = soma1;
if (soma1 != 0)
{
Valor_unitário = soma2 / soma1;
}
else
{
Valor_unitário = 0;
}
Valor_final_c_impostos = float.TryParse(xml.Descendants(ns + "pag").Elements(ns + "detPag").Elements(ns + "vPag").FirstOrDefault()?.Value.Replace(".",","), out float valor) ? valor : 0;
if (Valor_final_c_impostos == 0)
{
Valor_final_c_impostos = float.TryParse(xml.Descendants(ns + "cobr").Elements(ns + "fat").Elements(ns + "vLiq").FirstOrDefault()?.Value.Replace(".", ","), out float valor2) ? valor2 : 0;
}
Numero_NF = int.Parse(xml.Descendants(ns + "ide").Elements(ns + "nNF").FirstOrDefault()?.Value ?? "0");
ICMS_NF = (float.TryParse(xml.Descendants(ns + "imposto").Elements(ns + "ICMS").Elements(ns + "ICMS00").Elements(ns + "pICMS").FirstOrDefault()?.Value.Replace(".", ","), out float valor1) ? valor1 : 0)/100;
if (ICMS_NF == 0 && Valor_final_c_impostos != 0) {
ICMS_NF = 1 - (soma2 / Valor_final_c_impostos);
}
UF_NF = xml.Descendants(ns + "dest").Elements(ns + "enderDest").Elements(ns + "UF").FirstOrDefault()?.Value;
UF_Vend = xml.Descendants(ns + "emit").Elements(ns + "enderEmit").Elements(ns + "UF").FirstOrDefault()?.Value;
if (UF_NF == "SP" && UF_Vend == "SP")
{
Valor_unitário *= (1 - ICMS_NF);
}
Cod_TE = Id_operacao(CNPJ_Comprador, CNPJ_Vendedor, Montante_operação ?? 0, Valor_unitário ?? 0, Valor_final_c_impostos ?? 0);
}
return await Task.Run(() =>
{
// Formatar como CSV (com proteção contra vírgulas)
return $"{ProcessarArquivo(caminhoArquivo)};\"{
CNPJ_Comprador}\";\"{
CNPJ_Vendedor}\";\"{
Montante_operação.ToString()?.Replace(',','.')}\";\"{
Valor_unitário.ToString()?.Replace(',', '.')}\";\"{
Valor_final_c_impostos.ToString()?.Replace(',','.')}\";\"{
RS_Comprador}\";\"{
RS_Vendedor}\";\"{
Numero_NF}\";\"{
ICMS_NF.ToString()?.Replace(',','.')}\";\"{
UF_NF}\"";
//Cod_TE}\"";
});
}
static string Id_operacao(string CNPJ_Comprador, string CNPJ_Vendedor, float Montante_operação, float Valor_unitário, float Valor_final_c_impostos)
{
float tolerancia_montante = 0.01f;
float tolerancia_valor_unitario = 0.005f;
float tolerancia_valor_final = 0.01f;
try
{
var selecao = dados.Select($"CNPJ_comp = {CNPJ_Comprador} AND CNPJ_vend = {CNPJ_Vendedor}", $"Mes DESC");
foreach (var row in selecao)
{
double mont_LO = row.Field<double>("Mont_LO");
double prec_LO = row.Field<double>("Prec_LO");
double NF_c_ICMS = row.Field<double>("NF_c_ICMS");
bool montDentroDaTolerancia = Math.Abs(mont_LO - Montante_operação) / Montante_operação < tolerancia_montante;
bool unitDentroDaTolerancia = Math.Abs(prec_LO - Valor_unitário) / Valor_unitário < tolerancia_valor_unitario;
bool valorDentroDaTolerancia = Math.Abs(NF_c_ICMS - Valor_final_c_impostos) / Valor_final_c_impostos < tolerancia_valor_final;
if (montDentroDaTolerancia && unitDentroDaTolerancia)
{
return row.Field<string>("Cod_TE")!;
}
else if (valorDentroDaTolerancia)
{
return row.Field<string>("Cod_TE")!;
}
}
}
catch (Exception)
{
return "NAO IDENTIFICADA";
}
return "NAO IDENTIFICADA";
}
}

63
Properties/Resources.Designer.cs generated Normal file
View File

@ -0,0 +1,63 @@
//------------------------------------------------------------------------------
// <auto-generated>
// O código foi gerado por uma ferramenta.
// Versão de Tempo de Execução:4.0.30319.42000
//
// As alterações ao arquivo poderão causar comportamento incorreto e serão perdidas se
// o código for gerado novamente.
// </auto-generated>
//------------------------------------------------------------------------------
namespace OldComplianceNFs.Properties {
using System;
/// <summary>
/// Uma classe de recurso de tipo de alta segurança, para pesquisar cadeias de caracteres localizadas etc.
/// </summary>
// Essa classe foi gerada automaticamente pela classe StronglyTypedResourceBuilder
// através de uma ferramenta como ResGen ou Visual Studio.
// Para adicionar ou remover um associado, edite o arquivo .ResX e execute ResGen novamente
// com a opção /str, ou recrie o projeto do VS.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// Retorna a instância de ResourceManager armazenada em cache usada por essa classe.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("OldComplianceNFs.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Substitui a propriedade CurrentUICulture do thread atual para todas as
/// pesquisas de recursos que usam essa classe de recurso de tipo de alta segurança.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
}
}

101
Properties/Resources.resx Normal file
View File

@ -0,0 +1,101 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 1.3
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">1.3</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1">this is my long string</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
[base64 mime encoded serialized .NET Framework object]
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
[base64 mime encoded string representing a byte array form of the .NET Framework object]
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>