var guru = new GuruObject(true); // IMPORTANTE: // If this is used inside a constructor function, and the function was invoked with the new keyword, this refers to the object that will be created. this will continue to mean the object even in public methods. // If this is used anywhere else, including nested protected functions, it refers to the global scope (which in the case of the browser is the window object). function GuruObject(debugOn) { this.debugOn = debugOn; this.server = { "app":"/", "mediator":"M/", "facades": { "usuario":"guru.facades.UsuarioFacade" ,"venda":"guru.facades.VendaFacade" ,"produto":"guru.facades.ProdutoFacade" ,"producao":"guru.facades.ProducaoFacade" ,"estoque":"guru.facades.EstoqueFacade" ,"compra":"guru.facades.CompraFacade" ,"gerencia":"guru.facades.GerenciaFacade" ,"relatorio":"guru.facades.RelatorioFacade" ,"config":"guru.facades.ConfigFacade" } }; this.sessao = null; this.produtos = null; this.produtosHash = null; this.debug = function(s1,s2) { if (guru.debugOn) { if (s2!=null) { console.log(s1 + ":" + s2); } else { console.log(s1); } } } this.getFormasPagamento = function( ) { var usuario = guru.getUsuario(); console.log("getFormasPagamentos", usuario); console.log("getFormasPagamentos", Object.keys(usuario)); var formas = usuario._formaspagamento; return formas; } this.getCouvert = function() { var usuario = guru.getUsuario(); var couvert = usuario._couvert; return couvert; } this.getManobrista = function() { var usuario = guru.getUsuario(); var manobrista = usuario._manobrista; return manobrista; } this.callHTTP = function(url, fSuccess, fFailure, form) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState!=4) { return; } if (xmlhttp.status == 200) { if (fSuccess) { fSuccess(xmlhttp); } } else { if (fFailure) { fFailure(xmlhttp); } } } if (form) { var s = guru.getFormData(form); if (url.indexOf('?')==-1) { url += '?'; } else { url += '&'; } url += s; } if ( (!(typeof index === 'undefined')) && (index.emp>0) ) { if (url.indexOf('emp=')==-1) { if (url.indexOf('?')==-1) { url += '?'; } else { url += '&'; } url += 'emp=' + index.emp; } } console.log("callHTTP:", url); var method = "GET"; xmlhttp.open(method, url, true); xmlhttp.setRequestHeader('Pragma', 'Cache-Control: no-cache'); xmlhttp.setRequestHeader('Cache-Control', 'no-cache'); xmlhttp.send(); } this.callHTTP2 = function(url, fSuccess, fFailure, form) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState!=4) { return; } if (xmlhttp.status == 200) { if (fSuccess) { fSuccess(xmlhttp); } } else { if (fFailure) { fFailure(xmlhttp); } } } if (form) { var s = guru.getFormData(form); console.log("POST:"+ url, s); xmlhttp.open("POST", url, true); xmlhttp.setRequestHeader('Pragma', 'Cache-Control: no-cache'); xmlhttp.setRequestHeader('Cache-Control', 'no-cache'); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); xmlhttp.setRequestHeader("Content-length", s.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(s); } else { console.log("GET:", url); xmlhttp.open("GET", url, true); xmlhttp.setRequestHeader('Pragma', 'Cache-Control: no-cache'); xmlhttp.setRequestHeader('Cache-Control', 'no-cache'); xmlhttp.send(); } } this.getFormData2 = function(form) { var result = new FormData(); var s = ''; var delim = ''; for (i=0; i < form.length; i++) { var id = form[i].id; if ( (id) ) { var v = form[i].value; if (form[i].type=='checkbox') { if (form[i].checked) { v = 'S'; } else { v = 'N'; } } result.append(id, v); } } return result; } this.getFormData = function(form) { var s = ''; var delim = ''; for (i=0; i < form.length; i++) { if (form[i].id) { var v = form[i].value; if (form[i].type=='checkbox') { if (form[i].checked) { v = 'S'; } else { v = 'N'; } } var x = encodeURI(v); if (x!=v) { console.log('******',x,v, encodeURIComponent(v)); } s += delim + form[i].id + "=" + v; delim = '&'; } } return s; } this.loadInputs = function( form, dados ) { for (i=0; i < form.length; i++) { var id = form[i].id; if ( (id) ) { var v = dados[id]; if (form[i].type=='checkbox') { $('#' + id).attr("checked",(v=='S')).checkboxradio("refresh"); } else if ( (form[i].type=='text') || (form[i].type=='textarea') || (form[i].type=='hidden') || (form[i].type=='password') ) { form[i].value = v; } else if (form[i].type=='select-one') { if (dados['_' + id]) { var opcoes = dados['_' + id]; var s = ''; var colunaDesc = null; var flag = false; for (j=0; j' + desc + ''; } if ( (v==null) && (!flag) ) { s += ''; } form[i].innerHTML = s; $( "#" + id ).selectmenu( "refresh" ); } else { console.log('loadInputs - nao encontrada lista para select "_' + id + '"', Object.keys(dados)); } } else { console.log('loadInputs - nao implementado', form[i].type); } } } } this.getFacadeUsuario = function(metodo) { return guru.server.mediator + guru.server.facades.usuario + "/" + metodo; } this.getFacadeConfig = function(metodo) { return guru.server.mediator + guru.server.facades.config + "/" + metodo; } this.getFacadeEmpresa = function(metodo) { return guru.server.mediator + guru.server.facades.empresa + "/" + metodo; } this.getFacadeVenda = function(metodo) { return guru.server.mediator + guru.server.facades.venda + "/" + metodo; } this.getFacadeProduto = function(metodo) { return guru.server.mediator + guru.server.facades.produto + "/" + metodo; } this.getFacadeProducao = function(metodo) { return guru.server.mediator + guru.server.facades.producao + "/" + metodo; } this.getFacadeEstoque = function(metodo) { return guru.server.mediator + guru.server.facades.estoque + "/" + metodo; } this.getFacadeGerencia = function(metodo) { return guru.server.mediator + guru.server.facades.gerencia + "/" + metodo; } this.getFacadeRelatorio = function(metodo) { return guru.server.mediator + guru.server.facades.relatorio + "/" + metodo; } this.getFacadeCompra = function(metodo) { return guru.server.mediator + guru.server.facades.compra + "/" + metodo; } this.getUsuario = function() { if (guru.sessao && guru.sessao.Usuario) { return guru.sessao.Usuario; } return null; } this.setUsuario = function(u) { if (guru.sessao) { guru.sessao.Usuario = u; } } this.buildHash = function() { if (guru.produtos==null) { return; } guru.produtosHash = {}; for (i=0; i < guru.produtos.length; i++) { guru.produtosHash[guru.produtos[i].prd] = guru.produtos[i]; } } this.getStatusDesc = function(st) { if ( (!st) || (st.length!=1) ) { return '*** ERRO ***'; } switch(st.charAt(0)) { case 'B': return 'No Carrinho'; case 'C': return 'Aguardando Produ��o'; case 'F': return 'Em Produ��o'; case 'G': return 'Pronto (Em entrega)'; case 'K': return 'Entregue'; case 'P': return 'Em pausa'; case 'X': return 'Transferido'; case 'Y': return 'Cancelado'; case 'Z': return 'N.Entregue'; } return '*** ERRO ***'; } this.getStatusImg = function(st) { if ( (!st) || (st.length!=1) ) { return 'img/icones/Help.png'; } switch(st.charAt(0)) { case 'B': return 'img/icones/shopping-basket-refresh.png'; case 'C': return 'img/icones/wait_16x16.gif'; case 'F': return 'img/icones/686879-Icons_Outlined-08-16.png'; case 'G': return 'img/icones/Room_service.png'; case 'K': return 'img/icones/Check.png'; case 'P': return 'img/icones/pause-16.png'; case 'X': return 'img/icones/transfer-16.png'; case 'Y': return 'img/icones/button_cancel.png'; case 'Z': return 'img/icones/minus-16.png'; } return '*** ERRO ***'; } this.chaveiaCor = function(cor,array) { if (!cor) { return array[0]; } if (cor==array[0]) { return array[1]; } return array[0]; // TODO: implementar pra N cores } this.arred = function(n) { return Math.round( n*100 + 0.001 ) /100.0; } this.money = function(n,moeda) { var negativo = (n<0); if (negativo) { n = -n; } var i = Math.floor(n); var d = Math.round( (n-i)*100 + 0.001 ); var s = "00" + d; s = s.substring(s.length - 2); s = i + "." + s; if (moeda) { s = moeda + s; } if (negativo) { s = '-' + s; } return s; } this.hhmm = function(d) { if (!d) { return ''; } var s = d; if ( (s.indexOf(' ')>0) && (s.indexOf(' ') 5) { s = s.substring(0,5); } } return s; } this.hhmmss = function(d) { if (!d) { return ''; } var s = d; if ( (s.indexOf(' ')>0) && (s.indexOf(' ') 8) { s = s.substring(0,8); } } return s; } this.dd_hhmm = function(d) { if (!d) { return ''; } var s = d; if ( (s.indexOf(' ')>0) && (s.indexOf(' ') 8) { s = s.substring(0,8); } } return s; } this.yyyymmdd = function(d) { if (!d) { return ''; } var s = d; if (s.indexOf(' ')>0) { s = s.substring(0,s.indexOf(' ')); if (s.length > 10) { s = s.substring(0,10); } } return s; } var meses = ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez']; this.mmm = function(d) { var s = dateFunctions.yyyymmdd(d); if (s=='') { return ''; } var ano = d.substring(0,4); var mes = d.substring(5,7); var dia = d.substring(8,10); // Atencao - mes eh 0..11 var data = new Date(ano, (mes-1), dia,0,0,0,0); var mes = data.getMonth(); return meses[mes]; } this.ddmmm = function(d) { if (!d) { return ''; } var s = d; if (s.indexOf(' ')>0) { s = s.substring(0,s.indexOf(' ')); if (s.length > 10) { s = s.substring(0,10); } } var dia = s.substring(8); var mes = guru.mmm(d); return dia + '/' + mes; } var daysOfWeek = ["Dom","Seg","Ter","Qua","Qui","Sex","Sab"]; this.dayOfWeek = function(d) { var s = dateFunctions.yyyymmdd(d); if (s=='') { return ''; } var ano = d.substring(0,4); var mes = d.substring(5,7); var dia = d.substring(8,10); // Atencao - mes eh 0..11 var data = new Date(ano, (mes-1), dia,0,0,0,0); var dow = data.getDay(); return daysOfWeek[dow]; } this.dhms = function(segundos) { if (segundos == null) { return ''; } var minutos = 0; var horas = 0; var dias = 0; if (segundos>60) { minutos = Math.floor(segundos / 60); segundos -= minutos*60; } if (minutos>60) { horas = Math.floor(minutos / 60); minutos -= horas*60; } if (horas>24) { dias = Math.floor(horas / 24); horas -= dias*24; } var result = ''; if (dias>0) { result += dias + 'd'; } if ( (horas>0) && (dias<5) ) { result += horas + 'h'; } if ( (minutos>0) && (dias==0) ) { result += minutos + 'm'; } if ( (segundos>0) && (horas+dias==0) && (minutos<10) ) { result += segundos + 's'; } return result; } this.iconeStatusNaoEntregue = function(st) { var iconeStatus = ' '; if (st=='K') { iconeStatus =''; } return iconeStatus; } this.msg = function(divName, texto, cor) { var div = document.getElementById(divName); if ( (texto) && (texto!=null) ) { div.innerHTML = '
' + texto + '
'; if ( (!cor) || (cor=='') ) { cor = 'white'; } div.style.backgroundColor = cor; return; } div.innerHTML = ''; } this.renderMoney = function(data, type, full, meta) { if (!data) { return ''; } var s = util.money(data,'$'); return s; } this.renderNaoZero = function(data, type, full, meta) { if (!data) { return ''; } return data; } this.renderNaoZeroNegativo = function(data, type, full, meta) { var s = ''; if (data<0) { s=''; } return s + guru.renderNaoZero(data); } this.renderCents = function(data, type, full, meta) { if (!data) { return ''; } var s = util.money(data,''); return s; } this.renderAtivo = function(data, type, full, meta) { if ( (data==null) || (data=='') ) { data = 'N'; } if ( (data=='S') || (data=='Y') || (data=='T') ) { return '' + data + ''; } return '' + data + ''; } this.renderInativo = function(data, type, full, meta) { if ( (data==null) || (data=='') ) { data = 'N'; } if ( (data=='S') || (data=='Y') || (data=='T') ) { return 'Inativo'; } return ''; } this.renderSN = function(data, type, full, meta) { if ( (data==null) || (data=='') ) { data = 'N'; } if ( (data=='S') || (data=='Y') || (data=='T') ) { return 'Sim'; } return 'Não'; } this.ifNull = function(s1,s2) { if (!s1) { return s2; } return s1; } this.renderIfNull = function(data, type, full, meta) { return util.ifNull(data,''); } this.renderHHMM = function(data, type, full, meta) { return dateFunctions.hhmm(data); } this.renderHHMMSS = function(data, type, full, meta) { return dateFunctions.hhmmss(data); } this.renderYYYYMMDD = function(data, type, full, meta) { return '' + dateFunctions.yyyymmdd(data) + ''; } this.renderYYYYMMDD_HHMM = function(data, type, full, meta) { return '' + dateFunctions.yyyymmdd(data) + ' ' + dateFunctions.hhmm(data) + ''; } this.getRotina = function(i) { var usuario = guru.getUsuario(); var rotinas = usuario['_contahub.entities.Rotina']; return rotinas[i]; } this.httpAlert = function( xmlhttp ) { window.alert(xmlhttp.responseText); } this.bool = function( x ) { if ( (x==null) && (x=='') ) { return false; } if ('| |n|N|f|F|False|false|0|'.indexOf('|' + x + '|')>=0 ) { return false; } return true; } this.iif = function( b, t, f ) { if (b) { return t; } return f; } this.checked = function( id ) { var chk = document.getElementById(id); if ( ! chk) { window.alert("Elemento nao encontrado [" + id + "]"); return false; } return chk.checked; } this.VendaPagamento_desc = function (p) { var s = ''; if (p.pag_hrlancamento) { if (dateFunctions.yyyymmdd(p.pag_hrlancamento)!=dateFunctions.yyyymmdd(p.vd_dtgerencial)) { s += dateFunctions.yyyymmdd(p.pag_hrlancamento) + ' '; } s += dateFunctions.hhmm(p.pag_hrlancamento); } s += ' #' + p.pag; return s; } this.Mesa_desc = function (m) { if (m.mes) { return m.mes_desc } return "Nova mesa"; } this.Venda_desc = function (v) { if (v.vd) { var s = v.vd_mesadesc if (s==null) { if (v.mes) { s = guru.Mesa_desc(v); } else { s = "Avulso " + v.vd; } } else if (v.mes) { var m = guru.Mesa_desc(v); if ( (m) && (s.indexOf(m)==-1)) { s = m + " (" + s + ")"; } } return s; } else if (v.mes) { return guru.Mesa_desc(v); } console.log(Object.keys(v)); return "Nova Venda"; } this.setVenda = function (xvd, xdesc) { if (guru.venda) { guru.venda.destroy(); guru.venda = null; } guru.venda = new Venda(xvd,xdesc); } this.getVenda = function() { if (guru.venda) { return guru.venda; } window.alert("Venda nao setada!"); } this.getNFeTPagDescricao = function(tPag) // retirado da especificacao da NFe { if (tPag==null) { return "*** erro ***"; } if (tPag==1) { return "Dinheiro"; } if (tPag==2) { return "Cheque"; } if (tPag==3) { return "Credito"; } if (tPag==4) { return "Debito"; } if (tPag==5) { return "Credito Loja"; } if (tPag==10) { return "Vale Alimentacao"; } if (tPag==11) { return "Vale Refeicao"; } if (tPag==12) { return "Vale Presente"; } if (tPag==13) { return "Vale Combustivel"; } if (tPag==15) { return "Boleto"; } if (tPag==17) { return "PIX"; } if (tPag==99) { return "Outros"; } if (tPag==90) { return "Outros(SEM PAGAMENTO)"; } return "*** Desconhecido ***"; } }