2
0
forked from Wavyzz/dolibarr

Fix: Several bugs into filter on statistics pages.

This commit is contained in:
Laurent Destailleur
2013-05-20 19:24:27 +02:00
parent 71e14df62f
commit 6db07ac2d8
6 changed files with 69 additions and 58 deletions

View File

@@ -26,6 +26,7 @@
include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
include_once DOL_DOCUMENT_ROOT . '/comm/propal/class/propal.class.php';
include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
/**
@@ -49,14 +50,14 @@ class PropaleStats extends Stats
*
* @param DoliDB $db Database handler
* @param int $socid Id third party
* @param int $userid Id user for filter
* @param int $userid Id user for filter (creation user)
*/
function __construct($db, $socid=0, $userid=0)
{
global $user, $conf;
$this->db = $db;
$this->socid = $socid;
$this->socid = ($socid > 0 ? $socid : 0);
$this->userid = $userid;
$object=new Propal($this->db);
@@ -66,7 +67,7 @@ class PropaleStats extends Stats
$this->field='total_ht';
$this->where.= " fk_statut > 0";
$this->where.= " p.fk_statut > 0";
$this->where.= " AND p.fk_soc = s.rowid AND p.entity = ".$conf->entity;
if (!$user->rights->societe->client->voir && !$user->societe_id) $this->where .= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
if($this->socid)
@@ -79,7 +80,7 @@ class PropaleStats extends Stats
/**
* Return propals number by month for a year
*
*
* @param int $year year for stats
* @return array array with number by month
*/
@@ -90,7 +91,7 @@ class PropaleStats extends Stats
$sql = "SELECT date_format(p.datep,'%m') as dm, count(*)";
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(p.datep,'%Y') = '".$year."'";
$sql.= " WHERE p.datep BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
@@ -100,7 +101,7 @@ class PropaleStats extends Stats
/**
* Return propals number by year
*
*
* @return array array with number by year
*
*/
@@ -117,7 +118,7 @@ class PropaleStats extends Stats
return $this->_getNbByYear($sql);
}
/**
* Return the propals amount by month for a year
*
@@ -131,14 +132,14 @@ class PropaleStats extends Stats
$sql = "SELECT date_format(p.datep,'%m') as dm, sum(p.".$this->field.")";
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(p.datep,'%Y') = '".$year."'";
$sql.= " WHERE p.datep BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
return $this->_getAmountByMonth($year, $sql);
}
/**
* Return the propals amount average by month for a year
*
@@ -152,7 +153,7 @@ class PropaleStats extends Stats
$sql = "SELECT date_format(p.datep,'%m') as dm, avg(p.".$this->field.")";
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(p.datep,'%Y') = '".$year."'";
$sql.= " WHERE p.datep BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
@@ -162,7 +163,7 @@ class PropaleStats extends Stats
/**
* Return nb, total and average
*
*
* @return array Array of values
*/
function getAllByYear()

View File

@@ -31,8 +31,8 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
$WIDTH=DolGraph::getDefaultGraphSizeForStats('width');
$HEIGHT=DolGraph::getDefaultGraphSizeForStats('height');
$userid=GETPOST('userid','int'); if ($userid < 0) $userid=0;
$socid=GETPOST('socid','int'); if ($socid < 0) $socid=0;
$userid=GETPOST('userid','int');
$socid=GETPOST('socid','int');
// Security check
if ($user->societe_id > 0)
{
@@ -63,7 +63,7 @@ $dir=$conf->propal->dir_temp;
dol_mkdir($dir);
$stats = new PropaleStats($db, $socid, $userid);
$stats = new PropaleStats($db, $socid, ($userid>0?$userid:0));
// Build graphic number of object
$data = $stats->getNbByMonthWithPrevYear($endyear,$startyear);
@@ -233,12 +233,13 @@ print '<div class="fichecenter"><div class="fichethirdleft">';
print $form->select_company($socid,'socid',$filter,1);
print '</td></tr>';
// User
print '<tr><td align="left">'.$langs->trans("User").'/'.$langs->trans("SalesRepresentative").'</td><td align="left">';
print '<tr><td align="left">'.$langs->trans("CreatedBy").'</td><td align="left">';
print $form->select_users($userid,'userid',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;
if (! in_array($nowyear,$arrayyears)) $arrayyears[$nowyear]=$nowyear;
arsort($arrayyears);
print $form->selectarray('year',$arrayyears,$year,0);
print '</td></tr>';

View File

@@ -26,6 +26,7 @@
include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
include_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
include_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.commande.class.php';
include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
/**
@@ -49,7 +50,7 @@ class CommandeStats extends Stats
* @param DoliDB $db Database handler
* @param int $socid Id third party for filter
* @param string $mode Option
* @param int $userid Id user for filter
* @param int $userid Id user for filter (creation user)
*/
function __construct($db, $socid, $mode, $userid=0)
{
@@ -57,7 +58,7 @@ class CommandeStats extends Stats
$this->db = $db;
$this->socid = $socid;
$this->socid = ($socid > 0 ? $socid : 0);
$this->userid = $userid;
if ($mode == 'customer')
@@ -100,7 +101,7 @@ class CommandeStats extends Stats
$sql = "SELECT date_format(c.date_commande,'%m') as dm, count(*) nb";
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(c.date_commande,'%Y') = '".$year."'";
$sql.= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
@@ -143,7 +144,7 @@ class CommandeStats extends Stats
$sql = "SELECT date_format(c.date_commande,'%m') as dm, sum(c.".$this->field.")";
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(c.date_commande,'%Y') = '".$year."'";
$sql.= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
@@ -165,7 +166,7 @@ class CommandeStats extends Stats
$sql = "SELECT date_format(c.date_commande,'%m') as dm, avg(c.".$this->field.")";
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(c.date_commande,'%Y') = '".$year."'";
$sql.= " WHERE c.date_commande BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

View File

@@ -36,8 +36,8 @@ $mode=GETPOST("mode")?GETPOST("mode"):'customer';
if ($mode == 'customer' && ! $user->rights->commande->lire) accessforbidden();
if ($mode == 'supplier' && ! $user->rights->fournisseur->commande->lire) accessforbidden();
$userid=GETPOST('userid','int'); if ($userid < 0) $userid=0;
$socid=GETPOST('socid','int'); if ($socid < 0) $socid=0;
$userid=GETPOST('userid','int');
$socid=GETPOST('socid','int');
// Security check
if ($user->societe_id > 0)
{
@@ -77,7 +77,7 @@ print_fiche_titre($title);
dol_mkdir($dir);
$stats = new CommandeStats($db, $socid, $mode, $userid);
$stats = new CommandeStats($db, $socid, $mode, ($userid>0?$userid:0));
// Build graphic number of object
$data = $stats->getNbByMonthWithPrevYear($endyear,$startyear);
@@ -255,12 +255,13 @@ print '<div class="fichecenter"><div class="fichethirdleft">';
print $form->select_company($socid,'socid',$filter,1);
print '</td></tr>';
// User
print '<tr><td align="left">'.$langs->trans("User").'/'.$langs->trans("SalesRepresentative").'</td><td align="left">';
print '<tr><td align="left">'.$langs->trans("CreatedBy").'</td><td align="left">';
print $form->select_users($userid,'userid',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;
if (! in_array($nowyear,$arrayyears)) $arrayyears[$nowyear]=$nowyear;
arsort($arrayyears);
print $form->selectarray('year',$arrayyears,$year,0);
print '</td></tr>';

View File

@@ -48,12 +48,12 @@ class FactureStats extends Stats
* @param DoliDB $db Database handler
* @param int $socid Id third party
* @param string $mode Option
* @param int $userid Id user for filter
* @param int $userid Id user for filter (creation user)
* @return FactureStats
*/
function __construct($db, $socid, $mode, $userid=0)
{
global $conf;
global $user, $conf;
$this->db = $db;
$this->socid = ($socid > 0 ? $socid : 0);
@@ -62,41 +62,25 @@ class FactureStats extends Stats
if ($mode == 'customer')
{
$object=new Facture($this->db);
$this->from = MAIN_DB_PREFIX.$object->table_element;
$this->from = MAIN_DB_PREFIX.$object->table_element." as f";
$this->field='total';
}
if ($mode == 'supplier')
{
$object=new FactureFournisseur($this->db);
$this->from = MAIN_DB_PREFIX.$object->table_element;
$this->from = MAIN_DB_PREFIX.$object->table_element." as f";
$this->field='total_ht';
}
$this->where = " fk_statut > 0";
$this->where.= " AND entity = ".$conf->entity;
if ($mode == 'customer') $this->where.=" AND (fk_statut <> 3 OR close_code <> 'replaced')"; // Exclude replaced invoices as they are duplicated (we count closed invoices for other reasons)
$this->where = " f.fk_statut > 0";
$this->where.= " AND f.entity = ".$conf->entity;
if (!$user->rights->societe->client->voir && !$user->societe_id) $this->where .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
if ($mode == 'customer') $this->where.=" AND (f.fk_statut <> 3 OR f.close_code <> 'replaced')"; // Exclude replaced invoices as they are duplicated (we count closed invoices for other reasons)
if ($this->socid)
{
$this->where.=" AND fk_soc = ".$this->socid;
$this->where.=" AND f.fk_soc = ".$this->socid;
}
if ($this->userid > 0) $this->where.=' AND fk_user_author = '.$this->userid;
}
/**
* Renvoie le nombre de facture par annee
*
* @return array Array of values
*/
function getNbByYear()
{
$sql = "SELECT YEAR(datef) as dm, COUNT(*)";
$sql.= " FROM ".$this->from;
$sql.= " WHERE ".$this->where;
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
return $this->_getNbByYear($sql);
if ($this->userid > 0) $this->where.=' AND f.fk_user_author = '.$this->userid;
}
@@ -108,9 +92,10 @@ class FactureStats extends Stats
*/
function getNbByMonth($year)
{
$sql = "SELECT MONTH(datef) as dm, COUNT(*)";
$sql = "SELECT MONTH(f.datef) as dm, COUNT(*)";
$sql.= " FROM ".$this->from;
$sql.= " WHERE datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
@@ -121,6 +106,24 @@ class FactureStats extends Stats
}
/**
* Renvoie le nombre de facture par annee
*
* @return array Array of values
*/
function getNbByYear()
{
$sql = "SELECT YEAR(f.datef) as dm, COUNT(*)";
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE ".$this->where;
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
return $this->_getNbByYear($sql);
}
/**
* Renvoie le montant de facture par mois pour une annee donnee
*
@@ -129,9 +132,10 @@ class FactureStats extends Stats
*/
function getAmountByMonth($year)
{
$sql = "SELECT date_format(datef,'%m') as dm, SUM(".$this->field.")";
$sql = "SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.")";
$sql.= " FROM ".$this->from;
$sql.= " WHERE date_format(datef,'%Y') = '".$year."'";
if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
@@ -149,9 +153,10 @@ class FactureStats extends Stats
*/
function getAverageByMonth($year)
{
$sql = "SELECT date_format(datef,'%m') as dm, AVG(".$this->field.")";
$sql = "SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.")";
$sql.= " FROM ".$this->from;
$sql.= " WHERE datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE f.datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
@@ -166,8 +171,9 @@ class FactureStats extends Stats
*/
function getAllByYear()
{
$sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(".$this->field.") as total, AVG(".$this->field.") as avg";
$sql = "SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.") as total, AVG(f.".$this->field.") as avg";
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE ".$this->where;
$sql.= " GROUP BY year";
$sql.= $this->db->order('year','DESC');

View File

@@ -233,12 +233,13 @@ print '<div class="fichecenter"><div class="fichethirdleft">';
print $form->select_company($socid,'socid',$filter,1);
print '</td></tr>';
// User
print '<tr><td>'.$langs->trans("User").'/'.$langs->trans("SalesRepresentative").'</td><td>';
print '<tr><td>'.$langs->trans("CreatedBy").'</td><td>';
print $form->select_users($userid,'userid',1);
print '</td></tr>';
// Year
print '<tr><td>'.$langs->trans("Year").'</td><td>';
if (! in_array($year,$arrayyears)) $arrayyears[$year]=$year;
if (! in_array($nowyear,$arrayyears)) $arrayyears[$nowyear]=$nowyear;
arsort($arrayyears);
print $form->selectarray('year',$arrayyears,$year,0);
print '</td></tr>';