Imagen

DESCARGA SOFTWARE PARA FACTURA ELECTRONICA DE AQUI.
Facturacion, Validacion, Addendas, Librerias de programacion, etc.


CARTA PORTE V3.1

ECODEX TIENE ESTOS NUEVOS DATOS DE CONTACTO :
Comercializacion y Ventas - Evelia Vicke evicke@ecodex.com.mx 33-16-03-03-48
Soporte - Humberto Guerrero soporte@ecodex.com.mx 33-34-90-46-03


.

Archivo XML en VB Net (Serialize)

Para quienes programan Visual Basic aqui esta la solucion
[[ FORO CERRADO DEBIDO A QUE YA LA INFORMACION YA NO ES VIGENTE ]]
jerryeagle
Mensajes: 19
Registrado: Vie Mar 16, 2012 6:14 am

Archivo XML en VB Net (Serialize)

Mensaje por jerryeagle »

De recién que empezé a "indagar" en lo de las Facturas Electrónicas, me pasé mucho tiempo tratando de Crear el XML Válido, desafortunadamente aún no teníia la suficiente experiencia y no comprendía muchos términos del Lenguaje VB Net, crear un XML Válido fué para mi toda una proesa, no fué el mejor método ni el mas limpio pero funcionó, después volví a hecharle un vistazo a una técnica que al principio no comprendí bien, la "Serialización" (Serialize), y una vez que lo comprendí, me dí cuenta que a mi parecer es la mejor opción y la más fácil. Solo tienes que conseguir el XSD del CFD o del CFDI, en estos links los encuentras:

CFD v2.2 http://www.sat.gob.mx/cfd/2/cfdv22.xsd
CFD v3.2 http://www.sat.gob.mx/cfd/3/cfdv32.xsd

Ahora ve a Inicio-->Todos los Programas-->Microsoft Visual Studio-->Visual Studio Tools y dentro de esta carpeta esta el Archivo "Developer Command Prompt for VS2012" (es una ventana de cmd) ejecutalo, ve a la Carpeta donde bajaste los XSD (cd RutaCompletaDeCarpeta) y estando ahi, escribe lo siguiente:

Para Visual Basic
xsd cfdv22.xsd /classes /language:vb
Para C#
xsd cfdv22.xsd /classes /language:cs

Se creará 1 Archivo tipo vb o cs, crea un nuevo proyecto y agrégalo, no lo verás en Solution Explorer a menos que tengas activado Show All Files, pero está un nivel abajo del cfdv22.xsd, es la Classe Comprobante con todas las SubClasses, Propiedades, etc que necesitarás para crear el XML.

Agrega una nueva Classe:

Código: Seleccionar todo

Imports System.Text
Public Class clsCFD

    Public Shared Sub CreaXML(ByVal Comp As Comprobante, _
                                   Optional ByVal xmlFile As String = "", _
                                   Optional ByVal Guardar As Boolean = True)

        Dim serializer As New Xml.Serialization.XmlSerializer(GetType(Comprobante))
        Dim ws As New IO.StreamWriter(xmlFile, False, New UTF8Encoding)

        serializer.Serialize(ws, Comp)

        ws.Flush()
        ws.Close()
        ws.Dispose()
        serializer = Nothing

    End Sub

End Class
En el evento donde quieras crear el XML crea una instancia de Comprobante y llénala con los Datos necesarios, el ejemplo es un Proyecto WinForrms y agregué un WebBrowser para ver el XML Creado:

Código: Seleccionar todo

    Dim xmlFile As String = IO.Path.Combine(Application.StartupPath, "CFDv22 " & Today.ToString("yyyy-MMM-dd ss") & ".xml")
    Sub CreaXmlCFD()
        Dim cfd As New Comprobante
        With cfd
            .version = "2.2"
            .anoAprobacion = "2012"
            .noAprobacion = "10020020202102"
            .fecha = Now.ToString("s")
            .folio = "1"
            .metodoDePago = "TRANSFERENCIA"
            .NumCtaPago = "6722"
            .noCertificado = "01234567890123456789"
            .tipoDeComprobante = ComprobanteTipoDeComprobante.ingreso

            .Emisor = New ComprobanteEmisor With _
                    {.nombre = "NOMBRE EMISOR",
                     .rfc = "RFCEMISOR",
                     .DomicilioFiscal = New t_UbicacionFiscal With _
                                        {.calle = "Calle",
                                         .noExterior = "123",
                                         .codigoPostal = "44150",
                                         .municipio = "GUADALAJARA"}}
            .Receptor = New ComprobanteReceptor With _
                    {.nombre = "NOMBRE EMISOR",
                     .rfc = "RFCEMISOR",
                     .Domicilio = New t_Ubicacion With _
                                        {.calle = "Calle",
                                         .noExterior = "123",
                                         .codigoPostal = "44150",
                                         .municipio = "GUADALAJARA"}}

            .Conceptos = {New ComprobanteConcepto With _
                            {.cantidad = 3,
                             .unidad = "PZA",
                             .descripcion = "PRODUCTO 1",
                             .valorUnitario = 150,
                             .importe = CDec(CDbl(.cantidad * .valorUnitario))}}

            .Impuestos = New ComprobanteImpuestos With _
                        {.Traslados = {New ComprobanteImpuestosTraslado With _
                                            {.impuesto = ComprobanteImpuestosTrasladoImpuesto.IVA,
                                             .tasa = 16,
                                             .importe = CDec(cfd.Conceptos.Sum(Function(con) con.importe)) * CDec("." & .tasa)}}}

            .subTotal = CDec(cfd.Conceptos.Sum(Function(con) con.importe))
            .total = CDec(.subTotal * 1.16)
        End With

        clsCFD.CreaXML(cfd, xmlFile)
        WebBrowser1.Navigate(xmlFile)

    End Sub
Listo, XML creado, faltaría agregar el Sello Digital y el Certificado, por ahora les dejo una función para Obtener la Cadena Original:

Código: Seleccionar todo

    Public Shared Function GetCadenaOriginal(ByVal xmlDoc As String, ByVal fileXSLT As String) As String
        Dim strCadenaOriginal As String
        Dim newFile = Path.GetTempFileName()

        Dim Xsl = New Xml.Xsl.XslCompiledTransform()
        Xsl.Load(fileXSLT)
        Xsl.Transform(xmlDoc, newFile)
        Xsl = Nothing

        Dim sr = New IO.StreamReader(newFile)
        strCadenaOriginal = sr.ReadToEnd
        sr.Close()

        'Eliminamos el archivo Temporal
        System.IO.File.Delete(newFile)

        fileXSLT = Nothing
        newFile = Nothing
        Xsl = Nothing
        sr.Dispose()

        Return strCadenaOriginal
    End Function
Después incluyo como Crear el Sello Digital y cómo agregar éste y el Certificado al XML ya creado.

Saludos
Avatar de Usuario
Dado
Mensajes: 15981
Registrado: Mar Jul 06, 2010 7:56 pm

Re: Archivo XML en VB Net (Serialize)

Mensaje por Dado »

Muchas gracias por tu aportacion, estoy seguro que a mas de uno le va a ser muy util. :D
ADDENDAS? VALIDACION? CODIGO PARA PROGRAMAR TU PROPIA SOLUCION? TODO LO TENEMOS EN WWW.VALIDACFD.COM VISITANOS !!
CarlosLopez
Mensajes: 4
Registrado: Mié Dic 05, 2012 11:45 am

Re: Archivo XML en VB Net (Serialize)

Mensaje por CarlosLopez »

Gracias por la información, me ha sido de gran utilidad,

Estoy atorado en el Régimen Fiscal...
como se agregarian con vb 2 regimenes fiscales, usando la clase cfdv32.vb?

Gracias por su apoyo

saludos
jerryeagle
Mensajes: 19
Registrado: Vie Mar 16, 2012 6:14 am

Re: Archivo XML en VB Net (Serialize)

Mensaje por jerryeagle »

Puedes Crear una Classe aparte de la cfdv32.vb donde pongas variables, funciones y métodos para llenar los Datos de la Classe cfdv32.vb y en esta crear una variable de tipo ComprobanteEmisorRegimenFiscal (que es un Array de ComprobanteEmisorRegimenFiscal) y llenarla algo asi:

En la Clase aparte:

Código: Seleccionar todo

Dim cComprobante As New Comprobante
Dim cRegimen As ComprobanteEmisorRegimenFiscal

    Sub AgregaRegimenFiscal(ByVal ParamArray Regimen() As String)
        Dim Regs As New List(Of ComprobanteEmisorRegimenFiscal)

        For Each r In Regimen
            Regs.Add(New ComprobanteEmisorRegimenFiscal With {.Regimen = r})
        Next

        cRegimen = Regs.ToArray
    End Sub

cComprobante.RegimenFiscal() = cRegimen
Y lo llamarias asi:

Código: Seleccionar todo

'El método recibe Strings separados por coma "," yo puse dos pero pueden ser todos los necesarios
AgregaRegimenFiscal("REGIMEN INTERMEDIO", _
                    "REGIMEN GENERAL")
jerryeagle
Mensajes: 19
Registrado: Vie Mar 16, 2012 6:14 am

Re: Archivo XML en VB Net (Serialize)

Mensaje por jerryeagle »

Les agrego una Función para REMOVER elementos/Nodos/Atributos vacíos del XML creado, seguro a muchos les será de utilidad:

Código: Seleccionar todo

    Private Function RemoveAllemptys(ByVal el As XElement, 
                                     Optional ByVal includeChildNodes As Boolean = False) As XElement

        Dim current = el.LastAttribute

        Do While current IsNot Nothing
            Dim temp = current.PreviousAttribute
            If current.Value = "" Then
                If current.Name <> "sello" Then
                    current.Remove()
                End If
            End If
            current = temp
        Loop

        If includeChildNodes Then
            For Each child In el.Descendants
                RemoveAllemptys(child)
            Next
        End If

        Return el
    End Function
CarlosLopez
Mensajes: 4
Registrado: Mié Dic 05, 2012 11:45 am

Re: Archivo XML en VB Net (Serialize)

Mensaje por CarlosLopez »

JerryEagle,

Muchas Gracias por la recomendacion.

Soy nuevo en estos temas, tambien como puedo hacer para agregar mas de un concepto y un impuesto?

Aplique la recomendación pero me encuentro con el siguiente error.... Habra algun error al generar la librería? Estoy usando las versio 3.2

Sub AgregaRegimenFiscal(ByVal ParamArray Regimen() As String)
Dim Regs As New List(Of ComprobanteEmisorRegimenFiscal)

For Each r In Regimen
Regs.Add(New ComprobanteEmisorRegimenFiscal With {.Regimen = r})
Next

cRegimen = Regs.ToArray ==> "Value of type '1-dimensional array of Application.ComprobanteEmisorRegimenFiscal' cannot be converted to Application..ComprobanteEmisorRegimenFiscal

End Sub

Lo mismo al usar:

Comprobante.Emisor.RegimenFiscal() = cRegimen

Agradezco el apoyo
Saludos
jerryeagle
Mensajes: 19
Registrado: Vie Mar 16, 2012 6:14 am

Re: Archivo XML en VB Net (Serialize)

Mensaje por jerryeagle »

Pues a mi no me aparece ningun error, habria q ver cómo estás declarando la variable cRegimen
CarlosLopez
Mensajes: 4
Registrado: Mié Dic 05, 2012 11:45 am

Re: Archivo XML en VB Net (Serialize)

Mensaje por CarlosLopez »

Muchas Gracias el apoyo

Aqui el codigo...

Sub AgregaRegimenFiscal(ByVal ParamArray Regimen() As String)
Dim Regs As New List(Of ComprobanteEmisorRegimenFiscal)

For Each r In Regimen
Regs.Add(New ComprobanteEmisorRegimenFiscal With {.Regimen = r})
Next

cRegimen = Regs.ToArray ==> "Value of type '1-dimensional array of Application.ComprobanteEmisorRegimenFiscal' cannot be converted to Application.ComprobanteEmisorRegimenFiscal
End Sub

Private Sub GenCFD32()

Dim cRegimen As ComprobanteEmisorRegimenFiscal
Dim Regs As New List(Of ComprobanteEmisorRegimenFiscal)


Dim cfd As New Comprobante
With cfd

.version = "3.2"
.serie = "A"
.folio = "5001"
.fecha = Now.ToString("s")
.formaDePago = "En una Sola Exhibición"
.noCertificado = NoCertificado
.subTotal = "1000.00"


.total = "1160.00"
.tipoDeComprobante = ComprobanteTipoDeComprobante.ingreso 'ingreso egreso traslado
.metodoDePago = "Transferencia Bancaria" 'cheque, tarjeta de crédito o debito, depósito en cuenta, etc.
.LugarExpedicion = "Estado de México"



.Emisor = New ComprobanteEmisor With _
{.nombre = "Compañia de Prueba",
.rfc = "XAXX010101",
.DomicilioFiscal = New t_UbicacionFiscal With _
{.calle = "Calle",
.noExterior = "1",
.colonia = "Colonia",
.municipio = "Municipio",
.estado = "Distrito Federal",
.codigoPostal = "11000",
.pais = "México"}
}

AgregaRegimenFiscal("REGIMEN INTERMEDIO", "REGIMEN GENERAL DE LEY")


.Emisor.RegimenFiscal() = cRegimen ==> "Value of type '1-dimensional array of Application.ComprobanteEmisorRegimenFiscal' cannot be converted to Application.ComprobanteEmisorRegimenFiscal

.Receptor = New ComprobanteReceptor With _
{.nombre = "NOMBRE Receptor",
.rfc = "RFCReceptor",
.Domicilio = New t_Ubicacion With _
{.calle = "Calle",
.noExterior = "123",
.codigoPostal = "44150",
.municipio = "GUADALAJARA"}}

Dim ArrConceptos() As ComprobanteConcepto



.Conceptos = {New ComprobanteConcepto With _
{.cantidad = 3,
.unidad = "PZA",
.descripcion = "PRODUCTO 1",
.valorUnitario = 150,
.importe = CDec(CDbl(.cantidad * .valorUnitario))}}




.Impuestos = New ComprobanteImpuestos With _
{.Traslados = {New ComprobanteImpuestosTraslado With _
{.impuesto = ComprobanteImpuestosTrasladoImpuesto.IVA,
.tasa = 16,
.importe = CDec(cfd.Conceptos.Sum(Function(con) con.importe)) * CDec("." & .tasa)}

}
}

.subTotal = CDec(cfd.Conceptos.Sum(Function(con) con.importe))
.total = CDec(.subTotal * 1.16)
End With



End Sub
jerryeagle
Mensajes: 19
Registrado: Vie Mar 16, 2012 6:14 am

Re: Archivo XML en VB Net (Serialize)

Mensaje por jerryeagle »

El error esta aqui:
Dim cRegimen As ComprobanteEmisorRegimenFiscal

debe ser
Dim cRegimen() As ComprobanteEmisorRegimenFiscal
CarlosLopez
Mensajes: 4
Registrado: Mié Dic 05, 2012 11:45 am

Re: Archivo XML en VB Net (Serialize)

Mensaje por CarlosLopez »

jerryeagle escribió:El error esta aqui:
Dim cRegimen As ComprobanteEmisorRegimenFiscal

debe ser
Dim cRegimen() As ComprobanteEmisorRegimenFiscal
Muchas Gracias!
Cerrado