123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- 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: {},
- });
- }
- });
- }
|