93 lines
3.3 KiB
C#
93 lines
3.3 KiB
C#
using System.Reflection;
|
|
using NfProcessorApp.Domain.Services;
|
|
using NfProcessorApp.Handlers;
|
|
using Unimake.Business.DFe.Xml.NFe;
|
|
|
|
namespace NfProcessorApp.Domain.Entities
|
|
{
|
|
public class NF(InfNFe infNFe) : INFParser
|
|
{
|
|
private readonly InfNFe _infNFe = infNFe;
|
|
|
|
public string CNPJ_Comprador
|
|
=> Convert.ToInt64(_infNFe.Dest.CNPJ ?? _infNFe.Dest.CPF)
|
|
.ToString("00000000000000");
|
|
|
|
public string CNPJ_Vendedor
|
|
=> Convert.ToInt64(_infNFe.Emit.CNPJ)
|
|
.ToString("00000000000000");
|
|
|
|
public decimal Montante_operacao
|
|
=> _infNFe.Det.Sum(d =>
|
|
d.Prod.UCom == "KWH"
|
|
? d.Prod.QCom / 1000m
|
|
: d.Prod.QCom);
|
|
|
|
public double Soma2
|
|
=> (double)_infNFe.Det.Sum(d =>
|
|
((d.Prod.UCom == "KWH"
|
|
? d.Prod.QCom / 1000m
|
|
: d.Prod.QCom)
|
|
* (d.Prod.UCom == "KWH"
|
|
? d.Prod.VUnCom / 1000m
|
|
: d.Prod.VUnCom)));
|
|
|
|
public double Valor_unitario => GetValorUnit();
|
|
|
|
public double Valor_final_com_impostos
|
|
=> _infNFe.Pag.DetPag.Sum(p => p.VPag) != 0
|
|
? _infNFe.Pag.DetPag.Sum(p => p.VPag)
|
|
: _infNFe.Cobr.Fat.VLiq;
|
|
|
|
public string RS_Comprador
|
|
=> _infNFe.Dest.XNome.Replace("&", "&").Replace("&", "&");
|
|
|
|
public string RS_Vendedor
|
|
=> _infNFe.Emit.XNome.Replace("&", "&").Replace("&", "&");
|
|
|
|
public int Numero_NF
|
|
=> _infNFe.Ide.NNF;
|
|
|
|
public double ICMS_NF => GetICMS(_infNFe.Det.First().Imposto.ICMS);
|
|
public string UF_NF { get; set; } = infNFe.Dest.EnderDest.UF.ToString();
|
|
public string UF_Vend { get; set; } = infNFe.Emit.EnderEmit.UF.ToString();
|
|
public NF Parse(string caminhoArquivo)
|
|
{
|
|
// Implementar lógica de parsing aqui
|
|
throw new NotImplementedException();
|
|
}
|
|
public 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);
|
|
bool test = double
|
|
.TryParse(GetICMS(_infNFe.Det.First().Imposto.ICMS)
|
|
.ToString(), out double ICMS);
|
|
|
|
if (!test && Valor_final_com_impostos != 0)
|
|
{
|
|
return 1 - (Soma2 / Valor_final_com_impostos);
|
|
}
|
|
return ICMS;
|
|
}
|
|
public double GetValorUnit()
|
|
{
|
|
if (UF_NF == "SP" && UF_Vend == "SP")
|
|
{
|
|
return Montante_operacao == 0 ? 0 : (Soma2 / (double)Montante_operacao) * (1 - ICMS_NF);
|
|
}
|
|
else
|
|
{
|
|
return Montante_operacao == 0 ? 0 : (Soma2 / (double)Montante_operacao);
|
|
}
|
|
}
|
|
public static NFResult Invalid(string filePath) => new() { FilePath = filePath, IsValid = false };
|
|
}
|
|
}
|