var Conf = { appName: 'Verdnatura' ,dsName: 'verdnatura' ,dsPath: 'HKCU\\Software\\ODBC\\ODBC.INI\\verdnatura' ,regPath: 'HKCU\\Software\\Verdnatura\\vn-access' ,remoteUrl: 'https://verdnatura.es/vn-access' ,dbHost: 'db.verdnatura.es' ,defaultModule: 'tpv' ,defaultLocale: 'es' }; var Locale = { es: { "Enter a user name": "Introduce un nombre de usuario" ,"Enter a password": "Introduce una contraseña" ,"Server can't be reached": "No se ha podido conectar con el servidor" ,"Updating": "Actualizando" ,"Bad login": "Usuario o contraseña incorrectos" ,"Loading": "Cargando" ,"Error while updating": "Error al actualizar" ,"Microsoft Access 2003 is not installed": "Microsoft Access 2003 no está instalado en el sistema" ,"MDB file not found": "No se encontro el fichero MDB" } }; var App = { shell: new ActiveXObject('WScript.Shell'), fso: new ActiveXObject('scripting.filesystemobject'), init: function() { var width = 420; var height = 360; window.resizeTo(width, height); window.moveTo((screen.width - width) / 2, (screen.height - height) / 2); }, _onLoad: function() { // Initializes the global variables var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g); if (split.length > 1) this.module = split[1].replace(/^"+|"+$/g, ''); if (!this.module) this.module = Conf.defaultModule; this.appDir = this.getEnv('ProgramFiles') +'\\'+ Conf.appName; this.moduleDir = this.shell.SpecialFolders('AppData') +'\\'+ Conf.appName; this.remoteFile = Conf.remoteUrl +'/'+ this.module +'.7z'; this.compressFile = this.getEnv('TEMP') +'\\'+ this.module +'.7z'; this.mdbFile = this.moduleDir +'\\'+ this.module +'.mdb'; // Creates the necessary registry entries var configured = this.regRead(Conf.regPath, 'configured'); if (!configured) { var path; // Creates the Access configuration entries path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Settings'; this.regWrites(path, 'REG_DWORD', { 'Confirm Document Deletions' : 0, 'Confirm Action Queries' : 0, 'Confirm Record Changes' : 0 }); path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Security'; this.regWrite(path, 'Level', 1, 'REG_DWORD'); // Creates the MySQL ODBC connection var driverPath = this.getEnv('ProgramFiles') +'\\MySQL\\Connector ODBC 5.1\\myodbc5.dll'; var params = { 'Driver' : driverPath, 'DESCRIPTION' : Conf.appName, 'SERVER' : Conf.dbHost, 'DATABASE' : 'vn2008', 'SSLCA' : this.appDir +'\\cacert.pem', 'SSLVERIFY' : 1, 'AUTO_RECONNECT' : 1 }; this.createOdbc( Conf.dsName, 'Mysql ODBC 5.1 Driver', params ); // Marks the application as configured this.regWrite(Conf.regPath, 'configured', 1, 'REG_DWORD'); } // Loads the form data var user = this.regRead(Conf.dsPath, 'UID'); var password = this.regRead(Conf.dsPath, 'PWD'); var remember = this.regRead(Conf.regPath, 'remember'); if (user) this.$('user').value = user; if (remember && password) { this.$('password').value = password; this.$('remember').checked = true; this._onEnterClick(); } else this.resetForm(true); }, resetForm: function(clearPassword) { if (clearPassword) this.$('password').value = ''; this.$('user').focus(); this.$('user').select(); }, createOdbc: function(dsName, driverName, params) { var odbcPath = 'HKCU\\Software\\ODBC\\ODBC.INI\\'; this.regWrites(odbcPath + dsName, 'REG_SZ', params); this.regWrite(odbcPath + 'ODBC Data Sources', dsName, driverName, 'REG_SZ'); }, _disableUi: function(disabled, loadMessage) { if (disabled) this._hideMessage(); else loadMessage = ''; this.$('loading-message').innerHTML = loadMessage; this.$('user').disabled = disabled; this.$('password').disabled = disabled; this.$('remember').disabled = disabled; this.$('enter').disabled = disabled; var display = disabled ? 'block' : 'none'; this.$('background').style.display = display; this.$('spinner').style.display = display; }, _onKeyPress: function(e) { switch (e.keyCode) { case 13: // Enter this._onEnterPress(e); break; case 27: // Esc window.close(); break; } }, _onEnterPress: function(event) { var target = event.target || event.srcElement; if (target && target.id == 'user' && this.$('password').value == '') { this.$('password').focus(); return; } this._onEnterClick(); }, _onEnterClick: function() { this._disableUi(true, _('Loading')); setTimeout(function() {App._login();}, 0); }, _login: function() { var clearPassword; try { var user = this.$('user').value; var password = this.$('password').value; if (!user || user === '') throw new Error(_('Enter a user name')); if (!password || password === '') throw new Error(_('Enter a password')); this.regWrite(Conf.dsPath, 'UID', user, 'REG_SZ'); this.regWrite(Conf.dsPath, 'PWD', password, 'REG_SZ'); // Gets the last version number using the MySQL ODBC connection var mysqlConn = new ActiveXObject('ADODB.Connection'); try { mysqlConn.open(Conf.dsName); } catch (e) { var dbErrors = mysqlConn.errors; if (dbErrors.count > 0) { var errorMsg; var dbError = dbErrors.item(0); switch (dbError.NativeError) { case 1045: // Access denied clearPassword = true; errorMsg = _('Bad login'); break; case 2003: // Can't connect errorMsg = _('Server can\'t be reached'); break; default: errorMsg = dbError.description; } dbErrors.clear(); throw new Error(errorMsg); } else throw e; } var sql = 'SELECT version FROM versiones WHERE programa = \''+ this.module +'\''; var rs = mysqlConn.execute(sql); var lastVersion = rs.EOF ? 0 : parseInt(rs.fields(0).value); rs.close(); mysqlConn.close(); // Obtains the local version number from the MDB file var localVersion = -1; if (this.fso.fileExists(this.mdbFile)) try { var mdbConn = new ActiveXObject('ADODB.Connection'); mdbConn.open('Provider=Microsoft.Jet.OLEDB.4.0; Data Source='+ this.mdbFile +';'); var oRs = new ActiveXObject('ADODB.Recordset'); oRs.Open('SELECT Version FROM tblVariables', mdbConn); localVersion = oRs.EOF ? -1 : parseInt(oRs('Version')); oRs.close(); mdbConn.close(); } catch (e) {} // Compares the local version with the las version if (localVersion < lastVersion) { this._disableUi(true, _('Updating')); var queryString = lastVersion ? 'v'+ lastVersion : new Date().getTime(); var request = new ActiveXObject('MSXML2.XMLHTTP'); request.open('GET', this.remoteFile +'?'+ queryString, true); request.onreadystatechange = function() { App._onRequestReady(request); }; request.send(); } else this.openMdb(); } catch (e) { this._catchError(e, clearPassword); } }, _onRequestReady: function(request) { if (request.readyState !== 4) return; try { if (request.status !== 200) throw new Error('HTTP: '+ request.statusText); if (this.fso.fileExists(this.compressFile)) this.fso.deleteFile(this.compressFile); var stream = new ActiveXObject('ADODB.Stream'); stream.open(); stream.Type = 1; //adTypeBinary stream.write(request.responseBody); stream.Position = 0; stream.saveToFile(this.compressFile, 2); stream.close(); if (this.fso.fileExists(this.mdbFile)) this.fso.deleteFile(this.mdbFile); this.run('7za e "'+ this.compressFile +'" -o"'+ this.moduleDir +'"', true); this.fso.deleteFile(this.compressFile); } catch (e) { alert(_('Error while updating') +': '+ e.message); } try { if (!this.fso.fileExists(this.mdbFile)) throw new Error(_('MDB file not found')); this.openMdb(); } catch (e) { this._catchError(e); } }, openMdb: function() { var remember = this.$('remember').checked ? 1 : 0; this.regWrite(Conf.regPath, 'remember', remember, 'REG_DWORD'); var programFiles = this.getEnv('ProgramFiles'); var accessBin = programFiles +'\\Microsoft Office\\OFFICE11\\MSACCESS.EXE'; if (!this.fso.fileExists(accessBin)) throw new Error(_('Microsoft Access 2003 is not installed')); this.shell.exec('"'+ accessBin +'" "'+ this.mdbFile +'"'); window.close(); }, _catchError: function(error, clearPassword) { if (!this.$('remember').checked || clearPassword) this.regWrite(Conf.dsPath, 'PWD', '', 'REG_SZ'); this._disableUi(false); this.showMessage(error.message); this.resetForm(clearPassword); }, showMessage: function(message) { if (this.messageTimeout) clearTimeout(this.messageTimeout); var messageDiv = this.$('message'); messageDiv.innerHTML = message; messageDiv.style.display = 'block'; this.messageTimeout = setTimeout(function() {App._hideMessage();}, 10000); }, _onBodyClick: function() { this._hideMessage(); }, _hideMessage: function() { if (this.messageTimeout) { this.$('message').style.display = 'none'; clearTimeout(this.messageTimeout); this.messageTimeout = null; } }, _onUnload: function() { this._disableUi(false); }, $: function(id) { return document.getElementById(id); }, run: function(command, wait) { if (!wait) wait = false; this.shell.run(command, 0, wait); }, getEnv: function(varName) { return this.shell.expandEnvironmentStrings('%'+ varName +'%'); }, regRead: function(path, key) { try { var value = this.shell.regRead(path +'\\'+ key); } catch (e) { var value = null; } return value; }, regWrite: function(path, key, value, type) { this.shell.regWrite(path +'\\'+ key, value, type); }, regWrites: function(path, type, values) { for(var key in values) this.regWrite(path, key, values[key], type); } }; App.init(); function _(string) { var translation = Locale[Conf.defaultLocale][string]; return translation ? translation : string; }