ranking_sales_product.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. modelId: [],
  12. events: {
  13. // 'click canvas': 'showCustomers'
  14. 'click h2': 'showCustomers'
  15. },
  16. init: function (parent) {
  17. this._super(parent, {
  18. width: 6,
  19. height: 4
  20. });
  21. },
  22. start: function () {
  23. var self = this;
  24. self.fetchInitial();
  25. },
  26. fetchInitial: function() {
  27. var self = this;
  28. self.$el.block({
  29. message: null,
  30. overlayCSS: {
  31. backgroundColor: '#FAFAFA'
  32. }
  33. });
  34. self.$el.find('.widget-content.widget-loading').css('display','flex');
  35. self.fecthInvoice().then(function (accountInvoice) {
  36. return accountInvoice;
  37. }).then(function (accountInvoice) {
  38. self.accountInvoice = accountInvoice;
  39. return self.fecthInvoiceLine(accountInvoice);
  40. }).then(function (invoiceLine) {
  41. self.invoiceLine = invoiceLine;
  42. return self.fecthProductProduct(invoiceLine);
  43. }).then(function(productProduct){
  44. self.productProduct = productProduct;
  45. return self.fetchGetModelId();
  46. }).then(function(modelId){
  47. self.modelId = modelId;
  48. return self.fetchProductRanking();
  49. });
  50. },
  51. // getModelId
  52. fetchGetModelId: function() {
  53. var self = this;
  54. var defer = $.Deferred();
  55. var irModelData = new model.web.Model('ir.model.data');
  56. var getObtjectReference = irModelData.get_func('get_object_reference');
  57. this.alive(getObtjectReference('product', 'product_normal_form_view')).then(function(results) {
  58. defer.resolve(results);
  59. });
  60. return defer;
  61. },
  62. // Obtener factura
  63. fecthInvoice: function() {
  64. var self = this;
  65. var desde = moment().format('YYYY-MM-01');
  66. var hasta = moment().add(1,'months').format('YYYY-MM-01');
  67. var defer = $.Deferred();
  68. var fields = ['id', 'invoice_line', 'date_invoice'];
  69. var domain = [['type', '=', 'out_invoice'], ['date_invoice', '>=', desde], ['date_invoice', '<', hasta], ['state', 'in', ['open','paid']]];
  70. var invoice = new model.web.Model('account.invoice');
  71. invoice.query(fields).filter(domain).all().then(function (results) {
  72. defer.resolve(results);
  73. });
  74. return defer;
  75. },
  76. // Obtener linea de la factura
  77. fecthInvoiceLine: function(accountInvoice) {
  78. var self = this;
  79. var defer = $.Deferred();
  80. var invoice_line = _.flatten(_.map(accountInvoice, function (item) {
  81. return item.invoice_line;
  82. }));
  83. var fields = ['id', 'product_id', 'quantity'];
  84. var domain = [['id','in', invoice_line]];
  85. var invoiceLine = new model.web.Model('account.invoice.line');
  86. invoiceLine.query(fields).filter(domain).all().then(function (results) {
  87. defer.resolve(results);
  88. });
  89. return defer;
  90. },
  91. // Obtener Prodcutos
  92. fecthProductProduct: function (invoiceLine) {
  93. var self = this;
  94. var defer = $.Deferred();
  95. var product_id = _.flatten(_.map(invoiceLine,function (item) {
  96. return item.product_id[0];
  97. }));
  98. var fields = ['id', 'name_template', 'type'];
  99. var domain = [['id', 'in', product_id]];
  100. var product = new model.web.Model('product.product');
  101. product.query(fields).filter(domain).all().then(function (results) {
  102. defer.resolve(results);
  103. });
  104. return defer;
  105. },
  106. fetchProductRanking: function() {
  107. var self = this;
  108. var itemProduct;
  109. var itemLine;
  110. var ranking = [];
  111. var cat = 0;
  112. var lineUnik;
  113. for (var i = 0; i < self.productProduct.length; i++) {
  114. itemProduct = self.productProduct[i];
  115. itemLine = self.getInvoiceLine(itemProduct.id);
  116. // if (itemProduct.type === 'product') {
  117. if (itemLine.length > 0) {
  118. cat = _.reduce(_.map(itemLine, function (map) {
  119. return map.quantity
  120. }), function (meno,num) {
  121. return meno + num
  122. }, 0);
  123. lineUnik = itemLine.shift();
  124. ranking.push({
  125. product: itemProduct.name_template,
  126. qty: cat,
  127. id: lineUnik.product_id[0],
  128. type: itemProduct.type
  129. });
  130. }
  131. // }
  132. }
  133. ranking.sort(function(a, b) {
  134. return b.qty - a.qty
  135. });
  136. self.$el.unblock();
  137. self.$el.find('.widget-content.widget-loading').css('display','none');
  138. var fecha = new Date();
  139. var meses = new Array ("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
  140. self.$el.find('.widget-title').find('span').text("Ranking de productos y servicios del mes de " + meses[fecha.getMonth()]);
  141. // self.$el.find('.widget-title').find('span').text("Ranking de productos más vendidos del mes de " + meses[fecha.getMonth()]);
  142. self.ranking = ranking;
  143. self.fetchChart(ranking);
  144. },
  145. // get line
  146. getInvoiceLine: function (product_id) {
  147. var self = this;
  148. return _.flatten(_.filter(self.invoiceLine,function (line) {
  149. return line.product_id[0] === product_id
  150. }));
  151. },
  152. // Generar Grafico
  153. fetchChart: function (ranking){
  154. var self = this;
  155. var label = [];
  156. var bodyProducto = [];
  157. var labelService = [];
  158. var bodyService = [];
  159. var rank = 7;
  160. var item;
  161. // if (ranking.length < rank && ranking.length > 0)
  162. // rank= ranking.length;
  163. for (var i = 0; i < rank; i++) {
  164. if (ranking[i]){
  165. item = ranking[i];
  166. }else{
  167. item = {};
  168. item.product = "N/A";
  169. item.qty = 0;
  170. }
  171. // if (ranking.length === 0){
  172. // item = {};
  173. // item.product = "N/A";
  174. // item.qty = 0;
  175. // }
  176. var name = item.product.split(' ');
  177. if (name.length === 1) {
  178. label.push(name[0]);
  179. }else{
  180. label.push(name[0]+"("+name[name.length-1]+")");
  181. }
  182. if (item.type === 'product'){
  183. bodyService.push(0);
  184. bodyProducto.push(item.qty);
  185. }
  186. if (item.type === 'service'){
  187. bodyService.push(item.qty);
  188. bodyProducto.push(0);
  189. }
  190. }
  191. var chart = new Chart(this.$el.find(".widget-content").find('canvas'), {
  192. // type: 'line',
  193. type: 'bar',
  194. data: {
  195. labels: label,
  196. datasets: [
  197. {
  198. label: 'Productos ',
  199. borderWidth: 1,
  200. backgroundColor: [ '#EF5350', '#EC407A', '#AB47BC', '#7E57C2', '#5C6BC0', '#42A5F5', '#26C6DA', '#26A69A', '#66BB6A', '#9CCC65'],
  201. data: bodyProducto,
  202. },
  203. {
  204. label: 'Servicios ',
  205. borderWidth: 1,
  206. backgroundColor: [ '#EF5350', '#EC407A', '#AB47BC', '#7E57C2', '#5C6BC0', '#42A5F5', '#26C6DA', '#26A69A', '#66BB6A', '#9CCC65'],
  207. data: bodyService,
  208. }
  209. ]
  210. },
  211. options: {
  212. tooltips: {
  213. mode: 'index',
  214. intersect: true
  215. },
  216. legend: {
  217. labels: {
  218. boxWidth: 10,
  219. fontSize: 12,
  220. },
  221. position: 'left',
  222. },
  223. layout: {
  224. padding: {
  225. top: 20,
  226. bottom: 0,
  227. left: 0,
  228. rigth: 0,
  229. }
  230. },
  231. }
  232. });
  233. },
  234. showCustomers: function (e) {
  235. var self = this;
  236. if (self.ranking.length === 0){
  237. model.web.notification.do_warn("Atención","Sin datos");
  238. return
  239. }
  240. var hoy = moment().format('YYYY-MM-DD');
  241. var product_id = _.map(self.ranking,function(map){
  242. return map.id;
  243. })
  244. this.do_action({
  245. name:"Listado de productos más vendidos",
  246. type: 'ir.actions.act_window',
  247. res_model: "product.product",
  248. views: [[false, 'list'],[self.modelId[1],'form']],
  249. target: 'current',
  250. domain: [['id', 'in', product_id]],
  251. context: {},
  252. });
  253. }
  254. });
  255. }