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_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_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_SetNomina12W.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]
vxml.VXML_Nomina12SetEmisorW.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_Nomina12SetReceptorW.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]
vxml.VXML_Nomina12SetPercepcionesW.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_Nomina12AddPercepcionW.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_Nomina12SetDeduccionesW.argtypes = [c_void_p, c_wchar_p, c_wchar_p]
vxml.VXML_Nomina12AddDeduccionW.argtypes = [c_void_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
vxml.VXML_Nomina12AddOtroPagoW.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]

print(">>>>>>>>>>> Timbrado CFDI 4.0 de Nomina 1.2")

hXml = vxml.VXML_NewWithLogW("4.0","CFDI40_Demo_Nomina12.log")

# credenciales de timbrado
vxml.VXML_SetUserInfoW(hXml,"demo_cibertec","demo")
# Datos generales del comprobante*/
vxml.VXML_SetComprobanteInfo_cfdi40W(hXml,"NOM","1234","%cb_date","","","8628.40","2315.83","MXN","","6312.57","N","PUE","07270","","01")
# datos del emisor */
vxml.VXML_SetEmisorInfo_cfdi40W(hXml,"EKU9003173C9","ESCUELA KEMPER URGATE","601","")
# datos del receptor, uso del CFDI = CN01, Regimen del receptor = 605 */
vxml.VXML_SetReceptorInfo_cfdi40W(hXml,"FOSM380609Q76","MARTIN FLORES SOLORZANO","","","CN01","53050","605")
# concepto similar al concepto de Nomina 1.2, solo agregar 01 en objeto de impuestos */
vxml.VXML_AddConcepto_cfdi40W(hXml,"84111505","","1","ACT","","Pago de nómina","8628.40","8628.40","2315.83","01")

# complmento de nomina se elabora similiar a como se hace en la version anterior */
vxml.VXML_SetNomina12W(hXml,"O","2022-01-01","2022-01-15","2022-01-15","15","8628.40","2315.83","0.00")
vxml.VXML_Nomina12SetEmisorW(hXml,"","20035","","","")
vxml.VXML_Nomina12SetReceptorW(hXml,"FOSM380609HDFLLR08","01186258","2022-01-01","P15D","01","","01","02","164","UNIDAD DE APOYO ADMINISTRATIVA","Director","1","04","012","2785594979","8302.95","546.00","MEX")
vxml.VXML_Nomina12SetPercepcionesW(hXml,"8628.40","0.00","0.00","8210.40","418.00")
vxml.VXML_Nomina12AddPercepcionW(hXml,"001","001","SUELDO BASE","5560.80","0.00","","","","","","")
vxml.VXML_Nomina12AddPercepcionW(hXml,"002","002","GRATIFICACION","1581.15","0.00","","","","","","")
vxml.VXML_Nomina12AddPercepcionW(hXml,"038","016","COMPENSACION POR RETABULACION","1068.45","0.00","","","","","","")
vxml.VXML_Nomina12AddPercepcionW(hXml,"029","029","DESPENSA","0.00","418.00","","","","","","")
vxml.VXML_Nomina12SetDeduccionesW(hXml,"1116.65", "1199.18")
vxml.VXML_Nomina12AddDeduccionW(hXml,"001","001","CUOTAS DE SERVICIO DE SALUD","349.65")
vxml.VXML_Nomina12AddDeduccionW(hXml,"001","001","CUOTAS AL SISTEMA SOLIDARIO DE REPARTO","461.16")
vxml.VXML_Nomina12AddDeduccionW(hXml,"001","001","CUOTAS AL SISTEMA DE CAPITALIZACION INDIVIDUAL","105.84")
vxml.VXML_Nomina12AddDeduccionW(hXml,"002","002","ISR","1199.18")
vxml.VXML_Nomina12AddDeduccionW(hXml,"004","004","AHORRO VOLUNTARIO","200.00")
vxml.VXML_Nomina12AddOtroPagoW(hXml,"002","CredSal","Credito al Salario","0.00","","","","0.00")

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