From 6db07ac2d8f059205c4e550feb4f71c0903d1282 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 20 May 2013 19:24:27 +0200 Subject: [PATCH] Fix: Several bugs into filter on statistics pages. --- .../comm/propal/class/propalestats.class.php | 23 +++--- htdocs/comm/propal/stats/index.php | 9 +-- htdocs/commande/class/commandestats.class.php | 11 +-- htdocs/commande/stats/index.php | 9 +-- .../facture/class/facturestats.class.php | 72 ++++++++++--------- htdocs/compta/facture/stats/index.php | 3 +- 6 files changed, 69 insertions(+), 58 deletions(-) diff --git a/htdocs/comm/propal/class/propalestats.class.php b/htdocs/comm/propal/class/propalestats.class.php index 5911058cdbc..54ddc846272 100644 --- a/htdocs/comm/propal/class/propalestats.class.php +++ b/htdocs/comm/propal/class/propalestats.class.php @@ -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() diff --git a/htdocs/comm/propal/stats/index.php b/htdocs/comm/propal/stats/index.php index ef979275101..a4b7ad69d24 100644 --- a/htdocs/comm/propal/stats/index.php +++ b/htdocs/comm/propal/stats/index.php @@ -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 '
'; print $form->select_company($socid,'socid',$filter,1); print ''; // User - print ''.$langs->trans("User").'/'.$langs->trans("SalesRepresentative").''; + print ''.$langs->trans("CreatedBy").''; print $form->select_users($userid,'userid',1); print ''; // Year print ''.$langs->trans("Year").''; 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 ''; diff --git a/htdocs/commande/class/commandestats.class.php b/htdocs/commande/class/commandestats.class.php index fba88538145..56c69d84541 100644 --- a/htdocs/commande/class/commandestats.class.php +++ b/htdocs/commande/class/commandestats.class.php @@ -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'); diff --git a/htdocs/commande/stats/index.php b/htdocs/commande/stats/index.php index cb7e2034275..07dfb674c74 100644 --- a/htdocs/commande/stats/index.php +++ b/htdocs/commande/stats/index.php @@ -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 '
'; print $form->select_company($socid,'socid',$filter,1); print ''; // User - print ''.$langs->trans("User").'/'.$langs->trans("SalesRepresentative").''; + print ''.$langs->trans("CreatedBy").''; print $form->select_users($userid,'userid',1); print ''; // Year print ''.$langs->trans("Year").''; 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 ''; diff --git a/htdocs/compta/facture/class/facturestats.class.php b/htdocs/compta/facture/class/facturestats.class.php index f99b8f3bb27..0fe45fc6c66 100644 --- a/htdocs/compta/facture/class/facturestats.class.php +++ b/htdocs/compta/facture/class/facturestats.class.php @@ -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'); diff --git a/htdocs/compta/facture/stats/index.php b/htdocs/compta/facture/stats/index.php index 0b4a341e1c2..4abff5b63be 100644 --- a/htdocs/compta/facture/stats/index.php +++ b/htdocs/compta/facture/stats/index.php @@ -233,12 +233,13 @@ print '
'; print $form->select_company($socid,'socid',$filter,1); print ''; // User - print ''.$langs->trans("User").'/'.$langs->trans("SalesRepresentative").''; + print ''.$langs->trans("CreatedBy").''; print $form->select_users($userid,'userid',1); print ''; // Year print ''.$langs->trans("Year").''; 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 '';