Bläddra i källkod

[ADD] Agregado filtros en los graficos de ventas y compra-gastos

Rodney Elpidio Enciso Arias 7 år sedan
förälder
incheckning
f53a396825

+ 1 - 1
static/src/js/widgets/chart_invoice.js

@@ -11,7 +11,7 @@ function chart_invoice (widget) {
         modelId: [],
 
         events: {
-            // 'click canvas': 'showCustomers'
+            
         },
         init: function (parent) {
             this._super(parent, {

+ 115 - 34
static/src/js/widgets/chart_pos_order.js

@@ -6,21 +6,23 @@ function chart_pos_order (widget) {
     widget.ChartPosOrderWidget = widget.Base.extend({
         template: 'ChartPosOrder',
         data: [],
-        modelId: [],
-
         events: {
-            // 'click canvas': 'showCustomers'
+            'click .month': 'showMonth',
+            'click .week': 'showWeek',
         },
+
         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({
@@ -36,27 +38,10 @@ function chart_pos_order (widget) {
                 return PosOrder;
             }).then(function (PosOrder) {
                 self.PosOrder = PosOrder;
-                return self.fetchGetModelId();
-            }).then(function(modelId) {
-                self.modelId = modelId;
-                return self.BuildChart();
+                return self.showWeek();
             });
         },
-
-        // getModelId
-        fetchGetModelId: function() {
-            var self = this;
-            var defer = $.Deferred();
-            var irModelData = new model.web.Model('ir.model.data');
-            var getObtjectReference = irModelData.get_func('get_object_reference');
-
-            this.alive(getObtjectReference('base', 'view_partner_form')).then(function(results) {
-                defer.resolve(results);
-            });
-
-            return defer;
-        },
-        // Obtener Pedidos
+        
         fecthPosOrder: function() {
             var self = this;
             var defer = $.Deferred();
@@ -70,8 +55,7 @@ function chart_pos_order (widget) {
             return defer;
         },
 
-        // Obtener facturas por cada proveedores
-        getPosOrder:function(mes) {
+        getMonthPosOrder:function(mes) {
             var self = this;
             if (mes < 10){
                 var fecha = moment().format('YYYY')+'-'+'0'+mes;    
@@ -82,16 +66,23 @@ function chart_pos_order (widget) {
                 return moment(inv.date_order).format('YYYY-MM') === fecha;
             }));
         },
+
+        getWeekPosOrder:function(day) {
+            var self = this;
+            var week = moment().week();
+            return _.flatten(_.filter(self.PosOrder,function (inv) {
+                return moment(inv.date_order).week() === week & moment(inv.date_order).isoWeekday() === day;
+            }));
+        },
         
-        // Generar el Ranking
-        BuildChart: function() {
+        showMonth: function() {
             var self = this;
             var order;
             var mes = 0;
             var data = [];
             
             for (var i = 1; i <= 12; i++) {
-                order = self.getPosOrder(i);
+                order = self.getMonthPosOrder(i);
                 var total = _.reduce(_.map(order,function(item) {
                     return item.amount_total;
                 }),function(memo, num) {
@@ -101,22 +92,38 @@ function chart_pos_order (widget) {
                     amount_total: accounting.formatNumber(total,0,".",","),
                 })
             }
+            self.$el.unblock();
+            self.$el.find('.widget-content.widget-loading').css('display','none');
+            self.fetchMonthChart(data);
+        },
 
-            // self.data = data;
+        showWeek: function (){
+            var self = this;
+            var order;
+            var data = [];
+            for (var i = 1; i <= 7; i++) {
+                order = self.getWeekPosOrder(i);
+                var total = _.reduce(_.map(order,function(item) {
+                    return item.amount_total;
+                }),function(memo, num) {
+                    return memo + num;
+                },0);
+                data.push({
+                    amount_total: accounting.formatNumber(total,0,".",","),
+                });
+            }
             self.$el.unblock();
             self.$el.find('.widget-content.widget-loading').css('display','none');
-            self.fetchChart(data);
+            self.fetchWeekChart(data);
         },
 
-        // Generar Grafico
-        fetchChart: function (data) {
+        fetchMonthChart: function (data) {
             var self = this;
             var label = ['Enero', 'Febrero', 'Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'];
             var body = [];
             var item;
             var rank = 12;
 
-            // InInvoice
             if (data.length < rank && data.length > 0){
                 rank= ranking.length;
             }
@@ -155,7 +162,7 @@ function chart_pos_order (widget) {
                             backgroundColor: '#e3f2fd',
                             borderColor: '#64b5f6',
                             borderWidth: 2,
-                            fill: false,
+                            fill: true,
                         }
                     ]
                 },
@@ -173,7 +180,7 @@ function chart_pos_order (widget) {
                     },
                     layout: {
                         padding: {
-                            top: 30,
+                            top: 35,
                             bottom: 0,
                             left : 0,
                             rigth: 0,
@@ -182,5 +189,79 @@ function chart_pos_order (widget) {
                 }
             });
         },
+
+        fetchWeekChart: function (data) {
+            var self = this;
+            var label = ['Lunes', 'Martes', 'Miercoles','Jueves','Viernes','Sabado','Domingo'];
+            var body = [];
+            var item;
+            var rank = 7;
+
+            if (data.length < rank && data.length > 0){
+                rank= ranking.length;
+            }
+            for (var i = 0; i < rank; i++) {
+                if (data[i]) {
+                    item = data[i];
+                }
+                if (data.length === 0) {
+                    item = {};
+                    item.amount_total = 0;
+                }
+                body.push(item.amount_total);
+            }
+
+            // Global method for setting Y axis number format.
+            Chart.scaleService.updateScaleDefaults('linear', {
+              ticks: {
+                callback: function(tick) {
+                    return tick.toLocaleString('de-DE');
+                }
+              }
+            });
+            Chart.defaults.global.tooltips.callbacks.label = function(tooltipItem, data) {
+                var dataset = data.datasets[tooltipItem.datasetIndex];
+                var datasetLabel = dataset.label || '';
+                return datasetLabel +  dataset.data[tooltipItem.index].toLocaleString('de-DE');
+            };
+            var chart = new Chart(this.$el.find(".widget-content").find('canvas'), {
+                type: 'line',
+                data: {
+                    labels: label,
+                    datasets: [
+                        {
+                            label: false,
+                            data: body,
+                            backgroundColor: '#e3f2fd',
+                            borderColor: '#64b5f6',
+                            borderWidth: 2,
+                            fill: true,
+                        }
+                    ]
+                },
+                options: {
+                    responsive: true,
+                    title: {
+                        display: false,
+                    },
+                    hover: {
+                        mode: 'nearest',
+                        intersect: true
+                    },
+                    legend: {
+                       display: false,
+                    },
+                    layout: {
+                        padding: {
+                            top: 35,
+                            bottom: 15,
+                            left : 0,
+                            rigth: 0,
+                        }
+                    },
+                }
+            });
+        },
+
     });
 }

+ 5 - 48
static/src/js/widgets/chart_pos_order_customer.js

@@ -6,12 +6,9 @@ function chart_pos_order_customer (widget) {
     widget.ChartPosOrderCustomerWidget = widget.Base.extend({
         template: 'ChartPosOrderCustomer',
         data: [],
-        countPosOrder: [],
-        resPartner: [],
-        modelId: [],
 
         events: {
-            'click canvas': 'showCustomers'
+            
         },
         init: function (parent) {
             this._super(parent, {
@@ -41,25 +38,9 @@ function chart_pos_order_customer (widget) {
                 return self.fecthPartner(PosOrder);
             }).then(function (resPartner) {
                 self.resPartner = resPartner;
-                return self.fetchGetModelId();
-            }).then(function(modelId) {
-                self.modelId = modelId;
                 return self.fetchProductRanking();
             });
         },
-        // getModelId
-        fetchGetModelId: function() {
-            var self = this;
-            var defer = $.Deferred();
-            var irModelData = new model.web.Model('ir.model.data');
-            var getObtjectReference = irModelData.get_func('get_object_reference');
-
-            this.alive(getObtjectReference('base', 'view_partner_form')).then(function(results) {
-                defer.resolve(results);
-            });
-
-            return defer;
-        },
         // Obtener Pedidos
         fecthPosOrder: function() {
             var self = this;
@@ -129,20 +110,19 @@ function chart_pos_order_customer (widget) {
 
             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 = 3;
-
-            if (ranking.length < rank && ranking.length > 0 )
+            var rank = 7;
+            if (ranking.length < rank && ranking.length > 0 ){
                 rank= ranking.length;
-
+            }
             for (var i = 0; i < rank; i++) {
                 if (ranking[i]) {
                     item = ranking[i];
@@ -155,7 +135,6 @@ function chart_pos_order_customer (widget) {
                 label.push(item.name);
                 body.push(item.countPosOrder);
             }
-            // Global method for setting Y axis number format.
             Chart.scaleService.updateScaleDefaults('linear', {
               ticks: {
                 callback: function(tick) {
@@ -178,7 +157,6 @@ function chart_pos_order_customer (widget) {
                             backgroundColor: '#e3f2fd',
                             borderColor: '#64b5f6',
                             borderWidth: 2,
-                            // fill: false,
                             data: body,
                         }
                     ]
@@ -206,26 +184,5 @@ function chart_pos_order_customer (widget) {
                 }
             });
         },
-        // llamar a la lista
-        showCustomers: function (e) {
-            var self = this;
-            if (self.ranking.length === 0 ) {
-                model.web.notification.do_warn("Atención","Sin datos");
-                return
-            }
-            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'],[self.modelId[1],'form']],
-                target: 'current',
-                domain: [['id', 'in', partner_id]],
-                context: {},
-            });
-        }
     });
 }

+ 2 - 49
static/src/js/widgets/chart_pos_order_product.js

@@ -6,15 +6,9 @@ function chart_pos_order_product (widget) {
     widget.ChartPosOrderProductWidget = widget.Base.extend({
         template: 'ChartPosOrderProduct',
         data: [],
-        accountInvoice: [],
-        invoiceLine: [],
-        productProduct: [],
-        ranking: [],
-        modelId: [],
 
         events: {
-            // 'click canvas': 'showCustomers'
-            'click h2': 'showCustomers'
+            
         },
         init: function (parent) {
             this._super(parent, {
@@ -46,25 +40,10 @@ function chart_pos_order_product (widget) {
                 return self.fecthProductProduct(PosOrderLine);
             }).then(function(productProduct){
                 self.productProduct = productProduct;
-                return self.fetchGetModelId();
-            }).then(function(modelId){
-                self.modelId = modelId;
                 return self.fetchProductRanking();
             });
         },
-        // getModelId
-        fetchGetModelId: function() {
-            var self = this;
-            var defer = $.Deferred();
-            var irModelData = new model.web.Model('ir.model.data');
-            var getObtjectReference = irModelData.get_func('get_object_reference');
-
-            this.alive(getObtjectReference('product', 'product_normal_form_view')).then(function(results) {
-                defer.resolve(results);
-            });
-
-            return defer;
-        },
+        
         // Obtener Pedido
         fecthPosOrder: function() {
             var self = this;
@@ -158,11 +137,6 @@ function chart_pos_order_product (widget) {
             });
             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 y servicios del mes de " + meses[fecha.getMonth()]);
-            // self.$el.find('.widget-title').find('span').text("Ranking de productos más vendidos del mes de " + meses[fecha.getMonth()]);
-            self.ranking = ranking;
             self.fetchChart(ranking);
         },     
         // Generar Grafico
@@ -240,26 +214,5 @@ function chart_pos_order_product (widget) {
                 }
             });
         },
-        showCustomers: function (e) {
-            var self = this;
-            if (self.ranking.length === 0){
-                model.web.notification.do_warn("Atención","Sin datos");
-                return
-            }
-            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'],[self.modelId[1],'form']],
-                target: 'current',
-                domain: [['id', 'in', product_id]],
-                context: {},
-            });
-        }
     });
 }

+ 165 - 45
static/src/js/widgets/chart_purchase_expense.js

@@ -6,22 +6,24 @@ function chart_purchase_expense (widget) {
     widget.ChartPurchaseExpenseWidget = widget.Base.extend({
         template: 'ChartPurchaseExpense',
         data: [],
-        resPartner: [],
-        modelId: [],
 
         events: {
-           
+            'click .month': 'showMonth',
+            'click .week': 'showWeek',
         },
+        
         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({
@@ -37,28 +39,10 @@ function chart_purchase_expense (widget) {
                 return AccountInvoice;
             }).then(function (AccountInvoice) {
                 self.AccountInvoice = AccountInvoice;
-                return self.fetchGetModelId();
-            }).then(function(modelId) {
-                self.modelId = modelId;
-                return self.BuildChart();
-            });
-        },
-
-        // getModelId
-        fetchGetModelId: function() {
-            var self = this;
-            var defer = $.Deferred();
-            var irModelData = new model.web.Model('ir.model.data');
-            var getObtjectReference = irModelData.get_func('get_object_reference');
-
-            this.alive(getObtjectReference('base', 'view_partner_form')).then(function(results) {
-                defer.resolve(results);
+                return self.showWeek();
             });
-
-            return defer;
         },
 
-        // Obtener facturas
         fecthAccountInvoice: function() {
             var self = this;
             var defer = $.Deferred();
@@ -72,43 +56,55 @@ function chart_purchase_expense (widget) {
             return defer;
         },
 
-        // Obtener facturas por cada proveedores
-        getAccountInvoicePurchase:function(mes) {
+        getMonthAccountInvoicePurchase:function(month) {
             var self = this;
-            if (mes < 10){
-                var fecha = moment().format('YYYY')+'-'+'0'+mes;    
+            if (month < 10){
+                var fecha = moment().format('YYYY')+'-'+'0'+month;    
             }else{
-                var fecha = moment().format('YYYY')+'-'+mes;    
+                var fecha = moment().format('YYYY')+'-'+month;    
             }
             return _.flatten(_.filter(self.AccountInvoice,function (inv) {
                 return moment(inv.date_invoice).format('YYYY-MM') === fecha & inv.origin !== false;
             }));
         },
 
-        // Obtener facturas de clientes
-        getAccountInvoiceExpense:function(mes) {
+        getMonthAccountInvoiceExpense:function(month) {
             var self = this;
-            if (mes < 10){
-                var fecha = moment().format('YYYY')+'-'+'0'+mes;    
+            if (month < 10){
+                var fecha = moment().format('YYYY')+'-'+'0'+month;    
             }else{
-                var fecha = moment().format('YYYY')+'-'+mes;    
+                var fecha = moment().format('YYYY')+'-'+month;    
             }
             return _.flatten(_.filter(self.AccountInvoice,function (inv) {
                 return moment(inv.date_invoice).format('YYYY-MM') === fecha & inv.origin === false;
             }));
         },
-        
-        // Generar el Ranking
-        BuildChart: function() {
+
+        getWeekAccountInvoicePurchase:function(day) {
+            var self = this;
+            var week = moment().week();
+            return _.flatten(_.filter(self.AccountInvoice,function (inv) {
+                return moment(inv.date_invoice).week() === week & inv.origin !== false & moment(inv.date_invoice).isoWeekday() === day;
+            }));
+        },
+
+        getWeekAccountInvoiceExpense:function(day) {
+            var self = this;
+            var week = moment().week();
+            return _.flatten(_.filter(self.AccountInvoice,function (inv) {
+                return moment(inv.date_invoice).week() === week & inv.origin === false & moment(inv.date_invoice).isoWeekday() === day;
+            }));
+        },
+ 
+        showMonth: function() {
             var self = this;
-            console.log(self);
             var invoices;
             var mes = 0;
             var dataPurchase = [];
             var dataExpense = [];
 
             for (var i = 1; i <= 12; i++) {
-                invoices = self.getAccountInvoicePurchase(i);
+                invoices = self.getMonthAccountInvoicePurchase(i);
                 var total = _.reduce(_.map(invoices,function(item) {
                     return item.amount_total;
                 }),function(memo, num) {
@@ -120,7 +116,43 @@ function chart_purchase_expense (widget) {
             }
             
             for (var i = 1; i <= 12; i++) {
-                invoices = self.getAccountInvoiceExpense(i);
+                invoices = self.getMonthAccountInvoiceExpense(i);
+                var total = _.reduce(_.map(invoices,function(item) {
+                    return item.amount_total;
+                }),function(memo, num) {
+                    return memo + num;
+                },0);
+                dataExpense.push({
+                    amount_total: accounting.formatNumber(total,0,".",","),
+                })
+            }
+
+            self.$el.unblock();
+            self.$el.find('.widget-content.widget-loading').css('display','none');
+            self.fetchMonthChart(dataPurchase, dataExpense);
+        },
+
+        showWeek: function() {
+            var self = this;
+            var invoices;
+            var mes = 0;
+            var dataPurchase = [];
+            var dataExpense = [];
+
+            for (var i = 1; i <= 7; i++) {
+                invoices = self.getWeekAccountInvoicePurchase(i);
+                var total = _.reduce(_.map(invoices,function(item) {
+                    return item.amount_total;
+                }),function(memo, num) {
+                    return memo + num;
+                },0);
+                dataPurchase.push({
+                    amount_total: accounting.formatNumber(total,0,".",","),
+                })
+            }
+            
+            for (var i = 1; i <= 7; i++) {
+                invoices = self.getWeekAccountInvoiceExpense(i);
                 var total = _.reduce(_.map(invoices,function(item) {
                     return item.amount_total;
                 }),function(memo, num) {
@@ -131,14 +163,12 @@ function chart_purchase_expense (widget) {
                 })
             }
 
-            // self.data = data;
             self.$el.unblock();
             self.$el.find('.widget-content.widget-loading').css('display','none');
-            self.fetchChart(dataPurchase, dataExpense);
+            self.fetchWeekChart(dataPurchase, dataExpense);
         },
 
-        // Generar Grafico
-        fetchChart: function (dataPurchase,dataExpense) {
+        fetchMonthChart: function (dataPurchase,dataExpense) {
             var self = this;
             var label = ['Enero', 'Febrero', 'Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'];
             var bodyPurchase = [];
@@ -146,7 +176,6 @@ function chart_purchase_expense (widget) {
             var item;
             var rank = 12;
 
-            // InInvoice
             if (dataPurchase.length < rank && dataPurchase.length > 0){
                 rank= ranking.length;
             }
@@ -161,8 +190,6 @@ function chart_purchase_expense (widget) {
                 bodyPurchase.push(item.amount_total);
             }
 
-
-            // OutInvoice
             if (dataExpense.length < rank && dataExpense.length > 0){
                 rank= ranking.length;
             }
@@ -233,5 +260,98 @@ function chart_purchase_expense (widget) {
                 }
             });
         },
+
+        fetchWeekChart: function (dataPurchase,dataExpense) {
+            var self = this;
+            var label = ['Lunes', 'Martes', 'Miercoles','Jueves','Viernes','Sabado','Domingo'];
+            var bodyPurchase = [];
+            var bodyExpense = [];
+            var item;
+            var rank = 7;
+
+            if (dataPurchase.length < rank && dataPurchase.length > 0){
+                rank= ranking.length;
+            }
+            for (var i = 0; i < rank; i++) {
+                if (dataPurchase[i]) {
+                    item = dataPurchase[i];
+                }
+                if (dataPurchase.length === 0) {
+                    item = {};
+                    item.amount_total = 0;
+                }
+                bodyPurchase.push(item.amount_total);
+            }
+
+            if (dataExpense.length < rank && dataExpense.length > 0){
+                rank= ranking.length;
+            }
+            for (var i = 0; i < rank; i++) {
+                if (dataExpense[i]) {
+                    item = dataExpense[i];
+                }
+                if (dataExpense.length === 0) {
+                    item = {};
+                    item.amount_total = 0;
+                }
+                bodyExpense.push(item.amount_total);
+            } 
+
+            // Global method for setting Y axis number format.
+            Chart.scaleService.updateScaleDefaults('linear', {
+              ticks: {
+                callback: function(tick) {
+                    return tick.toLocaleString('de-DE');
+                }
+              }
+            });
+            Chart.defaults.global.tooltips.callbacks.label = function(tooltipItem, data) {
+                var dataset = data.datasets[tooltipItem.datasetIndex];
+                var datasetLabel = dataset.label || '';
+                return datasetLabel +  dataset.data[tooltipItem.index].toLocaleString('de-DE');
+            };
+            var chart = new Chart(this.$el.find(".widget-content").find('canvas'), {
+                type: 'line',
+                data: {
+                    labels: label,
+                    datasets: [
+                        {
+                            label: 'Compras  ',
+                            data: bodyPurchase,
+                            backgroundColor: '#e3f2fd',
+                            borderColor: '#64b5f6',
+                            borderWidth: 2,
+                            fill: false,
+                        },
+                        {
+                            label: 'Gastos  ',                            
+                            data: bodyExpense,
+                            backgroundColor: '#c8e6c9',
+                            borderColor: '#66bb6a',
+                            borderWidth: 2,
+                            fill: false,
+                        }
+                    ]
+                },
+                options: {
+                    responsive: true,
+                    title: {
+                        display: true,
+                    },
+                    hover: {
+                        mode: 'nearest',
+                        intersect: true
+                    },
+                    layout: {
+                        padding: {
+                            top: 0,
+                            bottom: 15,
+                            left : 0,
+                            rigth: 0,
+                        }
+                    },
+                }
+            });
+        },
     });
 }

+ 3 - 1
static/src/xml/widgets/chart_pos_order.xml

@@ -5,7 +5,9 @@
         <t t-call="WidgetReportingBaseTemplate">
             <h2  class="widget-title">
                 <i class="fa fa-bar-chart" aria-hidden="true"></i>
-                <span> Ventas </span>
+                <span> Ventas   </span>
+                <a class="btn btn-default week" role="button">Esta Semana</a>
+                <a class="btn btn-default month" role="button">Meses</a>
             </h2>
             <div class="widget-content">
                 <br/>

+ 3 - 1
static/src/xml/widgets/chart_purchase_expense.xml

@@ -5,7 +5,9 @@
         <t t-call="WidgetReportingBaseTemplate">
             <h2  class="widget-title">
                 <i class="fa fa-bar-chart" aria-hidden="true"></i>
-                <span>Compras y Gastos</span>
+                <span> Compras y Gastos     </span>
+                <a class="btn btn-default week" role="button">Esta Semana</a>
+                <a class="btn btn-default month" role="button">Meses</a>
             </h2>
             <div class="widget-content">
                 <br/>