|
@@ -0,0 +1,207 @@
|
|
|
+function report_stock_product (reporting){
|
|
|
+ "use strict";
|
|
|
+
|
|
|
+ var instance = openerp;
|
|
|
+
|
|
|
+ reporting.ReportStockProductWidget = reporting.Base.extend({
|
|
|
+ template : 'ReportStockProduct',
|
|
|
+ stockLocation : [],
|
|
|
+ stockQuant : [],
|
|
|
+ productProduct : [],
|
|
|
+ prodcut:[],
|
|
|
+
|
|
|
+ events : {
|
|
|
+ 'click #toolbar > button' : 'clickOnAction',
|
|
|
+ },
|
|
|
+ init : function(parent){
|
|
|
+ this._super(parent);
|
|
|
+ },
|
|
|
+ start : function(){
|
|
|
+ var self = this;
|
|
|
+ var dato=[];
|
|
|
+ var table = this.$el.find('#table');
|
|
|
+ table.bootstrapTable({data : self.prodcut});
|
|
|
+ self.fecthInitial();
|
|
|
+ },
|
|
|
+ // Consulta Inicial
|
|
|
+ fecthInitial: function(){
|
|
|
+ var self = this;
|
|
|
+ self.fecthLocation().then(function(stockLocation){
|
|
|
+ self.stockLocation=stockLocation
|
|
|
+ return stockLocation;
|
|
|
+ }).then(function(stockLocation){
|
|
|
+ return self.fecthStockQuant();
|
|
|
+ }).then(function(stockQuant){
|
|
|
+ self.stockQuant = stockQuant;
|
|
|
+ return self.fecthProduct(stockQuant);
|
|
|
+ }).then(function(productProduct){
|
|
|
+ self.productProduct = productProduct;
|
|
|
+ return self.fecthProducto(self.stockQuant, self.stockLocation);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ // Ubicacion
|
|
|
+ fecthLocation : function(){
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var location = new instance.web.Model('stock.location');
|
|
|
+ var fields = ['id', 'name', 'company_id', 'location_id'];
|
|
|
+ var domain =[['active', '=', true],['usage', '=', 'internal']];
|
|
|
+ location.query(fields).filter(domain).order_by('id').all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ })
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ // quant
|
|
|
+ fecthStockQuant : function(){
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var location = _.flatten(_.map(self.stockLocation,function(item){
|
|
|
+ return item.id;
|
|
|
+ }));
|
|
|
+ var company_id =(_.map(self.stockLocation, function(item){
|
|
|
+ return item.company_id[0];
|
|
|
+ })).shift();
|
|
|
+ var quant = new instance.web.Model('stock.quant');
|
|
|
+ var fields = ['id', 'product_id', 'qty', 'cost','location_id'];
|
|
|
+ var domain =[['company_id', '=', company_id],['location_id', 'in',location]];
|
|
|
+ quant.query(fields).filter(domain).all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ })
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ // ProductProduct
|
|
|
+ fecthProduct: function(quant){
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var porductIDS = _.flatten(_.map(quant, function (item) {
|
|
|
+ return item.product_id[0];
|
|
|
+ }));
|
|
|
+ var ProductProdcut = new instance.web.Model('product.product');
|
|
|
+ var fields = ['id','name','name_template', 'standard_price','type','attribute_value_ids', 'lst_price'];
|
|
|
+ ProductProdcut.query(fields).filter([['id', 'in', porductIDS]]).all().then(function (results) {
|
|
|
+ defer.resolve(results)
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ // Union de Modulos
|
|
|
+ fecthProducto : function(stockQuant,stockLocation){
|
|
|
+ var self = this;
|
|
|
+ var stock=[];
|
|
|
+ var itemLocation;
|
|
|
+ var itemProduct;
|
|
|
+ var itemQuant;
|
|
|
+ var productProduct;
|
|
|
+ var cat = 0;
|
|
|
+ var quant;
|
|
|
+
|
|
|
+ productProduct = self.getProdcutoProduct(self.productProduct,stockQuant);
|
|
|
+ for (var f = 0; f < productProduct.length; f++) {
|
|
|
+ itemProduct = productProduct[f];
|
|
|
+ itemQuant = self.getQuantProduct( itemProduct.id, stockQuant);
|
|
|
+ if (itemQuant.length >0){
|
|
|
+ cat = _.reduce(_.map(itemQuant,function(item){
|
|
|
+ return item.qty;
|
|
|
+ }),function(mamo, num){
|
|
|
+ return mamo + num;
|
|
|
+ },0);
|
|
|
+ quant=itemQuant.shift();
|
|
|
+ stock.push({
|
|
|
+ prodcut : quant.product_id[1],
|
|
|
+ qty : cat,
|
|
|
+ standard_price : accounting.formatNumber(itemProduct.standard_price,2, ".", ","),
|
|
|
+ lst_price : accounting.formatNumber(itemProduct.lst_price,2, ".", ","),
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ self.loadTable(stock);
|
|
|
+ },
|
|
|
+ // Obtener Producto Por quant y locations
|
|
|
+ getProdcutoProduct: function(productProduct, stockQuant){
|
|
|
+ var self = this;
|
|
|
+ var product_ids= _.flatten(_.map(stockQuant,function(map){
|
|
|
+ return map.product_id[0];
|
|
|
+ }));
|
|
|
+
|
|
|
+ return _.filter(productProduct,function(prod){return _.contains(product_ids, prod.id)});
|
|
|
+ },
|
|
|
+ // Obtener Qaunt por productos
|
|
|
+ getQuantProduct: function(product_id, quantObjs){
|
|
|
+ var self = this;
|
|
|
+ var quantProdcut = quantObjs;
|
|
|
+ if (product_id){
|
|
|
+ quantProdcut = _.filter(quantProdcut, function(item){
|
|
|
+ return item.product_id[0] === product_id;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return quantProdcut;
|
|
|
+ },
|
|
|
+ // Generar la table
|
|
|
+ loadTable:function(rowsTable){
|
|
|
+ var self = this;
|
|
|
+ this.prodcut=rowsTable;
|
|
|
+ var table = this.$el.find('#table');
|
|
|
+ table.bootstrapTable('load' ,rowsTable);
|
|
|
+ },
|
|
|
+ // Click pdf -Grafic
|
|
|
+ clickOnAction: function (e) {
|
|
|
+ var self = this;
|
|
|
+ var action = self.$el.find(e.target).val();
|
|
|
+ var getColumns=[];
|
|
|
+ var rows=[];
|
|
|
+ var table = self.$el.find("#table");
|
|
|
+ var data2 = table.bootstrapTable('getVisibleColumns');
|
|
|
+ if (action === 'pdf') {
|
|
|
+ var dataNEW = _.map(data2, function (val){
|
|
|
+ return val.field;
|
|
|
+ });
|
|
|
+ _.each(self.prodcut,function (item){
|
|
|
+ rows.push(_.pick(item, dataNEW));
|
|
|
+ });
|
|
|
+ // Obtener los nombre de la Cabezera
|
|
|
+ _.each(_.map(data2,function(val){return val}), function(item){
|
|
|
+ getColumns.push([{
|
|
|
+ title: item.title,
|
|
|
+ dataKey: item.field
|
|
|
+ }]);
|
|
|
+ });
|
|
|
+ self.drawPDF(_.flatten(getColumns),rows)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // pdfDoc
|
|
|
+ drawPDF:function(getColumns,rows){
|
|
|
+ var self = this;
|
|
|
+ var fechaActu= new Date();
|
|
|
+ var location = this.sucDescrip = this.$el.find('#current-location option:selected').text();
|
|
|
+ var totalPagesExp = "{total_pages_count_string}";
|
|
|
+ var pdfDoc = new jsPDF();
|
|
|
+ pdfDoc.autoTable(getColumns, rows, {
|
|
|
+ styles: { overflow: 'linebreak', fontSize:8 , columnWidth: 'wrap'},
|
|
|
+ columnStyles:{
|
|
|
+ prodcut :{columnWidth: '8px'},
|
|
|
+ qty : {halign:'center'},
|
|
|
+ standard_price : {halign:'right'},
|
|
|
+ lst_price : {halign:'right'},
|
|
|
+ },
|
|
|
+ margin: { top: 16, horizontal: 7},
|
|
|
+ addPageContent: function (data) {
|
|
|
+ pdfDoc.setFontSize(12);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
+ pdfDoc.text('Listado de productos', data.settings.margin.left, 10);
|
|
|
+ // FOOTER
|
|
|
+ var str = "Pagina " + data.pageCount;
|
|
|
+ if (typeof pdfDoc.putTotalPages === 'function') {
|
|
|
+ str = str + " de " + totalPagesExp +"\n Día de Expedición "+fechaActu.getDate()+"/"+fechaActu.getMonth()+"/"+fechaActu.getFullYear();
|
|
|
+ }
|
|
|
+ pdfDoc.setFontSize(9);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
+ pdfDoc.text(str, data.settings.margin.left, pdfDoc.internal.pageSize.height - 5);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (typeof pdfDoc.putTotalPages === 'function') {
|
|
|
+ pdfDoc.putTotalPages(totalPagesExp);
|
|
|
+ }
|
|
|
+ pdfDoc.save('Listado de productos de '+location+'.pdf');
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|