function widget_expense(widget) { "use strict"; var model = openerp; widget.WidgetExpenseWidget = widget.Base.extend({ template: 'WidgetExpense', events: { 'click .today': 'showToday', 'click .thisWeek': 'showThisWeek', 'click .thisMonth': 'showThisMonth', }, init: function (parent) { this._super(parent, { width: 3, height: 2 }); }, start: function () { var self = this; self.fetchInitial(); }, fetchInitial: function(){ var self = this; self.$el.find('#morosidad').block({ message: null, overlayCSS: { backgroundColor: '#FAFAFA' } }); self.$el.find('.widget-content.widget-loading').css('display','flex'); self.fetchCurrentUser().then(function (CurrentUser) { return CurrentUser; }).then(function (CurrentUser) { self.CurrentUser = CurrentUser; return self.fetchResUser(CurrentUser); }).then(function(ResUser) { self.ResUser = ResUser; return self.fetchAccountJournal(); }).then(function(AccountJournal) { self.AccountJournal = AccountJournal; return self.fetchAccountInvoice(); }).then(function(AccountInvoice) { self.AccountInvoice = AccountInvoice; return self.fetchResCompany(); }).then(function(ResCompany) { self.ResCompany = ResCompany; return self.fetchResCurrecy(); }).then(function(ResCurrecy) { self.ResCurrecy = ResCurrecy; return self.showThisMonth(); }); }, fetchCurrentUser: function() { var self = this; var ResUser = new model.web.Model('res.users'); return ResUser.call('get_user', { context: new model.web.CompoundContext() }); }, fetchResUser: function(id) { var self = this; var defer = $.Deferred(); var fields = ['id','name','store_id']; var domain = [['id','=',id]]; var ResUser = new model.web.Model('res.users'); ResUser.query(fields).filter(domain).all().then(function (results) { defer.resolve(results); }); return defer; }, fetchAccountJournal: function() { var self = this; var defer = $.Deferred(); var store_ids = self.ResUser[0].store_id[0]; var fields = ['id', 'name', 'store_ids']; var domain = [['type','=','purchase'],['store_ids','in',store_ids]]; var AccountJournal = new model.web.Model('account.journal'); AccountJournal.query(fields).filter(domain).all().then(function(results) { defer.resolve(results); }); return defer; }, fetchAccountInvoice: function() { var self = this; var defer = $.Deferred(); var journal_ids = _.flatten(_.map(this.AccountJournal, function (item) { return item.id; })); var fields = ['id', 'name', 'date_invoice', 'amount_total']; var domain = [['state', 'not in', ['draft','cancel']],['origin','=',false],['type','=','in_invoice'],['journal_id','in',journal_ids]]; var AccountInvoice = new model.web.Model('account.invoice'); AccountInvoice.query(fields).filter(domain).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', '=', 1]]; var ResCompany = new model.web.Model('res.company'); ResCompany.query(fields).filter(domain).all().then(function (results) { defer.resolve(results); }); return defer; }, fetchResCurrecy : 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 ResCurrecy = new model.web.Model('res.currency'); ResCurrecy.query(fields).filter(domain).all().then(function(results) { defer.resolve(results); }); return defer; }, getResCurrency: function (id) { var self = this; return _.filter(self.ResCurrecy,function (item) { return item.id === id; }) }, getTodayAccountInvoice:function() { var self = this; var date = moment().format('YYYY-MM-DD'); return _.flatten(_.filter(self.AccountInvoice,function (inv) { return moment(inv.date_invoice).format('YYYY-MM-DD') === date; })); }, getThisWeekAccountInvoice:function() { var self = this; var week = moment().week(); return _.flatten(_.filter(self.AccountInvoice,function (inv) { return moment(inv.date_invoice).week() === week & moment(inv.date_invoice).format('YYYY')=== moment().format('YYYY'); })); }, getThisMonthAccountInvoice:function() { var self = this; return _.flatten(_.filter(self.AccountInvoice,function (inv) { return moment(inv.date_invoice).format('YYYY-MM')=== moment().format('YYYY-MM'); })); }, showToday: function () { var self = this; var amount = 0; var data = []; var invoice = self.getTodayAccountInvoice(); var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift(); if(invoice.length > 0){ amount = _.reduce(_.map(invoice, function (map) { return map.amount_total; }), function (memo, num) { return memo + num; }); } self.$el.find('.widget-content.widget-loading').css('display','none'); self.$el.find('.widget-content').find('a').text(accounting.formatMoney(amount, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator)) }, showThisWeek: function () { var self = this; var amount = 0; var data = []; var invoice = self.getThisWeekAccountInvoice(); var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift(); if(invoice.length > 0){ amount = _.reduce(_.map(invoice, function (map) { return map.amount_total; }), function (memo, num) { return memo + num; }); } self.$el.find('.widget-content.widget-loading').css('display','none'); self.$el.find('.widget-content').find('a').text(accounting.formatMoney(amount, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator)) }, showThisMonth: function () { var self = this; var amount = 0; var data = []; var invoice = self.getThisMonthAccountInvoice(); var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift(); if(invoice.length > 0){ amount = _.reduce(_.map(invoice, function (map) { return map.amount_total; }), function (memo, num) { return memo + num; }); } self.$el.find('.widget-content.widget-loading').css('display','none'); self.$el.find('.widget-content').find('a').text(accounting.formatMoney(amount, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator)) }, }); }