monthly_expenses.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. function monthly_expenses(widget) {
  2. "use strict";
  3. var model = openerp;
  4. widget.MonthlyExpensesWidget = widget.Base.extend({
  5. template: 'MonthlyExpenses',
  6. accountVoucher: [],
  7. accountInvoice: [],
  8. resCurrecy: [],
  9. resCompany: [],
  10. events: {
  11. 'click a': 'showCustomers',
  12. 'click h2': 'showCustomers',
  13. },
  14. init: function (parent) {
  15. this._super(parent, {
  16. width: 3,
  17. height: 2
  18. });
  19. },
  20. start: function () {
  21. var self = this;
  22. self.fetchInitial();
  23. },
  24. fetchInitial: function(){
  25. var self = this;
  26. self.$el.find('#morosidad').block({
  27. message: null,
  28. overlayCSS: {
  29. backgroundColor: '#FAFAFA'
  30. }
  31. });
  32. self.$el.find('.widget-content.widget-loading').css('display','flex');
  33. self.fetchAccountVoucher().then(function (accountVoucher) {
  34. return accountVoucher;
  35. }).then(function (accountVoucher) {
  36. self.accountVoucher = accountVoucher;
  37. return self.fetchAccountInvoice(accountVoucher);
  38. }).then(function(accountInvoice) {
  39. self.accountInvoice = accountInvoice;
  40. return self.fetchResCompany();
  41. }).then(function(resCompany) {
  42. self.resCompany = resCompany;
  43. return self.fetchResCurrecy();
  44. }).then(function(resCurrecy) {
  45. self.resCurrecy = resCurrecy;
  46. return self.fetchReduceVoucher();
  47. });
  48. },
  49. // voucher pagos a proveedor rango de fecha um mes
  50. fetchAccountVoucher: function() {
  51. var self = this;
  52. var defer = $.Deferred();
  53. var desde = moment().format('YYYY-MM-01');
  54. var hasta = moment().add(1,'months').format('YYYY-MM-01');
  55. var fields = ['id', 'amount', 'currency_id', 'payment_rate_currency_id', 'reference'];
  56. var domain = [['type', '=', 'payment'],['state', '=', 'posted'],['date', '>=',desde],['date','<',hasta]];
  57. var accountVoucher = new model.web.Model('account.voucher');
  58. accountVoucher.query(fields).filter(domain).all().then(function(results) {
  59. defer.resolve(results);
  60. });
  61. return defer;
  62. },
  63. // invoice -> origin === false
  64. fetchAccountInvoice:function(voucher) {
  65. var self = this;
  66. var defer = $.Deferred();
  67. var number = _.map(voucher,function(map){return map.reference});
  68. var fields = ['id','number','date_invoice','origin'];
  69. var domain = [['type', '=', 'in_invoice'],['origin', '=', false ],['number', 'in', number]];
  70. var accountInvoice = new model.web.Model('account.invoice');
  71. accountInvoice.query(fields).filter(domain).all().then(function(results) {
  72. defer.resolve(results);
  73. });
  74. return defer;
  75. },
  76. //Comapnia
  77. fetchResCompany: function() {
  78. var self = this;
  79. var defer = $.Deferred();
  80. var fields = ['id','name', 'currency_id'];
  81. var domain = [['id', '=', 1]];
  82. var resCompany = new model.web.Model('res.company');
  83. resCompany.query(fields).filter(domain).all().then(function (results) {
  84. defer.resolve(results);
  85. });
  86. return defer;
  87. },
  88. // Moneda
  89. fetchResCurrecy : function(){
  90. var self = this;
  91. var defer = $.Deferred();
  92. var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
  93. var domain = [['active', '=', true]];
  94. var resCurrecy = new model.web.Model('res.currency');
  95. resCurrecy.query(fields).filter(domain).all().then(function(results) {
  96. defer.resolve(results);
  97. });
  98. return defer;
  99. },
  100. fetchReduceVoucher: function () {
  101. var self = this
  102. var voucher = [];
  103. var newVoucher = self.getVoucherInvoice();
  104. var itemVoucher;
  105. var currencyVoucher;
  106. var cat = 0;
  107. var company = self.resCompany.shift();
  108. var currencyBase = self.getCurrency(company.currency_id[0]).shift();
  109. for (var i = 0; i < newVoucher.length; i++) {
  110. itemVoucher = newVoucher[i];
  111. currencyVoucher = self.getCurrency(itemVoucher.payment_rate_currency_id[0]).shift();
  112. if(!currencyVoucher){
  113. currencyVoucher = {};
  114. currencyVoucher.rate = currencyBase.rate_silent;
  115. }
  116. voucher.push({
  117. ammount: itemVoucher.amount,
  118. amount_rate: (itemVoucher.amount* (currencyBase.rate_silent/currencyVoucher.rate_silent))
  119. });
  120. }
  121. if (voucher.length > 0) {
  122. cat = _.reduce(_.map(voucher, function (map) {
  123. return map.amount_rate;
  124. }), function (memo, num) {
  125. return memo + num;
  126. });
  127. }
  128. self.$el.find('.widget-content.widget-loading').css('display','none');
  129. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(cat, currencyBase.symbol, currencyBase.decimal_places, currencyBase.thousands_separator, currencyBase.decimal_separator))
  130. self.$el.find('#morosidad').unblock();
  131. },
  132. // Obtener la moneda
  133. getCurrency: function (id) {
  134. var self = this;
  135. return _.filter(self.resCurrecy,function (item) {
  136. return item.id === id;
  137. })
  138. },
  139. // Obtener los pagos de las factura sim origin
  140. getVoucherInvoice:function() {
  141. var self = this;
  142. var number = _.map(self.accountInvoice, function(map) {
  143. return map.number;
  144. });
  145. return _.flatten(_.filter(self.accountVoucher,function(item) {
  146. return _.contains(number,item.reference);
  147. }));
  148. },
  149. // Modal
  150. showCustomers: function (e) {
  151. var self = this;
  152. if (self.accountVoucher.length === 0) {
  153. model.web.notification.do_warn("Atención","Sin datos");
  154. return
  155. }
  156. var hoy = moment().format('YYYY-MM-DD');
  157. var desde = moment().format('YYYY-MM-01');
  158. var hasta = moment().add(1,'months').format('YYYY-MM-01');
  159. var number = _.map(self.accountInvoice,function(map) {
  160. return map.number;
  161. });
  162. this.do_action({
  163. name: "Listado de gastos del mes",
  164. type: 'ir.actions.act_window',
  165. res_model: "account.voucher",
  166. views: [[false, 'list'],[false,'form']],
  167. target: 'new',
  168. domain: [['type', '=', 'payment'],['state', '=', 'posted'],['date', '>=',desde],['date','<',hasta],['reference','in',number]],
  169. context: {},
  170. flags: {
  171. 'form': {
  172. 'action_buttons': false,
  173. 'options': {
  174. 'mode': 'view'
  175. }
  176. }
  177. },
  178. });
  179. }
  180. });
  181. }