Ejemplo n.º 1
0
 def _step1_verhoeff_5digits(self, number, nit, date_invoice, amount_total):
     # dummy verhoeff numbers
     data = (number, nit, date_invoice, amount_total)
     # append two verhoeff numbers to each param
     sum1 = sum([int(str(d) + verhoeff.encode(d, 2)) for d in data])
     # generate 5 verhoeff digits
     return verhoeff.encode(sum1, 5)
Ejemplo n.º 2
0
    def generate(self, number, amount_total_company_signed, **kwargs):
        """Changing values are bill & amount
           the others can be persisten through
           the instance (date_invoice, nit).
           Persistence values can be overrided by
           keyword argument"""
        # round amount upper if >= .5
        amount_total_company_signed = int(round(amount_total_company_signed))

        # nit
        if kwargs.has_key('nit'):
            nit = self.set_nit(kwargs['nit']).get_nit()
        else:
            nit = self.get_nit()

        # date
        if kwargs.has_key('date_invoice'):
            date_invoice = self.set_date(kwargs['date_invoice']).get_date()
        else:
            date_invoice = self.get_date()

        # validate parameters
        if not self.validate_bill(number):
            raise InvalidParam('Invalid bill %s' % number)

        if not self.validate_amount(amount_total_company_signed):
            raise InvalidParam('Invalid amount %s' %
                               amount_total_company_signed)

        if not self.validate_nit(nit):
            raise InvalidParam('Invalid nit %s' % nit)

        if not self.validate_date(date_invoice):
            raise InvalidParam('Invalid date %s' % date_invoice)

        ##
        ## Start process
        ##

        ## get persistent parameters
        auth = self.get_auth()
        secret = self.get_secret()

        debug('Begining process')

        ##
        ## step 1
        ##
        verhoeff5 = self._step1_verhoeff_5digits(number, nit, date_invoice,
                                                 amount_total_company_signed)
        debug('Verhoeff 5 Digits: ' + repr(verhoeff5), 1)

        ##
        ## step2
        ##
        strings = self._step2_slice_secret_by_verhoeff(verhoeff5)
        debug('String Slices: ' + repr(strings), 1)

        ## concatenate
        long_string = ''

        for s1, s2 in zip(
            (auth, number, nit, date_invoice, amount_total_company_signed),
                strings):
            ## add two verhoeff digits to each param
            ## but auth
            if s1 != auth:
                s1 = str(s1) + verhoeff.encode(s1, 2)
            ## concatenate string
            long_string += str(s1) + str(s2)

        debug('Long String: ' + repr(long_string), 2)

        ##
        ## step3
        ##
        long_string_rc4 = self._step3_encode_allegedrc4(verhoeff5, long_string)
        debug('ARC4 Long String: ' + repr(long_string_rc4), 1)

        ##
        ## step4
        ##
        partial_sums = self._step4_partial_sums(long_string_rc4)
        debug('Partial Sums: ' + repr(partial_sums), 2)
        debug('Total Sum: ' + str(sum(partial_sums)), 2)

        ##
        ## step5
        ##
        base64_string = self._step5_base64_sum(verhoeff5, partial_sums)
        debug('Base64 String: ' + base64_string, 1)

        ##
        ## step6
        ##
        code = self._step6_get_code(verhoeff5, base64_string)
        debug('Raw Code: ' + code, 1)

        ##
        ## Return print-ready code
        ##
        return self._format_code(code)