empresa-libre/source/static/js/controller/util.js

365 lines
8.5 KiB
JavaScript
Raw Normal View History

2017-10-04 00:11:49 -05:00
var PUBLICO = "Público en general";
var RFC_PUBLICO = "XAXX010101000";
var RFC_EXTRANJERO = "XEXX010101000";
var PAIS = "México";
2017-10-06 00:10:27 -05:00
var DECIMALES = 2;
2017-11-19 00:42:16 -06:00
var CLAVE_ANTICIPOS = '84111506';
2017-10-04 00:11:49 -05:00
var db = new loki('data.db');
var table_config = db.addCollection('config')
var table_taxes = db.addCollection('taxes')
var table_pt = db.addCollection('productstaxes')
var table_totals = db.addCollection('totals', {unique: ['tax']})
2017-10-04 23:22:05 -05:00
var table_series = db.addCollection('series')
var table_usocfdi = db.addCollection('usocfdi')
2017-11-08 23:47:15 -06:00
var table_relaciones = db.addCollection('relaciones')
2017-10-04 00:11:49 -05:00
2017-11-16 22:49:17 -06:00
var months = [
{id: -1, value: 'Todos'},
{id: 1, value: 'Enero'},
{id: 2, value: 'Febrero'},
{id: 3, value: 'Marzo'},
{id: 4, value: 'Abril'},
{id: 5, value: 'Mayo'},
{id: 6, value: 'Junio'},
{id: 7, value: 'Julio'},
{id: 8, value: 'Agosto'},
{id: 9, value: 'Septiembre'},
{id: 10, value: 'Octubre'},
{id: 11, value: 'Noviembre'},
{id: 12, value: 'Diciembre'},
]
2017-12-04 22:09:22 -06:00
function get_icon(tipo){
icons = {
xml: 'fa-file-code-o',
pdf: 'fa-file-pdf-o',
zip: 'fa-file-zip-o',
email: 'fa-envelope-o',
2017-12-21 01:13:28 -06:00
print: 'fa-print',
2017-12-04 22:09:22 -06:00
}
return "<span class='webix_icon " + icons[tipo] + "'></span>"
}
2017-11-21 00:48:51 -06:00
function focus(name){
webix.UIManager.setFocus(name)
}
2017-11-19 14:34:54 -06:00
function showvar(values){
2017-10-04 00:11:49 -05:00
webix.message(JSON.stringify(values, null, 2))
}
2017-12-20 01:15:48 -06:00
function showtype(values){
webix.message(typeof(values))
}
2017-11-19 14:34:54 -06:00
function show(nombre, value){
if(value == '0'){
value = false
}
if(value){
$$(nombre).show()
}else{
$$(nombre).hide()
}
}
2017-10-06 00:10:27 -05:00
function msg_error(msg){
2017-11-25 20:26:15 -06:00
if(!msg){
msg = 'Error al consultar'
}
2017-10-06 00:10:27 -05:00
webix.message({type: 'error', text: msg})
}
2017-11-25 20:26:15 -06:00
function msg_ok(msg){
webix.message({type: 'success', text: msg})
2017-10-06 00:10:27 -05:00
}
2017-10-05 15:33:25 -05:00
Number.prototype.round = function(decimals){
return Number((Math.round(this + "e" + decimals) + "e-" + decimals))
}
2017-11-11 23:58:11 -06:00
String.prototype.is_number = function(){
return /^\d+$/.test(this)
}
2017-11-22 00:46:23 -06:00
String.prototype.to_float = function(){
return get_float(this)
}
2017-11-21 00:48:51 -06:00
function get_float(value){
2017-12-20 01:15:48 -06:00
return parseFloat(value.replace('$', '').replace(',', '').trim()).round(DECIMALES)
2017-11-21 00:48:51 -06:00
}
2017-10-04 00:11:49 -05:00
webix.protoUI({
$cssName: "text",
name: "currency",
$init:function(){
this.attachEvent("onItemClick", function(){
this.$setValue(this.config.raw, true)
})
this.attachEvent("onBlur", function(){
this.$setValue(this.config.value)
})
},
$render:function(){
this.$setValue(this.config.value)
},
$setValue:function(value, raw){
this.config.raw = value
if(!raw){
value = webix.i18n.priceFormat(value)
}
this.getInputNode().value = value
}
}, webix.ui.text)
2017-06-27 15:43:02 -05:00
2017-12-20 01:15:48 -06:00
webix.ui.datafilter.countRows = webix.extend({
2017-06-27 17:07:22 -05:00
refresh:function(master, node, value){
2017-12-20 01:15:48 -06:00
node.firstChild.innerHTML = master.count();
2017-06-27 17:07:22 -05:00
}
2017-12-20 01:15:48 -06:00
}, webix.ui.datafilter.summColumn);
2017-06-27 17:07:22 -05:00
2018-01-01 22:14:49 -06:00
webix.ui.datafilter.summActive = webix.extend({
refresh:function(master, node, value){
node.firstChild.innerHTML = this.summGenerate(master);
},
summGenerate:function(master){
var sum = 0
master.eachRow(function(id){
var row = master.getItem(id)
if(row.estatus == 'Generado'){
var importe = row.total
if(typeof importe === 'string'){
importe = row.total.to_float()
}
sum += importe
}
})
return webix.i18n.priceFormat(sum)
}
}, webix.ui.datafilter.summColumn);
2018-01-02 18:24:55 -06:00
webix.ui.datafilter.summTimbrada = webix.extend({
refresh:function(master, node, value){
node.firstChild.innerHTML = this.summGenerate(master);
},
summGenerate:function(master){
var sum = 0
master.eachRow(function(id){
var row = master.getItem(id)
if(row.estatus == 'Timbrada'){
var importe = row.total_mn
if(typeof importe === 'string'){
importe = row.total_mn.to_float()
}
sum += importe
}
})
return webix.i18n.priceFormat(sum)
}
}, webix.ui.datafilter.summColumn);
2017-06-27 15:43:02 -05:00
function validate_rfc(value){
rfc = value.trim().toUpperCase();
if ( rfc == ""){
2017-11-25 20:26:15 -06:00
msg_error('El RFC no puede estar vacío')
2017-06-27 15:43:02 -05:00
return false
}
2017-10-02 00:12:22 -05:00
var tipo_persona = $$('tipo_persona').getValue()
var length = 13
var start = 4
if(tipo_persona==2){
length = 12
start = 2
}
if (rfc.length != length){
2017-11-25 20:26:15 -06:00
msg_error('Longitud incorrecta del RFC')
2017-06-27 15:43:02 -05:00
return false
}
2017-10-02 00:12:22 -05:00
if (tipo_persona < 3 && (rfc == RFC_PUBLICO || rfc == RFC_EXTRANJERO)){
2017-11-25 20:26:15 -06:00
msg_error('RFC incorrecto')
2017-10-02 00:12:22 -05:00
return false
2017-06-27 15:43:02 -05:00
}
2017-10-02 00:12:22 -05:00
2017-06-27 15:43:02 -05:00
var part = rfc.slice(0, start);
var re = new RegExp('[a-z&Ñ]{' + start + '}', 'i');
2017-12-03 00:09:44 -06:00
if (!part.match(re)){
msg_error('El RFC tiene caractéres inválidos al inicio')
return false
}
part = rfc.slice(-3);
re = new RegExp('[a-z0-9]{3}', 'i');
if (!part.match(re)){
msg_error('El RFC tiene caractéres inválidos al final')
return false
}
part = rfc.slice(-9, -3);
re = new RegExp('[0-9]{6}', 'i');
if (!part.match(re)){
msg_error('Fecha inválida')
return false
}
var month = parseInt(part.slice(-4, -2))
if (month == 0 || month > 12 ){
msg_error('Fecha inválida')
return false
}
var day = parseInt(part.slice(-2))
if (day == 0 || day > 31 ){
msg_error('Fecha inválida')
return false
}
return true
}
function validar_rfc(value){
rfc = value.trim().toUpperCase();
if (rfc == ""){
msg_error('El RFC no puede estar vacío')
return false
}
if (rfc.length < 12 || rfc.length > 13){
msg_error('Longitud incorrecta del RFC')
return false
}
var length = rfc.length
var start = 4
if(length==12){
start = 2
}
var part = rfc.slice(0, start);
var re = new RegExp('[a-z&Ñ]{' + start + '}', 'i');
2017-06-27 15:43:02 -05:00
if (!part.match(re)){
2017-11-25 20:26:15 -06:00
msg_error('El RFC tiene caractéres inválidos al inicio')
2017-06-27 15:43:02 -05:00
return false
}
part = rfc.slice(-3);
re = new RegExp('[a-z0-9]{3}', 'i');
if (!part.match(re)){
2017-11-25 20:26:15 -06:00
msg_error('El RFC tiene caractéres inválidos al final')
2017-06-27 15:43:02 -05:00
return false
}
part = rfc.slice(-9, -3);
re = new RegExp('[0-9]{6}', 'i');
if (!part.match(re)){
2017-11-25 20:26:15 -06:00
msg_error('Fecha inválida')
2017-06-27 15:43:02 -05:00
return false
}
var month = parseInt(part.slice(-4, -2))
if (month == 0 || month > 12 ){
2017-11-25 20:26:15 -06:00
msg_error('Fecha inválida')
2017-06-27 15:43:02 -05:00
return false
}
var day = parseInt(part.slice(-2))
if (day == 0 || day > 31 ){
2017-11-25 20:26:15 -06:00
msg_error('Fecha inválida')
2017-06-27 15:43:02 -05:00
return false
}
return true
}
function validate_email(email){
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return re.test(email)
}
2017-10-04 00:11:49 -05:00
function add_config(args){
var key = table_config.findOne({key: args.key})
if(key===null){
table_config.insert(args)
}else{
key.value = args.value
table_config.update(key)
}
}
function get_config(value){
var key = table_config.findOne({key: value})
if(key===null){
return ''
}else{
return key.value
}
}
2017-11-15 19:29:51 -06:00
webix.DataDriver.plainjs = webix.extend({
arr2hash:function(data){
var hash = {};
for (var i=0; i<data.length; i++){
var pid = data[i].parent_id;
if (!hash[pid]) hash[pid]=[];
hash[pid].push(data[i]);
}
return hash;
},
hash2tree:function(hash, level){
var top = hash[level];
for (var i=0; i<top.length; i++){
var branch = top[i].id;
if (hash[branch])
top[i].data = this.hash2tree(hash, branch);
}
return top;
},
getRecords:function(data, id){
var hash = this.arr2hash(data);
return this.hash2tree(hash, 0);
}
}, webix.DataDriver.json)
2017-12-20 01:15:48 -06:00
function get_forma_pago(control){
webix.ajax().get('/values/formapago', {key: true}, function(text, data){
var values = data.json()
$$(control).getList().parse(values)
})
2018-01-08 09:40:35 -06:00
}
function validate_regexp(value, pattern){
re = new RegExp(pattern, 'i');
if(value.match(re)){
return true
}else{
return false
}
}
function validate_pedimento(value){
2018-01-08 16:00:55 -06:00
var pattern = '[0-9]{2} [0-9]{2} [0-9]{4} [0-9]{7}'
2018-01-08 09:40:35 -06:00
return validate_regexp(value, pattern)
}