widget_expense.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.fetchCurrentUser().then(function (CurrentUser) {
  31. return CurrentUser;
  32. }).then(function (CurrentUser) {
  33. self.CurrentUser = CurrentUser;
  34. return self.fetchResUser(CurrentUser);
  35. }).then(function(ResUser) {
  36. self.ResUser = ResUser;
  37. return self.fetchAccountJournal();
  38. }).then(function(AccountJournal) {
  39. self.AccountJournal = AccountJournal;
  40. return self.fetchAccountInvoice();
  41. }).then(function(AccountInvoice) {
  42. self.AccountInvoice = AccountInvoice;
  43. return self.fetchResCompany();
  44. }).then(function(ResCompany) {
  45. self.ResCompany = ResCompany;
  46. return self.fetchResCurrecy();
  47. }).then(function(ResCurrecy) {
  48. self.ResCurrecy = ResCurrecy;
  49. return self.showThisMonth();
  50. });
  51. },
  52. fetchCurrentUser: function() {
  53. var self = this;
  54. var ResUser = new model.web.Model('res.users');
  55. return ResUser.call('get_user', {
  56. context: new model.web.CompoundContext()
  57. });
  58. },
  59. fetchResUser: function(id) {
  60. var self = this;
  61. var defer = $.Deferred();
  62. var fields = ['id','name','store_id'];
  63. var domain = [['id','=',id]];
  64. var ResUser = new model.web.Model('res.users');
  65. ResUser.query(fields).filter(domain).all().then(function (results) {
  66. defer.resolve(results);
  67. });
  68. return defer;
  69. },
  70. fetchAccountJournal: function() {
  71. var self = this;
  72. var defer = $.Deferred();
  73. var store_ids = self.ResUser[0].store_id[0];
  74. var fields = ['id', 'name', 'store_ids'];
  75. var domain = [['type','=','purchase'],['store_ids','in',store_ids]];
  76. var AccountJournal = new model.web.Model('account.journal');
  77. AccountJournal.query(fields).filter(domain).all().then(function(results) {
  78. defer.resolve(results);
  79. });
  80. return defer;
  81. },
  82. fetchAccountInvoice: function() {
  83. var self = this;
  84. var defer = $.Deferred();
  85. var journal_ids = _.flatten(_.map(this.AccountJournal, function (item) {
  86. return item.id;
  87. }));
  88. var fields = ['id', 'name', 'date_invoice', 'amount_total'];
  89. var domain = [['state', 'not in', ['draft','cancel']],['origin','=',false],['type','=','in_invoice'],['journal_id','in',journal_ids]];
  90. var AccountInvoice = new model.web.Model('account.invoice');
  91. AccountInvoice.query(fields).filter(domain).all().then(function(results) {
  92. defer.resolve(results);
  93. });
  94. return defer;
  95. },
  96. fetchResCompany: function() {
  97. var self = this;
  98. var defer = $.Deferred();
  99. var fields = ['id','name', 'currency_id'];
  100. var domain = [['id', '=', 1]];
  101. var ResCompany = new model.web.Model('res.company');
  102. ResCompany.query(fields).filter(domain).all().then(function (results) {
  103. defer.resolve(results);
  104. });
  105. return defer;
  106. },
  107. fetchResCurrecy : function(){
  108. var self = this;
  109. var defer = $.Deferred();
  110. var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
  111. var domain = [['active', '=', true]];
  112. var ResCurrecy = new model.web.Model('res.currency');
  113. ResCurrecy.query(fields).filter(domain).all().then(function(results) {
  114. defer.resolve(results);
  115. });
  116. return defer;
  117. },
  118. getResCurrency: function (id) {
  119. var self = this;
  120. return _.filter(self.ResCurrecy,function (item) {
  121. return item.id === id;
  122. })
  123. },
  124. getTodayAccountInvoice:function() {
  125. var self = this;
  126. var date = moment().format('YYYY-MM-DD');
  127. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  128. return moment(inv.date_invoice).format('YYYY-MM-DD') === date;
  129. }));
  130. },
  131. getThisWeekAccountInvoice:function() {
  132. var self = this;
  133. var week = moment().week();
  134. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  135. return moment(inv.date_invoice).week() === week & moment(inv.date_invoice).format('YYYY')=== moment().format('YYYY');
  136. }));
  137. },
  138. getThisMonthAccountInvoice:function() {
  139. var self = this;
  140. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  141. return moment(inv.date_invoice).format('YYYY-MM')=== moment().format('YYYY-MM');
  142. }));
  143. },
  144. showToday: function () {
  145. var self = this;
  146. var amount = 0;
  147. var data = [];
  148. var invoice = self.getTodayAccountInvoice();
  149. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  150. if(invoice.length > 0){
  151. amount = _.reduce(_.map(invoice, function (map) {
  152. return map.amount_total;
  153. }), function (memo, num) {
  154. return memo + num;
  155. });
  156. }
  157. self.$el.find('.widget-content.widget-loading').css('display','none');
  158. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(amount, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator))
  159. },
  160. showThisWeek: function () {
  161. var self = this;
  162. var amount = 0;
  163. var data = [];
  164. var invoice = self.getThisWeekAccountInvoice();
  165. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  166. if(invoice.length > 0){
  167. amount = _.reduce(_.map(invoice, function (map) {
  168. return map.amount_total;
  169. }), function (memo, num) {
  170. return memo + num;
  171. });
  172. }
  173. self.$el.find('.widget-content.widget-loading').css('display','none');
  174. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(amount, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator))
  175. },
  176. showThisMonth: function () {
  177. var self = this;
  178. var amount = 0;
  179. var data = [];
  180. var invoice = self.getThisMonthAccountInvoice();
  181. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  182. if(invoice.length > 0){
  183. amount = _.reduce(_.map(invoice, function (map) {
  184. return map.amount_total;
  185. }), function (memo, num) {
  186. return memo + num;
  187. });
  188. }
  189. self.$el.find('.widget-content.widget-loading').css('display','none');
  190. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(amount, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator))
  191. },
  192. });
  193. }