report_stock.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. function report_stock (reporting){
  2. "use strict";
  3. var instance = openerp;
  4. reporting.ReportStockWidget = reporting.Base.extend({
  5. template: 'ReportStock',
  6. stockLocation: [],
  7. stockQuant: [],
  8. productProduct : [],
  9. rowsData: [],
  10. content: [],
  11. newStock: [],
  12. events : {
  13. 'change #current-location' : 'factSearch',
  14. 'change #current-category' : 'factSearch',
  15. 'click #toolbar > button' : 'clickOnAction',
  16. 'click-row.bs.table #table ' : 'clickAnalysisDetail',
  17. },
  18. init : function(parent){
  19. this._super(parent);
  20. },
  21. start : function(){
  22. var self = this;
  23. var dato=[];
  24. var table = this.$el.find('#table');
  25. table.bootstrapTable({data : self.rowsData});
  26. self.submitForm();
  27. },
  28. valorNull:function(dato){
  29. var valor ="";
  30. if (dato){
  31. valor=dato;
  32. }
  33. return valor;
  34. },
  35. clickAnalysisDetail: function(e, row, $element,field){
  36. if (field == 'product'){
  37. this.do_action({
  38. name:"Variantes de Producto",
  39. type: 'ir.actions.act_window',
  40. res_model: "product.product",
  41. views: [[false,'form']],
  42. target: 'new',
  43. domain: [['id', '=',row.id ]],
  44. context: {},
  45. flags: {'form': {'action_buttons': false, 'options': {'mode': 'view'}}},
  46. res_id: row.id,
  47. });
  48. }
  49. e.stopImmediatePropagation();
  50. },
  51. submitForm: function(){
  52. var self = this;
  53. self.fecthStockLocation().then(function(StockLocation){
  54. self.StockLocation=StockLocation;
  55. return StockLocation;
  56. }).then(function(StockLocation){
  57. return self.fecthStockQuant();
  58. }).then(function(StockQuant){
  59. self.StockQuant = StockQuant;
  60. return self.fecthProductProduct();
  61. }).then(function(ProductProduct){
  62. self.ProductProduct = ProductProduct;
  63. return self.fecthProductCategory();
  64. }).then(function(ProductCategory){
  65. self.ProductCategory = ProductCategory;
  66. self.$el.find('#current-category').append('<option value="9999999">Todos las categorias</option>');
  67. _.each(ProductCategory, function (item) {
  68. self.$el.find('#current-category').append('<option value="' + item.id + '">' + item.name + '</option>');
  69. });
  70. self.search();
  71. return self.BuildTable(self.stockQuant, self.stockLocation);
  72. });
  73. },
  74. fecthStockLocation : function(){
  75. var self = this;
  76. var defer = $.Deferred();
  77. var location = new instance.web.Model('stock.location');
  78. var fields = ['id', 'name', 'company_id', 'location_id'];
  79. var domain =[['active', '=', true],['usage', '=', 'internal']];
  80. location.query(fields).filter(domain).order_by('id').all().then(function(results){
  81. defer.resolve(results);
  82. })
  83. return defer;
  84. },
  85. fecthStockQuant : function(){
  86. var self = this;
  87. var defer = $.Deferred();
  88. var location = _.flatten(_.map(self.StockLocation,function(item){
  89. return item.id;
  90. }));
  91. var company_id =(_.map(self.StockLocation, function(item){
  92. return item.company_id[0];
  93. })).shift();
  94. var quant = new instance.web.Model('stock.quant');
  95. var fields = ['id', 'product_id', 'qty', 'cost','location_id'];
  96. var domain =[['company_id', '=', company_id],['location_id', 'in',location]];
  97. quant.query(fields).filter(domain).all().then(function(results){
  98. defer.resolve(results);
  99. })
  100. return defer;
  101. },
  102. fecthProductProduct: function(){
  103. var self = this;
  104. var defer = $.Deferred();
  105. var ids = _.flatten(_.map(self.StockQuant, function (item) {
  106. return item.product_id[0];
  107. }));
  108. var fields = ['id','name','name_template', 'standard_price','type','attribute_value_ids', 'lst_price','ean13','categ_id','attribute_str'];
  109. var ProductProduct = new instance.web.Model('product.product');
  110. ProductProduct.query(fields).filter([['id', 'in', ids]]).all().then(function (results) {
  111. defer.resolve(results)
  112. });
  113. return defer;
  114. },
  115. fecthProductCategory: function(){
  116. var self = this;
  117. var defer = $.Deferred();
  118. var fields = ['id','name'];
  119. var ProductCategory = new instance.web.Model('product.category');
  120. ProductCategory.query(fields).filter([['type', '=', 'normal']]).all().then(function (results) {
  121. defer.resolve(results)
  122. });
  123. return defer;
  124. },
  125. getProductProduct: function(ProductProduct, StockQuant){
  126. var self = this;
  127. var product_ids= _.flatten(_.map(StockQuant,function(map){
  128. return map.product_id[0];
  129. }));
  130. return _.filter(ProductProduct,function(prod){return _.contains(product_ids, prod.id)});
  131. },
  132. getStockQuant: function(product_id, quantObjs){
  133. var self = this;
  134. var quantProduct = quantObjs;
  135. if (product_id){
  136. quantProduct = _.filter(quantProduct, function(item){
  137. return item.product_id[0] == product_id;
  138. });
  139. }
  140. return quantProduct;
  141. },
  142. BuildTable : function(stockQuant,stockLocation){
  143. var self = this;
  144. var data=[];
  145. var itemLocation;
  146. var itemProduct;
  147. var itemQuant;
  148. var ProductProduct;
  149. var product;
  150. var quantity = 0;
  151. var total=0;
  152. ProductProduct = self.getProductProduct(self.ProductProduct, self.StockQuant);
  153. _.each(ProductProduct, function(item){
  154. itemProduct = item;
  155. itemQuant = self.getStockQuant( itemProduct.id, self.StockQuant);
  156. if (itemQuant.length > 0){
  157. quantity = _.reduce(_.map(itemQuant,function(item){
  158. return item.qty;
  159. }),function(mamo, num){
  160. return mamo + num;
  161. },0);
  162. product = itemQuant.shift();
  163. total = parseInt(quantity * itemProduct.standard_price);
  164. data.push({
  165. id : product.product_id[0],
  166. product : product.product_id[1],
  167. ean13 : self.valorNull(itemProduct.ean13),
  168. category_name : itemProduct.categ_id[1],
  169. qty : accounting.formatNumber(quantity,2, ".", ","),
  170. standard_price : accounting.formatNumber(itemProduct.standard_price,0, ".", ","),
  171. lst_price : accounting.formatNumber(itemProduct.lst_price,0, ".", ","),
  172. valuation: accounting.formatNumber(total,0,".",","),
  173. category_id : itemProduct.categ_id[0],
  174. location_id : product.location_id[0],
  175. total : total
  176. });
  177. }
  178. })
  179. self.content = data;
  180. self.loadTable(data);
  181. },
  182. search: function () {
  183. var self = this;
  184. var results = self.ProductProduct;
  185. results = _.map(results, function (item) {
  186. return {
  187. label: item.id + '- '+ ' [ ' + self.valorNull(item.default_code) + ' - ' + self.valorNull(item.ean13) + ' ] ' + item.name + ' ( ' + self.valorNull(item.attribute_str) + ' ) ' ,
  188. value: item.id + '- '+ ' [ ' + self.valorNull(item.default_code) + ' - ' + self.valorNull(item.ean13) + ' ] ' + item.name + ' ( ' + self.valorNull(item.attribute_str) + ' ) '
  189. }
  190. });
  191. self.$('#product').autocomplete({
  192. source: results,
  193. minLength:0,
  194. search: function(event, ui) {
  195. if (!(self.$('#product').val())){
  196. self.factSearch();
  197. }
  198. },
  199. close: function( event, ui ) {
  200. self.factSearch();
  201. },
  202. select: function(event, ui) {
  203. self.factSearch();
  204. }
  205. });
  206. },
  207. factSearch: function(){
  208. var self = this;
  209. var category = this.$el.find('#current-category').val();
  210. var product= this.$el.find('#product').val().split('-');
  211. var content = self.content;
  212. if(category != 9999999){
  213. content = _.filter(content,function(inv){
  214. return inv.category_id == category;
  215. });
  216. }
  217. if (product != ""){
  218. content = _.filter(content, function(inv){
  219. return inv.id == product[0];
  220. });
  221. }
  222. self.loadTable(content)
  223. },
  224. loadTable:function(rowsTable){
  225. var self = this;
  226. self.rowsData = rowsTable;
  227. var table = self.$el.find('#table');
  228. table.bootstrapTable('load' ,rowsTable);
  229. },
  230. getObjectPdf: function(){
  231. var self = this;
  232. var rows=[];
  233. var rows = self.rowsData;
  234. var total = _.reduce(_.map(rows,function(map){
  235. return(map.total);
  236. }),function(memo, num){
  237. return memo + num;
  238. },0);
  239. if (rows.length > 0){
  240. rows.push({
  241. product: "Totales ",
  242. valuation: accounting.formatNumber(total,0,".",","),
  243. });
  244. }
  245. return rows;
  246. },
  247. clickOnAction: function (e) {
  248. var self = this;
  249. var rowsNew;
  250. var action = self.$el.find(e.target).val();
  251. var table = self.$el.find("#table");
  252. var data2 = table.bootstrapTable('getVisibleColumns');
  253. var getColumns=[];
  254. var rows=[];
  255. rowsNew = self.getObjectPdf();
  256. if (action === 'pdf') {
  257. var dataNEW = _.map(data2, function (val){
  258. return val.field;
  259. });
  260. _.each(rowsNew,function (item){
  261. rows.push(_.pick(item, dataNEW));
  262. });
  263. // Obtener los nombre de la Cabezera
  264. _.each(_.map(data2,function(val){
  265. return val;
  266. }), function(item){
  267. getColumns.push([{
  268. title: item.title,
  269. dataKey: item.field
  270. }]);
  271. });
  272. this.drawPDF(_.flatten(getColumns),rows);
  273. }
  274. },
  275. drawPDF:function(getColumns,rows){
  276. var self = this;
  277. var fechaActu= new Date();
  278. var location = this.sucDescrip = this.$el.find('#current-location option:selected').text();
  279. var totalPagesExp = "{total_pages_count_string}";
  280. var pdfDoc = new jsPDF();
  281. pdfDoc.autoTable(getColumns, rows, {
  282. styles: { overflow: 'linebreak', fontSize:8 , columnWidth: 'wrap'},
  283. columnStyles:{
  284. product :{columnWidth: '8px'},
  285. qty : {halign:'center'},
  286. standard_price : {halign:'right'},
  287. lst_price : {halign:'right'},
  288. valuation : {halign:'right'},
  289. },
  290. margin: { top: 16, horizontal: 7},
  291. addPageContent: function (data) {
  292. pdfDoc.setFontSize(12);
  293. pdfDoc.setFontStyle('bold');
  294. pdfDoc.setTextColor(40);
  295. pdfDoc.text('Listado de productos', data.settings.margin.left, 10);
  296. // FOOTER
  297. var str = "Pagina " + data.pageCount;
  298. if (typeof pdfDoc.putTotalPages === 'function') {
  299. str = str + " de " + totalPagesExp;
  300. // +"\n Día de Expedición "+fechaActu.getDate()+"/"+fechaActu.getMonth()+"/"+fechaActu.getFullYear();
  301. }
  302. pdfDoc.setFontSize(9);
  303. pdfDoc.setFontStyle('bold');
  304. pdfDoc.setTextColor(40);
  305. pdfDoc.text(str, data.settings.margin.left, pdfDoc.internal.pageSize.height - 5);
  306. }
  307. });
  308. if (typeof pdfDoc.putTotalPages === 'function') {
  309. pdfDoc.putTotalPages(totalPagesExp);
  310. }
  311. pdfDoc.save('Listado de productos.pdf');
  312. }
  313. });
  314. }