From c28ff6c257c749861c0a8593bb4d3d6f8f839a36 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Thu, 9 Jul 2015 12:29:01 +0200
Subject: [PATCH] NEW Add status into filters of graph

---
 htdocs/comm/propal/list.php                 |  4 ++--
 htdocs/comm/propal/stats/index.php          | 18 ++++++++++++++----
 htdocs/core/class/html.formpropal.class.php | 20 +++++++++++++++-----
 htdocs/core/class/stats.class.php           |  5 ++++-
 4 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/htdocs/comm/propal/list.php b/htdocs/comm/propal/list.php
index 5a5e520d455..1004986ffe5 100644
--- a/htdocs/comm/propal/list.php
+++ b/htdocs/comm/propal/list.php
@@ -58,8 +58,8 @@ $search_montant_ht=GETPOST('search_montant_ht','alpha');
 $search_author=GETPOST('search_author','alpha');
 $search_product_category=GETPOST('search_product_category','int');
 $search_town=GETPOST('search_town','alpha');
-$viewstatut=$db->escape(GETPOST('viewstatut'));
-$object_statut=$db->escape(GETPOST('propal_statut'));
+$viewstatut=GETPOST('viewstatut');
+$object_statut=GETPOST('propal_statut');
 
 $sall=GETPOST("sall");
 $mesg=(GETPOST("msg") ? GETPOST("msg") : GETPOST("mesg"));
diff --git a/htdocs/comm/propal/stats/index.php b/htdocs/comm/propal/stats/index.php
index 291d4b4557f..898f228fdb2 100644
--- a/htdocs/comm/propal/stats/index.php
+++ b/htdocs/comm/propal/stats/index.php
@@ -3,7 +3,7 @@
  * Copyright (C) 2004-2011 Laurent Destailleur  <eldy@users.sourceforge.net>
  * Copyright (C) 2005-2012 Regis Houssin        <regis.houssin@capnetworks.com>
  * Copyright (C) 2012      Marcos García        <marcosgdf@gmail.com>
- * Copyright (C) 2015       Jean-François Ferry	<jfefe@aternatik.fr>
+ * Copyright (C) 2015      Jean-François Ferry	<jfefe@aternatik.fr>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,10 +28,13 @@
 require '../../../main.inc.php';
 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propalestats.class.php';
 require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
+require_once DOL_DOCUMENT_ROOT.'/core/class/html.formpropal.class.php';
 
 $WIDTH=DolGraph::getDefaultGraphSizeForStats('width');
 $HEIGHT=DolGraph::getDefaultGraphSizeForStats('height');
 
+$object_statut=GETPOST('propal_statut');
+
 $userid=GETPOST('userid','int');
 $socid=GETPOST('socid','int');
 // Security check
@@ -55,11 +58,12 @@ $mode=GETPOST('mode');
  */
 
 $form=new Form($db);
+$formpropal=new FormPropal($db);
 
 $langs->load('propal');
 $langs->load('other');
 
-llxHeader();
+llxHeader('', $langs->trans("ProposalsStatistics"));
 
 print_fiche_titre($langs->trans("ProposalsStatistics"),'','title_commercial.png');
 
@@ -67,7 +71,9 @@ $dir=$conf->propal->dir_temp;
 
 dol_mkdir($dir);
 
+
 $stats = new PropaleStats($db, $socid, ($userid>0?$userid:0));
+if ($object_statut != '' && $object_statut >= 0) $stats->where .= ' AND p.fk_statut IN ('.$object_statut.')';
 
 // Build graphic number of object
 $data = $stats->getNbByMonthWithPrevYear($endyear,$startyear);
@@ -113,7 +119,7 @@ if (! $mesg)
 }
 
 // Build graphic amount of object
-$data = $stats->getAmountByMonthWithPrevYear($endyear,$startyear);
+$data = $stats->getAmountByMonthWithPrevYear($endyear,$startyear,0);
 // $data = array(array('Lib',val1,val2,val3),...)
 
 if (!$user->rights->societe->client->voir || $user->societe_id)
@@ -154,7 +160,7 @@ if (! $mesg)
     $px2->draw($filenameamount,$fileurlamount);
 }
 
-$data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear);
+$data = $stats->getAverageByMonthWithPrevYear($endyear, $startyear, $filter);
 
 $fileurl_avg='';
 if (!$user->rights->societe->client->voir || $user->societe_id)
@@ -239,6 +245,10 @@ print '<div class="fichecenter"><div class="fichethirdleft">';
 	print '<tr><td align="left">'.$langs->trans("CreatedBy").'</td><td align="left">';
 	print $form->select_dolusers($userid,'userid',1);
 	print '</td></tr>';
+	// Status
+	print '<tr><td align="left">'.$langs->trans("Status").'</td><td align="left">';
+	$formpropal->selectProposalStatus($object_statut,0,1);
+	print '</td></tr>';
 	// Year
 	print '<tr><td align="left">'.$langs->trans("Year").'</td><td align="left">';
 	if (! in_array($year,$arrayyears)) $arrayyears[$year]=$year;
diff --git a/htdocs/core/class/html.formpropal.class.php b/htdocs/core/class/html.formpropal.class.php
index 987d5d76682..e3f21212e8b 100644
--- a/htdocs/core/class/html.formpropal.class.php
+++ b/htdocs/core/class/html.formpropal.class.php
@@ -45,11 +45,13 @@ class FormPropal
      *    Return combo list of differents status of a proposal
      *    Values are id of table c_propalst
      *
-     *    @param	string	$selected   Preselected value
-     *    @param	int		$short		Use short labels
+     *    @param	string	$selected   	Preselected value
+     *    @param	int		$short			Use short labels
+     *    @param	int		$excludedraft	0=All status, 1=Exclude draft status
+     *    @param	int 	$showempty		1=Add empty line
      *    @return	void
      */
-    function selectProposalStatus($selected='',$short=0)
+    function selectProposalStatus($selected='',$short=0, $excludedraft=0, $showempty=1)
     {
         global $langs;
 
@@ -61,7 +63,7 @@ class FormPropal
         if ($resql)
         {
             print '<select class="flat" name="propal_statut">';
-            print '<option value="">&nbsp;</option>';
+            if ($showempty) print '<option value="">&nbsp;</option>';
             $num = $this->db->num_rows($resql);
             $i = 0;
             if ($num)
@@ -69,6 +71,14 @@ class FormPropal
                 while ($i < $num)
                 {
                     $obj = $this->db->fetch_object($resql);
+                    if ($excludedraft)
+                    {
+						if ($obj->code == 'Draft' || $obj->code == 'PR_DRAFT')
+						{
+							$i++;
+							continue;
+						}
+                    }
                     if ($selected == $obj->id)
                     {
                         print '<option value="'.$obj->id.'" selected>';
@@ -83,7 +93,7 @@ class FormPropal
                         print $langs->trans("PropalStatus".$key.($short?'Short':''));
                     }
                     else
-                    {
+					{
                         $conv_to_new_code=array('PR_DRAFT'=>'Draft','PR_OPEN'=>'Opened','PR_CLOSED'=>'Closed','PR_SIGNED'=>'Signed','PR_NOTSIGNED'=>'NotSigned','PR_FAC'=>'Billed');
                         if (! empty($conv_to_new_code[$obj->code])) $key=$conv_to_new_code[$obj->code];
                         print ($langs->trans("PropalStatus".$key.($short?'Short':''))!="PropalStatus".$key.($short?'Short':''))?$langs->trans("PropalStatus".$key.($short?'Short':'')):$obj->label;
diff --git a/htdocs/core/class/stats.class.php b/htdocs/core/class/stats.class.php
index ff9f29f55ff..9a459f33e41 100644
--- a/htdocs/core/class/stats.class.php
+++ b/htdocs/core/class/stats.class.php
@@ -125,7 +125,10 @@ abstract class Stats
 	}
 
 	/**
-	 * Return amount of elements by month for several years
+	 * Return amount of elements by month for several years.
+	 * Criterias used to build request are defined into the constructor of parent class into xxx/class/xxxstats.class.php
+	 * The caller of class can add more filters into sql request by adding criteris into the $stats->where property just after
+	 * calling constructor.
 	 *
 	 * @param	int		$endyear		Start year
 	 * @param	int		$startyear		End year
-- 
GitLab