-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1576 from ZeusAutomacao/Integracao_Hercules
Integracao Hercules em DFe.NET
- Loading branch information
Showing
9 changed files
with
395 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml.Serialization; | ||
|
||
namespace DFe.Utils { | ||
|
||
public abstract class XmlOrderFreeSerializerFactory { | ||
// Referência https://stackoverflow.com/a/33508815 | ||
readonly static XmlAttributeOverrides overrides = new XmlAttributeOverrides(); | ||
readonly static object locker = new object(); | ||
readonly static Dictionary<Type, XmlSerializer> serializers = new Dictionary<Type, XmlSerializer>(); | ||
static HashSet<string> overridesAdded = new HashSet<string>(); | ||
|
||
static void AddOverrideAttributes(Type type, XmlAttributeOverrides overrides) { | ||
|
||
if (type == null || type == typeof(object) || type.IsPrimitive || type == typeof(string) || type == typeof(DateTime)) { | ||
return; | ||
} | ||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) { | ||
AddOverrideAttributes(type.GetGenericArguments()[0], overrides); | ||
return; | ||
} | ||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>)) { | ||
AddOverrideAttributes(type.GetGenericArguments()[0], overrides); | ||
return; | ||
} | ||
|
||
var mask = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public; | ||
foreach (var member in type.GetProperties(mask).Cast<MemberInfo>().Union(type.GetFields(mask))) { | ||
var otTag = $"{type.Name}_{member.Name}"; | ||
if (overridesAdded.Contains(otTag)) { | ||
continue; | ||
} | ||
XmlAttributes overrideAttr = null; | ||
foreach (var attr in member.GetCustomAttributes<XmlElementAttribute>()) { | ||
overrideAttr = overrideAttr ?? new XmlAttributes(); | ||
var overrideElt = new XmlElementAttribute(attr.ElementName, attr.Type) { DataType = attr.DataType, ElementName = attr.ElementName, Form = attr.Form, Namespace = attr.Namespace, Type = attr.Type }; | ||
if(attr.IsNullable) { | ||
// Isso aqui deve ter uma logica personalizada no setter, colocar ali em cima causa erro | ||
overrideElt.IsNullable = true; | ||
} | ||
overrideAttr.XmlElements.Add(overrideElt); | ||
if(attr.Type != null) { | ||
AddOverrideAttributes(attr.Type, overrides); | ||
} | ||
} | ||
foreach (var attr in member.GetCustomAttributes<XmlArrayAttribute>()) { | ||
overrideAttr = overrideAttr ?? new XmlAttributes(); | ||
overrideAttr.XmlArray = new XmlArrayAttribute { ElementName = attr.ElementName, Form = attr.Form, IsNullable = attr.IsNullable, Namespace = attr.Namespace }; | ||
} | ||
foreach (var attr in member.GetCustomAttributes<XmlArrayItemAttribute>()) { | ||
overrideAttr = overrideAttr ?? new XmlAttributes(); | ||
overrideAttr.XmlArrayItems.Add(attr); | ||
} | ||
foreach (var attr in member.GetCustomAttributes<XmlAnyElementAttribute>()) { | ||
overrideAttr = overrideAttr ?? new XmlAttributes(); | ||
overrideAttr.XmlAnyElements.Add(new XmlAnyElementAttribute { Name = attr.Name, Namespace = attr.Namespace }); | ||
} | ||
if (overrideAttr != null) { | ||
overridesAdded.Add(otTag); | ||
overrides.Add(type, member.Name, overrideAttr); | ||
} | ||
var mType = (member is PropertyInfo pi ? pi.PropertyType : member is FieldInfo fi ? fi.FieldType : null); | ||
AddOverrideAttributes(mType, overrides); | ||
} | ||
} | ||
|
||
public static XmlSerializer GetSerializer(Type type) { | ||
if (type == null) | ||
throw new ArgumentNullException("type"); | ||
lock (locker) { | ||
XmlSerializer serializer; | ||
if (!serializers.TryGetValue(type, out serializer)) { | ||
AddOverrideAttributes(type, overrides); | ||
serializers[type] = serializer = new XmlSerializer(type, overrides); | ||
} | ||
return serializer; | ||
} | ||
} | ||
|
||
} | ||
|
||
public static class TypeExtensions { | ||
public static IEnumerable<Type> BaseTypesAndSelf(this Type type) { | ||
while (type != null) { | ||
yield return type; | ||
type = type.BaseType; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.ComponentModel; | ||
using System.Xml.Serialization; | ||
|
||
namespace NFe.Classes.Informacoes.Agropecuario | ||
{ | ||
/// <summary> | ||
/// Tipo da Guia | ||
/// <para>1 - GTA - Guia de Trânsito Animal</para> | ||
/// <para>2 - TTA - Termo de Trânsito Animal</para> | ||
/// <para>3 - DTA - Documento de Transferência Animal</para> | ||
/// <para>4 - ATV - Autorização de Trânsito Vegetal</para> | ||
/// <para>5 - PTV - Permissão de Trânsito Vegetal</para> | ||
/// <para>6 - GTV - Guia de Trânsito Vegetal</para> | ||
/// <para>7 - Guia Florestal (DOF, SisFlora - PA e MT ou SIAM - MG)</para> | ||
/// </summary> | ||
public enum TipoGuia | ||
{ | ||
/// <summary> | ||
/// 1 - GTA - Guia de Trânsito Animal | ||
/// </summary> | ||
[Description("GTA - Guia de Trânsito Animal")] | ||
[XmlEnum("1")] | ||
GTA = 1, | ||
|
||
/// <summary> | ||
/// 2 - TTA - Termo de Trânsito Animal | ||
/// </summary> | ||
[Description("TTA - Termo de Trânsito Animal")] | ||
[XmlEnum("2")] | ||
TTA = 2, | ||
|
||
/// <summary> | ||
/// 3 - DTA - Documento de Transferência Animal | ||
/// </summary> | ||
[Description("DTA - Documento de Transferência Animal")] | ||
[XmlEnum("3")] | ||
DTA = 3, | ||
|
||
/// <summary> | ||
/// 4 - ATV - Autorização de Trânsito Vegetal | ||
/// </summary> | ||
[Description("ATV - Autorização de Trânsito Vegetal")] | ||
[XmlEnum("4")] | ||
ATV = 4, | ||
|
||
/// <summary> | ||
/// 5 - PTV - Permissão de Trânsito Vegetal | ||
/// </summary> | ||
[Description("PTV - Permissão de Trânsito Vegetal")] | ||
[XmlEnum("5")] | ||
PTV = 5, | ||
|
||
/// <summary> | ||
/// 6 - GTV - Guia de Trânsito Vegetal | ||
/// </summary> | ||
[Description("GTV - Guia de Trânsito Vegetal")] | ||
[XmlEnum("6")] | ||
GTV = 6, | ||
|
||
/// <summary> | ||
/// 7 - Guia Florestal (DOF, SisFlora - PA e MT ou SIAM - MG) | ||
/// </summary> | ||
[Description("Guia Florestal (DOF, SisFlora - PA e MT ou SIAM - MG)")] | ||
[XmlEnum("7")] | ||
GuiaFlorestal = 7, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
namespace NFe.Classes.Informacoes.Agropecuario | ||
{ | ||
public class agropecuario | ||
{ | ||
#if NET5_0_OR_GREATER//o uso de tipos de referência anuláveis não é permitido até o C# 8.0. | ||
|
||
/// <summary> | ||
/// ZF02 - serieGuia | ||
/// </summary> | ||
public defensivo? defensivo { get; set; } | ||
Check warning on line 10 in NFe.Classes/Informacoes/Agropecuario/agropecuario.cs
|
||
|
||
/// <summary> | ||
/// ZF04 - Guia de Trânsito | ||
/// </summary> | ||
public guiaTransito? guiaTransito { get; set; } | ||
Check warning on line 15 in NFe.Classes/Informacoes/Agropecuario/agropecuario.cs
|
||
|
||
public bool ShouldSerializedefensivo() | ||
{ | ||
return defensivo != null; | ||
} | ||
public bool ShouldSerializeguiaTransito() | ||
{ | ||
return guiaTransito != null; | ||
} | ||
#else | ||
/// <summary> | ||
/// ZF02 - serieGuia | ||
/// </summary> | ||
public defensivo defensivo { get; set; } | ||
|
||
/// <summary> | ||
/// ZF04 - Guia de Trânsito | ||
/// </summary> | ||
public guiaTransito guiaTransito { get; set; } | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace NFe.Classes.Informacoes.Agropecuario | ||
{ | ||
public class defensivo | ||
{ | ||
/// <summary> | ||
/// ZF03 - Número da receita ou receituário do agrotóxico / defensivo agrícola | ||
/// </summary> | ||
public string nReceituario { get; set; } | ||
|
||
/// <summary> | ||
/// ZP03a - CPF do Responsável Técnico, emitente do receituário | ||
/// </summary> | ||
public string CPFRespTec { get; set; } | ||
} | ||
} |
Oops, something went wrong.