123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- function report_stock (reporting){
- "use strict";
- var instance = openerp;
- reporting.ReportStockWidget = reporting.Base.extend({
- template: 'ReportStock',
- stockLocation: [],
- stockQuant: [],
- productProduct : [],
- rowsData: [],
- content: [],
- newStock: [],
- events : {
- 'change #current-location' : 'factSearch',
- 'change #current-category' : 'factSearch',
- 'click #toolbar > button' : 'clickOnAction',
- 'click-row.bs.table #table ' : 'clickAnalysisDetail',
- },
- init : function(parent){
- this._super(parent);
- },
- start : function(){
- var self = this;
- var dato=[];
- var table = this.$el.find('#table');
- table.bootstrapTable({data : self.rowsData});
- self.submitForm();
- },
- valorNull:function(dato){
- var valor ="";
- if (dato){
- valor=dato;
- }
- return valor;
- },
- clickAnalysisDetail: function(e, row, $element,field){
- if (field == 'product'){
- this.do_action({
- name:"Variantes de Producto",
- type: 'ir.actions.act_window',
- res_model: "product.product",
- views: [[false,'form']],
- target: 'new',
- domain: [['id', '=',row.id ]],
- context: {},
- flags: {'form': {'action_buttons': false, 'options': {'mode': 'view'}}},
- res_id: row.id,
- });
- }
- e.stopImmediatePropagation();
- },
- submitForm: function(){
- var self = this;
- self.fecthStockLocation().then(function(StockLocation){
- self.StockLocation=StockLocation;
- return StockLocation;
- }).then(function(StockLocation){
- return self.fecthStockQuant();
- }).then(function(StockQuant){
- self.StockQuant = StockQuant;
- return self.fecthProductProduct();
- }).then(function(ProductProduct){
- self.ProductProduct = ProductProduct;
- return self.fecthProductCategory();
- }).then(function(ProductCategory){
- self.ProductCategory = ProductCategory;
- self.$el.find('#current-category').append('<option value="9999999">Todos las categorias</option>');
- _.each(ProductCategory, function (item) {
- self.$el.find('#current-category').append('<option value="' + item.id + '">' + item.name + '</option>');
- });
- self.search();
- return self.BuildTable(self.stockQuant, self.stockLocation);
- });
- },
- fecthStockLocation : 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;
- },
- 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;
- },
- fecthProductProduct: function(){
- var self = this;
- var defer = $.Deferred();
- var ids = _.flatten(_.map(self.StockQuant, function (item) {
- return item.product_id[0];
- }));
- var fields = ['id','name','name_template', 'standard_price','type','attribute_value_ids', 'lst_price','ean13','categ_id','attribute_str'];
- var ProductProduct = new instance.web.Model('product.product');
- ProductProduct.query(fields).filter([['id', 'in', ids]]).all().then(function (results) {
- defer.resolve(results)
- });
- return defer;
- },
- fecthProductCategory: function(){
- var self = this;
- var defer = $.Deferred();
- var fields = ['id','name'];
- var ProductCategory = new instance.web.Model('product.category');
- ProductCategory.query(fields).filter([['type', '=', 'normal']]).all().then(function (results) {
- defer.resolve(results)
- });
- return defer;
- },
- getProductProduct: 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)});
- },
- getStockQuant: function(product_id, quantObjs){
- var self = this;
- var quantProduct = quantObjs;
- if (product_id){
- quantProduct = _.filter(quantProduct, function(item){
- return item.product_id[0] == product_id;
- });
- }
- return quantProduct;
- },
- BuildTable : function(stockQuant,stockLocation){
- var self = this;
- var data=[];
- var itemLocation;
- var itemProduct;
- var itemQuant;
- var ProductProduct;
- var product;
- var quantity = 0;
- var total=0;
- ProductProduct = self.getProductProduct(self.ProductProduct, self.StockQuant);
- _.each(ProductProduct, function(item){
- itemProduct = item;
- itemQuant = self.getStockQuant( itemProduct.id, self.StockQuant);
- if (itemQuant.length > 0){
- quantity = _.reduce(_.map(itemQuant,function(item){
- return item.qty;
- }),function(mamo, num){
- return mamo + num;
- },0);
- product = itemQuant.shift();
- total = parseInt(quantity * itemProduct.standard_price);
- data.push({
- id : product.product_id[0],
- product : product.product_id[1],
- ean13 : self.valorNull(itemProduct.ean13),
- category_name : itemProduct.categ_id[1],
- qty : accounting.formatNumber(quantity,2, ".", ","),
- standard_price : accounting.formatNumber(itemProduct.standard_price,0, ".", ","),
- lst_price : accounting.formatNumber(itemProduct.lst_price,0, ".", ","),
- valuation: accounting.formatNumber(total,0,".",","),
- category_id : itemProduct.categ_id[0],
- location_id : product.location_id[0],
- total : total
- });
- }
- })
- self.content = data;
- self.loadTable(data);
- },
- search: function () {
- var self = this;
- var results = self.ProductProduct;
- results = _.map(results, function (item) {
- return {
- label: item.id + '- '+ ' [ ' + self.valorNull(item.default_code) + ' - ' + self.valorNull(item.ean13) + ' ] ' + item.name + ' ( ' + self.valorNull(item.attribute_str) + ' ) ' ,
- value: item.id + '- '+ ' [ ' + self.valorNull(item.default_code) + ' - ' + self.valorNull(item.ean13) + ' ] ' + item.name + ' ( ' + self.valorNull(item.attribute_str) + ' ) '
- }
- });
- self.$('#product').autocomplete({
- source: results,
- minLength:0,
- search: function(event, ui) {
- if (!(self.$('#product').val())){
- self.factSearch();
- }
- },
- close: function( event, ui ) {
- self.factSearch();
- },
- select: function(event, ui) {
- self.factSearch();
- }
- });
- },
- factSearch: function(){
- var self = this;
- var category = this.$el.find('#current-category').val();
- var product= this.$el.find('#product').val().split('-');
- var content = self.content;
- if(category != 9999999){
- content = _.filter(content,function(inv){
- return inv.category_id == category;
- });
- }
- if (product != ""){
- content = _.filter(content, function(inv){
- return inv.id == product[0];
- });
- }
- self.loadTable(content)
- },
- loadTable:function(rowsTable){
- var self = this;
- self.rowsData = rowsTable;
- var table = self.$el.find('#table');
- table.bootstrapTable('load' ,rowsTable);
- },
- getObjectPdf: function(){
- var self = this;
- var rows=[];
- var rows = self.rowsData;
- var total = _.reduce(_.map(rows,function(map){
- return(map.total);
- }),function(memo, num){
- return memo + num;
- },0);
- if (rows.length > 0){
- rows.push({
- product: "Totales ",
- valuation: accounting.formatNumber(total,0,".",","),
- });
- }
- return rows;
- },
- 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.getObjectPdf();
- 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);
- }
- },
- 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:{
- product :{columnWidth: '8px'},
- qty : {halign:'center'},
- standard_price : {halign:'right'},
- lst_price : {halign:'right'},
- valuation : {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.pdf');
- }
- });
- }
|