Replace LEFT JOIN with EXISTS
This commit is contained in:
parent
a2d9dd3388
commit
f87ec74b9f
@ -109,9 +109,7 @@ class PropaleStats extends Stats
|
||||
}
|
||||
|
||||
if ($categid) {
|
||||
$this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cs ON cs.fk_soc = p.fk_soc';
|
||||
$this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie as c ON c.rowid = cs.fk_categorie';
|
||||
$this->where .= ' AND c.rowid = '.((int) $categid);
|
||||
$this->where .= ' AND EXISTS (SELECT rowid FROM '.MAIN_DB_PREFIX.'categorie_societe as cats WHERE cats.fk_soc = p.fk_soc AND cats.fk_categorie = '.((int) $categid).')';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -109,9 +109,7 @@ class CommandeStats extends Stats
|
||||
}
|
||||
|
||||
if ($categid) {
|
||||
$this->join .= ' LEFT JOIN '.$this->categ_link.' as cats ON cats.fk_soc = c.fk_soc';
|
||||
$this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie as cat ON cat.rowid = cats.fk_categorie';
|
||||
$this->where .= ' AND cat.rowid = '.((int) $categid);
|
||||
$this->where .= ' AND EXISTS (SELECT rowid FROM '.$this->categ_link.' as cats WHERE cats.fk_soc = c.fk_soc AND cats.fk_categorie = '.((int) $categid).')';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -118,9 +118,7 @@ class FactureStats extends Stats
|
||||
}
|
||||
|
||||
if ($categid) {
|
||||
$this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie_societe as cs ON cs.fk_soc = f.fk_soc';
|
||||
$this->join .= ' LEFT JOIN '.MAIN_DB_PREFIX.'categorie as c ON c.rowid = cs.fk_categorie';
|
||||
$this->where .= ' AND c.rowid = '.((int) $categid);
|
||||
$this->where .= ' AND EXISTS (SELECT rowid FROM '.MAIN_DB_PREFIX.'categorie_societe as cats WHERE cats.fk_soc = f.fk_soc AND cats.fk_categorie = '.((int) $categid).')';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -69,7 +69,7 @@ class ReceptionStats extends Stats
|
||||
$this->where .= " c.fk_statut > 0"; // Not draft and not cancelled
|
||||
|
||||
//$this->where.= " AND c.fk_soc = s.rowid AND c.entity = ".$conf->entity;
|
||||
$this->where .= " AND c.entity = ".$conf->entity;
|
||||
$this->where .= " AND c.entity IN (".getEntity('reception').")";
|
||||
if (empty($user->rights->societe->client->voir) && !$this->socid) {
|
||||
$this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
|
||||
}
|
||||
@ -128,6 +128,54 @@ class ReceptionStats extends Stats
|
||||
return $this->_getNbByYear($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the orders amount by month for a year
|
||||
*
|
||||
* @param int $year Year to scan
|
||||
* @param int $format 0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month
|
||||
* @return array Array with amount by month
|
||||
*/
|
||||
public function getAmountByMonth($year, $format = 0)
|
||||
{
|
||||
global $user;
|
||||
|
||||
$sql = "SELECT date_format(c.date_valid,'%m') as dm, SUM(c.".$this->field.")";
|
||||
$sql .= " FROM ".$this->from;
|
||||
if (empty($user->rights->societe->client->voir) && !$this->socid) {
|
||||
$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
|
||||
}
|
||||
$sql .= " WHERE c.date_valid 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');
|
||||
|
||||
$res = $this->_getAmountByMonth($year, $sql, $format);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the orders amount average by month for a year
|
||||
*
|
||||
* @param int $year year for stats
|
||||
* @return array array with number by month
|
||||
*/
|
||||
public function getAverageByMonth($year)
|
||||
{
|
||||
global $user;
|
||||
|
||||
$sql = "SELECT date_format(c.date_valid,'%m') as dm, AVG(c.".$this->field.")";
|
||||
$sql .= " FROM ".$this->from;
|
||||
if (empty($user->rights->societe->client->voir) && !$this->socid) {
|
||||
$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
|
||||
}
|
||||
$sql .= " WHERE c.date_valid 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->_getAverageByMonth($year, $sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return nb, total and average
|
||||
*
|
||||
|
||||
Loading…
Reference in New Issue
Block a user