Browse Source

report done

robert2206 8 years ago
parent
commit
0220cd7fab
1 changed files with 91 additions and 30 deletions
  1. 91 30
      static/src/js/main.js

+ 91 - 30
static/src/js/main.js

@@ -2,17 +2,59 @@ openerp.variants_by_company_report = function (instance, local) {
 
     local.ReportWidget = instance.Widget.extend({
         template: 'ReportContainerTemplate',
+        // models: [
+        //     {
+        //         model: 'stock.location',
+        //         fields: ['display_name'],
+        //         domain: [['usage', '=', 'internal']]
+        //     },
+        //     {
+        //         model: 'product.category',
+        //         fields: ['display_name'],
+        //         domain: [['child_id.child_id.type', '=', 'normal'], ['parent_left', '!=', 0]]
+        //     },
+        //     {
+        //         model: 'stock.quant',
+        //         fields: ['product_id'],
+        //         domain: [['child_id.child_id.type', '=', 'normal'], ['parent_left', '!=', 0]]
+        //     },
+        //     {
+        //         model: 'product.product',
+        //         fields: ['name'],
+        //         domain: function (productIds, categoryId) {
+        //             return [['id', 'in', productIds], ['product_tmpl_id.categ_id.id', '=', categoryId], ['active', '=', true]]
+        //         }
+        //     }
+        // ],
         start: function () {
             this.fetchLocations();
             this.fetchCategories();
 
             this.$el.find('#report_form').submit(_.bind(this.submitForm, this));
         },
+        // fetch: function (modelName) {
+        //     var modelData = _.find(this.models, function (item) {
+        //         return item.model === modelName;
+        //     });
+        //
+        //     if (!modelData) {
+        //         return undefined;
+        //     }
+        //
+        //     var loaded = new $.Deferred();
+        //
+        //     var modelInstance = new instance.web.Model(modelData.model);
+        //     modelInstance.query(modelData.fields).filter(modelData.domain).then(function (results) {
+        //              loaded.resolve(results);
+        //     });
+        //
+        //     return loaded;
+        // },
         fetchLocations: function () {
             var self = this;
 
             var StockLocation = new instance.web.Model('stock.location');
-            StockLocation.query(['id', 'name', 'display_name']).filter([['usage', '=', 'internal']]).all().then(function (results) {
+            StockLocation.query(['display_name']).filter([['usage', '=', 'internal']]).all().then(function (results) {
                 _.each(results, function (item) {
                     self.$el.find('#current-location').append('<option value="' + item.id + '">' + item.display_name + '</option>');
                 });
@@ -23,7 +65,9 @@ openerp.variants_by_company_report = function (instance, local) {
             var self = this;
 
             var ProductCategory = new instance.web.Model('product.category');
-            ProductCategory.query().filter([['child_id.child_id.type', '=', 'normal'], ['parent_left', '!=', 0]]).all().then(function (results) {
+            ProductCategory.query(['display_name', 'child_id']).filter([['child_id.child_id.type', '=', 'normal'], ['parent_left', '!=', 0]]).all().then(function (results) {
+                self.categories = results;
+
                 _.each(results, function (item) {
                     self.$el.find('#current-category').append('<option value="' + item.id + '">' + item.display_name + '</option>');
                 });
@@ -32,7 +76,7 @@ openerp.variants_by_company_report = function (instance, local) {
         },
         fetchQuantByLocation: function (locationId, done) {
             var StockQuant = new instance.web.Model('stock.quant');
-            StockQuant.query(['product_id', 'qty', 'inventory_value']).filter([['location_id.id', '=', locationId]]).all().then(function (results) {
+            StockQuant.query(['product_id', 'qty']).filter([['location_id.id', '=', locationId], ['qty', '>', 0]]).all().then(function (results) {
                 done(results);
             });
         },
@@ -68,37 +112,54 @@ openerp.variants_by_company_report = function (instance, local) {
             }
             var self = this;
             this.fetchQuantByLocation(formData.location, function (results) {
-                console.log(results);
+                var productIds = _.map(results, function (item) {
+                    return item.product_id[0];
+                });
+
+                self.fetchProductByIdsAndCategory(productIds, formData.category);
             });
 
             return false;
         },
-        drawPDF: function (data) {
-            // var columns = ['Product', 'Cantidad'];
-            // var pdfDoc = new window.jsPDF();
-            //
-            // pdfDoc.autoTable(columns, _.map(data, function (item) {
-            //     return  _.values(_.pick(item, 'product_id', 'qty'));
-            // }), {
-            //     theme: 'grid', // 'striped', 'grid' or 'plain',
-            //     styles: {
-            //         fontSize: 8,
-            //         overflow: 'linebreak' // visible, hidden, ellipsize or linebreak
-            //     },
-            //     showHeader: 'firstPage', // 'everyPage', 'firstPage', 'never',
-            //     headerStyles: {
-            //         fontStyle: 'bold',
-            //         fillColor: '#000'
-            //     },
-            //     margin: {
-            //         top: 30
-            //     },
-            //     addPageContent: function (data) {
-            //         pdfDoc.text('Reporte de Productos', 15, 20);
-            //     }
-            // });
-            //
-            // pdfDoc.output('dataurlnewwindow');
+        fetchProductByIdsAndCategory: function (productIds, parentCategoryId) {
+            var ProductProduct = new instance.web.Model('product.product');
+
+            var fields = ['factory_reference', 'name'];
+            var domain = [['id', 'in', productIds], ['categ_id.id', 'in', _.find(this.categories, function (item) {
+                return item.id == parentCategoryId;
+            }).child_id], ['active', '=', true]];
+
+            var self = this;
+            ProductProduct.query(fields).filter(domain).all().then(function (results) {
+                self.drawPDF(_.map(results, function (item) {
+                    return _.values(_.pick(item, 'factory_reference', 'name'));
+                }));
+            });
+        },
+        drawPDF: function (rows) {
+            var columns = ['Ref. de Fábrica', 'Producto'];
+            var pdfDoc = new window.jsPDF();
+
+            pdfDoc.autoTable(columns, rows, {
+                theme: 'grid', // 'striped', 'grid' or 'plain',
+                styles: {
+                    fontSize: 8,
+                    overflow: 'linebreak' // visible, hidden, ellipsize or linebreak
+                },
+                showHeader: 'firstPage', // 'everyPage', 'firstPage', 'never',
+                headerStyles: {
+                    fontStyle: 'bold',
+                    fillColor: '#000'
+                },
+                margin: {
+                    top: 30
+                },
+                addPageContent: function (data) {
+                    pdfDoc.text('Reporte de Productos', 15, 20);
+                }
+            });
+
+            pdfDoc.output('dataurlnewwindow');
         }
     });