Skip to content
Snippets Groups Projects
dashboard_year.js 11.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • Benjamin's avatar
    Benjamin committed
     // © 2019 Le Filament (<http://www.le-filament.com>)
    
    Benjamin's avatar
    Benjamin committed
     // License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
    
    
    odoo.define('lefilament_tdb.dashboard_year', function (require) {
    
    
    	var core = require('web.core');
    	var session = require('web.session');
    
    Benjamin's avatar
    Benjamin committed
    	var AbstractAction = require('web.AbstractAction');
    
    	var QWeb = core.qweb;
    
    
    Benjamin's avatar
    Benjamin committed
    	var YearDashboardView = AbstractAction.extend({
    
    	    template: 'YearDashboard',
    
    
    	        'click #facture': function(e) {
    	        	var fiscalyear = e.target.dataset.fiscalyear;
    	        	this.facture(fiscalyear);
    
    Benjamin's avatar
    Benjamin committed
    	        'click #facture_non_encaisse': function() {
    	        	this.facture_non_encaisse();
    	        },
    
    	        'click #commandes': function() {
    	        	this.commandes();
    	        },
    
    	        'click #pipe_link': function(e) {
    	        	var fiscalyearnext = e.target.dataset.fiscalyearnext;
    	        	this.pipe(fiscalyearnext);
    
    Benjamin's avatar
    Benjamin committed
    	        },
    
    	        'click #pipe_n1_link': function(e) {
    	        	var fiscalyearnext = e.target.dataset.fiscalyearnext;
    	        	this.pipe_n1(fiscalyearnext);
    
    Benjamin's avatar
    Benjamin committed
    	        },
    	        'click #fournisseur_link': function() {
    	        	this.fournisseur();
    	        },
    	        'click #releve': function() {
    	        	this.releve();
    	        },
    
    	    init: function() {
    	      var result = this._super.apply(this, arguments);
    	      return result;
    	    },
    
    	    willStart: function() {
    	        var deferred = new jQuery.Deferred();
    	        var self = this;
    	        this.values = {};
    
    	        this.progess = 0;
    
    Benjamin's avatar
    Benjamin committed
    
    
    Benjamin's avatar
    Benjamin committed
    	        var invoice_view_id = this._rpc({
    				    model: 'ir.model.data',
    				    method: 'xmlid_to_res_id',
    				    args: ['account.invoice_form'],
    				}).then(function(results) {
    
    Benjamin's avatar
    Benjamin committed
    	        		self.invoice_view_id = results;
    	        	});
    
    
    Benjamin's avatar
    Benjamin committed
    	        var pipe_view_id = this._rpc({
    				    model: 'ir.model.data',
    				    method: 'xmlid_to_res_id',
    				    args: ['crm.crm_case_form_view_oppor'],
    				}).then(function(results) {
    
    Benjamin's avatar
    Benjamin committed
    	        		self.pipe_view_id = results;
    	        	});
    
    Benjamin's avatar
    Benjamin committed
    
    	        this._rpc({
    				    model: 'lefilament.dashboard',
    				    method: 'retrieve_datas_dashboard',
    				    args: [],
    				})
    
                    .then(function(results) {
                        self.values = results;
    
                        self.pfact2 = (self.values.facture);
    
    Benjamin's avatar
    Benjamin committed
    					self.pcomm2 = (self.values.commandes);
    					self.ppipe2_win = (self.values.pipe_win);
    					self.ppipe2_to_win = (self.values.pipe_to_win);
    
    
                        if (self.values.target > 0) {
                        	self.pfact = (self.values.facture / self.values.target * 100).toFixed(0);
                        	self.pcomm = (self.values.commandes / self.values.target * 100).toFixed(0);
    						self.ppipe_win = (self.values.pipe_win / self.values.target * 100).toFixed(0);
    						self.ppipe_to_win = (self.values.pipe_to_win / self.values.target * 100).toFixed(0);
    						self.ptarg = 100-self.pfact-self.pcomm-self.ppipe_win-self.ppipe_to_win;
    						self.ptarg2 = self.values.target -self.pfact2-self.pcomm2-self.ppipe2_to_win-self.ppipe2_win;
    						self.total = ((self.values.facture + self.values.commandes + self.values.pipe_win) / self.values.target * 100).toFixed(0);
                        } else {
                        	self.pfact = 'n/a';
                        	self.pcomm = 'n/a';
    						self.ppipe_win = 'n/a';
    						self.ppipe_to_win = 'n/a';
    						self.ptarg = 'n/a';
    						self.ptarg2 = 'n/a';
    						self.total = 'n/a';
                        }
    					
    
    Benjamin's avatar
    Benjamin committed
    					
    					self.total2 = self.pfact2 + self.pcomm2 + self.ppipe2_win
    
    					self.target = self.values.target
                        
    
                        deferred.resolve();
                    });
    	          return jQuery.when(this._super.apply(this, arguments),deferred);
    	    },
    
    	    start: function() {
    
    	    	return this.render_chart();
    
    Benjamin's avatar
    Benjamin committed
    	    },
    
    	    render_chart: function(chart) {
    
    	    	self = this;
    
    
    Benjamin's avatar
    Benjamin committed
    			/////////////////////////////////////////
                //    Etat d'avancement -> Bar Chart   //
                /////////////////////////////////////////
                
                this.ctx = this.$el.find('#target')[0].getContext('2d');
    
    Benjamin's avatar
    Benjamin committed
    
                var ptarg = this.ptarg;
                var max_xaxis = 100;
    
                if (this.ptarg < 0 ) {
                	ptarg = 0;
                	max_xaxis = 100 - this.ptarg;
                }
    
    Benjamin's avatar
    Benjamin committed
    	    		
       			var dataset_stacked = [
    
    Benjamin's avatar
    Benjamin committed
    		        { label: 'Facturé', data: [this.pfact], backgroundColor: '#8ED8A2', },
    		        { label: 'Commandes', data: [this.pcomm], backgroundColor: '#F6DCA2', },
    		        { label: 'Gagné', data: [this.ppipe_win], backgroundColor: '#F6CCA2', },
    		        { label: 'Pipe', data: [this.ppipe_to_win], backgroundColor: '#F6ACA2', },
    
    Benjamin's avatar
    Benjamin committed
    		        { label: 'To Do', data: [ptarg], backgroundColor: '#eee', },
    		        ];
    
    Benjamin's avatar
    Benjamin committed
    
    		    var label = 'Année ' + moment(Date.now()).format('YYYY');
    
    		    this.targetData = {
    	            labels : [label],
    	            datasets : dataset_stacked
    	        };
    
                var options = { 
    
    Benjamin's avatar
    Benjamin committed
                	responsive: true,
    
    Benjamin's avatar
    Benjamin committed
                	legend: {
                		display: false,
                	},
                	layout: { 
                		padding: { left:0, right: 0, bottom: 20, top: 20, },
                	},
                	scales: {
    		            xAxes: [{
    		                stacked: true,
    		                scaleShowLabels: false,
    
    Benjamin's avatar
    Benjamin committed
                            display : true ,
                            ticks: {
    		                    max: max_xaxis,
    
    Benjamin's avatar
    Benjamin committed
    		                    stepSize: 25,
    		                    fontSize: 9,
    
    Benjamin's avatar
    Benjamin committed
    		                    fontColor: '#999',
    		                    callback: function(value, index, values) {
    		                        return value + '%';
    		                    },
    		                },
    
    Benjamin's avatar
    Benjamin committed
    		                gridLines: {
    		                	zeroLineColor: 'rgba(0, 0, 0, 0.1)',
    		                	drawBorder: false,
    		                	tickMarkLength: 2,
    		                },
    
    Benjamin's avatar
    Benjamin committed
    		            }],
                        yAxes: [{
                                stacked: true,
                                scaleShowLabels: false,
                                display : false ,
                            }]
    		        },
    		        tooltips: {
    		        	backgroundColor: 'rgba(255,255,255,0.8)',
    		        	titleFontStyle: 'normal',
    		        	titleFontColor: '#999',
    		        	bodyFontColor: '#777',
    
    Benjamin's avatar
    Benjamin committed
    		        	callbacks: {
    	                    label: function(tooltipItems, data) { 
    
    Benjamin's avatar
    Benjamin committed
    	                        return (tooltipItems.xLabel * self.target  / 100000).toLocaleString('fr', { maximumFractionDigits: 2 }) + ' K€';
    
    Benjamin's avatar
    Benjamin committed
    	                    }
    	                }
    
    Benjamin's avatar
    Benjamin committed
    		        },
    		        responsive: true,
                }
    
                var myLineChart = new Chart(this.ctx, { type: 'horizontalBar', data: this.targetData, options } );
    
    
    	    },
    
    	    render_monetary: function(value) {
    	        value = value.toLocaleString('fr', { maximumFractionDigits: 0 }) + '';
    	        return value;
    	    },
    
    Benjamin's avatar
    Benjamin committed
    	    render_keur: function(value) {
    
    Benjamin's avatar
    Benjamin committed
    	        value = (value/ 1000).toLocaleString('fr', { maximumFractionDigits: 0 }) + ' K€';
    
    Benjamin's avatar
    Benjamin committed
    	        return value;
    	    },
    	    render_percent: function(value) {
    	        value = value.toLocaleString('fr', { maximumFractionDigits: 1 }) + ' %';
    	        return value;
    	    },
    
    Benjamin's avatar
    Benjamin committed
    	    render_date: function(value) {
    	    	var dateFormat = new Date(value);
                var new_value = moment(dateFormat).format('Do MMM YYYY');
                return new_value;
    	    },
    
    	    render_monetary_color: function(value) {
    
    	        if (value >= 0)
    	          value = '<span class="positive">'+value.toLocaleString('fr', { maximumFractionDigits: 0 }) + ' €</span>';
    	        else
    	          value = '<span class="negative">'+value.toLocaleString('fr', { maximumFractionDigits: 0 }) + ' €</span>';
    	        
    	        return value;
    	    },
    
    
    	    facture: function(fiscalyear) {
    
    	    	var self = this;
    
    Benjamin's avatar
    Benjamin committed
    	    	var context = { 'user_id': session.uid, }
    
    
            	var action = ({
                	type: 'ir.actions.act_window',
                 	res_model: 'account.invoice',
                	view_type: 'form',
                	view_mode: 'tree,form',
    
    Benjamin's avatar
    Benjamin committed
                	views: [[false, 'list'], [false, 'pivot'], [false, 'graph'], [self.invoice_view_id, 'form']],
    
                	domain: [['state','in',['open','paid']],['type','in',['out_invoice', 'out_refund']],['date_invoice','>=',fiscalyear]],
    
                	target:'current',
    
    Benjamin's avatar
    Benjamin committed
                	name: 'Facturé',
    
                	context: context
            	})
    
    
            	this.do_action(action);
    	    },
    	    commandes: function() {
    	    	var self = this;
    
    Benjamin's avatar
    Benjamin committed
    	    	var context = { 'user_id': session.uid, }
    
    
            	var action = ({
                	type: 'ir.actions.act_window',
                 	res_model: 'sale.order',
                	view_type: 'form',
                	view_mode: 'tree,form',
                	views: [[false, 'list'], [false, 'form']],
                	domain: [['invoice_status','=','to invoice']],
                	target:'current',
                	name: 'Commandes en cours',
                	context: context
            	})
    
    
    Benjamin's avatar
    Benjamin committed
            	this.do_action(action);
    	    },
    
    	    pipe: function(fiscalyearnext) {
    
    Benjamin's avatar
    Benjamin committed
    	    	var self = this;
    
    Benjamin's avatar
    Benjamin committed
    	    	var context = { 'user_id': session.uid, }
    
    Benjamin's avatar
    Benjamin committed
    
            	var action = ({
                	type: 'ir.actions.act_window',
                 	res_model: 'crm.lead',
                	view_type: 'form',
                	view_mode: 'tree,form',
    
    Benjamin's avatar
    Benjamin committed
                	views: [[false, 'kanban'], [false, 'list'], [this.pipe_view_id, 'form']],
    
                	domain: ['|',['date_deadline','<=', fiscalyearnext],['date_deadline','=', null], ['type','=','opportunity'] ],
    
    Benjamin's avatar
    Benjamin committed
                	target:'current',
                	name: 'Pipe',
                	context: context
            	})
    
            	this.do_action(action);
    	    },
    
    	    pipe_n1: function(fiscalyearnext) {
    
    Benjamin's avatar
    Benjamin committed
    	    	var self = this;
    
    Benjamin's avatar
    Benjamin committed
    	    	var context = { 'user_id': session.uid, }
    
    Benjamin's avatar
    Benjamin committed
    
            	var action = ({
                	type: 'ir.actions.act_window',
                 	res_model: 'crm.lead',
                	view_type: 'form',
                	view_mode: 'kanban,tree,form',
    
    Benjamin's avatar
    Benjamin committed
                	views: [[false, 'kanban'], [false, 'list'], [this.pipe_view_id, 'form']],
    
                	domain: [['type','=','opportunity'],['date_deadline','>', fiscalyearnext]],
    
    Benjamin's avatar
    Benjamin committed
                	target:'current',
                	name: 'Pipe',
                	context: context
            	})
    
            	this.do_action(action);
    	    },
    	    facture_non_encaisse: function() {
    	    	var self = this;
    
    Benjamin's avatar
    Benjamin committed
    	    	var context = { 'user_id': session.uid, }
    
    Benjamin's avatar
    Benjamin committed
    
            	var action = ({
                	type: 'ir.actions.act_window',
                 	res_model: 'account.invoice',
                	view_type: 'form',
    
    Benjamin's avatar
    Benjamin committed
                	view_mode: 'tree,form',
    
    Benjamin's avatar
    Benjamin committed
                	views: [[false, 'list'], [false, 'pivot'], [false, 'graph'], [self.invoice_view_id, 'form']],
    
    Benjamin's avatar
    Benjamin committed
                	domain: [['state','=','open'],['type','=','out_invoice']],
                	target:'current',
                	name: 'Factures en cours',
                	context: context
            	})
    
            	this.do_action(action);
    	    },
    	    fournisseur: function() {
    	    	var self = this;
    
    Benjamin's avatar
    Benjamin committed
    	    	var context = { 'user_id': session.uid, }
    
    Benjamin's avatar
    Benjamin committed
    
            	var action = ({
                	type: 'ir.actions.act_window',
                 	res_model: 'account.invoice',
                	view_type: 'form',
                	view_mode: 'tree,form',
                	views: [[false, 'list'], [false, 'form']],
                	domain: [['state','=','open'],['type','=','in_invoice']],
                	target:'current',
                	name: 'Factures fournisseurs en cours',
                	context: context
            	})
    
            	this.do_action(action);
    	    },
    	    releve: function() {
    	    	var self = this;
    
    Benjamin's avatar
    Benjamin committed
    	    	var context = { 'user_id': session.uid, }
    
    Benjamin's avatar
    Benjamin committed
    
            	var action = ({
                	type: 'ir.actions.act_window',
                 	res_model: 'account.bank.statement',
                	view_type: 'form',
                	view_mode: 'tree,form',
                	views: [[false, 'list'], [false, 'form']],
                	target:'current',
                	name: 'Relevés en cours',
                	context: context
            	})
    
    
            	this.do_action(action);
    
    	});
    
    	core.action_registry.add('lefilament_tdb.dashboard_year', YearDashboardView);
    
    
    });