report_stock.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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'];
  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 : quantity,
  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. });
  176. }
  177. })
  178. self.content = data;
  179. self.loadTable(data);
  180. },
  181. search: function () {
  182. var self = this;
  183. var results = self.ProductProduct;
  184. results = _.map(results, function (item) {
  185. return {
  186. label: item.id + '- '+ ' [ ' + self.valorNull(item.default_code) + ' ] ' + ' - ' + item.name + ' ( ' + self.valorNull(item.ean13) + ' )',
  187. value: item.id + '- '+ ' [ ' + self.valorNull(item.default_code) + ' ] ' + ' - ' + item.name + ' ( ' + self.valorNull(item.ean13) + ' ) '
  188. }
  189. });
  190. self.$('#product').autocomplete({
  191. source: results,
  192. minLength:0,
  193. search: function(event, ui) {
  194. if (!(self.$('#product').val())){
  195. self.factSearch();
  196. }
  197. },
  198. close: function( event, ui ) {
  199. self.factSearch();
  200. },
  201. select: function(event, ui) {
  202. self.factSearch();
  203. }
  204. });
  205. },
  206. factSearch: function(){
  207. var self = this;
  208. var category = this.$el.find('#current-category').val();
  209. var product= this.$el.find('#product').val().split('-');
  210. var content = self.content;
  211. if(category != 9999999){
  212. content = _.filter(content,function(inv){
  213. return inv.category_id == category;
  214. });
  215. }
  216. if (product != ""){
  217. content = _.filter(content, function(inv){
  218. return inv.id == product[0];
  219. });
  220. }
  221. self.loadTable(content)
  222. },
  223. loadTable:function(rowsTable){
  224. var self = this;
  225. self.rowsData = rowsTable;
  226. var table = self.$el.find('#table');
  227. table.bootstrapTable('load' ,rowsTable);
  228. },
  229. getObjectPdf: function(){
  230. var self = this;
  231. var rows=[];
  232. var newStock = self.newStock;
  233. var amount_total = 0;
  234. var qty = 0;
  235. for (var i = 0; i < newStock.length; i++) {
  236. rows.push({
  237. product : newStock[i].product,
  238. qty : newStock[i].qty,
  239. standard_price : newStock[i].standard_price,
  240. lst_price : newStock[i].lst_price,
  241. total: newStock[i].total
  242. });
  243. qty+=parseInt(newStock[i].qty);
  244. amount_total+=parseInt(newStock[i].total.replace(".",""));
  245. }
  246. rows.push({
  247. product : 'Totales',
  248. qty : accounting.formatNumber(qty,0,".",","),
  249. standard_price : '',
  250. lst_price : '',
  251. total: accounting.formatNumber(amount_total,0,".",","),
  252. });
  253. return rows;
  254. },
  255. clickOnAction: function (e) {
  256. var self = this;
  257. var rowsNew;
  258. var action = self.$el.find(e.target).val();
  259. var table = self.$el.find("#table");
  260. var data2 = table.bootstrapTable('getVisibleColumns');
  261. var getColumns=[];
  262. var rows=[];
  263. rowsNew = self.getObjectPdf();
  264. if (action === 'pdf') {
  265. var dataNEW = _.map(data2, function (val){
  266. return val.field;
  267. });
  268. _.each(rowsNew,function (item){
  269. rows.push(_.pick(item, dataNEW));
  270. });
  271. // Obtener los nombre de la Cabezera
  272. _.each(_.map(data2,function(val){
  273. return val;
  274. }), function(item){
  275. getColumns.push([{
  276. title: item.title,
  277. dataKey: item.field
  278. }]);
  279. });
  280. this.drawPDF(_.flatten(getColumns),rows);
  281. }
  282. },
  283. drawPDF:function(getColumns,rows){
  284. var self = this;
  285. var fechaActu= new Date();
  286. var location = this.sucDescrip = this.$el.find('#current-location option:selected').text();
  287. var totalPagesExp = "{total_pages_count_string}";
  288. var pdfDoc = new jsPDF();
  289. pdfDoc.autoTable(getColumns, rows, {
  290. styles: { overflow: 'linebreak', fontSize:8 , columnWidth: 'wrap'},
  291. columnStyles:{
  292. product :{columnWidth: '8px'},
  293. qty : {halign:'center'},
  294. standard_price : {halign:'right'},
  295. lst_price : {halign:'right'},
  296. total : {halign:'right'},
  297. },
  298. margin: { top: 16, horizontal: 7},
  299. addPageContent: function (data) {
  300. pdfDoc.setFontSize(12);
  301. pdfDoc.setFontStyle('bold');
  302. pdfDoc.setTextColor(40);
  303. pdfDoc.text('Listado de productos', data.settings.margin.left, 10);
  304. // FOOTER
  305. var str = "Pagina " + data.pageCount;
  306. if (typeof pdfDoc.putTotalPages === 'function') {
  307. str = str + " de " + totalPagesExp;
  308. // +"\n Día de Expedición "+fechaActu.getDate()+"/"+fechaActu.getMonth()+"/"+fechaActu.getFullYear();
  309. }
  310. pdfDoc.setFontSize(9);
  311. pdfDoc.setFontStyle('bold');
  312. pdfDoc.setTextColor(40);
  313. pdfDoc.text(str, data.settings.margin.left, pdfDoc.internal.pageSize.height - 5);
  314. }
  315. });
  316. if (typeof pdfDoc.putTotalPages === 'function') {
  317. pdfDoc.putTotalPages(totalPagesExp);
  318. }
  319. pdfDoc.save('Listado de productos.pdf');
  320. }
  321. });
  322. }