Last active
August 29, 2015 14:04
-
-
Save betobaz/cab5534d50c6c2bb8688 to your computer and use it in GitHub Desktop.
SugarCRM::Dashlet existencias
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
plugins: ['Dashlet'], | |
initialize: function (options) { | |
var self = this; | |
this._super('initialize', [options]); | |
window.google = window.google || false; | |
self.FOLDER_ID = ''; | |
self.CLIENT_ID = ''; | |
self.SCOPE = [ | |
'https://www.googleapis.com/auth/drive', | |
'https://www.googleapis.com/auth/drive.file', | |
'https://spreadsheets.google.com/feeds' | |
]; | |
if(!google){ | |
this._addJavascript('https://www.google.com/jsapi'); | |
this._addJavascript('https://apis.google.com/js/client.js'); | |
} | |
}, | |
loadData: function (options) { | |
var self = this; | |
_.delay(function(){ | |
google.load('visualization', '1', {'callback':function(){ | |
if(self.$el.find('table').length){ | |
self._checkAuth(); | |
} | |
}, 'packages':['corechart']}); | |
}, 2000); | |
}, | |
drawChart: function () { | |
var self = this; | |
var query = new google.visualization.Query('https://docs.google.com/spreadsheet/ccc?key=' + self.sheet_id + '&usp=drive_web#gid=0'); | |
query.setQuery("select A, B, C, G, H, I, J where ("+self.where_skus+")"); | |
query.send(_.bind(self.handleQueryResponse, self)); | |
}, | |
handleQueryResponse: function(response) { | |
var self = this; | |
if (response.isError()) { | |
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage()); | |
return; | |
} | |
var data_visualization = self.process_data_visualization(response.getDataTable()); | |
self.existencias = data_visualization; | |
self.render(); | |
}, | |
process_data_visualization: function(data){ | |
var self = this; | |
var data_process = []; | |
_.each(data.tf, function (item, index){ | |
data_process.push({ | |
"bodega": item.c[0].v, | |
"sku": item.c[1].v, | |
"producto": item.c[2].v, | |
"cantidad": item.c[3].v, | |
"um_principal": item.c[4].v, | |
"millares": item.c[5].v, | |
"m3": item.c[6].v | |
}) | |
}); | |
return data_process; | |
}, | |
getSkus: function(callback){ | |
var self = this; | |
request = App.api.call('get', '/rest/v10/CMX03_Pedidos/'+App.controller.context.get('model').id+'/link/cmx03_pedidos_cmx03_detallespedido'); | |
request.xhr.success(function(data){ | |
var skus_where = []; | |
var sku_pad = ""; | |
var pad = " "; | |
if(data.records && data.records.length){ | |
_.each(data.records, function(item, index){ | |
sku_pad = (item.cmx03_detallespedido_producttemplates_name + pad).substr(0,25); | |
where_b = "B = '"+ sku_pad +"'"; | |
skus_where.push(where_b); | |
}); | |
} | |
self.where_skus = skus_where.join(' or '); | |
callback(); | |
}); | |
}, | |
_addJavascript: function (jsrc,pos) { | |
pos = typeof pos !== 'undefined' ? pos : 'head'; | |
var th = document.getElementsByTagName(pos)[0]; | |
var s = document.createElement('script'); | |
s.setAttribute('type','text/javascript'); | |
s.setAttribute('src',jsrc); | |
th.appendChild(s); | |
}, | |
_rsvpCB: function(resp){ | |
var self = this; | |
var today = App.date().format("YYYYMMDD"); | |
var file_name = 'existencias_' + today; | |
for (i=0; i<resp.items.length; i++) { | |
if(resp.items[i].title == file_name){ | |
self.sheet_id = resp.items[i].id; | |
self.getSkus(_.bind(self.drawChart,self)); | |
break; | |
} | |
} | |
}, | |
_rqstCB: function() { | |
var self = this; | |
var rv = gapi.client.drive.files.list({ | |
'q': '"'+self.FOLDER_ID+'" in parents and trashed = false','fields' : 'items(id,title,description)' | |
}).execute(_.bind(self._rsvpCB, self)); | |
}, | |
_onAuthResult: function(authResult) { | |
var self = this; | |
var authButton = self.$el.find('.authorize-button'); | |
authButton.addClass("hidden"); | |
if (authResult && !authResult.error) { // access token successfully retrieved | |
gapi.client.load('drive', 'v2', _.bind(self._rqstCB, self)); | |
} else { // no access token retrieved, force the authorization flow. | |
authButton.removeClass("hidden"); | |
authButton.on("click", function() { | |
self._checkAuth(false); | |
}); | |
} | |
}, | |
_checkAuth: function(bNow) { | |
var self = this; | |
gapi.auth.authorize({'client_id':self.CLIENT_ID, 'scope':self.SCOPE, 'immediate':bNow}, _.bind(self._onAuthResult, self)); | |
}, | |
_onLoadCB:function() { | |
var self = this | |
self._checkAuth(true); | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$viewdefs['base']['view']['existencias'] = array( | |
'dashlets' => array( | |
array( | |
'label' => 'LBL_DASHLET_EXISTENCIAS_NAME', | |
'description' => 'LBL_DASHLET_EXISTENCIAS_DESC', | |
'config' => array( | |
'limit' => '3', | |
), | |
'preview' => array( | |
'limit' => '3', | |
), | |
'filter' => array( | |
'module' => array( | |
'CMX03_Pedidos', | |
), | |
'view' => 'record' | |
), | |
), | |
), | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment