# Objetivo: Generar un CFDI 4.0 de Ingreso con Carta Porte 3.1
import os
from ctypes import *

cCurDir = os.path.dirname(os.path.abspath(__file__))+"\\" # devuelve la ruta del archivo .py actual

# Cargar la DLL y crear sus wrappers
vxml = cdll.LoadLibrary(cCurDir+"VXML_x64.dll")
vxml.VXML_NewW.restype = c_void_p
vxml.VXML_NewW.argtypes = [c_wchar_p]
vxml.VXML_NewWithLogW.restype = c_void_p
vxml.VXML_NewWithLogW.argtypes = [c_wchar_p, c_wchar_p]
vxml.VXML_SetUserInfoW.argtypes = [c_void_p,c_wchar_p, c_wchar_p]
vxml.VXML_AddCfdiRelacionados_cfdi40W.argtypes = [c_void_p,c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_AddCfdiRelacionado_cfdi40W.argtypes = [c_void_p,c_wchar_p]
vxml.VXML_SetComprobanteInfo_cfdi40W.argtypes = [c_void_p,c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_SetEmisorInfo_cfdi40W.argtypes = [c_void_p,c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_SetReceptorInfo_cfdi40W.argtypes = [c_void_p,c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_AddConcepto_cfdi40W.argtypes = [c_void_p,c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_AddConceptoTraslado_cfdi40W.argtypes = [c_void_p,c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]           
vxml.VXML_SetImpuestosInfo_cfdi40W.argtypes = [c_void_p,c_wchar_p, c_wchar_p]       
vxml.VXML_AddTraslado_cfdi40W.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_ProcesaDocumentoW.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_GetValueW.argtypes = [c_void_p, c_int]
vxml.VXML_GetValueW.restype = c_wchar_p
vxml.VXML_FreeW.argtypes = [c_void_p]
vxml.VXML_SetCartaPorte31W.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_CartaPorte31AddUbicacionW.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_CartaPorte31SetMercanciasW.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_CartaPorte31AddMercanciaW.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_CartaPorte31AddFiguraTransporteTiposFiguraW.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_CartaPorte31SetMercanciasAutotransporteW.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_CartaPorte31AddMercanciaCantidadTransportaW.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]

print(">>>>>>>>>>> Timbrado CFDI 4.0 de Ingreso con Carta Porte 3.1")

# iniciamos el comprobante como si fuera de ingreso normal 
hXml = vxml.VXML_NewWithLogW("4.0",cCurDir+"CFDI40_Demo_Ingreso_CartaPorte31.log")
vxml.VXML_SetUserInfoW(hXml,"demo_cibertec","demo")
vxml.VXML_SetComprobanteInfo_cfdi40W(hXml,"T","746","%cb_date","99","credito 15 dias","21000.00","","MXN","1","24360.00","I","PPD","72150","","01")
vxml.VXML_SetEmisorInfo_cfdi40W(hXml,"EKU9003173C9","ESCUELA KEMPER URGATE","601","")
vxml.VXML_SetReceptorInfo_cfdi40W(hXml,"LERJ970506QN8","JESUS ELIEZER LEIJA RAMIREZ","","","G03","88200","626")

# como es un comprobante de INGRESO entonces un transportista esta cobrando un flete de mercancia que pertenece a un cliente y se factura como un concepto normal */
vxml.VXML_AddConcepto_cfdi40W(hXml,"78101802","001","1","E48","SERVICIO","SERVICIOS TRANSPORTE DE CARGA POR CARRETERA (EN CAMION) A NIVEL REGIONAL Y NACIONAL","21000.00","21000.00","","02")
vxml.VXML_AddConceptoTraslado_cfdi40W(hXml,"21000.00","002","Tasa","0.160000","3360.00")  

# resumen de impuestos 
vxml.VXML_SetImpuestosInfo_cfdi40W(hXml,"3360.00","")
vxml.VXML_AddTraslado_cfdi40W(hXml,"002","Tasa","0.160000","3360.00","21000.00")

# inicia complemento de Carta Porte 3.1, si necesita saber como obtener un UUID para su CFDI de Carta Porte, revise la función HB_GenUUID() */
vxml.VXML_SetCartaPorte31W(hXml,"CCC6FED9-1CDA-4955-B3FC-8E692AB44900","No","","","","1200","","","")
# establecemos origen y destino del flete */
vxml.VXML_CartaPorte31AddUbicacionW(hXml, "Origen", "OR000001","LERJ970506QN8", "TRANSPORTES GALS","","","","","","2025-02-24T12:00:00","","","EUGENIO AGUIRRE","10","","0920","06","","027","TAM","MEX","88284")
vxml.VXML_CartaPorte31AddUbicacionW(hXml, "Destino", "DE000001", "LERJ970506QN8", "PETROQUIMIA DEL GOLFO", "","","","","","2025-02-25T12:00:00","","1200","ANTONIE LAVOISIER","31","","6511","04","","121","MEX","MEX","54730")
#/* datos de las mercancias transportadas */
vxml.VXML_CartaPorte31SetMercanciasW(hXml,"15656.00","KGM","15656.00", "1", "", "Sí")
vxml.VXML_CartaPorte31AddMercanciaW(hXml,"13101902","","RESINA DE POLIESTER","80.000","X4G","BULTOS","","","","","","","","","","","","","","","","","","","","","","","","","","15656.000","","","","","","")
vxml.VXML_CartaPorte31AddMercanciaCantidadTransportaW(hXml,"80.000", "OR000001", "DE000001","")
# datos del transporte que mueve las mercancias */
vxml.VXML_CartaPorte31SetMercanciasAutotransporteW(hXml, "TPAF01", "1522TMG02052019021001009","T3S2S2","7000","91AS9E","2014","BBVA","782B0502FD","","","","","","CTR007","CY3748","","")
# datos del conductor del transporte */
vxml.VXML_CartaPorte31AddFiguraTransporteTiposFiguraW(hXml,"01", "NAVM740828IQ3","DF00231807", "MIGUEL ANGEL NANDO VELEZ","","","", "VERACRUZ","7","","3846","","","036","MEX","MEX","55679")

# generamos el CFDI
nResultado = vxml.VXML_ProcesaDocumentoW(
    hXml, cCurDir+"CSD_EKU9003173C9.cer", cCurDir+"CSD_EKU9003173C9.key", "12345678a", cCurDir+"CFDI40_Demo_Ingreso_CartaPorte31.xml")

if (nResultado == 0):
    for i in range(1, 22):
        value_wide = c_wchar_p(vxml.VXML_GetValueW(hXml, i))
        print("GetValue [" + str(i).zfill(2) + "] = " + value_wide.value)
else:
    value1_wide = c_wchar_p(vxml.VXML_GetValueW(hXml, 1))
    value2_wide = c_wchar_p(vxml.VXML_GetValueW(hXml, 2))
    print("Error en el CFDI [" + value1_wide.value +
          "] = " + value2_wide.value)

vxml.VXML_FreeW(hXml)

print(">>>>>>>>>>> Finished!")