@arquetic/billbo
Version:
Utilities for the national tax billing system of Bolivia
175 lines (174 loc) • 6.53 kB
JavaScript
"use strict";
exports.__esModule = true;
var CUF = /** @class */ (function () {
function CUF(data) {
this.lengths = [13, 8, 17, 4, 1, 1, 1, 2, 4];
if (data) {
this.data.NIT_EMISOR = data.NIT_EMISOR,
this.data.NÚMERO_FACTURA = data.NÚMERO_FACTURA,
this.data.FECHA_HORA = data.FECHA_HORA || this.getCUFTime(),
this.data.SUCURSAL = data.SUCURSAL || '0',
this.data.MODALIDAD = data.MODALIDAD || '1',
this.data.TIPO_EMISIÓN = data.TIPO_EMISIÓN || '1',
this.data.CÓDIGO_DOCUMENTO_FISCAL = data.CÓDIGO_DOCUMENTO_FISCAL || '1',
this.data.TIPO_DOCUMENTO_SECTOR = data.TIPO_DOCUMENTO_SECTOR || '1',
this.data.POS = data.POS || '0';
var i = 0;
for (var prop in this.data) {
var st = this.validate(this.data[prop], this.lengths[i]);
i++;
if (st.ok === true)
this.data[prop] = st.f;
else
throw new TypeError("Malformed arguments to CUF constructor");
}
this.data.CUF = this.calCUF();
return this.data;
}
else {
throw new TypeError("Unxpected arguments to CUF constructor");
}
}
/*
* Get CUF from object bill data
*/
CUF.prototype.getCUF = function () {
this.data.CUF = this.calCUF();
return this.data;
};
/*
* Function: fill
* Description: Left filled with character '0' if field length is less length.
* Arguments:
* f: field to fill
* l: reference length
* Return:
* String filled
*/
CUF.prototype.fill = function (f, l) {
var d = l - f.length;
if (d !== 0)
for (var i = 0; i < d; i++)
f = '0' + f;
return f;
};
/*
* Function: Validate
* Description: Check if field is Number and proper length.
* Return: Object adding validated field.
*/
CUF.prototype.validate = function (f, l) {
if (f.constructor === Number) {
var fo = f.toString();
if (fo.length <= l)
return { f: fo, ok: true };
}
else if (f.constructor === String) {
if (/^\+?[0-9][\d]*$/.test(f) === true && f.length <= l)
return { f: f, ok: true };
else
return { ok: false };
}
else
return { ok: false };
};
/*
* Function: mod11
* Description: Bolivian "Impuestos Nacionales" module 11 algorithm.
* Return: String with module 11 code.
*/
CUF.prototype.mod11 = function (toMod11, numDig, limMult, ten) {
if (numDig === void 0) { numDig = 1; }
if (limMult === void 0) { limMult = 9; }
if (ten === void 0) { ten = false; }
var mult, add, i, n, dig;
if (!ten)
numDig = 1;
for (n = 1; n <= numDig; n++) {
add = 0;
mult = 2;
for (i = toMod11.length - 1; i >= 0; i--) {
add += (mult * parseInt(toMod11.substring(i, i + 1)));
if (++mult > limMult)
mult = 2;
}
if (ten) {
dig = ((add * 10) % 11) % 10;
}
else {
dig = add % 11;
}
if (ten) {
dig = ((add * 10) % 11) % 10;
}
else {
dig = add % 11;
}
if (dig == 10) {
toMod11 += '1';
}
if (dig == 11) {
toMod11 += '0';
}
if (dig < 10) {
toMod11 += dig.toString();
}
}
return toMod11.substring(toMod11.length - numDig, toMod11.length);
};
/*
* Function: calCUF
* Description: Bolivian "Impuestos Nacionales" CUF (Código Único de Factura) calculation.
*/
CUF.prototype.calCUF = function () {
this.cuf =
this.fill(this.data.NIT_EMISOR, 13) +
this.data.FECHA_HORA +
this.fill(this.data.SUCURSAL, 4) +
this.data.MODALIDAD +
this.data.TIPO_EMISIÓN +
this.data.CÓDIGO_DOCUMENTO_FISCAL +
this.fill(this.data.TIPO_DOCUMENTO_SECTOR, 2) +
this.fill(this.data.NÚMERO_FACTURA, 8) +
this.fill(this.data.POS, 4);
this.cuf = this.cuf + this.mod11(this.cuf);
return this.cuf.toUpperCase();
};
/*
* Function: CUFTime
* Description: Bolivian "Impuestos Nacionales" CUF (Código Único de Factura) calculation.
* Return: Return current date in required format.
*/
CUF.prototype.getCUFTime = function () {
var dt = new Date();
var current_date = dt.getDate().toString();
var current_month = (dt.getMonth() + 1).toString();
var current_year = dt.getFullYear().toString();
var current_hrs = dt.getHours().toString();
var current_mins = dt.getMinutes().toString();
var current_secs = dt.getSeconds().toString();
var current_milliseconds = dt.getMilliseconds().toString();
// * Fill to left
current_date = +current_date < 10 ? '0' + current_date : current_date;
current_month = +current_month < 10 ? '0' + current_month : current_month;
current_hrs = +current_hrs < 10 ? '0' + current_hrs : current_hrs;
current_mins = +current_mins < 10 ? '0' + current_mins : current_mins;
current_secs = +current_secs < 10 ? '0' + current_secs : current_secs;
current_milliseconds =
current_milliseconds === '0' ? '000' :
+current_milliseconds < 10 ? '00' + current_milliseconds :
+current_milliseconds < 100 ? '0' + current_milliseconds :
current_milliseconds;
// * Concat in required way
var current_datetime = current_year +
current_month +
current_date +
current_hrs +
current_mins +
current_secs +
current_milliseconds;
return current_datetime;
};
return CUF;
}());
exports.CUF = CUF;