function monthly_expenses(widget) { "use strict"; var model = openerp; widget.MonthlyExpensesWidget = widget.Base.extend({ template: 'MonthlyExpenses', accountVoucher: [], accountInvoice: [], resCurrecy: [], resCompany: [], events: { 'click a': 'showCustomers', 'click h2': 'showCustomers', }, 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.fetchAccountVoucher().then(function (accountVoucher) { return accountVoucher; }).then(function (accountVoucher) { self.accountVoucher = accountVoucher; return self.fetchAccountInvoice(accountVoucher); }).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.fetchReduceVoucher(); }); }, // voucher pagos a proveedor rango de fecha um mes fetchAccountVoucher: 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', 'amount', 'currency_id', 'payment_rate_currency_id', 'reference']; var domain = [['type', '=', 'payment'],['state', '=', 'posted'],['date', '>=',desde],['date','<',hasta]]; var accountVoucher = new model.web.Model('account.voucher'); accountVoucher.query(fields).filter(domain).all().then(function(results) { defer.resolve(results); }); return defer; }, // invoice -> origin === false fetchAccountInvoice:function(voucher) { var self = this; var defer = $.Deferred(); var number = _.map(voucher,function(map){return map.reference}); var fields = ['id','number','date_invoice','origin']; var domain = [['type', '=', 'in_invoice'],['origin', '=', false ],['number', 'in', number]]; var accountInvoice = new model.web.Model('account.invoice'); accountInvoice.query(fields).filter(domain).all().then(function(results) { defer.resolve(results); }); return defer; }, //Comapnia 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; }, // Moneda 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; }, fetchReduceVoucher: function () { var self = this var voucher = []; var newVoucher = self.getVoucherInvoice(); var itemVoucher; var currencyVoucher; var cat = 0; var company = self.resCompany.shift(); var currencyBase = self.getCurrency(company.currency_id[0]).shift(); for (var i = 0; i < newVoucher.length; i++) { itemVoucher = newVoucher[i]; currencyVoucher = self.getCurrency(itemVoucher.payment_rate_currency_id[0]).shift(); if(!currencyVoucher){ currencyVoucher = {}; currencyVoucher.rate = currencyBase.rate_silent; } voucher.push({ ammount: itemVoucher.amount, amount_rate: (itemVoucher.amount* (currencyBase.rate_silent/currencyVoucher.rate_silent)) }); } if (voucher.length > 0) { cat = _.reduce(_.map(voucher, function (map) { return map.amount_rate; }), 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(cat, currencyBase.symbol, currencyBase.decimal_places, currencyBase.thousands_separator, currencyBase.decimal_separator)) self.$el.find('#morosidad').unblock(); }, // Obtener la moneda getCurrency: function (id) { var self = this; return _.filter(self.resCurrecy,function (item) { return item.id === id; }) }, // Obtener los pagos de las factura sim origin getVoucherInvoice:function() { var self = this; var number = _.map(self.accountInvoice, function(map) { return map.number; }); return _.flatten(_.filter(self.accountVoucher,function(item) { return _.contains(number,item.reference); })); }, // Modal showCustomers: function (e) { var self = this; if (self.accountVoucher.length === 0) { model.web.notification.do_warn("AtenciĆ³n","Sin datos"); return } var hoy = moment().format('YYYY-MM-DD'); var desde = moment().format('YYYY-MM-01'); var hasta = moment().add(1,'months').format('YYYY-MM-01'); var number = _.map(self.accountInvoice,function(map) { return map.number; }); this.do_action({ name: "Listado de gastos del mes", type: 'ir.actions.act_window', res_model: "account.voucher", views: [[false, 'list'],[false,'form']], target: 'new', domain: [['type', '=', 'payment'],['state', '=', 'posted'],['date', '>=',desde],['date','<',hasta],['reference','in',number]], context: {}, flags: { 'form': { 'action_buttons': false, 'options': { 'mode': 'view' } } }, }); } }); }