|
@@ -0,0 +1,460 @@
|
|
|
+function chart_sale_by_store (widget) {
|
|
|
+ "use strict";
|
|
|
+
|
|
|
+ var model = openerp;
|
|
|
+
|
|
|
+ widget.ChartSaleByStoreWidget = widget.Base.extend({
|
|
|
+ template: 'ChartSaleByStore',
|
|
|
+ modules: ['point_of_sale'],
|
|
|
+ data: [],
|
|
|
+ events: {
|
|
|
+ 'click .month': 'showMonth',
|
|
|
+ 'click .week': 'showWeek',
|
|
|
+ 'click .days': 'showToday',
|
|
|
+ },
|
|
|
+
|
|
|
+ init: function (parent) {
|
|
|
+ this._super(parent, {
|
|
|
+ width: 6,
|
|
|
+ height: 4
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ start: function () {
|
|
|
+ var self = this;
|
|
|
+ self.fetchInitial();
|
|
|
+ },
|
|
|
+
|
|
|
+ checkModel : function(model){
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.IrModuleModule,function(item){
|
|
|
+ return item.name === model
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchInitial:function() {
|
|
|
+ var self = this;
|
|
|
+ self.fecthIrModuleModule().then(function (IrModuleModule) {
|
|
|
+ return IrModuleModule;
|
|
|
+ }).then(function(IrModuleModule) {
|
|
|
+ self.IrModuleModule = IrModuleModule;
|
|
|
+ return self.fetchAccountJournal();
|
|
|
+ }).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.fetchResCompany();
|
|
|
+ }).then(function(ResCompany) {
|
|
|
+ self.ResCompany = ResCompany;
|
|
|
+ return self.fetchResCurrency();
|
|
|
+ }).then(function(ResCurrency) {
|
|
|
+ self.ResCurrency = ResCurrency;
|
|
|
+ return self.showMonth();
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /*
|
|
|
+ IR MODULES
|
|
|
+ */
|
|
|
+
|
|
|
+ fecthIrModuleModule: function(){
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields = ['name','id'];
|
|
|
+ var domain=[['state','=','installed'],['name','in',self.modules]];
|
|
|
+ var IrModuleModule = new model.web.Model('ir.module.module');
|
|
|
+ IrModuleModule.query(fields).filter(domain).all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ })
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ 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 modules = self.checkModel('point_of_sale');
|
|
|
+ if (modules.length > 0){
|
|
|
+ var date = moment().add(-1, 'month').format('YYYY-MM-20 00:00:00');
|
|
|
+ var fields = ['id','date_order','amount_total','sale_journal'];
|
|
|
+ var domain = [['state','not in',['draft','cancel']],['date_order','>',date]];
|
|
|
+ var PosOrder = new model.web.Model('pos.order');
|
|
|
+ PosOrder.query(fields).filter(domain).all().then(function (results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ }else{
|
|
|
+ var PosOrder = [];
|
|
|
+ return PosOrder;
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchAccountInvoice: function() {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var desde = moment().format('YYYY-MM-01');
|
|
|
+ var hasta = moment().add(1,'months').format('YYYY-MM-01');
|
|
|
+ var fields = ['id', 'name', 'date_invoice', 'amount_total','journal_id','currency_id'];
|
|
|
+ var domain = [['date_invoice','>=',desde], ['date_invoice','<',hasta],['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;
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchResCompany: function() {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields = ['id','name', 'currency_id'];
|
|
|
+ var domain = [['id', '=', self.session.company_id]];
|
|
|
+ var ResCompany = new model.web.Model('res.company');
|
|
|
+ ResCompany.query(fields).filter(domain).all().then(function (results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchResCurrency : function(){
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
|
|
|
+ var domain = [['active', '=', true]];
|
|
|
+ var ResCurrency = new model.web.Model('res.currency');
|
|
|
+ ResCurrency.query(fields).filter(domain).all().then(function(results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ getResCurrency: function (id) {
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.ResCurrency,function (item) {
|
|
|
+ return item.id === id;
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ /*=========
|
|
|
+ POS ORDER
|
|
|
+ =========*/
|
|
|
+
|
|
|
+ getMonthPosOrder:function(id) {
|
|
|
+ var self = this;
|
|
|
+ var journals = _.filter(self.AccountJournal,function (inv) {
|
|
|
+ return inv.store_ids[0] === id;
|
|
|
+ });
|
|
|
+ if(journals.length > 0){
|
|
|
+ var journal_ids = _.flatten(_.map(journals, function (item) {
|
|
|
+ return item.id;
|
|
|
+ }));
|
|
|
+ return _.flatten(_.filter(self.PosOrder,function (inv) {
|
|
|
+ var utc = moment.utc(inv.date_order,'YYYY-MM-DD h:mm:ss A');
|
|
|
+ return moment(utc._d).format('YYYY-MM') === moment().format('YYYY-MM') & _.contains(journal_ids, inv.sale_journal[0]);
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ getWeekPosOrder:function(id) {
|
|
|
+ var self = this;
|
|
|
+ var week = moment().week();
|
|
|
+ var journals = _.filter(self.AccountJournal,function (inv) {
|
|
|
+ return inv.store_ids[0] === id;
|
|
|
+ });
|
|
|
+ if(journals.length > 0){
|
|
|
+ var journal_ids = _.flatten(_.map(journals, function (item) {
|
|
|
+ return item.id;
|
|
|
+ }));
|
|
|
+ return _.flatten(_.filter(self.PosOrder,function (inv) {
|
|
|
+ var utc = moment.utc(inv.date_order,'YYYY-MM-DD h:mm:ss A');
|
|
|
+ utc = moment(utc._d).format('YYYY-MM-DD');
|
|
|
+ return moment(utc).week() === week & _.contains(journal_ids, inv.sale_journal[0]);
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ getTodayPosOrder:function(id) {
|
|
|
+ var self = this;
|
|
|
+ var today = moment().format('YYYY-MM-DD');
|
|
|
+ var journals = _.filter(self.AccountJournal,function (inv) {
|
|
|
+ return inv.store_ids[0] === id;
|
|
|
+ });
|
|
|
+ if(journals.length > 0){
|
|
|
+ var journal_ids = _.flatten(_.map(journals, function (item) {
|
|
|
+ return item.id;
|
|
|
+ }));
|
|
|
+ return _.flatten(_.filter(self.PosOrder,function (inv) {
|
|
|
+ var utc = moment.utc(inv.date_order,'YYYY-MM-DD h:mm:ss A');
|
|
|
+ return moment(utc._d).format('YYYY-MM-DD') === today & _.contains(journal_ids, inv.sale_journal[0]);
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ /*===============
|
|
|
+ ACCOUNT INVOICE
|
|
|
+ ===============*/
|
|
|
+
|
|
|
+ getMonthAccountInvoice:function(id) {
|
|
|
+ var self = this;
|
|
|
+ var journals = _.filter(self.AccountJournal,function (inv) {
|
|
|
+ return inv.store_ids[0] === id;
|
|
|
+ });
|
|
|
+ if(journals.length > 0){
|
|
|
+ var journal_ids = _.flatten(_.map(journals, function (item) {
|
|
|
+ return item.id;
|
|
|
+ }));
|
|
|
+ return _.flatten(_.filter(self.AccountInvoice,function (inv) {
|
|
|
+ return moment(inv.date_invoice).format('YYYY-MM') === moment().format('YYYY-MM') & _.contains(journal_ids, inv.journal_id[0]);
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ getWeekAccountInvoice:function(id) {
|
|
|
+ var self = this;
|
|
|
+ var week = moment().week();
|
|
|
+ var journals = _.filter(self.AccountJournal,function (inv) {
|
|
|
+ return inv.store_ids[0] === id;
|
|
|
+ });
|
|
|
+ if(journals.length > 0){
|
|
|
+ var journal_ids = _.flatten(_.map(journals, function (item) {
|
|
|
+ return item.id;
|
|
|
+ }));
|
|
|
+ return _.flatten(_.filter(self.AccountInvoice,function (inv) {
|
|
|
+ return moment(inv.date_invoice).week() === week & _.contains(journal_ids, inv.journal_id[0]);
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ getTodayAccountInvoice:function(id) {
|
|
|
+ var self = this;
|
|
|
+ var today = moment().format('YYYY-MM-DD');
|
|
|
+ var journals = _.filter(self.AccountJournal,function (inv) {
|
|
|
+ return inv.store_ids[0] === id;
|
|
|
+ });
|
|
|
+ if(journals.length > 0){
|
|
|
+ var journal_ids = _.flatten(_.map(journals, function (item) {
|
|
|
+ return item.id;
|
|
|
+ }));
|
|
|
+ return _.flatten(_.filter(self.AccountInvoice,function (inv) {
|
|
|
+ return moment(inv.date_invoice).format('YYYY-MM-DD') === today & _.contains(journal_ids, inv.journal_id[0])
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ showMonth: function() {
|
|
|
+ var self = this;
|
|
|
+ var order;
|
|
|
+ var invoice;
|
|
|
+ var title = [];
|
|
|
+ var data = [];
|
|
|
+ var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
|
|
|
+ _.each(self.ResStore, function(item){
|
|
|
+ var array = [];
|
|
|
+ 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);
|
|
|
+ _.each(invoice, function (item) {
|
|
|
+ var currency = self.getResCurrency(item.currency_id[0]).shift();
|
|
|
+ array.push({
|
|
|
+ amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
|
|
|
+ })
|
|
|
+ });
|
|
|
+ var invoice_total = 0;
|
|
|
+ if(array.length > 0){
|
|
|
+ invoice_total = _.reduce(_.map(array, function (map) {
|
|
|
+ return map.amount;
|
|
|
+ }), function (memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ var total = order_total + invoice_total;
|
|
|
+ data.push(total);
|
|
|
+ });
|
|
|
+ self.fetchChart(data,title);
|
|
|
+ },
|
|
|
+
|
|
|
+ showWeek: function () {
|
|
|
+ var self = this;
|
|
|
+ var order;
|
|
|
+ var invoice;
|
|
|
+ var title = [];
|
|
|
+ var data = [];
|
|
|
+ var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
|
|
|
+ _.each(self.ResStore, function(item){
|
|
|
+ var array = [];
|
|
|
+ title.push(item.name);
|
|
|
+ order = self.getWeekPosOrder(item.id);
|
|
|
+ invoice = self.getWeekAccountInvoice(item.id);
|
|
|
+ var order_total = _.reduce(_.map(order,function(item) {
|
|
|
+ return item.amount_total;
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ _.each(invoice, function (item) {
|
|
|
+ var currency = self.getResCurrency(item.currency_id[0]).shift();
|
|
|
+ array.push({
|
|
|
+ amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
|
|
|
+ })
|
|
|
+ });
|
|
|
+ var invoice_total = 0;
|
|
|
+ if(array.length > 0){
|
|
|
+ invoice_total = _.reduce(_.map(array, function (map) {
|
|
|
+ return map.amount;
|
|
|
+ }), function (memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ var total = order_total + invoice_total;
|
|
|
+ data.push(total);
|
|
|
+ });
|
|
|
+ self.fetchChart(data,title);
|
|
|
+ },
|
|
|
+
|
|
|
+ showToday: function (){
|
|
|
+ var self = this;
|
|
|
+ var order;
|
|
|
+ var invoice;
|
|
|
+ var title = [];
|
|
|
+ var data = [];
|
|
|
+ var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
|
|
|
+ _.each(self.ResStore, function(item){
|
|
|
+ var array = [];
|
|
|
+ title.push(item.name);
|
|
|
+ order = self.getTodayPosOrder(item.id);
|
|
|
+ invoice = self.getTodayAccountInvoice(item.id);
|
|
|
+ var order_total = _.reduce(_.map(order,function(item) {
|
|
|
+ return item.amount_total;
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ _.each(invoice, function (item) {
|
|
|
+ var currency = self.getResCurrency(item.currency_id[0]).shift();
|
|
|
+ array.push({
|
|
|
+ amount: item.amount_total * (CurrencyBase.rate_silent / currency.rate_silent),
|
|
|
+ })
|
|
|
+ });
|
|
|
+ var invoice_total = 0;
|
|
|
+ if(array.length > 0){
|
|
|
+ invoice_total = _.reduce(_.map(array, function (map) {
|
|
|
+ return map.amount;
|
|
|
+ }), function (memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ var total = order_total + invoice_total;
|
|
|
+ data.push(total);
|
|
|
+ });
|
|
|
+ self.fetchChart(data,title);
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchChart: function (data,title) {
|
|
|
+ var self = this;
|
|
|
+ var label = title;
|
|
|
+ var body = data;
|
|
|
+
|
|
|
+ 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: 'horizontalBar',
|
|
|
+ data: {
|
|
|
+ labels: label,
|
|
|
+ datasets: [
|
|
|
+ {
|
|
|
+ label: false,
|
|
|
+ data: body,
|
|
|
+ backgroundColor: '#bbdefb',
|
|
|
+ borderColor: '#0288d1',
|
|
|
+ borderWidth: 1,
|
|
|
+ fill: true,
|
|
|
+ datalabels : {
|
|
|
+ align : 'end',
|
|
|
+ anchor : 'end',
|
|
|
+ display: true,
|
|
|
+ backgroundColor: function(context) {
|
|
|
+ return context.dataset.backgroundColor;
|
|
|
+ },
|
|
|
+ borderRadius: 4,
|
|
|
+ color: '#001f3f',
|
|
|
+ font: {
|
|
|
+ weight: 'bold'
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ options: {
|
|
|
+ responsive: true,
|
|
|
+ title: {
|
|
|
+ display: false,
|
|
|
+ },
|
|
|
+ hover: {
|
|
|
+ mode: 'nearest',
|
|
|
+ intersect: true
|
|
|
+ },
|
|
|
+ legend: {
|
|
|
+ display: false,
|
|
|
+ },
|
|
|
+ layout: {
|
|
|
+ padding: {
|
|
|
+ top: 0,
|
|
|
+ bottom: 45,
|
|
|
+ left : 0,
|
|
|
+ rigth: 0,
|
|
|
+ }
|
|
|
+ },
|
|
|
+ events: ['click'],
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ });
|
|
|
+}
|