widget_expense.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. function widget_expense(widget) {
  2. "use strict";
  3. var model = openerp;
  4. widget.WidgetExpenseWidget = widget.Base.extend({
  5. template: 'WidgetExpense',
  6. events: {
  7. 'click .today': 'showToday',
  8. 'click .thisWeek': 'showThisWeek',
  9. 'click .thisMonth': 'showThisMonth',
  10. },
  11. init: function (parent) {
  12. this._super(parent, {
  13. width: 3,
  14. height: 2
  15. });
  16. },
  17. start: function () {
  18. var self = this;
  19. self.fetchInitial();
  20. },
  21. fetchInitial: function(){
  22. var self = this;
  23. self.$el.find('#morosidad').block({
  24. message: null,
  25. overlayCSS: {
  26. backgroundColor: '#FAFAFA'
  27. }
  28. });
  29. self.$el.find('.widget-content.widget-loading').css('display','flex');
  30. self.fetchAccountInvoice().then(function (AccountInvoice) {
  31. return AccountInvoice;
  32. }).then(function (AccountInvoice) {
  33. self.AccountInvoice = AccountInvoice;
  34. return self.fetchResCompany();
  35. }).then(function(ResCompany) {
  36. self.ResCompany = ResCompany;
  37. return self.fetchResCurrecy();
  38. }).then(function(ResCurrecy) {
  39. self.ResCurrecy = ResCurrecy;
  40. return self.showThisMonth();
  41. });
  42. },
  43. // Usuario Logeado
  44. fetchCurrentUser: function() {
  45. var self = this;
  46. var ResUser = new model.web.Model('res.users');
  47. return ResUser.call('get_user', {
  48. context: new model.web.CompoundContext()
  49. });
  50. },
  51. // Lista de Graficos disponibles para el usuario
  52. fetchResUser: function(id) {
  53. var self = this;
  54. var defer = $.Deferred();
  55. var fields = ['id','name','chart_ids'];
  56. var domain = [['id','=',id]];
  57. var ResUser = new model.web.Model('res.users');
  58. ResUser.query(fields).filter(domain).all().then(function (results) {
  59. defer.resolve(results);
  60. });
  61. return defer;
  62. },
  63. // Obtener detalles de la lista de graficos
  64. fetchChartList: function(chart_ids) {
  65. var self = this;
  66. var defer = $.Deferred();
  67. var fields = ['id','name'];
  68. var domain = [['id','in',chart_ids]];
  69. var ChartList = new model.web.Model('chart.list');
  70. ChartList.query(fields).filter(domain).all().then(function (results) {
  71. defer.resolve(results);
  72. });
  73. return defer;
  74. },
  75. fetchAccountInvoice: function() {
  76. var self = this;
  77. var defer = $.Deferred();
  78. var fields = ['id', 'name', 'date_invoice', 'amount_total'];
  79. var domain = [['state', 'not in', ['draft','cancel']],['origin','=',false],['type','=','in_invoice']];
  80. var AccountInvoice = new model.web.Model('account.invoice');
  81. AccountInvoice.query(fields).filter(domain).all().then(function(results) {
  82. defer.resolve(results);
  83. });
  84. return defer;
  85. },
  86. fetchResCompany: function() {
  87. var self = this;
  88. var defer = $.Deferred();
  89. var fields = ['id','name', 'currency_id'];
  90. var domain = [['id', '=', 1]];
  91. var ResCompany = new model.web.Model('res.company');
  92. ResCompany.query(fields).filter(domain).all().then(function (results) {
  93. defer.resolve(results);
  94. });
  95. return defer;
  96. },
  97. fetchResCurrecy : function(){
  98. var self = this;
  99. var defer = $.Deferred();
  100. var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
  101. var domain = [['active', '=', true]];
  102. var ResCurrecy = new model.web.Model('res.currency');
  103. ResCurrecy.query(fields).filter(domain).all().then(function(results) {
  104. defer.resolve(results);
  105. });
  106. return defer;
  107. },
  108. getResCurrency: function (id) {
  109. var self = this;
  110. return _.filter(self.ResCurrecy,function (item) {
  111. return item.id === id;
  112. })
  113. },
  114. getTodayAccountInvoice:function() {
  115. var self = this;
  116. var date = moment().format('YYYY-MM-DD');
  117. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  118. return moment(inv.date_invoice).format('YYYY-MM-DD') === date;
  119. }));
  120. },
  121. getThisWeekAccountInvoice:function() {
  122. var self = this;
  123. var week = moment().week();
  124. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  125. return moment(inv.date_invoice).week() === week & moment(inv.date_invoice).format('YYYY')=== moment().format('YYYY');
  126. }));
  127. },
  128. getThisMonthAccountInvoice:function() {
  129. var self = this;
  130. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  131. return moment(inv.date_invoice).format('YYYY-MM')=== moment().format('YYYY-MM');
  132. }));
  133. },
  134. showToday: function () {
  135. var self = this;
  136. var amount = 0;
  137. var data = [];
  138. var invoice = self.getTodayAccountInvoice();
  139. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  140. if(invoice.length > 0){
  141. amount = _.reduce(_.map(invoice, function (map) {
  142. return map.amount_total;
  143. }), function (memo, num) {
  144. return memo + num;
  145. });
  146. }
  147. self.$el.find('.widget-content.widget-loading').css('display','none');
  148. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(amount, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator))
  149. },
  150. showThisWeek: function () {
  151. var self = this;
  152. var amount = 0;
  153. var data = [];
  154. var invoice = self.getThisWeekAccountInvoice();
  155. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  156. if(invoice.length > 0){
  157. amount = _.reduce(_.map(invoice, function (map) {
  158. return map.amount_total;
  159. }), function (memo, num) {
  160. return memo + num;
  161. });
  162. }
  163. self.$el.find('.widget-content.widget-loading').css('display','none');
  164. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(amount, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator))
  165. },
  166. showThisMonth: function () {
  167. var self = this;
  168. var amount = 0;
  169. var data = [];
  170. var invoice = self.getThisMonthAccountInvoice();
  171. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  172. if(invoice.length > 0){
  173. amount = _.reduce(_.map(invoice, function (map) {
  174. return map.amount_total;
  175. }), function (memo, num) {
  176. return memo + num;
  177. });
  178. }
  179. self.$el.find('.widget-content.widget-loading').css('display','none');
  180. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(amount, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator))
  181. },
  182. });
  183. }