예제 #1
0
 def wrap_up(tf, of):
     with open(of, 'w', encoding="utf-8") as a:
         a.write(
             sign_cfdi(
                 dat['KEY_PRIVATE'],
                 dat['XSLT_SCRIPT'],
                 tf
             )
         )
     os.remove(tf)
예제 #2
0
    def format_wrt(self, output_file, dat):
        self.logger.debug('dumping contents of dat: {}'.format(repr(dat)))

        def save(xo):
            tmp_dir = tempfile.gettempdir()
            f = os.path.join(tmp_dir, HelperStr.random_str())
            writedom_cfdi(xo.toDOM(), self.__MAKEUP_PROPOS, f)
            return f

        c = Comprobante()
        c.Version = '3.3'
        c.Serie = dat['CONTROL']['SERIE']  # optional
        c.Folio = dat['CONTROL']['FOLIO']  # optional
        c.Fecha = dat['TIME_STAMP']
        c.Sello = '__DIGITAL_SIGN_HERE__'
        c.FormaPago = dat["FORMA_PAGO"]['CLAVE']  # optional
        c.NoCertificado = dat['NUMERO_CERTIFICADO']
        c.Certificado = dat['CERT_B64']
        c.SubTotal = dat['TOTALES']['IMPORTE_SUM']
        c.Descuento = dat['TOTALES'][
            'DESCTO_SUM'] if dat['TOTALES']['DESCTO_SUM'] > 0 else None
        c.Total = dat['TOTALES']['MONTO_TOTAL']
        if dat['MONEDA']['ISO_4217'] == 'MXN':
            c.TipoCambio = 1
        else:
            # optional (requerido en ciertos casos)
            c.TipoCambio = truncate(dat['MONEDA']['TIPO_DE_CAMBIO'],
                                    self.__NDECIMALS)
        c.Moneda = dat['MONEDA']['ISO_4217']
        c.TipoDeComprobante = 'I'
        c.MetodoPago = dat['METODO_PAGO']  # optional
        c.LugarExpedicion = dat['LUGAR_EXPEDICION']

        c.Emisor = pyxb.BIND()
        c.Emisor.Nombre = dat['EMISOR']['RAZON_SOCIAL']  # optional
        c.Emisor.Rfc = dat['EMISOR']['RFC']
        c.Emisor.RegimenFiscal = dat['EMISOR']['REGIMEN_FISCAL']

        c.Receptor = pyxb.BIND()
        c.Receptor.Nombre = dat['RECEPTOR']['RAZON_SOCIAL']  # optional
        c.Receptor.Rfc = dat['RECEPTOR']['RFC']
        c.Receptor.UsoCFDI = dat['RECEPTOR']['USO_CFDI']

        c.Conceptos = pyxb.BIND()
        for i in dat['CONCEPTOS']:
            c.Conceptos.append(
                pyxb.BIND(
                    Cantidad=i['CANTIDAD'],
                    ClaveUnidad=i['UNIDAD'],
                    ClaveProdServ=i['PRODSERV'],
                    Descripcion=i['DESCRIPCION'],
                    ValorUnitario=i['PRECIO_UNITARIO'],
                    NoIdentificacion=i['SKU'],  # optional
                    Importe=truncate(i['IMPORTE'], self.__NDECIMALS),
                    Descuento=i['DESCTO'] if i['DESCTO'] > 0 else None,
                    Impuestos=self.__tag_impuestos(i)
                    if i['TASA_IMPUESTO'] > 0 else None))

        def traslado(c, tc, imp):
            return pyxb.BIND(TipoFactor='Tasa',
                             Impuesto=c,
                             TasaOCuota=tc,
                             Importe=imp)

        def zigma(v):
            z = Decimal(0)
            for w in v:
                z += self.__narf(w['importe'])
            return float(z)

        c.Impuestos = pyxb.BIND(
            TotalImpuestosRetenidos=0,
            TotalImpuestosTrasladados=zigma(dat['TRASLADOS']),
            Traslados=pyxb.BIND(*tuple([
                traslado(t['clave'], self.__place_tasa(t['tasa']),
                         t['importe']) for t in dat['TRASLADOS']
            ])))

        tmp_file = save(c)
        HelperStr.edit_pattern('TipoCambio="1.0"', 'TipoCambio="1"',
                               tmp_file)  # XXX: Horrible workaround
        HelperStr.edit_pattern(
            '(Descuento=)"([0-9]*(\.[0-9]{0,1})?)"',
            lambda x: 'Descuento="%.2f"' % (float(x.group(2)), ), tmp_file)
        HelperStr.edit_pattern(
            '(Importe=)"([0-9]*(\.[0-9]{0,1})?)"', lambda x: 'Importe="%.2f"' %
            (float(x.group(2)), ), tmp_file)
        with open(output_file, 'w', encoding="utf-8") as a:
            a.write(sign_cfdi(dat['KEY_PRIVATE'], dat['XSLT_SCRIPT'],
                              tmp_file))
        os.remove(tmp_file)
예제 #3
0
    def format_wrt(self, output_file, dat):
        self.logger.debug('dumping contents of dat: {}'.format(repr(dat)))

        def save(xo):
            tmp_dir = tempfile.gettempdir()
            f = os.path.join(tmp_dir, HelperStr.random_str())
            writedom_cfdi(xo.toDOM(), self.__MAKEUP_PROPOS, f)
            return f

        c = Comprobante()
        c.Version = '3.3'
        c.Serie = dat['CONTROL']['SERIE']  # optional
        c.Folio = dat['CONTROL']['FOLIO']  # optional
        c.Fecha = dat['TIME_STAMP']
        c.Sello = '__DIGITAL_SIGN_HERE__'
        c.FormaPago = "01"  # optional
        c.NoCertificado = dat['NUMERO_CERTIFICADO']
        c.Certificado = dat['CERT_B64']
        c.SubTotal = dat['TOTALES']['IMPORTE_SUM']
        c.Total = dat['TOTALES']['MONTO_TOTAL']
        if dat['MONEDA']['ISO_4217'] == 'MXN':
            c.TipoCambio = 1
        else:
            # optional (requerido en ciertos casos)
            c.TipoCambio = truncate(dat['MONEDA']['TIPO_DE_CAMBIO'], self.__NDECIMALS)
        c.Moneda = dat['MONEDA']['ISO_4217']
        c.TipoDeComprobante = 'I'
        c.MetodoPago = "PUE"  # optional and hardcode until ui can suply such value
        c.LugarExpedicion = dat['LUGAR_EXPEDICION']

        c.Emisor = pyxb.BIND()
        c.Emisor.Nombre = dat['EMISOR']['RAZON_SOCIAL']  # optional
        c.Emisor.Rfc = dat['EMISOR']['RFC']
        c.Emisor.RegimenFiscal = dat['EMISOR']['REGIMEN_FISCAL']

        c.Receptor = pyxb.BIND()
        c.Receptor.Nombre = dat['RECEPTOR']['RAZON_SOCIAL']  # optional
        c.Receptor.Rfc = dat['RECEPTOR']['RFC']
        c.Receptor.UsoCFDI = dat['RECEPTOR']['USO_CFDI']

        c.Conceptos = pyxb.BIND()
        for i in dat['CONCEPTOS']:
            c.Conceptos.append(pyxb.BIND(
                Cantidad=i['CANTIDAD'],
                ClaveUnidad=i['UNIDAD'],
                ClaveProdServ=i['PRODSERV'],
                Descripcion=i['DESCRIPCION'],
                ValorUnitario=i['PRECIO_UNITARIO'],
                NoIdentificacion=i['SKU'],  # optional
                Importe=truncate(i['IMPORTE'], self.__NDECIMALS),
                Impuestos=self.__tag_impuestos(i)
            ))

        def traslado(c, tc, imp):
            return pyxb.BIND(TipoFactor='Tasa',
                Impuesto=c, TasaOCuota=tc, Importe=imp)

        def zigma(v):
            z = 0
            for w in v:
                z += w['importe']
            return z

        c.Impuestos = pyxb.BIND(
            TotalImpuestosRetenidos=0,
            TotalImpuestosTrasladados=zigma(dat['TRASLADOS']),
            Traslados=pyxb.BIND(
                *tuple([traslado(t['clave'], self.__place_tasa(t['tasa']), t['importe']) for t in dat['TRASLADOS']])
            )
        )

        tmp_file = save(c)
        with open(output_file, 'w') as a:
            a.write(sign_cfdi(dat['KEY_PRIVATE'], dat['XSLT_SCRIPT'], tmp_file))
        os.remove(tmp_file)