function ranking_sales_partner (widget) { "use strict"; var model= openerp; widget.RankingSalesPartnerWidget = widget.Base.extend({ template: 'RankingSalesPartner', data : [], accountInvoice : [], resPartner : [], events: { 'click canvas': 'showCustomers' }, init: function (parent) { this._super(parent, { width : 6, height: 4 }); }, start: function () { var self = this; self.fetchInitial(); }, fetchInitial:function(){ var self = this; self.$el.block({ message: null, overlayCSS: { backgroundColor: '#FAFAFA' } }); self.$el.find('.widget-content.widget-loading').css('display','flex'); self.fecthInvoice().then(function (accountInvoice) { return accountInvoice; }).then(function (accountInvoice) { self.accountInvoice = accountInvoice; return self.fecthPartner(accountInvoice); }).then(function (resPartner) { self.resPartner = resPartner; return self.fetchProductRanking(); }); }, // Obtener factura fecthInvoice: function(){ var self = this; var desde =moment().format('YYYY-MM-01'); var hasta =moment().add(1,'months').format('YYYY-MM-01'); var defer = $.Deferred(); var fields = ['id', 'invoice_line', 'date_invoice','partner_id']; var domain = [['type', '=', 'out_invoice'], ['date_invoice', '>=', desde], ['date_invoice', '<', hasta], ['state', 'in', ['open','paid']]]; var invoice = new model.web.Model('account.invoice'); invoice.query(fields).filter(domain).all().then(function (results) { defer.resolve(results); }); return defer; }, // Partner fecthPartner: function(invoice){ var self = this; var defer = $.Deferred(); var partner_id = _.map(invoice,function(map){ return map.partner_id[0]; }); var fields =['id', 'name']; var domain =[['id', 'in', partner_id],['active', '=', true],['customer','=',true]]; var resPartner = new model.web.Model('res.partner'); resPartner.query(fields).filter(domain).all().then(function(results){ defer.resolve(results); }); return defer; }, // Obtener facturas por cada clientes getAccountInvoice:function(id_partner){ var self = this; return _.flatten(_.filter(self.accountInvoice,function (inv) { return inv.partner_id[0] === id_partner; })); }, fetchProductRanking: function(){ var self = this; var itemPartner; var itemInvoice; var countInvoice = 0 var ranking = []; for (var i = 0; i < self.resPartner.length; i++) { itemPartner = self.resPartner[i]; itemInvoice = self.getAccountInvoice(itemPartner.id); countInvoice =_.countBy(_.map(itemInvoice,function(map){ return map.id; }),function(num){ return num ? 'even': 'odd'; }); ranking.push({ id : itemPartner.id, name: itemPartner.name, countInvoice :countInvoice.even }); } ranking.sort(function (a, b) { return b.countInvoice - a.countInvoice }); self.ranking=ranking; self.$el.unblock(); self.$el.find('.widget-content.widget-loading').css('display','none'); self.$el.find('.widget-title').find('span').text("Ranking de clientes con más compras en el mes"); self.fetchChart(ranking); }, // Generar Grafico fetchChart: function (ranking){ var self = this; var label = []; var body = []; var item; var rank = 10; rank= ranking.length; for (var i = 0; i < rank; i++) { if (ranking[i]){ item = ranking[i]; } label.push(item.name); body.push(item.countInvoice); } var chart = new Chart(this.$el.find(".widget-content").find('canvas'), { type: 'pie', data: { labels: label, datasets: [ { backgroundColor: ['#01579B','#0277BD','#0288D1','#039BE5','#03A9F4','#29B6F6','#4FC3F7','#81D4FA','#B3E5FC','#E1F5FE'], data: body, } ] }, options: { animation: { animateScale: true, animateRotate: true }, maintainAspectRatio: false, layout: { padding: 20 }, legend: { display: false, }, } }); // var chart = new Chart(this.$el.find(".widget-content").find('canvas'), { // // type: 'doughnut', // type: 'horizontalBar', // // type: '', // data: { // labels: label, // datasets: [ // { // backgroundColor: ['#0288d1', '#0288d1', '#0288d1', '#0288d1', '#0288d1', '#0288d1', '#0288d1', '#0288d1', '#0288d1', '#0288d1'], // data: body, // } // ] // }, // options: { // maintainAspectRatio: false, // layout: { // padding: 20 // }, // scales: { // xAxes: [{ stacked: true }], // yAxes: [{ stacked: true }], // }, // legend: { // display: false, // }, // } // }); }, // llamar a la lista showCustomers: function (e) { var self = this; var hoy =moment().format('YYYY-MM-DD'); var partner_id= _.map(self.ranking,function(map){ return map.id; }) this.do_action({ name:"Listado de clientes con más compras", type: 'ir.actions.act_window', res_model: "res.partner", views: [[false,'list'],[false,'form']], target: 'new', domain: [['id', 'in', partner_id]], context: {}, flags: {'form': {'action_buttons': false, 'options': {'mode': 'view'}}}, }); } }); }