Skip to content

Commit

Permalink
Add COFINS
Browse files Browse the repository at this point in the history
  • Loading branch information
rodolfoghi committed Aug 24, 2024
1 parent 15217c0 commit 117a25c
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
@using static DanfeFluentBlazor.Extensions.EnumExtensions
@using NFe.Classes.Informacoes.Detalhe.Tributacao.Federal

<Imposto NomeDoImposto="COFINS" CST="@_cst" BaseDeCalculo="@_baseDeCalculo" Aliquota="@_aliquota" Valor="@_valor" />

@code {
[Parameter]
public COFINS DadosCOFINS { get; set; }

Check warning on line 8 in DanfeFluentBlazor/DanfeFluentBlazor/Components/Impostos/ImpostoCOFINS.razor

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'DadosCOFINS' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

private string _cst = string.Empty;
private decimal _baseDeCalculo;
private decimal _aliquota;
private decimal _valor;

protected override void OnInitialized()
{
_cst = ObterCST();
_baseDeCalculo = ObterBaseDeCalculo();
_aliquota = ObterAliquota();
_valor = ObterValor();
}

private string ObterCST()
{
var tipoCOFINS = DadosCOFINS.TipoCOFINS;

if (tipoCOFINS is COFINSAliq COFINSAliq)
{
return GetXmlEnumValue(COFINSAliq.CST);
}

if (tipoCOFINS is COFINSQtde COFINSQtde)
{
return GetXmlEnumValue(COFINSQtde.CST);
}

if (tipoCOFINS is COFINSNT COFINSNT)
{
return GetXmlEnumValue(COFINSNT.CST);
}

if (tipoCOFINS is COFINSOutr COFINSOutr)
{
return GetXmlEnumValue(COFINSOutr.CST);
}

return string.Empty;
}

private decimal ObterBaseDeCalculo()
{
var tipoCOFINS = DadosCOFINS.TipoCOFINS;

if (tipoCOFINS is COFINSAliq COFINSAliq)
{
return COFINSAliq.vBC;
}

if (tipoCOFINS is COFINSQtde COFINSQtde)
{
return COFINSQtde.qBCProd;
}

if (tipoCOFINS is COFINSOutr COFINSOutr)
{
return COFINSOutr.vBC ?? 0;
}

return 0;
}

private decimal ObterAliquota()
{
var tipoCOFINS = DadosCOFINS.TipoCOFINS;

if (tipoCOFINS is COFINSAliq COFINSAliq)
{
return COFINSAliq.pCOFINS;
}

if (tipoCOFINS is COFINSQtde COFINSQtde)
{
return COFINSQtde.vAliqProd;
}

if (tipoCOFINS is COFINSOutr COFINSOutr)
{
return COFINSOutr.pCOFINS ?? 0;
}

return 0;
}

private decimal ObterValor()
{
var tipoCOFINS = DadosCOFINS.TipoCOFINS;

if (tipoCOFINS is COFINSAliq COFINSAliq)
{
return COFINSAliq.vCOFINS;
}

if (tipoCOFINS is COFINSQtde COFINSQtde)
{
return COFINSQtde.vCOFINS;
}

if (tipoCOFINS is COFINSOutr COFINSOutr)
{
return COFINSOutr.vCOFINS ?? 0;
}

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@using NFe.Classes.Informacoes.Detalhe.Tributacao.Federal
@using System.Reflection
@using System.Xml.Serialization
@using static Extensions.EnumExtensions

<Imposto NomeDoImposto="PIS" CST="@_cst" BaseDeCalculo="@_baseDeCalculo" Aliquota="@_aliquota" Valor="@_valor" />

Expand Down Expand Up @@ -113,17 +112,4 @@

return 0;
}

public static string GetXmlEnumValue(Enum enumValue)
{
FieldInfo fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
XmlEnumAttribute[] attributes = (XmlEnumAttribute[])fieldInfo.GetCustomAttributes(typeof(XmlEnumAttribute), false);

if (attributes.Length > 0)
{
return attributes[0]?.Name!;
}

return string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@

<ImpostoICMS DadosICMS="@detalhe.imposto.ICMS" />
<ImpostoPIS DadosPIS="@detalhe.imposto.PIS" />
<ImpostoCOFINS DadosCOFINS="@detalhe.imposto.COFINS" />
</FluentGrid>
</FluentAccordionItem>
}
Expand Down
17 changes: 16 additions & 1 deletion DanfeFluentBlazor/DanfeFluentBlazor/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.ComponentModel;
using System.Reflection;
using System.Xml.Serialization;

namespace DanfeFluentBlazor.Extensions;

public static class EnumExtensions
{
static public string GetDescription(this Enum enumValue)
public static string GetDescription(this Enum enumValue)
{
var field = enumValue.GetType().GetField(enumValue.ToString());
if (field == null)
Expand All @@ -18,4 +20,17 @@ static public string GetDescription(this Enum enumValue)

return enumValue.ToString();
}

public static string GetXmlEnumValue(Enum enumValue)
{
FieldInfo fieldInfo = enumValue.GetType().GetField(enumValue.ToString());

Check warning on line 26 in DanfeFluentBlazor/DanfeFluentBlazor/Extensions/EnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 26 in DanfeFluentBlazor/DanfeFluentBlazor/Extensions/EnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
XmlEnumAttribute[] attributes = (XmlEnumAttribute[])fieldInfo.GetCustomAttributes(typeof(XmlEnumAttribute), false);

Check warning on line 27 in DanfeFluentBlazor/DanfeFluentBlazor/Extensions/EnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 27 in DanfeFluentBlazor/DanfeFluentBlazor/Extensions/EnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

if (attributes.Length > 0)
{
return attributes[0]?.Name!;
}

return string.Empty;
}
}

0 comments on commit 117a25c

Please sign in to comment.