   
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]

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

#/* iniciamos el comprobante esta vez sera un TRASLADO */
hXml = vxml.VXML_NewWithLogW("4.0",cCurDir+"CFDI_Demo_Traslado_CartaPorte31.log")
vxml.VXML_SetUserInfoW(hXml,"demo_cibertec","demo")
vxml.VXML_SetComprobanteInfo_cfdi40W(hXml,"T","3381652892","%cb_date","","","0","","XXX","","0","T","","42051","","01")
vxml.VXML_SetEmisorInfo_cfdi40W(hXml,  "EKU9003173C9","ESCUELA KEMPER URGATE","601","")
vxml.VXML_SetReceptorInfo_cfdi40W(hXml,"EKU9003173C9","ESCUELA KEMPER URGATE","","","S01","42501","601")

# como es un comprobante de TRASLADO, los conceptos son los bienes que se trasladan y se establecen en valores 0.00 y sin impuestos */
vxml.VXML_AddConcepto_cfdi40W(hXml,"24112902","","1677","XUN","Unidad","Tina Plastica BBU/ Plastic tray. Marca: Orbis Norseman y Hus.","0.00","0.00","","01")
vxml.VXML_AddConcepto_cfdi40W(hXml,"24101508","","120","XUN","Unidad","Base de plastico con rodajas para empaque y transporte/Dollies. Marca: Orbis","0.00","0.00","","01")
# 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,"CCC69ff4-e09f-47bc-ba1e-e8a2e92958da","No","","","","12.71","","","")
#/* establecemos origen y destino del traslado */
vxml.VXML_CartaPorte31AddUbicacionW(hXml, "Origen", "", "TEX090814EQ1",  "","","","","","","2025-02-25T11:05:00","","","C. OASYS, CANON DEL PADRE","","","","04","","004","BCN","MEX","22215")
vxml.VXML_CartaPorte31AddUbicacionW(hXml, "Destino", "", "BIM011108DJ5", "", "","","","","","2025-02-25T12:00:00","","12.71","Av. Juan Alesio","12490","s/n","1525","04","INDUSTRIAL PACIFICO III TIJUANA BAJA CALIFORNIA","004","BCN","MEX","22643")
#    /* datos de las mercancias transportadas */
vxml.VXML_CartaPorte31SetMercanciasW(hXml,"5162","KGM","", "2", "", "")
vxml.VXML_CartaPorte31AddMercanciaW(hXml,"24112902","","Tina Plastica BBU/ Plastic tray. Marca: Orbis Norseman y Hus.","1677","XUN","","","No","","","","","","","","","","","","","","","","","","","","","","","","4411","","","","","","")
vxml.VXML_CartaPorte31AddMercanciaW(hXml,"24101508","","Base de plastico con rodajas para empaque y transporte/Dollies. Marca: Orbis","120","XUN","","","","","","","","","","","","","","","","","","","","","","","","","","","751","","","","","","")
# datos del transporte que mueve las mercancias */
vxml.VXML_CartaPorte31SetMercanciasAutotransporteW(hXml, "TPAF01", "0203THG15092014133901227","T3S2","12.6","50EN6B","2019","QUALITAS COMPA�IA DE SEGUROS S.A DE C.V.","540326225","","","","","","CTR007","4KE1943","","")
#/* datos del conductor del transporte */
vxml.VXML_CartaPorte31AddFiguraTransporteTiposFiguraW(hXml,"01", "VEGD861207ND3","LFD01107519", "VENEGAS GARCIA DANIEL","","","", "Prolongacion Callejon Vecinal","524","A","","04","","004","BCN","MEX","22414")
 
 # generamos el CFDI
nResultado = vxml.VXML_ProcesaDocumentoW(
    hXml, cCurDir+"CSD_EKU9003173C9.cer", cCurDir+"CSD_EKU9003173C9.key", "12345678a", cCurDir+"CFDI_Demo_Traslado_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!")