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_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]
vxml.VXML_SetInformacionGlobal_cfdi40W.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p]

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

# indicamos la nueva version 4.0 
hXml = vxml.VXML_NewW("4.0")
vxml.VXML_SetLogFileW(hXml, "CFDI40_Demo_IngresoVentasDelDia.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","11500.00","0.00","MXN","1","13340.00","I","PPD","07270","","01")

# Añadir nodo de informacion global, esto solo es necesario cuando se trate de VENTAS DEL DIAS 
vxml.VXML_SetInformacionGlobal_cfdi40W(hXml,"01","01","2022")

 # datos del emisor
vxml.VXML_SetEmisorInfo_cfdi40W(hXml,"EKU9003173C9","ESCUELA KEMPER URGATE","601","")
# datos del receptor, se debe usar el RFC: XAXX010101000, nombre: "PUBLICO EN GENERAL" uso cfdi: "S01" y el mismo CP que emision del CFDI   
vxml.VXML_SetReceptorInfo_cfdi40W(hXml,"XAXX010101000","PUBLICO EN GENERAL","","","S01","07270","616")

# agregamos conceptos
vxml.VXML_AddConcepto_cfdi40W(hXml,"01010101","TKT-001","1","ACT","","Venta","1000.00","1000.00","0.00","02")
vxml.VXML_AddConceptoTraslado_cfdi40W(hXml,"1000.00","002","Tasa","0.160000","160.00")  
vxml.VXML_AddConcepto_cfdi40W(hXml,"01010101","TKT-002","1","ACT","","Venta","500.00","500.00","0.00","02")
vxml.VXML_AddConceptoTraslado_cfdi40W(hXml,"500.00","002","Tasa","0.160000","80.00")  
vxml.VXML_AddConcepto_cfdi40W(hXml,"01010101","TKT-003","1","ACT","","Venta","10000.00","10000.00","0.00","02")
vxml.VXML_AddConceptoTraslado_cfdi40W(hXml,"10000.00","002","Tasa","0.160000","1600.00") 

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

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