vn-access/src/main.js

345 lines
8.1 KiB
JavaScript
Raw Normal View History

2016-03-14 08:23:40 +00:00
var Modules =
{
'tpv': 'TPV_MySQL2',
'ent': 'ENT_MySQL',
'com': 'COM_MySQL',
'enc': 'ENC_MySQL',
'eti': 'ETI',
'lab': 'LAB_MySQL'
};
var App =
{
appName: 'Verdnatura',
dsName: 'verdnatura',
regPath: 'HKCU\\Software\\Verdnatura',
odbcPath: 'HKCU\\Software\\ODBC\\ODBC.INI\\verdnatura',
remoteUrl: 'https://www.verdnatura.es/download',
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 ()
{
// Inicializa variables globales
var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g);
if (split.length > 1)
this.module = split[1].replace (/^"+|"+$/g, '');
if (!this.module || Modules[this.module] === undefined)
this.module = 'tpv';
this.fileName = Modules[this.module];
this.appDir = this.getEnv ('ProgramFiles') +'\\'+ this.appName;
this.moduleDir = this.shell.SpecialFolders ('AppData') +'\\'+ this.appName;
this.remoteFile = this.remoteUrl +'/'+ this.fileName +'.7z';
this.compressFile = this.getEnv ('TEMP') +'\\'+ this.fileName +'.7z';
this.mdbFile = this.moduleDir +'\\'+ this.fileName +'.mdb';
// Crea las entradas necesarias en el registro
var configured = this.regRead (this.regPath, 'configured');
if (!configured)
{
var path;
// Crea las entradas de configuración de Access
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
});
2016-03-14 11:01:40 +00:00
path = 'HKCU\\Software\\Microsoft\\Office\\11.0\\Access\\Security';
this.regWrite (path, 'Level', 1, 'REG_DWORD');
2016-03-14 08:23:40 +00:00
// Crea el ODBC de MySQL
var dsDriverPath = this.getEnv ('ProgramFiles')
+'\\MySQL\\Connector ODBC 5.1\\myodbc5.dll';
this.regWrites (this.odbcPath, 'REG_SZ', {
'Driver' : dsDriverPath,
'DESCRIPTION' : this.appName,
'SERVER' : 'db.verdnatura.es',
'DATABASE' : 'vn2008',
'SSLCA' : this.appDir +'\\cacert.pem',
'SSLVERIFY' : 1,
'AUTO_RECONNECT' : 1
});
path = 'HKCU\\Software\\ODBC\\ODBC.INI\\ODBC Data Sources';
this.regWrite (path, this.dsName, 'Mysql ODBC 5.1 Driver', 'REG_SZ');
this.regWrite (this.regPath, 'configured', 1, 'REG_DWORD');
}
// Carga los datos del formulario
var user = this.regRead (this.odbcPath, 'UID');
var password = this.regRead (this.odbcPath, 'PWD');
var remember = this.regRead (this.regPath, 'remember');
if (user)
this.$('user').value = user;
if (remember && password)
{
this.$('password').value = password;
this.$('remember').checked = true;
this._onEnterClick ();
}
else
this.reset ();
},
reset: function ()
{
this.$('user').focus ();
this.$('user').select ();
this.$('password').value = '';
},
_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._onEnterClick ();
break;
case 27: // Esc
window.close ();
break;
}
},
_onEnterClick: function ()
{
this._disableUi (true, 'Cargando');
setTimeout (function () {App.login();}, 0);
},
login: function ()
{
var user = this.$('user').value;
var password = this.$('password').value;
try {
if (!user || user === '')
throw new Error ('Introduce un nombre de usuario');
if (!password || password === '')
throw new Error ('Introduce una contraseña');
this.regWrite (this.odbcPath, 'UID', user, 'REG_SZ');
this.regWrite (this.odbcPath, 'PWD', password, 'REG_SZ');
// Obtiene la última versión usando la conexión ODBC de MySQL
var mysqlConn = new ActiveXObject ('ADODB.Connection');
try {
mysqlConn.Open (this.dsName);
}
catch (e) {
throw new Error ('Usuario o contraseña incorrectos');
}
var sql = 'SELECT version FROM vn2008.versiones WHERE programa = \''+ this.module +'\'';
var rs = mysqlConn.execute (sql);
var lastVersion = parseInt (rs.fields(0).value);
rs.close ();
mysqlConn.close ();
// Obtiene el número de versión local del archivo MDB
var localVersion = -1;
if (this.fso.FileExists (this.mdbFile))
{
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 ();
}
// Compara la versión local con la última versión
if (localVersion < lastVersion)
{
this._disableUi (true, 'Actualizando');
var request = new ActiveXObject ('MSXML2.XMLHTTP');
request.open ('GET', this.remoteFile, true);
request.onreadystatechange = function () {App._onRequestReady (request);};
request.send ();
}
else
this.openMdb ();
}
catch (e)
{
//this.regWrite (this.odbcPath, 'PWD', '', 'REG_SZ');
this._disableUi (false);
this.reset ();
this.showMessage (e.message);
}
},
_onRequestReady: function (request)
{
if (request.readyState !== 4)
return;
try {
if (request.status !== 200)
throw new Error ('Error al actualizar: '+ 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);
this.openMdb ();
}
catch (e)
{
this.showMessage (e.message);
}
this._disableUi (false);
},
openMdb: function ()
{
var remember = this.$('remember').checked ? 1 : 0;
this.regWrite (this.regPath, 'remember', remember, 'REG_DWORD');
this.shell.exec ('"%ProgramFiles%\\Microsoft Office\\OFFICE11\\MSACCESS.EXE" "'+
this.mdbFile +'" /cmd "'+ this.$('user').value +'"');
window.close ();
},
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 ();