FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Factura Electr贸nica Colombia - Agradecimiento
Posts: 1816
Joined: Wed Oct 26, 2005 02:49 PM
Re: Factura Electr贸nica Colombia - Agradecimiento
Posted: Sat Aug 24, 2019 02:40 AM
Buenas noches para todos:

Adjunto la clase que env铆a de las facturas electr贸nicas al proveedor tecnol贸gico. Espero les sea de utilidad.

Code (fw): Select all Collapse
*************************************
* CLASE PARA EL ENVIO DE LAS FACTURAS 
*************************************
#include "FiveWin.ch"
#include "xbrowse.ch"
#include "report.ch"

CLASS eFactura
聽 聽 
聽 聽 /*DATAS CREDENCIALES AUTENTICACION*/
聽 聽 data user 
聽 聽 data pass 

聽 聽 */DATAS DEL DOCUMENTO */
聽 聽 data documento 
聽 聽 data encabezado 
聽 聽 data sucursal 
聽 聽 data tercero 
聽 聽 DATA impuestos AS array
聽 聽 DATA detalle AS array
聽 聽 data final 
聽 聽 data respuesta
聽 聽 data nroItems
聽 聽 data cdnaJson
聽 聽 data urlenvio
聽 聽 data tipoenvio
聽 聽 
聽 聽 /*DATAS DE LOS ITEMS O ARTICULOS*/
聽 聽 data infoItem
聽 聽 /*DATAS DE LOS IMPUESTOS*/
聽 聽 data infoImpu

聽 聽 /*METODOS DEL DOCUMENTO*/
聽 聽 METHOD New() CONSTRUCTOR
聽 聽 METHOD DatoEncabezado聽 聽()
聽 聽 METHOD DatoSucursal()
聽 聽 METHOD DatoTercero()
聽 聽 METHOD CreaJson()
聽 聽 METHOD Enviar()
聽 聽 METHOD EnviarFactura()
聽 聽 METHOD EnviarEstado()
聽 聽 METHOD EnviarPdf()
聽 聽 METHOD EnviarResumen()
聽 聽 
聽 聽 /*METODOS MANEJO DE LOS ITEMS O ARTICULOS*/
聽 聽 METHOD NuevoItem()
聽 聽 METHOD DatoItem()
聽 聽 METHOD AgregaItem() 
聽 聽 
聽 聽 /*METODOS MANEJO DE LOS IMPUESTOS*/
聽 聽 METHOD NuevoImpuesto()
聽 聽 METHOD DatoImpuesto()
聽 聽 METHOD AgregaImpuesto() 聽 聽 
聽 聽 
聽 聽 /*VISUALIZAR EL JSON*/
聽 聽 METHOD Resultado()
聽 聽 
聽 聽 ERROR HANDLER OnError( uParam1 )

ENDCLASS

********************************************************************************
METHOD New() CLASS eFactura
聽 聽 ::encabezado := {=>}
聽 聽 ::sucursal := {=>}
聽 聽 ::tercero := {=>}
聽 聽 ::impuestos := {}
聽 聽 ::detalle := {}
聽 聽 ::documento := {=>}
聽 聽 ::final := {=>}
return self

********************************************************************************
METHOD DatoEncabezado(indice,valor) CLASS eFactura
聽 聽 ::encabezado[indice] = valor
return nil

********************************************************************************
METHOD DatoTercero(indice,valor) CLASS eFactura
聽 聽 ::tercero[indice] = valor
return nil

********************************************************************************
METHOD DatoSucursal(indice,valor) CLASS eFactura
聽 聽 ::sucursal[indice] = valor
return nil

********************************************************************************
METHOD NuevoItem() CLASS eFactura
聽 聽 ::infoItem := {=>}
return nil
********************************************************************************
METHOD DatoItem(indice,valor) CLASS eFactura
聽 聽 ::infoItem[indice] = valor
return nil
********************************************************************************
METHOD AgregaItem() CLASS eFactura
聽 聽 AADD(::detalle , ::infoItem)
return nil

********************************************************************************
METHOD NuevoImpuesto() CLASS eFactura
聽 聽 ::infoImpu := {=>}
return nil
********************************************************************************
METHOD DatoImpuesto(indice,valor) CLASS eFactura
聽 聽 ::infoImpu[indice] = valor
return nil
********************************************************************************
METHOD AgregaImpuesto() CLASS eFactura
聽 聽 AADD(::impuestos ,::infoImpu)
return nil

********************************************************************************
METHOD CreaJson() CLASS eFactura
聽 聽 //agregamos la informacion al json documento
聽 聽 ::documento["detalle"] = ::detalle
聽 聽 ::documento["encabezado"] = ::encabezado
聽 聽 ::documento["sucursal"] = ::sucursal
聽 聽 ::documento["tercero"] = ::tercero
聽 聽 ::documento["impuesto"] = ::impuestos

聽 聽 //creamos la cadena json con el documento final
聽 聽 ::final["documento"] = ::documento
聽 聽 ::cdnaJson := hb_jsonEncode( ::final )
return nil

********************************************************************************
METHOD EnviarFactura() CLASS eFactura
聽 聽 LOCAL aRet
聽 聽 ::tipoenvio := 0
聽 聽 ::urlenvio := "https://Factura/factura"
聽 聽 aRet := ::enviar()
return aRet

********************************************************************************
METHOD EnviarPdf() CLASS eFactura
聽 聽 LOCAL aRet
聽 聽 ::tipoenvio := 1
聽 聽 ::urlenvio := "https://Factura/pdf"
聽 聽 aRet := ::enviar()
return aRet

********************************************************************************
METHOD EnviarEstado() CLASS eFactura
聽 聽 LOCAL aRet
聽 聽 ::tipoenvio := 2
聽 聽 ::urlenvio := "https://Factura/estado"
聽 聽 aRet := ::enviar()
return aRet

********************************************************************************
METHOD EnviarResumen() CLASS eFactura
聽 聽 LOCAL aRet
聽 聽 ::tipoenvio := 3
聽 聽 ::urlenvio := "https://Factura/estado"
聽 聽 aRet := ::enviar()
return aRet

********************************************************************************
METHOD Enviar() CLASS eFactura

聽 聽 local cBas64 聽 聽
聽 聽 local exito,response
聽 聽 local ohttp,doc
聽 聽 //creamos el json antes de ser enviado
聽 聽 ::CreaJson()
聽 聽 
聽 聽 IF Empty(::user) .or. Empty(::pass)
聽 聽 聽 聽 聽::OnError( "user-pass" , "Variable de usuario o contrase帽a no definida" 聽)
聽 聽 ELSE
聽 聽 聽 聽 IF !IsInternet()
聽 聽 聽 聽 聽 聽 exito := {.f.,"No Hay Conexi贸n a Internet - Intente Nuevamente"}
聽 聽 聽 聽 聽 聽 //msginfo("No Hay Conexi贸n a Internet","Intente Nuevamente")
聽 聽 聽 聽 ELSE
聽 聽 聽 聽 聽 聽 cBas64 := hb_base64encode(::user+":"+::pass,len(::user+":"+::pass))聽
聽 聽 聽 聽 聽 聽 doc := CreateObject( "MSXML2.DOMDocument" )
聽 聽 聽 聽 聽 聽 ohttp := CreateObject( "MSXML2.XMLHTTP" )
聽 聽 聽 聽 聽 聽 ohttp:Open( "POST" , ::urlenvio ,.F.)
聽 聽 聽 聽 聽 聽 oHttp:SetRequestHeader("cache-control", "no-cache")
聽 聽 聽 聽 聽 聽 ohttp:SetRequestHeader("content-type", "application/json" )
聽 聽 聽 聽 聽 聽 ohttp:SetRequestHeader("authorization", "Basic "+cBas64 )
聽 聽 聽 聽 聽 聽 TRY
聽 聽 聽 聽 聽 聽 聽 聽 ohttp:Send( ::cdnaJson )
聽 聽 聽 聽 聽 聽 CATCH
聽 聽 聽 聽 聽 聽 聽 聽 exito := {.f.,"No Se Pudo Enviar Documento JSON - Error URL"}
聽 聽 聽 聽 聽 聽 聽 聽 return exito
聽 聽 聽 聽 聽 聽 END
聽 聽 聽 聽 聽 聽 response := ohttp:responseText
聽 聽 聽 聽 聽 聽 ::respuesta 聽:= hash()
聽 聽 聽 聽 聽 聽 hb_jsondecode(response ,@::respuesta ) //Parse JSON to hash*/
聽 聽 聽 聽 聽 聽 IF valtype(::respuesta)=="U"
聽 聽 聽 聽 聽 聽 聽 聽 exito := {.f.,"Problemas Con Archivo JSON"}
聽 聽 聽 聽 聽 聽 ELSE
聽 聽 聽 聽 聽 聽 聽 聽 IF ::tipoenvio=0 //envio factura
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 IF Empty(::respuesta["error"])
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 exito := {.t.,::respuesta["qrdata"],::respuesta["cufe"]}
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ELSE 
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 exito := {.f.,::respuesta["error"]}
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ENDIF聽 聽聽 聽 聽 聽 
聽 聽 聽 聽 聽 聽 聽 聽 ELSEIF ::tipoenvio=1 //envio pdf
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 if "error" IN ::respuesta
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 exito := {.f.,::respuesta["error"]}
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 else 
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 exito := {.t.,::respuesta["result"]}
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 endif
聽 聽 聽 聽 聽 聽 聽 聽 ELSEIF ::tipoenvio=2 //consulta qr
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 IF Empty(::respuesta["error"])
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 exito := {.t.,hb_base64decode(::respuesta["qrdata"]),::respuesta["cufe"]}
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ELSE 
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 exito := {.f.,::respuesta["error"]}
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ENDIF聽 聽聽 聽 聽 聽 聽 聽 聽 聽 
聽 聽 聽 聽 聽 聽 聽 聽 ELSEIF ::tipoenvio=3 //Consultar la factura completa
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 exito := {.t.,::respuesta}
聽 聽 聽 聽 聽 聽 聽 聽 ENDIF
聽 聽 聽 聽 聽 聽 ENDIF聽 聽聽 聽 聽 聽 
聽 聽 聽 聽 ENDIF
聽 聽 ENDIF
聽 聽 //xbrowse(exito)
return exito

********************************************************************************
METHOD Resultado() CLASS eFactura //PARA CONSULTAR COMO ESTA EL JSON
聽 聽 ::CreaJson()
聽 聽 xBrowse(::final)
return nil

********************************************************************************
METHOD OnError( uParam , vDescripcion 聽) CLASS eFactura
聽 聽 local cMsg := __GetMessage()
聽 聽 local uRet, oError
聽 聽 oError := ErrorNew()
聽 聽 oError:Args := { Self, cMsg, uParam }
聽 聽 oError:Description := vDescripcion
聽 聽 oError:CanDefault := .F.
聽 聽 oError:CanRetry := .F.
聽 聽 uRet := Eval( ErrorBlock(), oError )
return( uRet )

********************************************************************************
*/FUNCION QUE VERIFICA LA CONEXION CON INTERNET*/
Function IsInternet()
WSAStartup()
RETURN (IIF(GetHostByName("google.com")<>"0.0.0.0",.T.,.F.))


Peque帽o ejemplo de uso

Code (fw): Select all Collapse
*************************************
**CONSULTA ESTADO FACTURA ELECTRONICA
*************************************
function consulta_estado()

聽 聽 local oFct
聽 聽 
聽 聽 oFct:= eFactura():New()
聽 聽 
聽 聽 //ENCABEZADO DE LA FACTURA
聽 聽 oFct:DatoEncabezado("emisor" , "987654321" )
聽 聽 oFct:DatoEncabezado("idsuc" , 聽1 )
聽 聽 oFct:DatoEncabezado("numero" , 聽56 )
聽 聽 oFct:DatoEncabezado("prefijo" , 聽"FTE" )

聽 聽 //INGRESAMOS EL USUARIO Y LA CLAVE PARA ENVIO
聽 聽 oFct:user := "tusuario"
聽 聽 oFct:pass := "tucontrase帽a"
聽 聽 
聽 聽 //ENVIAMOS LA FACTURA PARA SER CONSULTADA
聽 聽 oFct:EnviarEstado()

return nil
Saludos
LEANDRO AREVALO
Bogot谩 (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Turbo Incremental Link64 6.98 Embarcadero 7.70 ] [ FiveWin 25.01 ] [ xHarbour 64 bits) ]

Continue the discussion