ranking_sales_product.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. productProduct : [],
  10. ranking:[],
  11. events: {
  12. 'click canvas': 'showCustomers'
  13. },
  14. init: function (parent) {
  15. this._super(parent, {
  16. width : 6,
  17. height: 4
  18. });
  19. },
  20. start: function () {
  21. var self = this;
  22. self.fetchInitial();
  23. },
  24. fetchInitial:function(){
  25. var self = this;
  26. self.$el.block({
  27. message: null,
  28. overlayCSS: {
  29. backgroundColor: '#FAFAFA'
  30. }
  31. });
  32. self.$el.find('.widget-content.widget-loading').css('display','flex');
  33. self.fecthInvoice().then(function (accountInvoice) {
  34. return accountInvoice;
  35. }).then(function (accountInvoice) {
  36. self.accountInvoice = accountInvoice;
  37. return self.fecthInvoiceLine(accountInvoice);
  38. }).then(function (invoiceLine) {
  39. self.invoiceLine = invoiceLine;
  40. return self.fecthProductProduct(invoiceLine);
  41. }).then(function(productProduct){
  42. self.productProduct = productProduct;
  43. return self.fetchProductRanking();
  44. });
  45. },
  46. // Obtener factura
  47. fecthInvoice: function(){
  48. var self = this;
  49. var desde =moment().format('YYYY-MM-01');
  50. var hasta =moment().add(1,'months').format('YYYY-MM-01');
  51. var defer = $.Deferred();
  52. var fields = ['id', 'invoice_line', 'date_invoice'];
  53. var domain = [['type', '=', 'out_invoice'], ['date_invoice', '>=', desde], ['date_invoice', '<', hasta], ['state', 'in', ['open','paid']]];
  54. var invoice = new model.web.Model('account.invoice');
  55. invoice.query(fields).filter(domain).all().then(function (results) {
  56. defer.resolve(results);
  57. });
  58. return defer;
  59. },
  60. // Obtener linea de la factura
  61. fecthInvoiceLine: function(accountInvoice){
  62. var self = this;
  63. var defer = $.Deferred();
  64. var invoice_line = _.flatten(_.map(accountInvoice, function (item) {
  65. return item.invoice_line
  66. }));
  67. var fields =['id', 'product_id', 'quantity'];
  68. var domain = [['id','in', invoice_line]];
  69. var invoiceLine = new model.web.Model('account.invoice.line');
  70. invoiceLine.query(fields).filter(domain).all().then(function (results) {
  71. defer.resolve(results);
  72. });
  73. return defer;
  74. },
  75. // Obtener Prodcutos
  76. fecthProductProduct: function (invoiceLine) {
  77. var self = this;
  78. var defer = $.Deferred();
  79. var product_id = _.flatten(_.map(invoiceLine,function (item) {
  80. return item.product_id[0]
  81. }));
  82. var fields = ['id', 'name_template', 'type'];
  83. var domain =[['id', 'in', product_id]];
  84. var product= new model.web.Model('product.product');
  85. product.query(fields).filter(domain).all().then(function (results) {
  86. defer.resolve(results);
  87. });
  88. return defer;
  89. },
  90. fetchProductRanking: function(){
  91. var self = this;
  92. var itemProduct;
  93. var itemLine;
  94. var ranking = [];
  95. var cat = 0;
  96. var lineUnik;
  97. for (var i = 0; i < self.productProduct.length; i++) {
  98. itemProduct = self.productProduct[i];
  99. itemLine= self.getInvoiceLine(itemProduct.id);
  100. if (itemProduct.type == 'product'){
  101. if (itemLine.length > 0){
  102. cat = _.reduce(_.map(itemLine, function (map) {
  103. return map.quantity
  104. }), function (meno,num) {
  105. return meno + num
  106. }, 0);
  107. lineUnik = itemLine.shift();
  108. console.log(lineUnik);
  109. ranking.push({
  110. product: lineUnik.product_id[1],
  111. qty: cat,
  112. id: lineUnik.product_id[0]
  113. });
  114. }
  115. }
  116. }
  117. ranking.sort(function (a, b) {
  118. return b.qty - a.qty
  119. });
  120. self.$el.unblock();
  121. self.$el.find('.widget-content.widget-loading').css('display','none');
  122. var fecha = new Date();
  123. var meses = new Array ("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
  124. self.$el.find('.widget-title').find('span').text("Ranking de productos más vendidos del mes de " + meses[fecha.getMonth()]);
  125. // self.$el.find('.widget-title').find('span').text("Ranking de productos más vendidos");
  126. self.ranking=ranking;
  127. self.fetchChart(ranking);
  128. },
  129. // get line
  130. getInvoiceLine: function (product_id){
  131. var self = this;
  132. return _.flatten(_.filter(self.invoiceLine,function (line) {
  133. return line.product_id[0] === product_id
  134. }));
  135. },
  136. // Generar Grafico
  137. fetchChart: function (ranking){
  138. var self = this;
  139. var label = [];
  140. var body = [];
  141. var item;
  142. var rank = 10;
  143. rank= ranking.length;
  144. for (var i = 0; i < rank; i++) {
  145. if (ranking[i]){
  146. item = ranking[i];
  147. }
  148. label.push(item.product);
  149. body.push(item.qty);
  150. }
  151. var chart = new Chart(this.$el.find(".widget-content").find('canvas'), {
  152. // type: 'doughnut',
  153. type: 'pie',
  154. data: {
  155. labels: label,
  156. datasets: [
  157. {
  158. backgroundColor: ['#01579B','#0277BD','#0288D1','#039BE5','#03A9F4','#29B6F6','#4FC3F7','#81D4FA','#B3E5FC','#E1F5FE'],
  159. data: body,
  160. }
  161. ]
  162. },
  163. options: {
  164. animation: {
  165. animateScale: true,
  166. animateRotate: true
  167. },
  168. maintainAspectRatio: false,
  169. layout: {
  170. padding: 20
  171. },
  172. legend: {
  173. display: false,
  174. },
  175. }
  176. });
  177. },
  178. showCustomers: function (e) {
  179. var self = this;
  180. var hoy =moment().format('YYYY-MM-DD');
  181. var product_id= _.map(self.ranking,function(map){
  182. return map.id;
  183. })
  184. this.do_action({
  185. name:"Listado de productos más vendidos",
  186. type: 'ir.actions.act_window',
  187. res_model: "product.product",
  188. views: [[false, 'list']],
  189. target: 'new',
  190. domain: [['id', 'in', product_id],['type','=', 'product']],
  191. context: {},
  192. });
  193. }
  194. });
  195. }