2016-03-14 08:23:40 +00:00
|
|
|
|
2016-03-31 10:51:45 +00:00
|
|
|
var Conf =
|
2016-03-14 08:23:40 +00:00
|
|
|
{
|
|
|
|
appName: 'Verdnatura',
|
|
|
|
dsName: 'verdnatura',
|
2016-03-18 13:12:24 +00:00
|
|
|
dsPath: 'HKCU\\Software\\ODBC\\ODBC.INI\\verdnatura',
|
2016-03-31 10:51:45 +00:00
|
|
|
regPath: 'HKCU\\Software\\Verdnatura\\vn-access',
|
|
|
|
remoteUrl: 'https://www.verdnatura.es/vn-access',
|
2016-03-18 13:12:24 +00:00
|
|
|
dbHost: 'db.verdnatura.es',
|
2016-03-31 10:51:45 +00:00
|
|
|
defaultModule: 'tpv'
|
|
|
|
};
|
|
|
|
|
|
|
|
var App =
|
|
|
|
{
|
2016-03-14 08:23:40 +00:00
|
|
|
|
|
|
|
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, '');
|
2016-03-31 10:51:45 +00:00
|
|
|
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';
|
|
|
|
|
2016-03-14 08:23:40 +00:00
|
|
|
// Crea las entradas necesarias en el registro
|
|
|
|
|
2016-03-31 10:51:45 +00:00
|
|
|
var configured = this.regRead (Conf.regPath, 'configured');
|
2016-03-14 08:23:40 +00:00
|
|
|
|
|
|
|
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,
|
2016-04-14 13:16:19 +00:00
|
|
|
'Confirm Record Changes' : 0
|
2016-03-14 08:23:40 +00:00
|
|
|
});
|
|
|
|
|
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
|
2016-03-18 13:12:24 +00:00
|
|
|
|
|
|
|
var driverPath = this.getEnv ('ProgramFiles')
|
2016-03-14 08:23:40 +00:00
|
|
|
+'\\MySQL\\Connector ODBC 5.1\\myodbc5.dll';
|
2016-03-18 13:12:24 +00:00
|
|
|
|
|
|
|
var params = {
|
|
|
|
'Driver' : driverPath,
|
2016-03-31 10:51:45 +00:00
|
|
|
'DESCRIPTION' : Conf.appName,
|
|
|
|
'SERVER' : Conf.dbHost,
|
2016-03-14 08:23:40 +00:00
|
|
|
'DATABASE' : 'vn2008',
|
|
|
|
'SSLCA' : this.appDir +'\\cacert.pem',
|
|
|
|
'SSLVERIFY' : 1,
|
|
|
|
'AUTO_RECONNECT' : 1
|
2016-03-18 13:12:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.createOdbc (
|
2016-03-31 10:51:45 +00:00
|
|
|
Conf.dsName,
|
2016-03-18 13:12:24 +00:00
|
|
|
'Mysql ODBC 5.1 Driver',
|
|
|
|
params
|
|
|
|
);
|
|
|
|
|
|
|
|
// Crea el ODBC de PosgreSQL
|
|
|
|
|
|
|
|
var driverPath = this.getEnv ('ProgramFiles')
|
|
|
|
+'\\psqlODBC\\0804\\bin\\psqlodbc35w.dll';
|
2016-03-14 08:23:40 +00:00
|
|
|
|
2016-03-18 13:12:24 +00:00
|
|
|
var params = {
|
|
|
|
'Driver' : driverPath,
|
2016-03-31 10:51:45 +00:00
|
|
|
'DESCRIPTION' : Conf.appName,
|
|
|
|
'Servername' : Conf.dbHost,
|
2016-03-18 13:12:24 +00:00
|
|
|
'DATABASE' : 'vn',
|
|
|
|
'UID' : '',
|
|
|
|
'USERNAME' : '',
|
|
|
|
'PASSWORD' : ''
|
|
|
|
};
|
|
|
|
|
|
|
|
this.createOdbc (
|
|
|
|
'verdnaturapg',
|
|
|
|
'PostgreSQL Unicode',
|
|
|
|
params
|
|
|
|
);
|
|
|
|
|
|
|
|
// Marca la aplicación como configurada
|
2016-03-14 08:23:40 +00:00
|
|
|
|
2016-03-31 10:51:45 +00:00
|
|
|
this.regWrite (Conf.regPath, 'configured', 1, 'REG_DWORD');
|
2016-03-14 08:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Carga los datos del formulario
|
|
|
|
|
2016-03-31 10:51:45 +00:00
|
|
|
var user = this.regRead (Conf.dsPath, 'UID');
|
|
|
|
var password = this.regRead (Conf.dsPath, 'PWD');
|
|
|
|
var remember = this.regRead (Conf.regPath, 'remember');
|
2016-03-14 08:23:40 +00:00
|
|
|
|
|
|
|
if (user)
|
|
|
|
this.$('user').value = user;
|
|
|
|
|
|
|
|
if (remember && password)
|
|
|
|
{
|
|
|
|
this.$('password').value = password;
|
|
|
|
this.$('remember').checked = true;
|
|
|
|
this._onEnterClick ();
|
|
|
|
}
|
|
|
|
else
|
2016-04-14 13:16:19 +00:00
|
|
|
this.resetForm ();
|
2016-03-14 08:23:40 +00:00
|
|
|
},
|
2016-03-18 13:12:24 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
},
|
2016-03-14 08:23:40 +00:00
|
|
|
|
2016-04-14 13:16:19 +00:00
|
|
|
resetForm: function ()
|
2016-03-14 08:23:40 +00:00
|
|
|
{
|
|
|
|
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');
|
|
|
|
|
2016-03-31 10:51:45 +00:00
|
|
|
this.regWrite (Conf.dsPath, 'UID', user, 'REG_SZ');
|
|
|
|
this.regWrite (Conf.dsPath, 'PWD', password, 'REG_SZ');
|
2016-03-14 08:23:40 +00:00
|
|
|
|
|
|
|
// Obtiene la última versión usando la conexión ODBC de MySQL
|
|
|
|
|
|
|
|
var mysqlConn = new ActiveXObject ('ADODB.Connection');
|
|
|
|
|
|
|
|
try {
|
2016-03-31 10:51:45 +00:00
|
|
|
mysqlConn.open (Conf.dsName);
|
2016-03-14 08:23:40 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
throw new Error ('Usuario o contraseña incorrectos');
|
|
|
|
}
|
|
|
|
|
2016-03-31 10:51:45 +00:00
|
|
|
var sql = 'SELECT version FROM versiones WHERE programa = \''+ this.module +'\'';
|
2016-03-14 08:23:40 +00:00
|
|
|
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;
|
|
|
|
|
2016-03-31 10:51:45 +00:00
|
|
|
if (this.fso.fileExists (this.mdbFile))
|
|
|
|
try {
|
2016-03-14 08:23:40 +00:00
|
|
|
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 ();
|
|
|
|
}
|
2016-03-31 10:51:45 +00:00
|
|
|
catch (e) {}
|
2016-03-14 08:23:40 +00:00
|
|
|
|
|
|
|
// Compara la versión local con la última versión
|
2016-03-31 10:51:45 +00:00
|
|
|
|
2016-03-14 08:23:40 +00:00
|
|
|
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)
|
|
|
|
{
|
2016-04-14 13:16:19 +00:00
|
|
|
this.regWrite (Conf.dsPath, 'PWD', '', 'REG_SZ');
|
2016-03-14 08:23:40 +00:00
|
|
|
this._disableUi (false);
|
2016-04-14 13:16:19 +00:00
|
|
|
this.resetForm ();
|
2016-03-14 08:23:40 +00:00
|
|
|
this.showMessage (e.message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onRequestReady: function (request)
|
|
|
|
{
|
|
|
|
if (request.readyState !== 4)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (request.status !== 200)
|
2016-04-14 13:16:19 +00:00
|
|
|
throw new Error ('HTTP: '+ request.statusText);
|
2016-03-14 08:23:40 +00:00
|
|
|
|
2016-03-31 10:51:45 +00:00
|
|
|
if (this.fso.fileExists (this.compressFile))
|
2016-04-14 13:16:19 +00:00
|
|
|
this.fso.deleteFile (this.compressFile);
|
2016-03-14 08:23:40 +00:00
|
|
|
|
|
|
|
var stream = new ActiveXObject ('ADODB.Stream');
|
2016-03-31 10:51:45 +00:00
|
|
|
stream.open ();
|
2016-03-14 08:23:40 +00:00
|
|
|
stream.Type = 1; //adTypeBinary
|
2016-03-31 10:51:45 +00:00
|
|
|
stream.write (request.responseBody);
|
2016-03-14 08:23:40 +00:00
|
|
|
stream.Position = 0;
|
2016-03-31 10:51:45 +00:00
|
|
|
stream.saveToFile (this.compressFile, 2);
|
|
|
|
stream.close ();
|
2016-03-14 08:23:40 +00:00
|
|
|
|
2016-03-31 10:51:45 +00:00
|
|
|
if (this.fso.fileExists (this.mdbFile))
|
|
|
|
this.fso.deleteFile (this.mdbFile);
|
2016-03-14 08:23:40 +00:00
|
|
|
|
2016-03-31 10:51:45 +00:00
|
|
|
// XXX: Code for the old installer modules
|
|
|
|
|
|
|
|
var oldModules = {
|
|
|
|
'tpv': 'TPV_MySQL2',
|
|
|
|
'ent': 'ENT_MySQL',
|
|
|
|
'com': 'COM_MySQL',
|
|
|
|
'enc': 'ENC_MySQL',
|
|
|
|
'eti': 'ETI',
|
|
|
|
'lab': 'LAB_MySQL'
|
|
|
|
};
|
|
|
|
var oldMdb = this.moduleDir +'\\'+ oldModules[this.module] +'.mdb';
|
|
|
|
|
|
|
|
if (this.fso.fileExists (oldMdb))
|
|
|
|
this.fso.deleteFile (oldMdb);
|
|
|
|
|
|
|
|
// XXX: End
|
|
|
|
|
2016-03-14 08:23:40 +00:00
|
|
|
this.run ('7za e "'+ this.compressFile +'" -o"'+ this.moduleDir +'"', true);
|
2016-03-31 10:51:45 +00:00
|
|
|
this.fso.deleteFile (this.compressFile);
|
|
|
|
|
|
|
|
// XXX: Code for the old installer modules
|
|
|
|
|
|
|
|
if (!this.fso.fileExists (this.mdbFile) && this.fso.fileExists (oldMdb))
|
|
|
|
this.fso.moveFile (oldMdb, this.mdbFile);
|
|
|
|
|
|
|
|
// XXX: End
|
2016-03-14 08:23:40 +00:00
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
2016-04-14 13:16:19 +00:00
|
|
|
alert ('Error al actualizar: '+ e.message);
|
2016-03-14 08:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._disableUi (false);
|
2016-04-14 13:16:19 +00:00
|
|
|
|
|
|
|
if (this.fso.fileExists (this.mdbFile))
|
|
|
|
this.openMdb ();
|
2016-03-31 10:51:45 +00:00
|
|
|
},
|
|
|
|
|
2016-03-14 08:23:40 +00:00
|
|
|
openMdb: function ()
|
|
|
|
{
|
|
|
|
var remember = this.$('remember').checked ? 1 : 0;
|
2016-03-31 10:51:45 +00:00
|
|
|
this.regWrite (Conf.regPath, 'remember', remember, 'REG_DWORD');
|
2016-03-14 08:23:40 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2016-03-31 10:51:45 +00:00
|
|
|
return this.shell.expandEnvironmentStrings ('%'+ varName +'%');
|
2016-03-14 08:23:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
regRead: function (path, key)
|
|
|
|
{
|
|
|
|
try {
|
2016-03-31 10:51:45 +00:00
|
|
|
var value = this.shell.regRead (path +'\\'+ key);
|
2016-03-14 08:23:40 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
var value = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
|
|
|
|
regWrite: function (path, key, value, type)
|
|
|
|
{
|
2016-03-31 10:51:45 +00:00
|
|
|
this.shell.regWrite (path +'\\'+ key, value, type);
|
2016-03-14 08:23:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
regWrites: function (path, type, values)
|
|
|
|
{
|
|
|
|
for (var key in values)
|
|
|
|
this.regWrite (path, key, values[key], type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
App.init ();
|