function ranking_sales_product (widget) { "use strict"; var model= openerp; widget.RankingSalesProductWidget = widget.Base.extend({ template: 'RankingSalesProductTmpl', data : [], accountInvoice : [], invoiceLine : [], productProduct : [], ranking:[], 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.fecthInvoiceLine(accountInvoice); }).then(function (invoiceLine) { self.invoiceLine = invoiceLine; return self.fecthProductProduct(invoiceLine); }).then(function(productProduct){ self.productProduct = productProduct; 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']; 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; }, // Obtener linea de la factura fecthInvoiceLine: function(accountInvoice){ var self = this; var defer = $.Deferred(); var invoice_line = _.flatten(_.map(accountInvoice, function (item) { return item.invoice_line })); var fields =['id', 'product_id', 'quantity']; var domain = [['id','in', invoice_line]]; var invoiceLine = new model.web.Model('account.invoice.line'); invoiceLine.query(fields).filter(domain).all().then(function (results) { defer.resolve(results); }); return defer; }, // Obtener Prodcutos fecthProductProduct: function (invoiceLine) { var self = this; var defer = $.Deferred(); var product_id = _.flatten(_.map(invoiceLine,function (item) { return item.product_id[0] })); var fields = ['id', 'name_template', 'type']; var domain =[['id', 'in', product_id]]; var product= new model.web.Model('product.product'); product.query(fields).filter(domain).all().then(function (results) { defer.resolve(results); }); return defer; }, fetchProductRanking: function(){ var self = this; var itemProduct; var itemLine; var ranking = []; var cat = 0; var lineUnik; for (var i = 0; i < self.productProduct.length; i++) { itemProduct = self.productProduct[i]; itemLine= self.getInvoiceLine(itemProduct.id); if (itemProduct.type == 'product'){ if (itemLine.length > 0){ cat = _.reduce(_.map(itemLine, function (map) { return map.quantity }), function (meno,num) { return meno + num }, 0); lineUnik = itemLine.shift(); console.log(lineUnik); ranking.push({ product: lineUnik.product_id[1], qty: cat, id: lineUnik.product_id[0] }); } } } ranking.sort(function (a, b) { return b.qty - a.qty }); self.$el.unblock(); self.$el.find('.widget-content.widget-loading').css('display','none'); var fecha = new Date(); var meses = new Array ("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"); self.$el.find('.widget-title').find('span').text("Ranking de productos más vendidos del mes de " + meses[fecha.getMonth()]); // self.$el.find('.widget-title').find('span').text("Ranking de productos más vendidos"); self.ranking=ranking; self.fetchChart(ranking); }, // get line getInvoiceLine: function (product_id){ var self = this; return _.flatten(_.filter(self.invoiceLine,function (line) { return line.product_id[0] === product_id })); }, // 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.product); body.push(item.qty); } var chart = new Chart(this.$el.find(".widget-content").find('canvas'), { // type: 'doughnut', 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, }, } }); }, showCustomers: function (e) { var self = this; var hoy =moment().format('YYYY-MM-DD'); var product_id= _.map(self.ranking,function(map){ return map.id; }) this.do_action({ name:"Listado de productos más vendidos", type: 'ir.actions.act_window', res_model: "product.product", views: [[false, 'list']], target: 'new', domain: [['id', 'in', product_id],['type','=', 'product']], context: {}, }); } }); }