widget_balance.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. function widget_balance(widget) {
  2. "use strict";
  3. var model = openerp;
  4. widget.WidgetBalanceWidget = widget.Base.extend({
  5. template: 'WidgetBalance',
  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.fetchPosOrder();
  35. }).then(function(PosOrder) {
  36. self.PosOrder = PosOrder;
  37. return self.fetchResCompany();
  38. }).then(function(ResCompany) {
  39. self.ResCompany = ResCompany;
  40. return self.fetchResCurrecy();
  41. }).then(function(ResCurrecy) {
  42. self.ResCurrecy = ResCurrecy;
  43. return self.showThisMonth();
  44. });
  45. },
  46. // Usuario Logeado
  47. fetchCurrentUser: function() {
  48. var self = this;
  49. var ResUser = new model.web.Model('res.users');
  50. return ResUser.call('get_user', {
  51. context: new model.web.CompoundContext()
  52. });
  53. },
  54. // Lista de Graficos disponibles para el usuario
  55. fetchResUser: function(id) {
  56. var self = this;
  57. var defer = $.Deferred();
  58. var fields = ['id','name','chart_ids'];
  59. var domain = [['id','=',id]];
  60. var ResUser = new model.web.Model('res.users');
  61. ResUser.query(fields).filter(domain).all().then(function (results) {
  62. defer.resolve(results);
  63. });
  64. return defer;
  65. },
  66. // Obtener detalles de la lista de graficos
  67. fetchChartList: function(chart_ids) {
  68. var self = this;
  69. var defer = $.Deferred();
  70. var fields = ['id','name'];
  71. var domain = [['id','in',chart_ids]];
  72. var ChartList = new model.web.Model('chart.list');
  73. ChartList.query(fields).filter(domain).all().then(function (results) {
  74. defer.resolve(results);
  75. });
  76. return defer;
  77. },
  78. fetchAccountInvoice: function() {
  79. var self = this;
  80. var defer = $.Deferred();
  81. var fields = ['id', 'name', 'date_invoice', 'amount_total'];
  82. var domain = [['state', 'not in', ['draft','cancel']],['type','=','in_invoice']];
  83. var AccountInvoice = new model.web.Model('account.invoice');
  84. AccountInvoice.query(fields).filter(domain).all().then(function(results) {
  85. defer.resolve(results);
  86. });
  87. return defer;
  88. },
  89. fetchPosOrder: function() {
  90. var self = this;
  91. var defer = $.Deferred();
  92. var fields = ['id', 'name', 'date_order', 'amount_total'];
  93. var domain = [['state', 'not in', ['draft','cancel']]];
  94. var PosOrder = new model.web.Model('pos.order');
  95. PosOrder.query(fields).filter(domain).all().then(function(results) {
  96. defer.resolve(results);
  97. });
  98. return defer;
  99. },
  100. fetchResCompany: function() {
  101. var self = this;
  102. var defer = $.Deferred();
  103. var fields = ['id','name', 'currency_id'];
  104. var domain = [['id', '=', 1]];
  105. var ResCompany = new model.web.Model('res.company');
  106. ResCompany.query(fields).filter(domain).all().then(function (results) {
  107. defer.resolve(results);
  108. });
  109. return defer;
  110. },
  111. fetchResCurrecy : function(){
  112. var self = this;
  113. var defer = $.Deferred();
  114. var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
  115. var domain = [['active', '=', true]];
  116. var ResCurrecy = new model.web.Model('res.currency');
  117. ResCurrecy.query(fields).filter(domain).all().then(function(results) {
  118. defer.resolve(results);
  119. });
  120. return defer;
  121. },
  122. getResCurrency: function (id) {
  123. var self = this;
  124. return _.filter(self.ResCurrecy,function (item) {
  125. return item.id === id;
  126. })
  127. },
  128. // Facturas
  129. getTodayAccountInvoice:function() {
  130. var self = this;
  131. var date = moment().format('YYYY-MM-DD');
  132. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  133. return moment(inv.date_invoice).format('YYYY-MM-DD') === date;
  134. }));
  135. },
  136. getThisWeekAccountInvoice:function() {
  137. var self = this;
  138. var week = moment().week();
  139. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  140. return moment(inv.date_invoice).week() === week & moment(inv.date_invoice).format('YYYY')=== moment().format('YYYY');
  141. }));
  142. },
  143. getThisMonthAccountInvoice:function() {
  144. var self = this;
  145. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  146. return moment(inv.date_invoice).format('YYYY-MM')=== moment().format('YYYY-MM');
  147. }));
  148. },
  149. // POS Orders
  150. getTodayPosOrder:function() {
  151. var self = this;
  152. var date = moment().format('YYYY-MM-DD');
  153. return _.flatten(_.filter(self.PosOrder,function (inv) {
  154. return moment(inv.date_order).format('YYYY-MM-DD') === date;
  155. }));
  156. },
  157. getThisWeekPosOrder:function() {
  158. var self = this;
  159. var week = moment().week();
  160. return _.flatten(_.filter(self.PosOrder,function (inv) {
  161. return moment(inv.date_order).week() === week & moment(inv.date_order).format('YYYY')=== moment().format('YYYY');
  162. }));
  163. },
  164. getThisMonthPosOrder:function() {
  165. var self = this;
  166. return _.flatten(_.filter(self.PosOrder,function (inv) {
  167. return moment(inv.date_order).format('YYYY-MM')=== moment().format('YYYY-MM');
  168. }));
  169. },
  170. showToday: function () {
  171. var self = this;
  172. var amount_order = 0;
  173. var amount_invoice = 0;
  174. var balance = 0;
  175. var data = [];
  176. var order = self.getTodayPosOrder();
  177. var invoice = self.getTodayAccountInvoice();
  178. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  179. if(order.length > 0){
  180. amount_order = _.reduce(_.map(order, function (map) {
  181. return map.amount_total;
  182. }), function (memo, num) {
  183. return memo + num;
  184. });
  185. }
  186. if(invoice.length > 0){
  187. amount_invoice = _.reduce(_.map(invoice, function (map) {
  188. return map.amount_total;
  189. }), function (memo, num) {
  190. return memo + num;
  191. });
  192. }
  193. balance = amount_order - amount_invoice;
  194. self.$el.find('.widget-content.widget-loading').css('display','none');
  195. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(balance, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator))
  196. },
  197. showThisWeek: function () {
  198. var self = this;
  199. var amount_order = 0;
  200. var amount_invoice = 0;
  201. var balance = 0;
  202. var data = [];
  203. var order = self.getThisWeekPosOrder();
  204. var invoice = self.getThisWeekAccountInvoice();
  205. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  206. if(order.length > 0){
  207. amount_order = _.reduce(_.map(order, function (map) {
  208. return map.amount_total;
  209. }), function (memo, num) {
  210. return memo + num;
  211. });
  212. }
  213. if(invoice.length > 0){
  214. amount_invoice = _.reduce(_.map(invoice, function (map) {
  215. return map.amount_total;
  216. }), function (memo, num) {
  217. return memo + num;
  218. });
  219. }
  220. balance = amount_order - amount_invoice;
  221. self.$el.find('.widget-content.widget-loading').css('display','none');
  222. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(balance, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator))
  223. },
  224. showThisMonth: function () {
  225. var self = this;
  226. var amount_order = 0;
  227. var amount_invoice = 0;
  228. var balance = 0;
  229. var data = [];
  230. var order = self.getThisMonthPosOrder();
  231. var invoice = self.getThisMonthAccountInvoice();
  232. var CurrencyBase = self.getResCurrency(self.ResCompany[0].currency_id[0]).shift();
  233. if(order.length > 0){
  234. amount_order = _.reduce(_.map(order, function (map) {
  235. return map.amount_total;
  236. }), function (memo, num) {
  237. return memo + num;
  238. });
  239. }
  240. if(invoice.length > 0){
  241. amount_invoice = _.reduce(_.map(invoice, function (map) {
  242. return map.amount_total;
  243. }), function (memo, num) {
  244. return memo + num;
  245. });
  246. }
  247. balance = amount_order - amount_invoice;
  248. self.$el.find('.widget-content.widget-loading').css('display','none');
  249. self.$el.find('.widget-content').find('a').text(accounting.formatMoney(balance, CurrencyBase.symbol, CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator))
  250. },
  251. });
  252. }