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