Browse Source

[ADD] ventas por sucursal

Rodney Elpidio Enciso Arias 6 years ago
parent
commit
25612855e3

+ 3 - 0
data/charts_data.xml

@@ -49,5 +49,8 @@
         <record model="chart.list" id="chart_WidgetAccountToPay">
             <field name="name">WidgetAccountToPay</field>
         </record>
+        <record model="chart.list" id="chart_ChartSaleByStore">
+            <field name="name">ChartSaleByStore</field>
+        </record>
     </data>
 </openerp>

+ 13 - 0
static/src/js/dashboard.js

@@ -153,6 +153,17 @@ function dashboard_reporting_widget (instance, widget) {
                             self.grid.addWidget(wChartPurchaseExpense.$el, 0, 0, wChartPurchaseExpense.size.width,  wChartPurchaseExpense.size.height, true);
                         }
 
+                        // *************************** Ventas por Sucursal
+                        var chart =  _.flatten(_.filter(ChartList,function (inv) {
+                            return inv.name == 'ChartSaleByStore';
+                        }));
+                        if(chart.length > 0){
+                            var wWidgetSaleByStore = new widgets.ChartSaleByStoreWidget(self);
+                            wWidgetSaleByStore.renderElement();
+                            wWidgetSaleByStore.start();
+                            self.grid.addWidget(wWidgetSaleByStore.$el, 0, 0, wWidgetSaleByStore.size.width,  wWidgetSaleByStore.size.height, true);
+                        }
+
                         // *************************** Caja Efectivo
                         var chart =  _.flatten(_.filter(ChartList,function (inv) {
                             return inv.name == 'WidgetCash';
@@ -196,6 +207,8 @@ function dashboard_reporting_widget (instance, widget) {
                             wWidgetAccountToPay.start();
                             self.grid.addWidget(wWidgetAccountToPay.$el, 0, 0, wWidgetAccountToPay.size.width,  wWidgetAccountToPay.size.height, true);
                         }
+
+                        
                     });
                 });
             });

+ 1 - 0
static/src/js/main.js

@@ -19,6 +19,7 @@ openerp.eiru_reporting_dashboard = function (instance) {
         chart_pos_order(dashboard);
         chart_purchase_expense(dashboard);
         chart_ingreso_egreso(dashboard);
+        chart_sale_by_store(dashboard);
         widget_pos_order(dashboard);
         widget_purchase(dashboard);
         widget_expense(dashboard);

+ 208 - 0
static/src/js/widgets/chart_sale_by_store.js

@@ -0,0 +1,208 @@
+function chart_sale_by_store (widget) {
+    "use strict";
+
+    var model = openerp;
+
+    widget.ChartSaleByStoreWidget = widget.Base.extend({
+        template: 'ChartSaleByStore',
+        data: [],
+        events: {
+        },
+
+        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.fetchAccountJournal().then(function (AccountJournal) {
+                return AccountJournal;
+            }).then(function (AccountJournal) {
+                self.AccountJournal = AccountJournal;
+                return self.fetchPosOrder();
+            }).then(function (PosOrder) {
+                self.PosOrder = PosOrder;
+                return self.fetchAccountInvoice();
+            }).then(function (AccountInvoice) {
+                self.AccountInvoice = AccountInvoice;
+                return self.fetchResStore();
+            }).then(function (ResStore) {
+                self.ResStore = ResStore;
+                return self.showMonth();
+            });
+        },
+
+        fetchAccountJournal: function() {
+            var self = this;
+            var defer = $.Deferred();
+            var fields = ['id', 'name', 'store_ids'];
+            var domain = [['type','=','sale']];
+            var AccountJournal = new model.web.Model('account.journal');
+            AccountJournal.query(fields).filter(domain).all().then(function(results) {
+                defer.resolve(results);
+            });
+
+            return defer;
+        },
+        
+        fetchPosOrder: function() {
+            var self = this;
+            var defer = $.Deferred();
+            var fields = ['id','date_order','amount_total','sale_journal'];
+            var domain = [['state','not in',['draft','cancel']]];
+            var PosOrder = new model.web.Model('pos.order');
+            PosOrder.query(fields).filter(domain).all().then(function (results) {
+                defer.resolve(results);
+            });
+
+            return defer;
+        },
+
+        fetchAccountInvoice: function() {
+            var self = this;
+            var defer = $.Deferred();
+            var fields = ['id', 'name', 'date_invoice', 'amount_total','journal_id'];
+            var domain = [['state', 'not in', ['draft','cancel']],['type','=','out_invoice']];
+            var AccountInvoice = new model.web.Model('account.invoice');
+            AccountInvoice.query(fields).filter(domain).all().then(function(results) {
+                defer.resolve(results);
+            });
+
+            return defer;
+        },
+
+        fetchResStore: function() {
+            var self = this;
+            var defer = $.Deferred();
+            var fields = ['id','name','journal_ids'];
+            var ResStore = new model.web.Model('res.store');
+            ResStore.query(fields).filter().all().then(function(results) {
+                defer.resolve(results);
+            });
+
+            return defer;
+        },
+
+        getMonthPosOrder:function(id) {
+            var self = this;
+            var journal_id  = _.filter(self.AccountJournal,function (inv) {
+                return inv.store_ids[0] === id;
+            });
+            return _.flatten(_.filter(self.PosOrder,function (inv) {
+                return moment(inv.date_order).format('YYYY-MM') === moment().format('YYYY-MM') & inv.sale_journal[0] == journal_id[0].id;
+            }));
+        },
+
+        getMonthAccountInvoice:function(id) {
+            var self = this;
+            var journal_id  = _.filter(self.AccountJournal,function (inv) {
+                return inv.store_ids[0] === id;
+            });
+            return _.flatten(_.filter(self.AccountInvoice,function (inv) {
+                return moment(inv.date_invoice).format('YYYY-MM') === moment().format('YYYY-MM') & inv.journal_id[0] == journal_id[0].id;
+            }));
+        },
+  
+        showMonth: function() {
+            var self = this;
+            var order;
+            var invoice;
+            var title = [];
+            var data = [];            
+            _.each(self.ResStore, function(item){
+                title.push(item.name);
+                order = self.getMonthPosOrder(item.id);
+                invoice = self.getMonthAccountInvoice(item.id);
+                var order_total = _.reduce(_.map(order,function(item) {
+                    return item.amount_total;
+                }),function(memo, num) {
+                    return memo + num;
+                },0);
+                var invoice_total = _.reduce(_.map(invoice,function(item) {
+                    return item.amount_total;
+                }),function(memo, num) {
+                    return memo + num;
+                },0);
+                var total = order_total + invoice_total;
+                data.push(total); 
+            });
+            self.$el.unblock();
+            self.$el.find('.widget-content.widget-loading').css('display','none');
+            self.fetchChart(data,title);
+        },
+
+        fetchChart: function (data,title) {
+            var self = this;
+            var label = title;
+            var body = data;
+
+            // 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: 20,
+                            left : 0,
+                            rigth: 0,
+                        }
+                    },
+                }
+            });
+        },
+    });
+}

+ 23 - 0
static/src/xml/widgets/chart_sale_by_store.xml

@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<template xml:space="preserve">
+    <t t-name="ChartSaleByStore">
+        <t t-call="WidgetReportingBaseTemplate"> 
+            <h2  class="widget-title">
+                <div class="row">
+                    <div class="col-xs-6">
+                        <i class="fa fa-bar-chart" aria-hidden="true"></i>
+                        <span>Ventas por Sucursal</span> 
+                    </div>
+                </div>
+            </h2>
+            <div class="widget-content">
+                <br/>
+                <canvas></canvas>
+            </div>
+            <div class="widget-content widget-loading">
+                <i class='fa fa-cog fa-spin fa-3x fa-fw'></i>
+            </div>
+        </t>
+    </t>
+</template>

+ 1 - 0
templates.xml

@@ -20,6 +20,7 @@
                 <script type="text/javascript" src="/eiru_reporting_dashboard/static/src/js/widgets/chart_pos_order_product.js" />
                 <script type="text/javascript" src="/eiru_reporting_dashboard/static/src/js/widgets/chart_pos_order_salesman.js" />
                 <script type="text/javascript" src="/eiru_reporting_dashboard/static/src/js/widgets/chart_pos_order.js" />
+                <script type="text/javascript" src="/eiru_reporting_dashboard/static/src/js/widgets/chart_sale_by_store.js" />
                 <script type="text/javascript" src="/eiru_reporting_dashboard/static/src/js/widgets/chart_purchase_expense.js" />
                 <script type="text/javascript" src="/eiru_reporting_dashboard/static/src/js/widgets/widget_pos_order.js" />
                 <script type="text/javascript" src="/eiru_reporting_dashboard/static/src/js/widgets/widget_purchase.js" />