|
@@ -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,
|
|
|
+ }
|
|
|
+ },
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ });
|
|
|
+}
|