monthly_purchases.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. function monthly_purchases(widget) {
  2. "use strict";
  3. var model = openerp;
  4. widget.MonthlyPurchasesWidget = widget.Base.extend({
  5. template: 'MonthlyPurchases',
  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. // Account Voucher Pagos a facturas Proveedores
  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
  64. fetchAccountInvoice:function(voucher) {
  65. var self = this;
  66. var defer = $.Deferred();
  67. var number = _.map(voucher,function(map) {
  68. return map.reference;
  69. });
  70. var fields = ['id','number','date_invoice','origin'];
  71. var domain = [['type', '=', 'in_invoice'],['origin', '!=', false ],['number', 'in', number]];
  72. var accountInvoice = new model.web.Model('account.invoice');
  73. accountInvoice.query(fields).filter(domain).all().then(function(results) {
  74. defer.resolve(results);
  75. });
  76. return defer;
  77. },
  78. //Comapnia
  79. fetchResCompany: function() {
  80. var self = this;
  81. var defer = $.Deferred();
  82. var fields = ['id','name', 'currency_id', 'logo'];
  83. var domain = [['id', '=', 1]];
  84. var resCompany = new model.web.Model('res.company');
  85. resCompany.query(fields).filter(domain).all().then(function (results) {
  86. defer.resolve(results);
  87. });
  88. return defer;
  89. },
  90. // Res currecy
  91. fetchResCurrecy : function() {
  92. var self = this;
  93. var defer = $.Deferred();
  94. var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
  95. var domain = [['active','=', true]];
  96. var resCurrecy = new model.web.Model('res.currency');
  97. resCurrecy.query(fields).filter(domain).all().then(function(results) {
  98. defer.resolve(results);
  99. });
  100. return defer;
  101. },
  102. fetchReduceVoucher: function () {
  103. var self = this
  104. var voucher = [];
  105. var newVoucher = self.getVoucherInvoice();
  106. var itemVoucher;
  107. var currencyVoucher;
  108. var cat = 0;
  109. var company = self.resCompany.shift();
  110. var currencyBase = self.getCurrency(company.currency_id[0]).shift();
  111. for (var i = 0; i < newVoucher.length; i++) {
  112. itemVoucher = newVoucher[i];
  113. currencyVoucher = self.getCurrency(itemVoucher.payment_rate_currency_id[0]).shift();
  114. if(!currencyVoucher) {
  115. currencyVoucher = {};
  116. currencyVoucher.rate = currencyBase.rate_silent;
  117. }
  118. voucher.push({
  119. ammount: itemVoucher.amount,
  120. amount_rate: (itemVoucher.amount* (currencyBase.rate_silent/currencyVoucher.rate_silent))
  121. });
  122. }
  123. if (voucher.length > 0) {
  124. cat = _.reduce(_.map(voucher, function (map) {
  125. return map.amount_rate;
  126. }), function (memo, num) {
  127. return memo + num;
  128. });
  129. }
  130. self.$el.find('.widget-content.widget-loading').css('display','none');
  131. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(cat, currencyBase.symbol, currencyBase.decimal_places, currencyBase.thousands_separator, currencyBase.decimal_separator));
  132. self.$el.find('#morosidad').unblock();
  133. },
  134. // Obtener la moneda
  135. getCurrency: function (id) {
  136. var self = this;
  137. return _.filter(self.resCurrecy,function (item) {
  138. return item.id === id
  139. })
  140. },
  141. // obtneer los pagos con origin
  142. getVoucherInvoice:function() {
  143. var self = this;
  144. var number = _.map(self.accountInvoice, function(map) {
  145. return map.number;
  146. });
  147. return _.flatten(_.filter(self.accountVoucher,function(item) {
  148. return _.contains(number,item.reference);
  149. }));
  150. },
  151. // modal
  152. showCustomers: function (e) {
  153. var self = this;
  154. if (self.accountInvoice.length === 0) {
  155. model.web.notification.do_warn("Atención","Sin datos");
  156. return
  157. }
  158. var hoy = moment().format('YYYY-MM-DD');
  159. var desde = moment().format('YYYY-MM-01');
  160. var hasta = moment().add(1,'months').format('YYYY-MM-01');
  161. var number = _.map(self.accountInvoice,function(map){return map.number});
  162. this.do_action({
  163. name: "Listado de compras 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. }