chart_purchase_expense.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. function chart_purchase_expense (widget) {
  2. "use strict";
  3. var model = openerp;
  4. widget.ChartPurchaseExpenseWidget = widget.Base.extend({
  5. template: 'ChartPurchaseExpense',
  6. data: [],
  7. events: {
  8. 'click .month': 'showMonth',
  9. 'click .week': 'showWeek',
  10. 'click .days': 'showDays',
  11. },
  12. init: function (parent) {
  13. this._super(parent, {
  14. width: 6,
  15. height: 4
  16. });
  17. },
  18. start: function () {
  19. var self = this;
  20. self.fetchInitial();
  21. },
  22. fetchInitial:function() {
  23. var self = this;
  24. self.$el.block({
  25. message: null,
  26. overlayCSS: {
  27. backgroundColor: '#FAFAFA'
  28. }
  29. });
  30. self.$el.find('.widget-content.widget-loading').css('display','flex');
  31. self.fecthAccountInvoice().then(function (AccountInvoice) {
  32. return AccountInvoice;
  33. }).then(function (AccountInvoice) {
  34. self.AccountInvoice = AccountInvoice;
  35. return self.showWeek();
  36. });
  37. },
  38. // Usuario Logeado
  39. fetchCurrentUser: function() {
  40. var self = this;
  41. var ResUser = new model.web.Model('res.users');
  42. return ResUser.call('get_user', {
  43. context: new model.web.CompoundContext()
  44. });
  45. },
  46. // Lista de Graficos disponibles para el usuario
  47. fetchResUser: function(id) {
  48. var self = this;
  49. var defer = $.Deferred();
  50. var fields = ['id','name','chart_ids'];
  51. var domain = [['id','=',id]];
  52. var ResUser = new model.web.Model('res.users');
  53. ResUser.query(fields).filter(domain).all().then(function (results) {
  54. defer.resolve(results);
  55. });
  56. return defer;
  57. },
  58. // Obtener detalles de la lista de graficos
  59. fetchChartList: function(chart_ids) {
  60. var self = this;
  61. var defer = $.Deferred();
  62. var fields = ['id','name'];
  63. var domain = [['id','in',chart_ids]];
  64. var ChartList = new model.web.Model('chart.list');
  65. ChartList.query(fields).filter(domain).all().then(function (results) {
  66. defer.resolve(results);
  67. });
  68. return defer;
  69. },
  70. fecthAccountInvoice: function() {
  71. var self = this;
  72. var defer = $.Deferred();
  73. var fields = ['id','date_invoice','amount_total','origin'];
  74. var domain = [['state','in',['paid','open']],['type','=','in_invoice']];
  75. var AccountInvoice = new model.web.Model('account.invoice');
  76. AccountInvoice.query(fields).filter(domain).all().then(function (results) {
  77. defer.resolve(results);
  78. });
  79. return defer;
  80. },
  81. // Facturas de proveedor - Meses
  82. getMonthAccountInvoicePurchase:function(month) {
  83. var self = this;
  84. if (month < 10){
  85. var fecha = moment().format('YYYY')+'-'+'0'+month;
  86. }else{
  87. var fecha = moment().format('YYYY')+'-'+month;
  88. }
  89. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  90. return moment(inv.date_invoice).format('YYYY-MM') === fecha & inv.origin !== false;
  91. }));
  92. },
  93. getMonthAccountInvoiceExpense:function(month) {
  94. var self = this;
  95. if (month < 10){
  96. var fecha = moment().format('YYYY')+'-'+'0'+month;
  97. }else{
  98. var fecha = moment().format('YYYY')+'-'+month;
  99. }
  100. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  101. return moment(inv.date_invoice).format('YYYY-MM') === fecha & inv.origin === false;
  102. }));
  103. },
  104. // Facturas de proveedor - Semana Actual
  105. getWeekAccountInvoicePurchase:function(day) {
  106. var self = this;
  107. var week = moment().week();
  108. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  109. return moment(inv.date_invoice).week() === week & inv.origin !== false & moment(inv.date_invoice).isoWeekday() === day;
  110. }));
  111. },
  112. getWeekAccountInvoiceExpense:function(day) {
  113. var self = this;
  114. var week = moment().week();
  115. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  116. return moment(inv.date_invoice).week() === week & inv.origin === false & moment(inv.date_invoice).isoWeekday() === day;
  117. }));
  118. },
  119. // Facturas de proveedor - 15 dias
  120. getDaysAccountInvoicePurchase:function(day) {
  121. var self = this;
  122. var date = moment().subtract(day, 'days').format('YYYY-MM-DD');
  123. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  124. return moment(inv.date_invoice).format('YYYY-MM-DD') === date & inv.origin !== false;
  125. }));
  126. },
  127. getDaysAccountInvoiceExpense:function(day) {
  128. var self = this;
  129. var date = moment().subtract(day, 'days').format('YYYY-MM-DD');
  130. return _.flatten(_.filter(self.AccountInvoice,function (inv) {
  131. return moment(inv.date_invoice).format('YYYY-MM-DD') === date & inv.origin === false;
  132. }));
  133. },
  134. showMonth: function() {
  135. var self = this;
  136. var invoices;
  137. var title = ['Enero', 'Febrero', 'Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'];
  138. var dataPurchase = [];
  139. var dataExpense = [];
  140. for (var i = 1; i <= 12; i++) {
  141. invoices = self.getMonthAccountInvoicePurchase(i);
  142. var total = _.reduce(_.map(invoices,function(item) {
  143. return item.amount_total;
  144. }),function(memo, num) {
  145. return memo + num;
  146. },0);
  147. dataPurchase.push(total)
  148. }
  149. for (var i = 1; i <= 12; i++) {
  150. invoices = self.getMonthAccountInvoiceExpense(i);
  151. var total = _.reduce(_.map(invoices,function(item) {
  152. return item.amount_total;
  153. }),function(memo, num) {
  154. return memo + num;
  155. },0);
  156. dataExpense.push(total)
  157. }
  158. self.$el.unblock();
  159. self.$el.find('.widget-content.widget-loading').css('display','none');
  160. self.fetchChart(dataPurchase, dataExpense, title);
  161. },
  162. showWeek: function() {
  163. var self = this;
  164. var invoices;
  165. var title = ['Lunes','Martes','Miercoles','Jueves','Viernes','Sabado','Domingo'];
  166. var dataPurchase = [];
  167. var dataExpense = [];
  168. for (var i = 1; i <= 7; i++) {
  169. invoices = self.getWeekAccountInvoicePurchase(i);
  170. var total = _.reduce(_.map(invoices,function(item) {
  171. return item.amount_total;
  172. }),function(memo, num) {
  173. return memo + num;
  174. },0);
  175. dataPurchase.push(total);
  176. }
  177. for (var i = 1; i <= 7; i++) {
  178. invoices = self.getWeekAccountInvoiceExpense(i);
  179. var total = _.reduce(_.map(invoices,function(item) {
  180. return item.amount_total;
  181. }),function(memo, num) {
  182. return memo + num;
  183. },0);
  184. dataExpense.push(total);
  185. }
  186. self.$el.unblock();
  187. self.$el.find('.widget-content.widget-loading').css('display','none');
  188. self.fetchChart(dataPurchase, dataExpense, title);
  189. },
  190. showDays: function() {
  191. var self = this;
  192. var title = [];
  193. var invoices;
  194. var dataPurchase = [];
  195. var dataExpense = [];
  196. for (var i = 15; i >= 0; i--) {
  197. invoices = self.getDaysAccountInvoicePurchase(i);
  198. var total = _.reduce(_.map(invoices,function(item) {
  199. return item.amount_total;
  200. }),function(memo, num) {
  201. return memo + num;
  202. },0);
  203. dataPurchase.push(total);
  204. title.push(moment().subtract(i, 'days').format('DD / MM'));
  205. }
  206. for (var i = 15; i >= 0; i--) {
  207. invoices = self.getDaysAccountInvoiceExpense(i);
  208. var total = _.reduce(_.map(invoices,function(item) {
  209. return item.amount_total;
  210. }),function(memo, num) {
  211. return memo + num;
  212. },0);
  213. dataExpense.push(total);
  214. }
  215. self.$el.unblock();
  216. self.$el.find('.widget-content.widget-loading').css('display','none');
  217. self.fetchChart(dataPurchase, dataExpense, title);
  218. },
  219. fetchChart: function (dataPurchase, dataExpense, title) {
  220. var self = this;
  221. var label = title;
  222. var bodyPurchase = dataPurchase;
  223. var bodyExpense = dataExpense;
  224. // Global method for setting Y axis number format.
  225. Chart.scaleService.updateScaleDefaults('linear', {
  226. ticks: {
  227. callback: function(tick) {
  228. return tick.toLocaleString('de-DE');
  229. }
  230. }
  231. });
  232. Chart.defaults.global.tooltips.callbacks.label = function(tooltipItem, data) {
  233. var dataset = data.datasets[tooltipItem.datasetIndex];
  234. var datasetLabel = dataset.label || '';
  235. return datasetLabel + dataset.data[tooltipItem.index].toLocaleString('de-DE');
  236. };
  237. var chart = new Chart(this.$el.find(".widget-content").find('canvas'), {
  238. type: 'line',
  239. data: {
  240. labels: label,
  241. datasets: [
  242. {
  243. label: 'Compras ',
  244. data: bodyPurchase,
  245. backgroundColor: '#e3f2fd',
  246. borderColor: '#64b5f6',
  247. borderWidth: 2,
  248. fill: false,
  249. },
  250. {
  251. label: 'Gastos ',
  252. data: bodyExpense,
  253. backgroundColor: '#c8e6c9',
  254. borderColor: '#66bb6a',
  255. borderWidth: 2,
  256. fill: false,
  257. }
  258. ]
  259. },
  260. options: {
  261. responsive: true,
  262. title: {
  263. display: true,
  264. },
  265. hover: {
  266. mode: 'nearest',
  267. intersect: true
  268. },
  269. layout: {
  270. padding: {
  271. top: 5,
  272. bottom: 20,
  273. left : 0,
  274. rigth: 0,
  275. }
  276. },
  277. }
  278. });
  279. },
  280. });
  281. }