GenerarCUF
Generates the complete CUF (Código Único de Factura - Unique Invoice Code) for an invoice according to Bolivian tax authority specifications. The CUF is a unique identifier that combines invoice metadata with a verification digit and is encoded in hexadecimal format. It includes a control code (CUFD) to ensure authenticity.Function Signature
Parameters
Tax identification number (NIT) of the issuer. Formatted to 13 digits.
Invoice date and time. Formatted as yyyyMMddHHmmssSSS (17 digits).
Branch office code. Formatted to 4 digits.
Invoice modality code. Formatted to 1 digit.
Emission type code. Formatted to 1 digit.
Invoice type code. Formatted to 1 digit.
Document sector type code. Formatted to 2 digits.
Sequential invoice number. Formatted to 10 digits.
Point of sale code. Formatted to 4 digits.
Control code (CUFD) obtained from tax authority authorization.
Returns
The complete CUF in hexadecimal format with the control code appended.
Error if CUF generation fails.
CUF Generation Algorithm
The CUF is generated through the following steps:-
Format Fields: All input parameters are formatted to fixed-length strings with leading zeros:
- NIT: 13 digits
- Date/Time: 17 digits (yyyyMMddHHmmssSSS)
- Branch: 4 digits
- Modality: 1 digit
- Emission Type: 1 digit
- Invoice Type: 1 digit
- Document Sector: 2 digits
- Invoice Number: 10 digits
- Point of Sale: 4 digits
- Concatenate: All formatted fields are concatenated into a single numeric string (53 digits total).
- Calculate Verification Digit: A Module 11 algorithm is applied to calculate a verification digit, which is appended to the string (54 digits total).
- Convert to Hexadecimal: The numeric string is converted to hexadecimal (base 16) and converted to uppercase.
- Append Control Code: The CUFD control code is appended to create the final CUF.
Verification Digit Calculation
The Module 11 algorithm (calculaDigitoMod11) calculates the verification digit:
- Iterates through the string from right to left
- Multiplies each digit by a factor (2-9, cycling)
- Sums all products
- Applies modulo 11 to the sum
- Returns the verification digit (special handling for 10→1, 11→0)
Example
Notes
- The CUF must be unique for each invoice
- The control code (CUFD) must be obtained from the tax authority before generating invoices
- All numeric fields are zero-padded to their required length
- The date/time must include milliseconds
- The final CUF is returned in uppercase hexadecimal format
Related
- XML Signing - Sign invoices with digital certificates
