salix/services/print/application/util/format.js

99 lines
2.9 KiB
JavaScript

let database = require('../database.js');
let strftime = require('strftime');
module.exports = {
/**
* Devuelve el iban
* @param {String} addressNumber - Dirección de cuenta bancaria
* @param {Object} cb - Callback
*/
accountAddressIban: function(addressNumber, cb) {
database.pool.query('SELECT vn2008.cc_to_iban(?) AS iban', [addressNumber], function(error, result) {
cb(result[0].iban);
});
},
/**
* Obtiene el numero de cuenta completo incluyendo iban
* @param {String} addressNumber - Dirección de cuenta bancaria
* @return {String} Cuenta bancaria formateada
*/
accountAddress: function(addressNumber) {
var formattedAccountAddress = addressNumber.replace(/(.{4})/g, '$1-');
return formattedAccountAddress.substring(0, formattedAccountAddress.length - 1);
},
/**
* Devuelve el numero de cuenta mostrando únicamente los últimos 4 dígitos.
* @param {String} addressNumber - Dirección de cuenta bancaria
* @return {String} Cuenta bancaria formateada
*/
partialAccountAddress: function(addressNumber) {
let address = this.accountAddress(addressNumber);
return address.substring(0, 19).replace(/[0-9]/g, 'X') + address.substring(19, 24);
},
phone: function(number) {
return number;
},
/**
* Format number
* @param {Integer} number Input number
* @return {Integer} Formatted number
*/
number: function(number, minDigits = 0, maxDigits = 2) {
let options = {
style: 'decimal',
minimumFractionDigits: minDigits,
maximumFractionDigits: maxDigits
};
return new Intl.NumberFormat('es-ES', options).format(number);
},
/**
* Format currency
* @param {Integer} ammount Input ammount
* @param {String} type Currency type [EUR, USD, ...]
* @return {Integer} Formatted currency
*/
currency: function(ammount, type = 'EUR', minDigits = 2, maxDigits = 2) {
let options = {
style: 'currency',
currency: type,
minimumFractionDigits: minDigits,
maximumFractionDigits: maxDigits
};
return new Intl.NumberFormat('es-ES', options).format(ammount);
},
percent: function(number, minDigits = 2, maxDigits = 2) {
let options = {
style: 'percent',
minimumFractionDigits: minDigits,
maximumFractionDigits: maxDigits
};
return new Intl.NumberFormat('es-ES', options).format(number);
},
/**
* Format date dd-mm-yyyy
* @param {Object} date - Date object
* @param {String} delimiter - Date delimiter
* @return {String} Formatted date
*/
date: function(date, format = '%d-%m-%Y') {
return strftime(format, date);
},
code: function(code, pad) {
code = String(code);
pad = String(pad);
return pad.substring(0, (pad.length - code.length)) + code;
}
};