From 24ee174a1b6561260775dec4c79b36e86d4952e2 Mon Sep 17 00:00:00 2001
From: Benjamin <Benjamin@MBP-de-Benj.(none)>
Date: Fri, 12 Oct 2018 15:51:15 +0200
Subject: [PATCH] =?UTF-8?q?modif=20dates=20dashboard=20sur=20ann=C3=A9e=20?=
 =?UTF-8?q?fiscale?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 __manifest__.py                   |  1 +
 models/__init__.py                |  1 +
 models/lefilament_tdb.py          | 33 +++++++++++++++++++++----------
 static/src/js/dashboard_year.js   | 30 ++++++++++++++--------------
 static/src/xml/lefilament_tdb.xml |  6 +++---
 5 files changed, 43 insertions(+), 28 deletions(-)

diff --git a/__manifest__.py b/__manifest__.py
index 17d368c..a575350 100644
--- a/__manifest__.py
+++ b/__manifest__.py
@@ -26,6 +26,7 @@
         'security/ir.model.access.csv',
         'views/assets.xml',
         'views/views.xml',
+        # 'views/account_invoice.xml',
         'views/schedule.xml',
 	    'data/ir_module_category.xml'
     ],
diff --git a/models/__init__.py b/models/__init__.py
index 98d0727..5cc4332 100644
--- a/models/__init__.py
+++ b/models/__init__.py
@@ -3,3 +3,4 @@
 import lefilament_tdb
 import res_company
 import hr_employee
+# import account_invoice
diff --git a/models/lefilament_tdb.py b/models/lefilament_tdb.py
index 826febc..c1e7561 100644
--- a/models/lefilament_tdb.py
+++ b/models/lefilament_tdb.py
@@ -3,9 +3,14 @@
 # © 2017 Le Filament (<http://www.le-filament.com>)
 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
 
+from datetime import datetime, timedelta
+from dateutil.relativedelta import relativedelta
+
 from odoo import models, fields, api
 from odoo import tools
-from datetime import datetime
+
+from odoo.fields import Date
+
 
 class LeFilamentTdb(models.Model):
 	_name = "lefilament.dashboard"
@@ -103,6 +108,11 @@ class LeFilamentTdb(models.Model):
 
 	@api.model
 	def retrieve_datas_dashboard(self):
+		## Get fiscal years
+		fiscal_year = "'" + Date.to_string(datetime(datetime.now().year, self.env.user.company_id.fiscalyear_last_month, self.env.user.company_id.fiscalyear_last_day)) + "'"
+		fiscal_year_next = "'" + Date.to_string(datetime((datetime.now() + relativedelta(years=1)).year, self.env.user.company_id.fiscalyear_last_month, self.env.user.company_id.fiscalyear_last_day)) + "'"
+
+		## Prepare values
 		res = {
 			'facture': 0,
 			'commandes': 0,
@@ -120,25 +130,28 @@ class LeFilamentTdb(models.Model):
 			'date_maj': 0,
 			'a_encaisser': 0,
 			'a_payer': 0,
+			'fiscal_year': fiscal_year,
+			'fiscal_year_next': fiscal_year_next,
 		}
 
 		self._cr.execute("""
 			SELECT
             	(select count(*) from account_invoice) as id,
-            	(select sum(amount_untaxed_signed) from account_invoice where state!='draft' and (type='out_invoice' or type='out_refund') and date >= date_trunc('year', current_date) and date < date_trunc('year', current_date + interval '1' year) ) as facture, 
+            	(select sum(amount_untaxed_signed) from account_invoice where state!='draft' and (type='out_invoice' or type='out_refund') and date > %s and date <= %s ) as facture, 
             	(select sum(residual_company_signed) from account_invoice where state!='draft' and type='out_invoice' ) as a_encaisser, 
             	(select sum(residual_company_signed) from account_invoice where state!='draft' and type='in_invoice' ) as a_payer, 
-            	(select sum(planned_revenue*probability/100) from crm_lead where active=True and (date_deadline < date_trunc('year', current_date  + interval '1' year) or date_deadline is null) ) as pipe, 
-            	(select sum(planned_revenue*probability/100) from crm_lead where active=True and date_deadline >= date_trunc('year', current_date + interval '1' year) ) as pipe_n1, 
-            	(select sum(planned_revenue*probability/100) from crm_lead where active=True and probability=100  and (date_deadline < date_trunc('year', current_date + interval '1' year) or date_deadline is null) ) as pipe_win, 
-            	(select sum(planned_revenue*probability/100) from crm_lead where active=True and probability!=100  and (date_deadline < date_trunc('year', current_date + interval '1' year) or date_deadline is null) ) as pipe_to_win,
+            	(select sum(planned_revenue*probability/100) from crm_lead where active=True and (date_deadline <= %s or date_deadline is null) ) as pipe, 
+            	(select sum(planned_revenue*probability/100) from crm_lead where active=True and date_deadline > %s ) as pipe_n1, 
+            	(select sum(planned_revenue*probability/100) from crm_lead where active=True and probability=100  and (date_deadline <= %s or date_deadline is null) ) as pipe_win, 
+            	(select sum(planned_revenue*probability/100) from crm_lead where active=True and probability!=100  and (date_deadline < %s or date_deadline is null) ) as pipe_to_win,
             	(select date from account_bank_statement ORDER BY ID DESC LIMIT 1) as date_maj,
             	(select sum(amount) from account_bank_statement_line ) as tresorerie,
-            	(select sum(amount) from account_bank_statement_line where amount > 0 and date >= date_trunc('year', current_date) ) as entree,
-            	(select sum(amount) from account_bank_statement_line where amount < 0 and date >= date_trunc('year', current_date) ) as sortie,
-            	(select sum(amount) from account_bank_statement_line where date >= date_trunc('year', current_date) ) as variation,
+            	(select sum(amount) from account_bank_statement_line where amount > 0 and date > %s ) as entree,
+            	(select sum(amount) from account_bank_statement_line where amount < 0 and date > %s ) as sortie,
+            	(select sum(amount) from account_bank_statement_line where date > %s ) as variation,
             	(select sum(es.total_amount) from hr_expense_sheet es, hr_expense e where es.id = e.sheet_id and e.payment_mode='own_account' and es.state!='done' ) as cca,
-            	(select sum(price_subtotal-qty_invoiced*price_unit) from sale_order_line where invoice_status='to invoice') as commandes; """)
+            	(select sum(price_subtotal-qty_invoiced*price_unit) from sale_order_line where invoice_status='to invoice') as commandes; """
+            	% (fiscal_year, fiscal_year_next, fiscal_year_next, fiscal_year_next, fiscal_year_next, fiscal_year_next, fiscal_year, fiscal_year, fiscal_year) )
 		datas = self._cr.dictfetchall()
 
 		self._cr.execute("select ca_target from res_company;")
diff --git a/static/src/js/dashboard_year.js b/static/src/js/dashboard_year.js
index 6c953e8..abe2a4e 100644
--- a/static/src/js/dashboard_year.js
+++ b/static/src/js/dashboard_year.js
@@ -20,8 +20,9 @@ odoo.define('lefilament_tdb.dashboard_year', function (require) {
 	    template: 'YearDashboard',
 
 	    events: {
-	        'click #facture': function() {
-	        	this.facture();
+	        'click #facture': function(e) {
+	        	var fiscalyear = e.target.dataset.fiscalyear;
+	        	this.facture(fiscalyear);
 	        },
 	        'click #facture_non_encaisse': function() {
 	        	this.facture_non_encaisse();
@@ -29,11 +30,13 @@ odoo.define('lefilament_tdb.dashboard_year', function (require) {
 	        'click #commandes': function() {
 	        	this.commandes();
 	        },
-	        'click #pipe_link': function() {
-	        	this.pipe();
+	        'click #pipe_link': function(e) {
+	        	var fiscalyearnext = e.target.dataset.fiscalyearnext;
+	        	this.pipe(fiscalyearnext);
 	        },
-	        'click #pipe_n1_link': function() {
-	        	this.pipe_n1();
+	        'click #pipe_n1_link': function(e) {
+	        	var fiscalyearnext = e.target.dataset.fiscalyearnext;
+	        	this.pipe_n1(fiscalyearnext);
 	        },
 	        'click #fournisseur_link': function() {
 	        	this.fournisseur();
@@ -205,9 +208,8 @@ odoo.define('lefilament_tdb.dashboard_year', function (require) {
 	        return value;
 	    },
 
-	    facture: function() {
+	    facture: function(fiscalyear) {
 	    	var self = this;
-	    	var date = moment().startOf('year').format('YYYY-MM-DD');
 	    	var context = { 'user_id': this.session.uid, }
 
         	var action = ({
@@ -216,7 +218,7 @@ odoo.define('lefilament_tdb.dashboard_year', function (require) {
             	view_type: 'form',
             	view_mode: 'tree,form',
             	views: [[false, 'list'], [false, 'pivot'], [false, 'graph'], [self.invoice_view_id, 'form']],
-            	domain: [['state','in',['open','paid']],['type','=','out_invoice'],['date_invoice','>=',date]],
+            	domain: [['state','in',['open','paid']],['type','=','out_invoice'],['date_invoice','>=',fiscalyear]],
             	target:'current',
             	name: 'Facturé',
             	context: context
@@ -242,9 +244,8 @@ odoo.define('lefilament_tdb.dashboard_year', function (require) {
 
         	this.do_action(action);
 	    },
-	    pipe: function() {
+	    pipe: function(fiscalyearnext) {
 	    	var self = this;
-	    	var deadline = moment().endOf('year').format('YYYY-MM-DD');
 	    	var context = { 'user_id': this.session.uid, }
 
         	var action = ({
@@ -253,7 +254,7 @@ odoo.define('lefilament_tdb.dashboard_year', function (require) {
             	view_type: 'form',
             	view_mode: 'tree,form',
             	views: [[false, 'kanban'], [false, 'list'], [this.pipe_view_id, 'form']],
-            	domain: [['type','=','opportunity'],('|',['date_deadline','<=', deadline],['date_deadline','=', null])],
+            	domain: [['type','=','opportunity'],('|',['date_deadline','<=', fiscalyearnext],['date_deadline','=', null])],
             	target:'current',
             	name: 'Pipe',
             	context: context
@@ -261,9 +262,8 @@ odoo.define('lefilament_tdb.dashboard_year', function (require) {
 
         	this.do_action(action);
 	    },
-	    pipe_n1: function() {
+	    pipe_n1: function(fiscalyearnext) {
 	    	var self = this;
-	    	var deadline = moment().endOf('year').format('YYYY-MM-DD');
 	    	var context = { 'user_id': this.session.uid, }
 
         	var action = ({
@@ -272,7 +272,7 @@ odoo.define('lefilament_tdb.dashboard_year', function (require) {
             	view_type: 'form',
             	view_mode: 'kanban,tree,form',
             	views: [[false, 'kanban'], [false, 'list'], [this.pipe_view_id, 'form']],
-            	domain: [['type','=','opportunity'],['date_deadline','>', deadline]],
+            	domain: [['type','=','opportunity'],['date_deadline','>', fiscalyearnext]],
             	target:'current',
             	name: 'Pipe',
             	context: context
diff --git a/static/src/xml/lefilament_tdb.xml b/static/src/xml/lefilament_tdb.xml
index 57b2bdb..1a32d32 100644
--- a/static/src/xml/lefilament_tdb.xml
+++ b/static/src/xml/lefilament_tdb.xml
@@ -87,7 +87,7 @@
 						<div class="card">
 							<p class="card-header">Facturé</p>
 							<p class="card-number">
-								<a id="facture">
+								<a id="facture" t-attf-data-fiscalyear="#{widget.values.fiscal_year}" >
 									<t t-esc="widget.render_monetary(widget.values.facture)"></t>
 								</a>
 							</p>
@@ -111,14 +111,14 @@
 						<div class="tab-content card-tab" id="myTab1Content">
 							<div id="pipe" class="tab-pane fade active in">
 								<p class="card-number">
-									<a id="pipe_link">
+									<a id="pipe_link" t-attf-data-fiscalyearnext="#{widget.values.fiscal_year_next}" >
 										<t t-esc="widget.render_monetary(widget.values.pipe)"></t>
 									</a>
 								</p>
 							</div>
 							<div id="pipe_n1" class="tab-pane fade">
 								<p class="card-number">
-									<a id="pipe_n1_link">									
+									<a id="pipe_n1_link" t-attf-data-fiscalyearnext="#{widget.values.fiscal_year_next}" >									
 										<t t-esc="widget.render_monetary(widget.values.pipe_n1)"></t>
 									</a>
 								</p>
-- 
GitLab