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_NewWithLogW.restype = c_void_p
vxml.VXML_NewWithLogW.argtypes = [c_wchar_p, c_wchar_p]
vxml.VXML_SetLogFileW.argtypes = [c_void_p, c_wchar_p]
vxml.VXML_SetUserInfoW.argtypes = [c_void_p,c_wchar_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]

print(">>>>>>>>>>> Timbrado CFDI 4.0 de Ingreso por Ventas del día")

# indicamos la nueva version 4.0
hXml = vxml.VXML_NewWithLogW("4.0","CFDI40_Demo_Ingreso_PublicoGeneral.log")

# credenciales de timbrado
vxml.VXML_SetUserInfoW(hXml,"demo_cibertec","demo")

# Datos generales del comprobante
vxml.VXML_SetComprobanteInfo_cfdi40W(hXml,"CJZ","7303","%cb_date","99","CREDITO","6034.48","0.00","MXN","1","7000.00","I","PPD","07270","","01")
# datos del emisor
vxml.VXML_SetEmisorInfo_cfdi40W(hXml,"EKU9003173C9","ESCUELA KEMPER URGATE","601","")

# datos del receptor, para publico en general RFC: XAXX010101000, cualquier nombre QUE NO SEA "PUBLICO EN GENERAL" uso cfdi: "S01" y el mismo CP que emision del CFDI 
# recuerda que el nombre PUBLICO EN GENERAL solo se puede usar para las VENTAS DEL DIA, las ventas de mostrador deben llevar cualquier otro nombre
vxml.VXML_SetReceptorInfo_cfdi40W(hXml,"XAXX010101000","PONER CUALQUIER NOMBRE AQUI","","","S01","07270","616")

# agregamos conceptos
vxml.VXML_AddConcepto_cfdi40W(hXml,"43211500","EQU-0025","1.000","H87","Computadora","COMPUTADORA HP PORTATIL","6034.48","6034.48","0.00","02")
vxml.VXML_AddConceptoTraslado_cfdi40W(hXml,"6034.48","002","Tasa","0.160000","965.52")  
# resumen de impuestos
vxml.VXML_SetImpuestosInfo_cfdi40W(hXml,"965.52","")
vxml.VXML_AddTraslado_cfdi40W(hXml,"002","Tasa","0.160000","965.52","6034.48")

# generamos el CFDI
nResultado = vxml.VXML_ProcesaDocumentoW(
    hXml, cCurDir+"CSD_EKU9003173C9.cer", cCurDir+"CSD_EKU9003173C9.key", "12345678a", cCurDir+"CFDI40_Demo_Ingreso_PublicoGeneral.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!")