|  | @@ -0,0 +1,630 @@
 | 
	
		
			
				|  |  | +function report_mrp_bom_analysis (reporting){
 | 
	
		
			
				|  |  | +    "use strict";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    var model = openerp;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    reporting.ReportMrpAnalysisWidget = reporting.Base.extend({
 | 
	
		
			
				|  |  | +        template: 'ReportMrpAnalysis',
 | 
	
		
			
				|  |  | +        ResMrp: [],
 | 
	
		
			
				|  |  | +        mrpLines: [],
 | 
	
		
			
				|  |  | +        productProduct:[],
 | 
	
		
			
				|  |  | +        modules: ['mrp','eiru_mrp_cost'],
 | 
	
		
			
				|  |  | +        ResCurrency:[],
 | 
	
		
			
				|  |  | +        rowsData :[],
 | 
	
		
			
				|  |  | +        content :[],
 | 
	
		
			
				|  |  | +        rowOrigin:[],
 | 
	
		
			
				|  |  | +        ResCompany:[],
 | 
	
		
			
				|  |  | +        events: {
 | 
	
		
			
				|  |  | +          'click #toolbar > button': 'clickOnAction',
 | 
	
		
			
				|  |  | +          'click #generate': 'fetchGenerate',
 | 
	
		
			
				|  |  | +          'click-row.bs.table #table ': 'clickAnalysisDetail',
 | 
	
		
			
				|  |  | +          'change #current-company' : 'updateSelections',
 | 
	
		
			
				|  |  | +          'change #current-date': 'ShowDateRange',
 | 
	
		
			
				|  |  | +          'change #current-store': 'updateUser',
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +        init : function(parent){
 | 
	
		
			
				|  |  | +          this._super(parent);
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +        start: function () {
 | 
	
		
			
				|  |  | +            var self = this;
 | 
	
		
			
				|  |  | +            var table = this.$el.find('#table');
 | 
	
		
			
				|  |  | +            table.bootstrapTable({data : self.rowsData});
 | 
	
		
			
				|  |  | +            var date = new model.eiru_reports.ReportDatePickerWidget(self);
 | 
	
		
			
				|  |  | +            date.fecthFecha();
 | 
	
		
			
				|  |  | +            this.fetchInitial();
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +        // ckickAnalysisDetail: function(e, row, $element, field){
 | 
	
		
			
				|  |  | +            // if (field == 'name'){
 | 
	
		
			
				|  |  | +            //     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_product]],
 | 
	
		
			
				|  |  | +            //         context : {},
 | 
	
		
			
				|  |  | +            //         flags : {'form': {'action_buttons': false, 'options': {'mode': 'view'}}},
 | 
	
		
			
				|  |  | +            //         res_id : row.id_product,
 | 
	
		
			
				|  |  | +            //     });
 | 
	
		
			
				|  |  | +            // }
 | 
	
		
			
				|  |  | +        //     if (field == 'name'){
 | 
	
		
			
				|  |  | +        //         this.do_action({
 | 
	
		
			
				|  |  | +        //             name : "Producción",
 | 
	
		
			
				|  |  | +        //             type : 'ir.actions.act_window',
 | 
	
		
			
				|  |  | +        //             res_model : "mrp.production",
 | 
	
		
			
				|  |  | +        //             views : [[false,'form']],
 | 
	
		
			
				|  |  | +        //             target : 'new',
 | 
	
		
			
				|  |  | +        //             domain : [['id','=', row.id]],
 | 
	
		
			
				|  |  | +        //             context : {},
 | 
	
		
			
				|  |  | +        //             flags : {'form': {'action_buttons': false, 'options': {'mode': 'view'}}},
 | 
	
		
			
				|  |  | +        //             res_id : row.id,
 | 
	
		
			
				|  |  | +        //         });
 | 
	
		
			
				|  |  | +        //     }
 | 
	
		
			
				|  |  | +        //     e.stopImmediatePropagation();
 | 
	
		
			
				|  |  | +        // },
 | 
	
		
			
				|  |  | +        // Redirecionar
 | 
	
		
			
				|  |  | +        renderReport: function() {
 | 
	
		
			
				|  |  | +          var self = this;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +          var container = this.$el.closest('.oe_form_sheet.oe_form_sheet_width');
 | 
	
		
			
				|  |  | +          this.$el.closest('.report_view').remove();
 | 
	
		
			
				|  |  | +          container.find('.report_view').show({
 | 
	
		
			
				|  |  | +            effect: 'fade',
 | 
	
		
			
				|  |  | +            duration: 200,
 | 
	
		
			
				|  |  | +          });
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Verificar el modelo
 | 
	
		
			
				|  |  | +        checkModel: function(model) {
 | 
	
		
			
				|  |  | +          var self = this;
 | 
	
		
			
				|  |  | +          return _.filter(self.modules, function(item) {
 | 
	
		
			
				|  |  | +            return item.name === model
 | 
	
		
			
				|  |  | +          });
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +        // Lanzar el mensaje
 | 
	
		
			
				|  |  | +        showMensaje: function(modelos) {
 | 
	
		
			
				|  |  | +          var self = this;
 | 
	
		
			
				|  |  | +          $("#dialog").dialog({
 | 
	
		
			
				|  |  | +            autoOpen: true,
 | 
	
		
			
				|  |  | +            resizable: false,
 | 
	
		
			
				|  |  | +            modal: true,
 | 
	
		
			
				|  |  | +            title: 'Atención',
 | 
	
		
			
				|  |  | +            width: 500,
 | 
	
		
			
				|  |  | +            open: function() {
 | 
	
		
			
				|  |  | +              $(this).html('Reporte in-disponible, contacte con el administrador del sistema ref : ' + modelos);
 | 
	
		
			
				|  |  | +            },
 | 
	
		
			
				|  |  | +            show: {
 | 
	
		
			
				|  |  | +              effect: "fade",
 | 
	
		
			
				|  |  | +              duration: 200
 | 
	
		
			
				|  |  | +            },
 | 
	
		
			
				|  |  | +            hide: {
 | 
	
		
			
				|  |  | +              effect: "fade",
 | 
	
		
			
				|  |  | +              duration: 200
 | 
	
		
			
				|  |  | +            },
 | 
	
		
			
				|  |  | +            buttons: {
 | 
	
		
			
				|  |  | +              Aceptar: function() {
 | 
	
		
			
				|  |  | +                $(this).dialog('close');
 | 
	
		
			
				|  |  | +                self.renderReport()
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +          });
 | 
	
		
			
				|  |  | +          return
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        valorNull: function(dato) {
 | 
	
		
			
				|  |  | +          var valor = "";
 | 
	
		
			
				|  |  | +          if (dato) {
 | 
	
		
			
				|  |  | +            valor = dato;
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +          return valor;
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +         // Fecha
 | 
	
		
			
				|  |  | +         ShowDateRange: function() {
 | 
	
		
			
				|  |  | +           var self = this;
 | 
	
		
			
				|  |  | +           var date = self.$el.find('#current-date').val();
 | 
	
		
			
				|  |  | +           if (date == 'range') {
 | 
	
		
			
				|  |  | +             self.$el.find('.datepicker').css('display', 'block');
 | 
	
		
			
				|  |  | +           }
 | 
	
		
			
				|  |  | +           if (date != 'range') {
 | 
	
		
			
				|  |  | +             self.$el.find('.datepicker').css('display', 'none');
 | 
	
		
			
				|  |  | +           }
 | 
	
		
			
				|  |  | +         },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Cansultar
 | 
	
		
			
				|  |  | +        fetchInitial: function () {
 | 
	
		
			
				|  |  | +            var self = this;
 | 
	
		
			
				|  |  | +            self.fecthIrModuleModule().then(function(IrModuleModule) {
 | 
	
		
			
				|  |  | +              return IrModuleModule;
 | 
	
		
			
				|  |  | +            }).then(function(IrModuleModule) {
 | 
	
		
			
				|  |  | +              self.IrModuleModule = IrModuleModule;
 | 
	
		
			
				|  |  | +              return self.fetchResCompany();
 | 
	
		
			
				|  |  | +            }).then(function(ResCompany) {
 | 
	
		
			
				|  |  | +              self.ResCompany = ResCompany;
 | 
	
		
			
				|  |  | +              if (ResCompany.length > 1) {
 | 
	
		
			
				|  |  | +                self.$el.find('#current-company').append('<option value="9999999">Todas las empresas</option>');
 | 
	
		
			
				|  |  | +                _.each(ResCompany, function(item) {
 | 
	
		
			
				|  |  | +                  self.$el.find('#current-company').append('<option value="' + item.id + '">' + item.name + '</option>');
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +              } else {
 | 
	
		
			
				|  |  | +                self.$el.find('.company').css('display', 'none');
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +              self.fecthCheckType();
 | 
	
		
			
				|  |  | +              return self.fetchResStore();
 | 
	
		
			
				|  |  | +            }).then(function(ResStore) {
 | 
	
		
			
				|  |  | +              self.ResStore = ResStore;
 | 
	
		
			
				|  |  | +              if (ResStore.length > 1) {
 | 
	
		
			
				|  |  | +                self.$el.find('#current-store').append('<option value="9999999">Todas las sucursales</option>');
 | 
	
		
			
				|  |  | +                _.each(ResStore, function(item) {
 | 
	
		
			
				|  |  | +                  self.$el.find('#current-store').append('<option value="' + item.id + '">' + item.name + '</option>');
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +              } else {
 | 
	
		
			
				|  |  | +                self.$el.find('.store').css('display', 'none');
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +              return self.fetchResUser();
 | 
	
		
			
				|  |  | +            }).then(function(ResUser) {
 | 
	
		
			
				|  |  | +              self.ResUser = ResUser;
 | 
	
		
			
				|  |  | +              if (ResUser.length > 1) {
 | 
	
		
			
				|  |  | +                self.$el.find('#current-user').append('<option value="9999999">Todos los usuarios</option>');
 | 
	
		
			
				|  |  | +                _.each(ResUser, function(item) {
 | 
	
		
			
				|  |  | +                  self.$el.find('#current-user').append('<option value="' + item.id + '">' + item.name + '</option>');
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +               } else {
 | 
	
		
			
				|  |  | +                self.$el.find('.user').css('display', 'none');
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +              });
 | 
	
		
			
				|  |  | +              self.$el.find('#generate').css('display', 'inline');
 | 
	
		
			
				|  |  | +              return;
 | 
	
		
			
				|  |  | +            },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            fetchGenerate: function() {
 | 
	
		
			
				|  |  | +              var self = this;
 | 
	
		
			
				|  |  | +              self.$el.find('.search-form').block({
 | 
	
		
			
				|  |  | +                message: null,
 | 
	
		
			
				|  |  | +                overlayCSS: {
 | 
	
		
			
				|  |  | +                  backgroundColor: '#FAFAFA'
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +              });
 | 
	
		
			
				|  |  | +              self.$el.find('.report-form').block({
 | 
	
		
			
				|  |  | +                message: null,
 | 
	
		
			
				|  |  | +                overlayCSS: {
 | 
	
		
			
				|  |  | +                  backgroundColor: '#FAFAFA'
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +              });
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +              this.fetchMrp().then(function(ResMrp) {
 | 
	
		
			
				|  |  | +                  return ResMrp;
 | 
	
		
			
				|  |  | +              }).then(function(ResMrp) {
 | 
	
		
			
				|  |  | +                  self.ResMrp =ResMrp;
 | 
	
		
			
				|  |  | +                  return self.fetchMrpLine();
 | 
	
		
			
				|  |  | +              }).then(function (mrpLines) {
 | 
	
		
			
				|  |  | +                  self.mrpLines = mrpLines;
 | 
	
		
			
				|  |  | +                  return self.fetchProductProduct();
 | 
	
		
			
				|  |  | +              }).then(function(ProductProduct){
 | 
	
		
			
				|  |  | +                  self.ProductProduct = ProductProduct;
 | 
	
		
			
				|  |  | +                  return self.BuildTable();
 | 
	
		
			
				|  |  | +              });
 | 
	
		
			
				|  |  | +            },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            fecthIrModuleModule: function() {
 | 
	
		
			
				|  |  | +              var self = this;
 | 
	
		
			
				|  |  | +              var defer = $.Deferred();
 | 
	
		
			
				|  |  | +              var fields = ['name', 'id'];
 | 
	
		
			
				|  |  | +              var domain = [
 | 
	
		
			
				|  |  | +                ['state', '=', 'installed'],
 | 
	
		
			
				|  |  | +                ['name', 'in', self.modules]
 | 
	
		
			
				|  |  | +              ];
 | 
	
		
			
				|  |  | +              var IrModuleModule = new model.web.Model('ir.module.module');
 | 
	
		
			
				|  |  | +              IrModuleModule.query(fields).filter(domain).all().then(function(results) {
 | 
	
		
			
				|  |  | +                defer.resolve(results);
 | 
	
		
			
				|  |  | +              })
 | 
	
		
			
				|  |  | +              return defer;
 | 
	
		
			
				|  |  | +            },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /*=====================================================================
 | 
	
		
			
				|  |  | +            Check type
 | 
	
		
			
				|  |  | +        =====================================================================*/
 | 
	
		
			
				|  |  | +        fecthCheckType: function() {
 | 
	
		
			
				|  |  | +          var self = this;
 | 
	
		
			
				|  |  | +          var modules = self.checkModel('eiru_mrp_cost');
 | 
	
		
			
				|  |  | +          if (modules.length == 0) {
 | 
	
		
			
				|  |  | +            self.$el.find('.type').css('display', 'none');
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /*=====================================================================
 | 
	
		
			
				|  |  | +            USER
 | 
	
		
			
				|  |  | +        =====================================================================*/
 | 
	
		
			
				|  |  | +        fetchResUser: function() {
 | 
	
		
			
				|  |  | +          var self = this;
 | 
	
		
			
				|  |  | +          var defer = $.Deferred();
 | 
	
		
			
				|  |  | +          var fields = ['id', 'name', 'store_id'];
 | 
	
		
			
				|  |  | +          var ResUser = new model.web.Model('res.users');
 | 
	
		
			
				|  |  | +          ResUser.query(fields).filter().all().then(function(results) {
 | 
	
		
			
				|  |  | +            defer.resolve(results);
 | 
	
		
			
				|  |  | +          });
 | 
	
		
			
				|  |  | +          return defer;
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /*====================================================================
 | 
	
		
			
				|  |  | +            RES COMPANY
 | 
	
		
			
				|  |  | +        ====================================================================*/
 | 
	
		
			
				|  |  | +        fetchResCompany: function() {
 | 
	
		
			
				|  |  | +          var self = this;
 | 
	
		
			
				|  |  | +          var defer = $.Deferred();
 | 
	
		
			
				|  |  | +          var currency = new model.web.Model('res.company');
 | 
	
		
			
				|  |  | +          var field = ['id', 'name', 'currency_id', 'logo'];
 | 
	
		
			
				|  |  | +          currency.query(field).filter().all().then(function(results) {
 | 
	
		
			
				|  |  | +            defer.resolve(results);
 | 
	
		
			
				|  |  | +          });
 | 
	
		
			
				|  |  | +          return defer;
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        getResCompany: function(id) {
 | 
	
		
			
				|  |  | +          var self = this;
 | 
	
		
			
				|  |  | +          return _.filter(self.ResCompany, function(item) {
 | 
	
		
			
				|  |  | +            return item.id == id;
 | 
	
		
			
				|  |  | +          })
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        fetchResStore: function(){
 | 
	
		
			
				|  |  | +            var self = this;
 | 
	
		
			
				|  |  | +            var defer = $.Deferred();
 | 
	
		
			
				|  |  | +            var field=['id', 'name'];
 | 
	
		
			
				|  |  | +            var ResStore = new model.web.Model('res.store');
 | 
	
		
			
				|  |  | +            ResStore.query(field).all().then(function(results){
 | 
	
		
			
				|  |  | +                defer.resolve(results);
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            return defer;
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Invoice (FACTURAS)
 | 
	
		
			
				|  |  | +        fetchMrp: function () {
 | 
	
		
			
				|  |  | +            var self = this;
 | 
	
		
			
				|  |  | +            var defer = $.Deferred();
 | 
	
		
			
				|  |  | +            var company = self.$el.find('#current-company').val();
 | 
	
		
			
				|  |  | +            var store = self.$el.find('#current-store').val();
 | 
	
		
			
				|  |  | +            var user = self.$el.find('#current-user').val();
 | 
	
		
			
				|  |  | +            var state = self.$el.find('#current-state').val();
 | 
	
		
			
				|  |  | +            var date = self.$el.find('#current-date').val();
 | 
	
		
			
				|  |  | +            var desde = self.$el.find('#from').val();
 | 
	
		
			
				|  |  | +            var hasta = self.$el.find('#to').val();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            // var filter ="[['state', 'in',['progress','done']]]";
 | 
	
		
			
				|  |  | +            var domain = [
 | 
	
		
			
				|  |  | +            ];
 | 
	
		
			
				|  |  | +            if (company && company != 9999999) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +              domain.push(['company_id', '=', company]);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (store && store != 9999999) {
 | 
	
		
			
				|  |  | +              var user_ids = _.map(_.filter(self.ResUser, function(item) {
 | 
	
		
			
				|  |  | +                return item.store_id[0] == store;
 | 
	
		
			
				|  |  | +              }), function(map) {
 | 
	
		
			
				|  |  | +                return map.id;
 | 
	
		
			
				|  |  | +              });
 | 
	
		
			
				|  |  | +              domain.push(['user_id', 'in', user_ids]);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (user && user != 9999999) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +              domain.push(['user_id', '=', parseInt(user)]);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (state != 9999999) {
 | 
	
		
			
				|  |  | +              domain.push(['state', '=', state]);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (date && date != 9999999) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +              if (desde) {
 | 
	
		
			
				|  |  | +                var date = desde.split('/')
 | 
	
		
			
				|  |  | +                date = (date[2] + "-" + date[1] + "-" + date[0]);
 | 
	
		
			
				|  |  | +                domain.push(['date_planned', '>=', date]);
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +              if (hasta) {
 | 
	
		
			
				|  |  | +                var date = hasta.split('/')
 | 
	
		
			
				|  |  | +                date = (date[2] + "-" + date[1] + "-" + date[0]);
 | 
	
		
			
				|  |  | +                domain.push(['date_planned', '<=', date]);
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +              if (date == 'today') {
 | 
	
		
			
				|  |  | +                var today = moment().format('YYYY-MM-DD');
 | 
	
		
			
				|  |  | +                domain.push(['date_planned', '=', today]);
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +              if (date == 'yesterday') {
 | 
	
		
			
				|  |  | +                var yesterday = moment().add(-1, 'days').format('YYYY-MM-DD');
 | 
	
		
			
				|  |  | +                domain.push(['date_planned', '=', yesterday]);
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +              if (date == 'currentMonth') {
 | 
	
		
			
				|  |  | +                var currentMonth = moment().format('YYYY-MM');
 | 
	
		
			
				|  |  | +                domain.push(['date_planned', 'like', currentMonth]);
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +              if (date == 'lastMonth') {
 | 
	
		
			
				|  |  | +                var lastMonth = moment().add(-1, 'months').format('YYYY-MM');
 | 
	
		
			
				|  |  | +                domain.push(['date_planned', 'like', lastMonth]);
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            var field =['id', 'name', 'company_id', 'product_id', 'state', 'prod_price_unit', 'product_qty', 'product_price', 'move_lines2','date_planned','user_id'];
 | 
	
		
			
				|  |  | +            var defer = $.Deferred();
 | 
	
		
			
				|  |  | +            var ResMrp = new model.web.Model('mrp.production');
 | 
	
		
			
				|  |  | +            ResMrp.query(field).filter(domain).all().then(function (results) {
 | 
	
		
			
				|  |  | +                defer.resolve(results);
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            return defer;
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Invoice line (Linea de Factura)
 | 
	
		
			
				|  |  | +        fetchMrpLine: function () {
 | 
	
		
			
				|  |  | +            var self = this;
 | 
	
		
			
				|  |  | +            var defer = $.Deferred();
 | 
	
		
			
				|  |  | +            var linesIds = _.flatten(_.map(self.ResMrp, function (item) {
 | 
	
		
			
				|  |  | +                return item.move_lines2;
 | 
	
		
			
				|  |  | +            }));
 | 
	
		
			
				|  |  | +            var mrpLines = new model.web.Model('stock.move');
 | 
	
		
			
				|  |  | +            mrpLines.query(['id', 'product_id', 'product_qty', 'price_unit',  'name', 'origin','raw_material_production_id','production_id']).filter([['id', 'in', linesIds]]).all().then(function (results) {
 | 
	
		
			
				|  |  | +                defer.resolve(results)
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            return defer;
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Product Product
 | 
	
		
			
				|  |  | +        fetchProductProduct: function(){
 | 
	
		
			
				|  |  | +            var self = this;
 | 
	
		
			
				|  |  | +            var defer = $.Deferred();
 | 
	
		
			
				|  |  | +            var product_ids = _.flatten(_.map(self.mrpLines, function (item) {
 | 
	
		
			
				|  |  | +                return item.product_id[0];
 | 
	
		
			
				|  |  | +            }));
 | 
	
		
			
				|  |  | +            var domain = [['id','in',product_ids]];
 | 
	
		
			
				|  |  | +            var ProductProduct = new model.web.Model('product.product');
 | 
	
		
			
				|  |  | +            ProductProduct.query(['id','name','name_template', 'standard_price','type','attribute_value_ids', 'lst_price','ean13','categ_id','attribute_str']).filter(domain).all().then(function (results) {
 | 
	
		
			
				|  |  | +                defer.resolve(results)
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            return defer;
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Obtener la Detalles de la Factura
 | 
	
		
			
				|  |  | +        getInvoice: function (id_line){
 | 
	
		
			
				|  |  | +            var self = this;
 | 
	
		
			
				|  |  | +            return _.find(this.ResMrp, function (inv) {
 | 
	
		
			
				|  |  | +                return _.contains(inv.move_lines2, id_line);
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // Obtener las lineas de las Facturas
 | 
	
		
			
				|  |  | +        getProductProduct: function(pro_id){
 | 
	
		
			
				|  |  | +            var self = this;
 | 
	
		
			
				|  |  | +            return _.find(self.ProductProduct, function(item){
 | 
	
		
			
				|  |  | +                return _.contains(pro_id, item.id);
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        BuildTable: function(){
 | 
	
		
			
				|  |  | +            var self = this;
 | 
	
		
			
				|  |  | +            var data=[];
 | 
	
		
			
				|  |  | +            var item;
 | 
	
		
			
				|  |  | +            var mrp=[];
 | 
	
		
			
				|  |  | +            var product;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            for (var i = 0; i < this.mrpLines.length; i++) {
 | 
	
		
			
				|  |  | +                item = this.mrpLines[i];
 | 
	
		
			
				|  |  | +                mrp = this.getInvoice(item.id);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                // _.each(ResMrp, function(item) {
 | 
	
		
			
				|  |  | +                    if (mrp.state == 'confirmed') {
 | 
	
		
			
				|  |  | +                      var state = 'Confirmado';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    if (mrp.state == 'planned') {
 | 
	
		
			
				|  |  | +                      var state = 'Planeado';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    if (mrp.state == 'progress') {
 | 
	
		
			
				|  |  | +                      var state = 'En progreso';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    if (mrp.state == 'done') {
 | 
	
		
			
				|  |  | +                      var state = 'Realizado';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    if (mrp.state == 'cancel') {
 | 
	
		
			
				|  |  | +                      var state = 'Cancelado';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                        product =this.getProductProduct(item.product_id);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                        if (product != undefined){
 | 
	
		
			
				|  |  | +                            // var store_id = self.getAccountJournal(mrp.journal_id[0]);
 | 
	
		
			
				|  |  | +                            // if(store_id.length > 0){
 | 
	
		
			
				|  |  | +                                data.push({
 | 
	
		
			
				|  |  | +                                    id : mrp.id,
 | 
	
		
			
				|  |  | +                                    name : (mrp.name),
 | 
	
		
			
				|  |  | +                                    product_id : item.product_id[0],
 | 
	
		
			
				|  |  | +                                    product_name : item.product_id[1],
 | 
	
		
			
				|  |  | +                                    product_qty : accounting.formatNumber((item.product_qty),2, ".", ","),
 | 
	
		
			
				|  |  | +                                    price_unit : accounting.formatNumber((item.price_unit),0, ".", ","),
 | 
	
		
			
				|  |  | +                                    price_tot : accounting.formatNumber((item.product_qty * (item.price_unit)),2, ".", ","),
 | 
	
		
			
				|  |  | +                                    quantity_tot : (item.product_qty),
 | 
	
		
			
				|  |  | +                                    price_tot_tot : ((item.product_qty* (item.price_unit))),
 | 
	
		
			
				|  |  | +                                    date_planned :moment(mrp.date_planned).format("DD/MM/YYYY"),
 | 
	
		
			
				|  |  | +                                    date_invoice : (mrp.date_planned),
 | 
	
		
			
				|  |  | +                                    user_id : self.valorNull(mrp.user_id[0]),
 | 
	
		
			
				|  |  | +                                    user : mrp.user_id[1],
 | 
	
		
			
				|  |  | +                                    state: state,
 | 
	
		
			
				|  |  | +                                });
 | 
	
		
			
				|  |  | +                            // }
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +          self.content = data;
 | 
	
		
			
				|  |  | +          self.loadTable(data);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +          self.$el.find('.report-form').css('display', 'block');
 | 
	
		
			
				|  |  | +          self.$el.find('.search-form').unblock();
 | 
	
		
			
				|  |  | +          self.$el.find('.report-form').unblock();
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        loadTable: function(rowsTable) {
 | 
	
		
			
				|  |  | +          var self = this;
 | 
	
		
			
				|  |  | +          self.rowsData = rowsTable;
 | 
	
		
			
				|  |  | +          var table = this.$el.find('#table');
 | 
	
		
			
				|  |  | +          table.bootstrapTable('load', rowsTable);
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        clickOnAction: function(e) {
 | 
	
		
			
				|  |  | +          var self = this;
 | 
	
		
			
				|  |  | +          var action = this.$el.find(e.target).val();
 | 
	
		
			
				|  |  | +          var company = $('#current-company').val();
 | 
	
		
			
				|  |  | +          if (company && company != 9999999) {
 | 
	
		
			
				|  |  | +            var ResCompany = self.getResCompany(company).shift();
 | 
	
		
			
				|  |  | +          } else {
 | 
	
		
			
				|  |  | +            var ResCompany = self.ResCompany[0];
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +          var getColumns = [];
 | 
	
		
			
				|  |  | +          var rows = [];
 | 
	
		
			
				|  |  | +          var table = this.$el.find("#table");
 | 
	
		
			
				|  |  | +          var column = table.bootstrapTable('getVisibleColumns');
 | 
	
		
			
				|  |  | +          var row = table.bootstrapTable('getData');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +          var product_qty = QuantityFooter(row);
 | 
	
		
			
				|  |  | +          var price_tot = TotalFooter(row);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +         row.push({
 | 
	
		
			
				|  |  | +             date_planned : 'Totales',
 | 
	
		
			
				|  |  | +             product_qty : product_qty,
 | 
	
		
			
				|  |  | +             price_tot : price_tot,
 | 
	
		
			
				|  |  | +         });
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +          if (action === 'pdf') {
 | 
	
		
			
				|  |  | +            var data = _.map(column, function(val) {
 | 
	
		
			
				|  |  | +              return val.field
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            _.each(_.map(column, function(val) {
 | 
	
		
			
				|  |  | +              return val
 | 
	
		
			
				|  |  | +            }), function(item) {
 | 
	
		
			
				|  |  | +              getColumns.push([{
 | 
	
		
			
				|  |  | +                title: item.title,
 | 
	
		
			
				|  |  | +                dataKey: item.field
 | 
	
		
			
				|  |  | +              }]);
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            var pdf_title = 'Análisis de Producción';
 | 
	
		
			
				|  |  | +            var pdf_type = 'l';
 | 
	
		
			
				|  |  | +            var pdf_name = 'Análisis de Producción_';
 | 
	
		
			
				|  |  | +            var pdf_columnStyles = {
 | 
	
		
			
				|  |  | +              date_planned: {columnWidth: 18,halign: 'center'},
 | 
	
		
			
				|  |  | +              name: {columnWidth: 25,halign: 'center'},
 | 
	
		
			
				|  |  | +              product: {columnWidth: 60,halign: 'left'},
 | 
	
		
			
				|  |  | +              prod_price_unit: {columnWidth: 18,halign: 'right'},
 | 
	
		
			
				|  |  | +              product_qty: {columnWidth: 18,halign: 'center'},
 | 
	
		
			
				|  |  | +              price_tot : {columnWidth: 20,halign: 'right'},
 | 
	
		
			
				|  |  | +              user: {columnWidth: 30,halign: 'left'},
 | 
	
		
			
				|  |  | +              state: {columnWidth: 16,halign: 'center'},
 | 
	
		
			
				|  |  | +            };
 | 
	
		
			
				|  |  | +            var filter = self.getFilter();
 | 
	
		
			
				|  |  | +            var pdf = new model.eiru_reports.ReportPdfWidget(self);
 | 
	
		
			
				|  |  | +            pdf.drawPDF(
 | 
	
		
			
				|  |  | +              _.flatten(getColumns),
 | 
	
		
			
				|  |  | +              row,
 | 
	
		
			
				|  |  | +              ResCompany,
 | 
	
		
			
				|  |  | +              pdf_title,
 | 
	
		
			
				|  |  | +              pdf_type,
 | 
	
		
			
				|  |  | +              pdf_name,
 | 
	
		
			
				|  |  | +              pdf_columnStyles,
 | 
	
		
			
				|  |  | +              filter,
 | 
	
		
			
				|  |  | +            );
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        getFilter: function() {
 | 
	
		
			
				|  |  | +          var self = this;
 | 
	
		
			
				|  |  | +          var company = self.$el.find('#current-company').val();
 | 
	
		
			
				|  |  | +          var store = self.$el.find('#current-store').val();
 | 
	
		
			
				|  |  | +          var user = self.$el.find('#current-user').val();
 | 
	
		
			
				|  |  | +          var state = self.$el.find('#current-state').val();
 | 
	
		
			
				|  |  | +          var date = self.$el.find('#current-date').val();
 | 
	
		
			
				|  |  | +          var desde = self.$el.find('#from').val();
 | 
	
		
			
				|  |  | +          var hasta = self.$el.find('#to').val();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +          var filter = [];
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +          if (company && company != 9999999) {
 | 
	
		
			
				|  |  | +            var ResCompany = _.filter(self.ResCompany, function(item) {
 | 
	
		
			
				|  |  | +              return item.id == company;
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            filter.push({
 | 
	
		
			
				|  |  | +              title: 'Empresa',
 | 
	
		
			
				|  |  | +              value: ResCompany[0].name,
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +          if (store && store != 9999999) {
 | 
	
		
			
				|  |  | +            var ResStore = _.filter(self.ResStore, function(item) {
 | 
	
		
			
				|  |  | +              return item.id == store;
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            filter.push({
 | 
	
		
			
				|  |  | +              title: 'Sucursal',
 | 
	
		
			
				|  |  | +              value: ResStore[0].name,
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +          if (user && user != 9999999) {
 | 
	
		
			
				|  |  | +            var ResUser = _.filter(self.ResUser, function(item) {
 | 
	
		
			
				|  |  | +              return item.id == user;
 | 
	
		
			
				|  |  | +            })
 | 
	
		
			
				|  |  | +            filter.push({
 | 
	
		
			
				|  |  | +              title: 'Responsable',
 | 
	
		
			
				|  |  | +              value: ResUser[0].name,
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +          if (state && state != 9999999) {
 | 
	
		
			
				|  |  | +            filter.push({
 | 
	
		
			
				|  |  | +              title: 'Estado',
 | 
	
		
			
				|  |  | +              value: $('#current-state option:selected').text(),
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +          if (date && date != 9999999) {
 | 
	
		
			
				|  |  | +            moment.locale('es', {
 | 
	
		
			
				|  |  | +              months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'),
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (date == 'range') {
 | 
	
		
			
				|  |  | +              filter.push({
 | 
	
		
			
				|  |  | +                title: 'Fecha',
 | 
	
		
			
				|  |  | +                value: desde + ' al ' + hasta,
 | 
	
		
			
				|  |  | +              });
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +              if (date == 'today') {
 | 
	
		
			
				|  |  | +                var fecha = moment().format('DD/MM/YYYY');
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +              if (date == 'yesterday') {
 | 
	
		
			
				|  |  | +                var fecha = moment().add(-1, 'days').format('DD/MM/YYYY');
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +              if (date == 'currentMonth') {
 | 
	
		
			
				|  |  | +                var fecha = moment().format('MMMM/YYYY');
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +              if (date == 'lastMonth') {
 | 
	
		
			
				|  |  | +                var fecha = moment().add(-1, 'months').format('MMMM/YYYY');
 | 
	
		
			
				|  |  | +              }
 | 
	
		
			
				|  |  | +              filter.push({
 | 
	
		
			
				|  |  | +                title: 'Fecha',
 | 
	
		
			
				|  |  | +                value: fecha,
 | 
	
		
			
				|  |  | +              });
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +          }
 | 
	
		
			
				|  |  | +          return filter;
 | 
	
		
			
				|  |  | +        },
 | 
	
		
			
				|  |  | +      });
 | 
	
		
			
				|  |  | +     }
 |