ranking_sales_product.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. function ranking_sales_product (widget) {
  2. "use strict";
  3. var model= openerp;
  4. widget.RankingSalesProductWidget = widget.Base.extend({
  5. template: 'RankingSalesProductTmpl',
  6. data : [],
  7. accountInvoice : [],
  8. invoiceLine : [],
  9. productProdcut : [],
  10. init: function (parent) {
  11. this._super(parent, {
  12. width : 6,
  13. height: 4
  14. });
  15. },
  16. start: function () {
  17. var self = this;
  18. self.fecthInitial();
  19. },
  20. fecthInitial:function(){
  21. var self = this;
  22. self.$el.block({ message: null, overlayCSS: { backgroundColor: '#FAFAFA' }});
  23. self.$el.find('.widget-content.widget-loading').css('display','flex');
  24. self.fecthInvoice().then(function(accountInvoice){
  25. return accountInvoice;
  26. }).then(function(accountInvoice){
  27. self.accountInvoice = accountInvoice;
  28. return self.fecthInvoiceLine(accountInvoice);
  29. }).then(function(invoiceLine){
  30. self.invoiceLine = invoiceLine;
  31. return self.fecthProductProduct(invoiceLine);
  32. }).then(function(productProdcut){
  33. self.productProdcut = productProdcut;
  34. return self.fecthRankingProdcuto();
  35. });
  36. return false;
  37. },
  38. // Obtener factura
  39. fecthInvoice :function(){
  40. var self = this;
  41. var fecha = new Date
  42. var desde =fecha.getFullYear()+"-"+(fecha.getMonth()+1)+"-01";
  43. var hasta =fecha.getFullYear()+"-"+(fecha.getMonth()+2)+"-01";
  44. var defer =$.Deferred();
  45. var fields =['id','invoice_line', 'date_invoice'];
  46. var domain =[['type', '=', 'out_invoice'],['date_invoice', '>=', desde],['date_invoice', '<', hasta],['state', '=',['open','paid']]];
  47. // date_invoice
  48. var invoice = new model.web.Model('account.invoice');
  49. invoice.query(fields).filter(domain).all().then(function(results){
  50. defer.resolve(results);
  51. });
  52. return defer;
  53. },
  54. // Obtener linea de la factura
  55. fecthInvoiceLine : function(accountInvoice){
  56. var self = this;
  57. var defer =$.Deferred();
  58. var invoice_line =_.flatten(_.map(accountInvoice, function(item){return item.invoice_line}));
  59. var fields =['id', 'product_id','quantity'];
  60. var domain = [['id','=', invoice_line]];
  61. var invoiceLine = new model.web.Model('account.invoice.line');
  62. invoiceLine.query(fields).filter(domain).all().then(function(results){
  63. defer.resolve(results);
  64. });
  65. return defer;
  66. },
  67. // Obtener Prodcutos
  68. fecthProductProduct : function(invoiceLine){
  69. var self = this;
  70. var defer =$.Deferred();
  71. var prodcut_id = _.flatten(_.map(invoiceLine,function(item){return item.product_id[0]}));
  72. var fields = ['id', 'name_template', 'type'];
  73. var domain =[['id', '=', prodcut_id]];
  74. var prodcut= new model.web.Model('product.product');
  75. prodcut.query(fields).filter(domain).all().then(function(results){
  76. defer.resolve(results);
  77. });
  78. return defer;
  79. },
  80. fecthRankingProdcuto : function(){
  81. var self = this;
  82. var itemProduct;
  83. var itemLine;
  84. var ranking=[];
  85. var cat=0;
  86. var lineUnik;
  87. for (var i = 0; i < self.productProdcut.length; i++) {
  88. itemProduct = self.productProdcut[i];
  89. itemLine= self.getInvoiceLine(itemProduct.id);
  90. if (itemProduct.type == 'product'){
  91. if (itemLine.length > 0){
  92. cat = _.reduce(_.map(itemLine,function(map){return map.quantity}),function(meno,num){return meno + num},0);
  93. lineUnik=itemLine.shift();
  94. ranking.push({ product :lineUnik.product_id[1],qty:cat });
  95. }
  96. }
  97. }
  98. ranking.sort(function(a,b){return b.qty - a.qty});
  99. self.$el.unblock()
  100. self.$el.find('.widget-content.widget-loading').css('display','none');
  101. var fecha = new Date();
  102. var meses = new Array ("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
  103. self.$el.find('.widget-title').find('span').text("Ranking de Producto mas Vendido Mes de "+meses[fecha.getMonth()]);
  104. self.fecthChart(ranking);
  105. },
  106. // get line
  107. getInvoiceLine(product_id){
  108. var self = this;
  109. return _.flatten(_.filter(self.invoiceLine,function(line){return line.product_id[0] === product_id}));
  110. },
  111. // Generar Grafico
  112. fecthChart: function(ranking){
  113. var self = this;
  114. var label=[];
  115. var body=[];
  116. var item;
  117. var rank=10;
  118. for (var i = 0; i < rank; i++) {
  119. if (ranking[i]){
  120. item = ranking[i];
  121. }else{
  122. item={};
  123. item.product="N/A";
  124. item.qty=0;
  125. }
  126. label.push(item.product);
  127. body.push(item.qty);
  128. }
  129. var chart = new Chart(this.$el.find(".widget-content").find('canvas'), {
  130. // type: 'doughnut',
  131. type: 'horizontalBar',
  132. // type: '',
  133. data: {
  134. labels: label,
  135. datasets: [
  136. {
  137. backgroundColor: ['#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad', '#7c7bad'],
  138. data: body,
  139. }
  140. ]
  141. },
  142. options: {
  143. maintainAspectRatio: false,
  144. layout: {
  145. padding: 20
  146. },
  147. scales: {
  148. xAxes: [{ stacked: true }],
  149. yAxes: [{ stacked: true }],
  150. },
  151. legend: {
  152. display: false,
  153. },
  154. }
  155. });
  156. }
  157. });
  158. }