|
@@ -0,0 +1,477 @@
|
|
|
+function report_stock_gral (reporting){
|
|
|
+ "use strict";
|
|
|
+
|
|
|
+ var instance = openerp;
|
|
|
+
|
|
|
+ reporting.ReportStockGralWidget = reporting.Base.extend({
|
|
|
+ template : 'ReportStockGral',
|
|
|
+ stockLocation : [],
|
|
|
+ stockQuant : [],
|
|
|
+ productProduct : [],
|
|
|
+ productTemplate : [],
|
|
|
+ productBrand : [],
|
|
|
+ productCategory : [],
|
|
|
+ product:[],
|
|
|
+ content:[],
|
|
|
+
|
|
|
+ events : {
|
|
|
+ 'click td > button' : 'clickOnAction',
|
|
|
+ 'change #current-location': 'fecthSearch',
|
|
|
+ 'change #current-category' : 'fecthSearch',
|
|
|
+ 'change #current-brand' : 'fecthSearch',
|
|
|
+ },
|
|
|
+ init : function(parent){
|
|
|
+ this._super(parent);
|
|
|
+ },
|
|
|
+ start : function(){
|
|
|
+ var self = this;
|
|
|
+ var dato=[];
|
|
|
+ var table = this.$el.find('#table');
|
|
|
+ table.bootstrapTable({data : self.productProduct});
|
|
|
+ self.fecthInitial();
|
|
|
+ },
|
|
|
+ fecthInitial: function(){
|
|
|
+ var self = this;
|
|
|
+ self.fecthLocation().then(function(stockLocation){
|
|
|
+ self.stockLocation=stockLocation
|
|
|
+ return stockLocation;
|
|
|
+ }).then(function(stockLocation){
|
|
|
+ self.$el.find('#current-location').append('<option value="9999999">Todas las Ubicaciones.</option>');
|
|
|
+ _.each(stockLocation,function(item){
|
|
|
+ self.$el.find('#current-location').append('<option value="' + item.id + '">' + item.location_id[1]+" / "+item.name + '</option>');
|
|
|
+ });
|
|
|
+ return self.fecthStockQuant();
|
|
|
+ }).then(function(stockQuant){
|
|
|
+ self.stockQuant = stockQuant;
|
|
|
+ return self.fecthProduct(stockQuant);
|
|
|
+ }).then(function(productProduct){
|
|
|
+ self.productProduct = productProduct;
|
|
|
+ return self.fecthCategoryChild();
|
|
|
+ }).then(function(categoryChild){
|
|
|
+ self.categoryChild = categoryChild;
|
|
|
+ return self.fecthProductBrand();
|
|
|
+ }).then(function(productBrand){
|
|
|
+ self.productBrand = productBrand;
|
|
|
+ self.$el.find('#current-brand').append('<option value="9999999">Todas las marcas</option>');
|
|
|
+ _.each(productBrand, function (item) {
|
|
|
+ self.$el.find('#current-brand').append('<option value="' + item.id + '">' + item.name + '</option>');
|
|
|
+ });
|
|
|
+ return self.fecthCategory();
|
|
|
+ }).then(function(productCategory){
|
|
|
+ self.productCategory = productCategory;
|
|
|
+ self.$el.find('#current-category').append('<option value="9999999">Todas las cat.</option>');
|
|
|
+ _.each(productCategory, function (item) {
|
|
|
+ self.$el.find('#current-category').append('<option value="' + item.id + '">' + item.name + '</option>');
|
|
|
+ });
|
|
|
+ return self.fecthProducto(self.stockQuant, self.stockLocation, self.productBrand);
|
|
|
+ });
|
|
|
+ },
|
|
|
+ // 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 productIDS = _.flatten(_.map(quant, function (item) {
|
|
|
+ return item.product_id[0];
|
|
|
+ }));
|
|
|
+ var ProductProduct = new instance.web.Model('product.product');
|
|
|
+ var fields = ['id','name', 'standard_price','product_brand_id','categ_id','factory_reference'];
|
|
|
+ ProductProduct.query(fields).filter([['id', 'in', productIDS]]).all().then(function (results) {
|
|
|
+ defer.resolve(results)
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ // Buscar Category
|
|
|
+ fecthCategory: function () {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var productCategory = new instance.web.Model('product.category');
|
|
|
+ productCategory.query(['id', 'name','parent_id','child_id']).filter([['parent_id', '=', 1]]).all().then(function (results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ fecthCategoryChild: function () {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var productCategoryChild = new instance.web.Model('product.category');
|
|
|
+ productCategoryChild.query(['id', 'name','child_id']).filter().all().then(function (results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ // Buscar marca
|
|
|
+ fecthProductBrand: function () {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var productBrand = new instance.web.Model('product.brand');
|
|
|
+ productBrand.query(['id', 'name']).filter().all().then(function (results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ // Verificar si los Valores no son nulos
|
|
|
+ valorNull:function(dato){
|
|
|
+ var valor ="";
|
|
|
+ if (dato){
|
|
|
+ valor=dato;
|
|
|
+ }
|
|
|
+ return valor;
|
|
|
+ },
|
|
|
+ // Union de objetos
|
|
|
+ fecthProducto : function(stockQuant,stockLocation){
|
|
|
+ var self = this;
|
|
|
+ var stock=[];
|
|
|
+ var Catdata=[];
|
|
|
+ var itemLocation;
|
|
|
+ var itemProduct;
|
|
|
+ var itemQuant;
|
|
|
+ var brand = this.$el.find('#current-brand').val();
|
|
|
+ var category = this.$el.find('#current-category').val();
|
|
|
+ var categories = self.categoryChild
|
|
|
+ var prodCategory = self.categoryChild
|
|
|
+ var product = self.productProduct;
|
|
|
+ var productProduct;
|
|
|
+ var categ;
|
|
|
+ var cat = 0;
|
|
|
+ var quant;
|
|
|
+ var valuation;
|
|
|
+ var prod = self.productProduct
|
|
|
+ var product;
|
|
|
+ var cat;
|
|
|
+ var newCategory;
|
|
|
+ var newProduct = [];
|
|
|
+ var otherCategory;
|
|
|
+
|
|
|
+ categories = _.filter(categories, function(item){
|
|
|
+ return item.id == category;
|
|
|
+ });
|
|
|
+ if (category != 9999999) {
|
|
|
+ Catdata.push({
|
|
|
+ id : categories[0].id,
|
|
|
+ name : categories[0].name,
|
|
|
+ child_id : categories[0].child_id
|
|
|
+ });
|
|
|
+ cat = categories[0].child_id
|
|
|
+ for (var i = 0; i < cat.length; i++) {
|
|
|
+ for (var j = 0; j< prodCategory.length; j++) {
|
|
|
+ if(prodCategory[j].id === cat[i]){
|
|
|
+ Catdata.push({
|
|
|
+ id : prodCategory[j].id,
|
|
|
+ name : prodCategory[j].name,
|
|
|
+ child_id : prodCategory[j].child_id
|
|
|
+ });
|
|
|
+ newCategory = prodCategory[j];
|
|
|
+ if (newCategory.child_id.length > 0) {
|
|
|
+ for (var k = 0; k < newCategory.child_id.length; k++) {
|
|
|
+ for (var l = 0; l< prodCategory.length; l++) {
|
|
|
+ if(prodCategory[l].id === newCategory.child_id[k]){
|
|
|
+ Catdata.push({
|
|
|
+ id : prodCategory[l].id,
|
|
|
+ name : prodCategory[l].name,
|
|
|
+ child_id : prodCategory[l].child_id
|
|
|
+ });
|
|
|
+ otherCategory = prodCategory[l];
|
|
|
+ if (prodCategory[l].child_id.length > 0) {
|
|
|
+ for (var m = 0; m < otherCategory.child_id.length; m++) {
|
|
|
+ for (var n = 0; n< prodCategory.length; n++) {
|
|
|
+ if(prodCategory[n].id === otherCategory.child_id[m]){
|
|
|
+ Catdata.push({
|
|
|
+ id : prodCategory[n].id,
|
|
|
+ name : prodCategory[n].name,
|
|
|
+ child_id : prodCategory[n].child_id
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (Catdata.length > 0) {
|
|
|
+ for (var p = 0; p < product.length; p++) {
|
|
|
+ for (var o = 0; o < Catdata.length; o++) {
|
|
|
+ if (product[p].categ_id[0] == Catdata[o].id) {
|
|
|
+ newProduct.push({
|
|
|
+ id: product[p].id,
|
|
|
+ factory_reference : product[p].factory_reference,
|
|
|
+ name: product[p].name,
|
|
|
+ categ_id: product[p].categ_id,
|
|
|
+ product_brand_id: product[p].product_brand_id,
|
|
|
+ standard_price: product[p].standard_price
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ product = newProduct;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ for (var i = 0; i < stockLocation.length; i++) {
|
|
|
+ itemLocation = stockLocation[i];
|
|
|
+ productProduct = self.getProductProduct(product, stockQuant, itemLocation.id);
|
|
|
+ for (var f = 0; f < productProduct.length; f++) {
|
|
|
+ itemProduct = productProduct[f];
|
|
|
+ if(itemProduct.product_brand_id[0] == brand || brand == 9999999){
|
|
|
+ itemQuant = self.getQuantProduct(itemLocation.id, 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();
|
|
|
+ valuation = itemProduct.standard_price * cat
|
|
|
+ stock.push({
|
|
|
+ factory_reference : itemProduct.factory_reference,
|
|
|
+ product : quant.product_id[1],
|
|
|
+ qty : cat,
|
|
|
+ brand_id : itemProduct.product_brand_id[0],
|
|
|
+ brand : itemProduct.product_brand_id[1],
|
|
|
+ category_id : itemProduct.categ_id[0],
|
|
|
+ category : itemProduct.categ_id[1],
|
|
|
+ standard_price : accounting.formatNumber(itemProduct.standard_price,2, ".", ","),
|
|
|
+ valuation : accounting.formatNumber(valuation,2, ".", ","),
|
|
|
+ value : valuation,
|
|
|
+ location :(itemLocation.location_id[1]+" / "+itemLocation.name)
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ self.content = stock;
|
|
|
+ self.loadTable(stock);
|
|
|
+ },
|
|
|
+ // Obtener Producto Por quant y locations
|
|
|
+ getProductProduct: function(productProduct, stockQuant, location_id){
|
|
|
+ var self = this;
|
|
|
+ var product_ids= _.flatten(_.map(_.filter(stockQuant,function(item){
|
|
|
+ return item.location_id[0] === location_id;
|
|
|
+ }),function(map){
|
|
|
+ return map.product_id[0];
|
|
|
+ }));
|
|
|
+ return _.filter(productProduct,function(prod){return _.contains(product_ids, prod.id)});
|
|
|
+ },
|
|
|
+ // // Obtener Qaunt por productos
|
|
|
+ getQuantProduct: function(location_id, product_id, quantObjs){
|
|
|
+ var self = this;
|
|
|
+ var quantProduct = quantObjs;
|
|
|
+ if (location_id){
|
|
|
+ quantProduct = _.filter(quantProduct, function(item){
|
|
|
+ return item.location_id[0] === location_id;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (product_id){
|
|
|
+ quantProduct = _.filter(quantProduct, function(item){
|
|
|
+ return item.product_id[0] === product_id;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ return quantProduct;
|
|
|
+ },
|
|
|
+ getLocation: function (id) {
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.stockLocation,function (item) {
|
|
|
+ return item.id == id;
|
|
|
+ });
|
|
|
+ },
|
|
|
+ getBrand: function (id) {
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.productBrand,function (item) {
|
|
|
+ return item.id == id;
|
|
|
+ });
|
|
|
+ },
|
|
|
+ getCategory: function (id) {
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.productCategory,function (item) {
|
|
|
+ return item.id == id;
|
|
|
+ });
|
|
|
+ },
|
|
|
+ // Buscar
|
|
|
+ fecthSearch: function(){
|
|
|
+ var self = this;
|
|
|
+ var location =this.$el.find('#current-location').val();
|
|
|
+ var locationObjs = self.stockLocation;
|
|
|
+ var quantObjs = self.stockQuant;
|
|
|
+ var quant;
|
|
|
+ if (location != 9999999){
|
|
|
+ locationObjs=_.filter(locationObjs, function (inv){
|
|
|
+ return inv.id == location;
|
|
|
+ });
|
|
|
+ quantObjs=_.filter(quantObjs, function (inv){
|
|
|
+ return inv.location_id[0] == location;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ self.fecthProducto(quantObjs,locationObjs);
|
|
|
+ },
|
|
|
+ // Generar la table
|
|
|
+ loadTable:function(rowsTable){
|
|
|
+ var self = this;
|
|
|
+ this.product=rowsTable;
|
|
|
+ var table = this.$el.find('#table');
|
|
|
+ table.bootstrapTable('load' ,rowsTable);
|
|
|
+ },
|
|
|
+ clickOnAction: function (e) {
|
|
|
+ var self = this;
|
|
|
+ var rowsNew;
|
|
|
+ var action = self.$el.find(e.target).val();
|
|
|
+ var table = self.$el.find("#table");
|
|
|
+ var data2 = table.bootstrapTable('getVisibleColumns');
|
|
|
+ var getColumns=[];
|
|
|
+ var rows=[];
|
|
|
+ rowsNew = self.getObjetPdf();
|
|
|
+ if (action === 'pdf') {
|
|
|
+ var dataNEW = _.map(data2, function (val){
|
|
|
+ return val.field;
|
|
|
+ });
|
|
|
+ _.each(rowsNew,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
|
|
|
+ }]);
|
|
|
+ });
|
|
|
+ this.drawPDF(_.flatten(getColumns),rows);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ getObjetPdf: function(){
|
|
|
+ var self = this;
|
|
|
+ var rows=[];
|
|
|
+ var data = self.content;
|
|
|
+ var value = _.reduce(_.map(data,function(map){
|
|
|
+ return(map.value);
|
|
|
+ }),function(memo, num){
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ var qty = _.reduce(_.map(data,function(map){
|
|
|
+ return(map.qty);
|
|
|
+ }),function(memo, num){
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ data.push({
|
|
|
+ factory_reference : 'Totales',
|
|
|
+ qty : qty,
|
|
|
+ valuation : accounting.formatNumber(value,2,'.',',')
|
|
|
+ });
|
|
|
+
|
|
|
+ // console.log(data);
|
|
|
+ rows = data;
|
|
|
+
|
|
|
+ return rows;
|
|
|
+ },
|
|
|
+ drawPDF: function (getColumns,rows) {
|
|
|
+ var self = this;
|
|
|
+ var brand = this.$el.find('#current-brand').val();
|
|
|
+ var category = this.$el.find('#current-category').val();
|
|
|
+ var location = this.$el.find('#current-location').val();
|
|
|
+ var totalPagesExp = "{total_pages_count_string}";
|
|
|
+ var pdfDoc = new jsPDF();
|
|
|
+
|
|
|
+ pdfDoc.autoTable(getColumns, rows, {
|
|
|
+ styles: { fontSize: 8, cellPadding: 0.5,},
|
|
|
+ columnStyles: {
|
|
|
+ factory_reference : {columnWidth: '5px'},
|
|
|
+ product : {columnWidth: '5px'},
|
|
|
+ brand : {columnWidth: '5px'},
|
|
|
+ qty : {align:'center', columnWidth: '3px'},
|
|
|
+ standard_price : {halign:'right',columnWidth: '5px'},
|
|
|
+ valuation : {halign:'right',columnWidth: '8px'},
|
|
|
+ },
|
|
|
+ margin: { top: 20, horizontal: 7},
|
|
|
+
|
|
|
+ addPageContent: function (data) {
|
|
|
+ pdfDoc.setFontSize(13);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
+ pdfDoc.text('Listado general de stock', data.settings.margin.left, 8);
|
|
|
+
|
|
|
+ if(location != 9999999){
|
|
|
+ var sucursal = self.getLocation(location).shift();
|
|
|
+ sucursal = 'Sucursal: ' + sucursal.location_id[1];
|
|
|
+ }else{
|
|
|
+ sucursal = 'Sucursal: Todas las Sucursales';
|
|
|
+ }
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40)
|
|
|
+ pdfDoc.text(sucursal, data.settings.margin.left,15);
|
|
|
+
|
|
|
+ if(brand != 9999999){
|
|
|
+ var marca = self.getBrand(brand).shift();
|
|
|
+ marca = 'Marca: ' + marca.name;
|
|
|
+ }else{
|
|
|
+ marca = 'Marca: Todas las Marcas';
|
|
|
+ }
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40)
|
|
|
+ pdfDoc.text(marca, 85,15);
|
|
|
+
|
|
|
+ if(category != 9999999){
|
|
|
+ var categoria = self.getCategory(category).shift();
|
|
|
+ categoria = 'Categoria: ' + categoria.name;
|
|
|
+ }else{
|
|
|
+ categoria = 'Categoria: Todas las Categorias';
|
|
|
+ }
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40)
|
|
|
+ pdfDoc.text(categoria, 145,15);
|
|
|
+
|
|
|
+ // FOOTER
|
|
|
+ var str = "Pagina " + data.pageCount;
|
|
|
+ // Total page number plugin only available in jspdf v1.0+
|
|
|
+ if (typeof pdfDoc.putTotalPages === 'function') {
|
|
|
+ str = str + " de " + totalPagesExp;
|
|
|
+ }
|
|
|
+ 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 General de Stock')
|
|
|
+ },
|
|
|
+ });
|
|
|
+}
|